blob: ed7207fee80915bc56e3bb42ec8a681c22db2e34 [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));
rgindaa09e7332012-08-17 12:49:51 -070053 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080054
rginda87b86462011-12-14 13:48:03 -080055 // The div that contains this terminal.
56 this.div_ = null;
57
rgindac9bc5502012-01-18 11:48:44 -080058 // The document that contains the scrollPort. Defaulted to the global
59 // document here so that the terminal is functional even if it hasn't been
60 // inserted into a document yet, but re-set in decorate().
61 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080062
rginda8ba33642011-12-14 12:31:31 -080063 // The rows that have scrolled off screen and are no longer addressable.
64 this.scrollbackRows_ = [];
65
rgindac9bc5502012-01-18 11:48:44 -080066 // Saved tab stops.
67 this.tabStops_ = [];
68
David Benjamin66e954d2012-05-05 21:08:12 -040069 // Keep track of whether default tab stops have been erased; after a TBC
70 // clears all tab stops, defaults aren't restored on resize until a reset.
71 this.defaultTabStops = true;
72
rginda8ba33642011-12-14 12:31:31 -080073 // The VT's notion of the top and bottom rows. Used during some VT
74 // cursor positioning and scrolling commands.
75 this.vtScrollTop_ = null;
76 this.vtScrollBottom_ = null;
77
78 // The DIV element for the visible cursor.
79 this.cursorNode_ = null;
80
Robert Ginda830583c2013-08-07 13:20:46 -070081 // The current cursor shape of the terminal.
82 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
83
84 // The current color of the cursor.
85 this.cursorColor_ = null;
86
Robert Gindaea2183e2014-07-17 09:51:51 -070087 // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded.
88 this.cursorBlinkCycle_ = [100, 100];
89
90 // Pre-bound onCursorBlink_ handler, so we don't have to do this for each
91 // cursor on/off servicing.
92 this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
93
rginda9f5222b2012-03-05 11:53:28 -080094 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070095 // each output and keystroke. They are initialized by the preference manager.
Robert Ginda8cb7d902013-06-20 14:37:18 -070096 this.backgroundColor_ = null;
97 this.foregroundColor_ = null;
Robert Ginda57f03b42012-09-13 11:02:48 -070098 this.scrollOnOutput_ = null;
99 this.scrollOnKeystroke_ = null;
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400100 this.scrollWheelArrowKeys_ = null;
rginda9f5222b2012-03-05 11:53:28 -0800101
Robert Ginda6aec7eb2015-06-16 10:31:30 -0700102 // True if we should override mouse event reporting to allow local selection.
103 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -0800104
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400105 // Whether to auto hide the mouse cursor when typing.
106 this.setAutomaticMouseHiding();
107 // Timer to keep mouse visible while it's being used.
108 this.mouseHideDelay_ = null;
109
rgindaf0090c92012-02-10 14:58:52 -0800110 // Terminal bell sound.
111 this.bellAudio_ = this.document_.createElement('audio');
Mike Frysingerd826f1a2017-07-06 16:20:06 -0400112 this.bellAudio_.id = 'hterm:bell-audio';
rgindaf0090c92012-02-10 14:58:52 -0800113 this.bellAudio_.setAttribute('preload', 'auto');
114
Raymes Khoury3e44bc92018-05-17 10:54:23 +1000115 // The AccessibilityReader object for announcing command output.
116 this.accessibilityReader_ = null;
117
Michael Kelly485ecd12014-06-09 11:41:56 -0400118 // All terminal bell notifications that have been generated (not necessarily
119 // shown).
120 this.bellNotificationList_ = [];
121
122 // Whether we have permission to display notifications.
123 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400124
rginda6d397402012-01-17 10:58:29 -0800125 // Cursor position and attributes saved with DECSC.
126 this.savedOptions_ = {};
127
rginda8ba33642011-12-14 12:31:31 -0800128 // The current mode bits for the terminal.
129 this.options_ = new hterm.Options();
130
131 // Timeouts we might need to clear.
132 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800133
134 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800135 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800136
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800137 this.saveCursorAndState(true);
138
Zhu Qunying30d40712017-03-14 16:27:00 -0700139 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800140 this.keyboard = new hterm.Keyboard(this);
141
rginda87b86462011-12-14 13:48:03 -0800142 // General IO interface that can be given to third parties without exposing
143 // the entire terminal object.
144 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800145
rgindad5613292012-06-19 15:40:37 -0700146 // True if mouse-click-drag should scroll the terminal.
147 this.enableMouseDragScroll = true;
148
Robert Ginda57f03b42012-09-13 11:02:48 -0700149 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400150 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700151 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700152
Zhu Qunying30d40712017-03-14 16:27:00 -0700153 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700154 this.useDefaultWindowCopy = false;
155
156 this.clearSelectionAfterCopy = true;
157
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400158 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800159 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700160
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400161 // Whether we allow images to be shown.
162 this.allowImagesInline = null;
163
Gabriel Holodake8a09be2017-10-10 01:07:11 -0400164 this.reportFocus = false;
165
Robert Ginda57f03b42012-09-13 11:02:48 -0700166 this.setProfile(opt_profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500167 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800168};
169
170/**
Robert Ginda830583c2013-08-07 13:20:46 -0700171 * Possible cursor shapes.
172 */
173hterm.Terminal.cursorShape = {
174 BLOCK: 'BLOCK',
175 BEAM: 'BEAM',
176 UNDERLINE: 'UNDERLINE'
177};
178
179/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700180 * Clients should override this to be notified when the terminal is ready
181 * for use.
182 *
183 * The terminal initialization is asynchronous, and shouldn't be used before
184 * this method is called.
185 */
186hterm.Terminal.prototype.onTerminalReady = function() { };
187
188/**
rginda35c456b2012-02-09 17:29:05 -0800189 * Default tab with of 8 to match xterm.
190 */
191hterm.Terminal.prototype.tabWidth = 8;
192
193/**
rginda9f5222b2012-03-05 11:53:28 -0800194 * Select a preference profile.
195 *
196 * This will load the terminal preferences for the given profile name and
197 * associate subsequent preference changes with the new preference profile.
198 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500199 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800200 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700201 * @param {function} opt_callback Optional callback to invoke when the profile
202 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800203 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700204hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
205 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800206
Robert Ginda57f03b42012-09-13 11:02:48 -0700207 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800208
Robert Ginda57f03b42012-09-13 11:02:48 -0700209 if (this.prefs_)
210 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800211
Robert Ginda57f03b42012-09-13 11:02:48 -0700212 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
213 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800214 'alt-gr-mode': function(v) {
215 if (v == null) {
216 if (navigator.language.toLowerCase() == 'en-us') {
217 v = 'none';
218 } else {
219 v = 'right-alt';
220 }
221 } else if (typeof v == 'string') {
222 v = v.toLowerCase();
223 } else {
224 v = 'none';
225 }
226
227 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v))
228 v = 'none';
229
230 terminal.keyboard.altGrMode = v;
231 },
232
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700233 'alt-backspace-is-meta-backspace': function(v) {
234 terminal.keyboard.altBackspaceIsMetaBackspace = v;
235 },
236
Robert Ginda57f03b42012-09-13 11:02:48 -0700237 'alt-is-meta': function(v) {
238 terminal.keyboard.altIsMeta = v;
239 },
240
241 'alt-sends-what': function(v) {
242 if (!/^(escape|8-bit|browser-key)$/.test(v))
243 v = 'escape';
244
245 terminal.keyboard.altSendsWhat = v;
246 },
247
248 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800249 var ary = v.match(/^lib-resource:(\S+)/);
250 if (ary) {
251 terminal.bellAudio_.setAttribute('src',
252 lib.resource.getDataUrl(ary[1]));
253 } else {
254 terminal.bellAudio_.setAttribute('src', v);
255 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700256 },
257
Michael Kelly485ecd12014-06-09 11:41:56 -0400258 'desktop-notification-bell': function(v) {
259 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700260 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400261 Notification.permission === 'granted';
262 if (!terminal.desktopNotificationBell_) {
263 // Note: We don't call Notification.requestPermission here because
264 // Chrome requires the call be the result of a user action (such as an
265 // onclick handler), and pref listeners are run asynchronously.
266 //
267 // A way of working around this would be to display a dialog in the
268 // terminal with a "click-to-request-permission" button.
269 console.warn('desktop-notification-bell is true but we do not have ' +
270 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400271 }
272 } else {
273 terminal.desktopNotificationBell_ = false;
274 }
275 },
276
Robert Ginda57f03b42012-09-13 11:02:48 -0700277 'background-color': function(v) {
278 terminal.setBackgroundColor(v);
279 },
280
281 'background-image': function(v) {
282 terminal.scrollPort_.setBackgroundImage(v);
283 },
284
285 'background-size': function(v) {
286 terminal.scrollPort_.setBackgroundSize(v);
287 },
288
289 'background-position': function(v) {
290 terminal.scrollPort_.setBackgroundPosition(v);
291 },
292
293 'backspace-sends-backspace': function(v) {
294 terminal.keyboard.backspaceSendsBackspace = v;
295 },
296
Brad Town18654b62015-03-12 00:27:45 -0700297 'character-map-overrides': function(v) {
298 if (!(v == null || v instanceof Object)) {
299 console.warn('Preference character-map-modifications is not an ' +
300 'object: ' + v);
301 return;
302 }
303
Mike Frysinger095d4062017-06-14 00:29:48 -0700304 terminal.vt.characterMaps.reset();
305 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700306 },
307
Robert Ginda57f03b42012-09-13 11:02:48 -0700308 'cursor-blink': function(v) {
309 terminal.setCursorBlink(!!v);
310 },
311
Robert Gindaea2183e2014-07-17 09:51:51 -0700312 'cursor-blink-cycle': function(v) {
313 if (v instanceof Array &&
314 typeof v[0] == 'number' &&
315 typeof v[1] == 'number') {
316 terminal.cursorBlinkCycle_ = v;
317 } else if (typeof v == 'number') {
318 terminal.cursorBlinkCycle_ = [v, v];
319 } else {
320 // Fast blink indicates an error.
321 terminal.cursorBlinkCycle_ = [100, 100];
322 }
323 },
324
Robert Ginda57f03b42012-09-13 11:02:48 -0700325 'cursor-color': function(v) {
326 terminal.setCursorColor(v);
327 },
328
329 'color-palette-overrides': function(v) {
330 if (!(v == null || v instanceof Object || v instanceof Array)) {
331 console.warn('Preference color-palette-overrides is not an array or ' +
332 'object: ' + v);
333 return;
rginda9f5222b2012-03-05 11:53:28 -0800334 }
rginda9f5222b2012-03-05 11:53:28 -0800335
Robert Ginda57f03b42012-09-13 11:02:48 -0700336 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700337
Robert Ginda57f03b42012-09-13 11:02:48 -0700338 if (v) {
339 for (var key in v) {
340 var i = parseInt(key);
341 if (isNaN(i) || i < 0 || i > 255) {
342 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
343 continue;
344 }
345
346 if (v[i]) {
347 var rgb = lib.colors.normalizeCSS(v[i]);
348 if (rgb)
349 lib.colors.colorPalette[i] = rgb;
350 }
351 }
rginda30f20f62012-04-05 16:36:19 -0700352 }
rginda30f20f62012-04-05 16:36:19 -0700353
Evan Jones5f9df812016-12-06 09:38:58 -0500354 terminal.primaryScreen_.textAttributes.resetColorPalette();
Robert Ginda57f03b42012-09-13 11:02:48 -0700355 terminal.alternateScreen_.textAttributes.resetColorPalette();
356 },
rginda30f20f62012-04-05 16:36:19 -0700357
Robert Ginda57f03b42012-09-13 11:02:48 -0700358 'copy-on-select': function(v) {
359 terminal.copyOnSelect = !!v;
360 },
rginda9f5222b2012-03-05 11:53:28 -0800361
Rob Spies0bec09b2014-06-06 15:58:09 -0700362 'use-default-window-copy': function(v) {
363 terminal.useDefaultWindowCopy = !!v;
364 },
365
366 'clear-selection-after-copy': function(v) {
367 terminal.clearSelectionAfterCopy = !!v;
368 },
369
Robert Ginda7e5e9522014-03-14 12:23:58 -0700370 'ctrl-plus-minus-zero-zoom': function(v) {
371 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
372 },
373
Robert Gindafb5a3f92014-05-13 14:12:00 -0700374 'ctrl-c-copy': function(v) {
375 terminal.keyboard.ctrlCCopy = v;
376 },
377
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100378 'ctrl-v-paste': function(v) {
379 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700380 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100381 },
382
Masaya Suzuki273aa982014-05-31 07:25:55 +0900383 'east-asian-ambiguous-as-two-column': function(v) {
384 lib.wc.regardCjkAmbiguous = v;
385 },
386
Robert Ginda57f03b42012-09-13 11:02:48 -0700387 'enable-8-bit-control': function(v) {
388 terminal.vt.enable8BitControl = !!v;
389 },
rginda30f20f62012-04-05 16:36:19 -0700390
Robert Ginda57f03b42012-09-13 11:02:48 -0700391 'enable-bold': function(v) {
392 terminal.syncBoldSafeState();
393 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400394
Robert Ginda3e278d72014-03-25 13:18:51 -0700395 'enable-bold-as-bright': function(v) {
396 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
397 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
398 },
399
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400400 'enable-blink': function(v) {
Mike Frysinger261597c2017-12-28 01:14:21 -0500401 terminal.setTextBlink(!!v);
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400402 },
403
Robert Ginda57f03b42012-09-13 11:02:48 -0700404 'enable-clipboard-write': function(v) {
405 terminal.vt.enableClipboardWrite = !!v;
406 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400407
Robert Ginda3755e752013-05-31 13:34:09 -0700408 'enable-dec12': function(v) {
409 terminal.vt.enableDec12 = !!v;
410 },
411
Robert Ginda57f03b42012-09-13 11:02:48 -0700412 'font-family': function(v) {
413 terminal.syncFontFamily();
414 },
rginda30f20f62012-04-05 16:36:19 -0700415
Robert Ginda57f03b42012-09-13 11:02:48 -0700416 'font-size': function(v) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500417 v = parseInt(v);
418 if (v <= 0) {
419 console.error(`Invalid font size: ${v}`);
420 return;
421 }
422
Robert Ginda57f03b42012-09-13 11:02:48 -0700423 terminal.setFontSize(v);
424 },
rginda9875d902012-08-20 16:21:57 -0700425
Robert Ginda57f03b42012-09-13 11:02:48 -0700426 'font-smoothing': function(v) {
427 terminal.syncFontFamily();
428 },
rgindade84e382012-04-20 15:39:31 -0700429
Robert Ginda57f03b42012-09-13 11:02:48 -0700430 'foreground-color': function(v) {
431 terminal.setForegroundColor(v);
432 },
rginda30f20f62012-04-05 16:36:19 -0700433
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400434 'hide-mouse-while-typing': function(v) {
435 terminal.setAutomaticMouseHiding(v);
436 },
437
Robert Ginda57f03b42012-09-13 11:02:48 -0700438 'home-keys-scroll': function(v) {
439 terminal.keyboard.homeKeysScroll = v;
440 },
rginda4bba5e12012-06-20 16:15:30 -0700441
Robert Gindaa8165692015-06-15 14:46:31 -0700442 'keybindings': function(v) {
443 terminal.keyboard.bindings.clear();
444
445 if (!v)
446 return;
447
448 if (!(v instanceof Object)) {
449 console.error('Error in keybindings preference: Expected object');
450 return;
451 }
452
453 try {
454 terminal.keyboard.bindings.addBindings(v);
455 } catch (ex) {
456 console.error('Error in keybindings preference: ' + ex);
457 }
458 },
459
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700460 'media-keys-are-fkeys': function(v) {
461 terminal.keyboard.mediaKeysAreFKeys = v;
462 },
463
Robert Ginda57f03b42012-09-13 11:02:48 -0700464 'meta-sends-escape': function(v) {
465 terminal.keyboard.metaSendsEscape = v;
466 },
rginda30f20f62012-04-05 16:36:19 -0700467
Mike Frysinger847577f2017-05-23 23:25:57 -0400468 'mouse-right-click-paste': function(v) {
469 terminal.mouseRightClickPaste = v;
470 },
471
Robert Ginda57f03b42012-09-13 11:02:48 -0700472 'mouse-paste-button': function(v) {
473 terminal.syncMousePasteButton();
474 },
rgindaa8ba17d2012-08-15 14:41:10 -0700475
Robert Gindae76aa9f2014-03-14 12:29:12 -0700476 'page-keys-scroll': function(v) {
477 terminal.keyboard.pageKeysScroll = v;
478 },
479
Robert Ginda40932892012-12-10 17:26:40 -0800480 'pass-alt-number': function(v) {
481 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800482 // Let Alt-1..9 pass to the browser (to control tab switching) on
483 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500484 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800485 }
486
487 terminal.passAltNumber = v;
488 },
489
490 'pass-ctrl-number': function(v) {
491 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800492 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
493 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500494 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800495 }
496
497 terminal.passCtrlNumber = v;
498 },
499
500 'pass-meta-number': function(v) {
501 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800502 // Let Meta-1..9 pass to the browser (to control tab switching) on
503 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500504 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800505 }
506
507 terminal.passMetaNumber = v;
508 },
509
Marius Schilder77857b32014-05-14 16:21:26 -0700510 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700511 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700512 },
513
Robert Ginda8cb7d902013-06-20 14:37:18 -0700514 'receive-encoding': function(v) {
515 if (!(/^(utf-8|raw)$/).test(v)) {
516 console.warn('Invalid value for "receive-encoding": ' + v);
517 v = 'utf-8';
518 }
519
520 terminal.vt.characterEncoding = v;
521 },
522
Robert Ginda57f03b42012-09-13 11:02:48 -0700523 'scroll-on-keystroke': function(v) {
524 terminal.scrollOnKeystroke_ = v;
525 },
rginda9f5222b2012-03-05 11:53:28 -0800526
Robert Ginda57f03b42012-09-13 11:02:48 -0700527 'scroll-on-output': function(v) {
528 terminal.scrollOnOutput_ = v;
529 },
rginda30f20f62012-04-05 16:36:19 -0700530
Robert Ginda57f03b42012-09-13 11:02:48 -0700531 'scrollbar-visible': function(v) {
532 terminal.setScrollbarVisible(v);
533 },
rginda9f5222b2012-03-05 11:53:28 -0800534
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400535 'scroll-wheel-may-send-arrow-keys': function(v) {
536 terminal.scrollWheelArrowKeys_ = v;
537 },
538
Rob Spies49039e52014-12-17 13:40:04 -0800539 'scroll-wheel-move-multiplier': function(v) {
540 terminal.setScrollWheelMoveMultipler(v);
541 },
542
Robert Ginda8cb7d902013-06-20 14:37:18 -0700543 'send-encoding': function(v) {
544 if (!(/^(utf-8|raw)$/).test(v)) {
545 console.warn('Invalid value for "send-encoding": ' + v);
546 v = 'utf-8';
547 }
548
549 terminal.keyboard.characterEncoding = v;
550 },
551
Robert Ginda57f03b42012-09-13 11:02:48 -0700552 'shift-insert-paste': function(v) {
553 terminal.keyboard.shiftInsertPaste = v;
554 },
rginda9f5222b2012-03-05 11:53:28 -0800555
Mike Frysingera7768922017-07-28 15:00:12 -0400556 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400557 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400558 },
559
Robert Gindae76aa9f2014-03-14 12:29:12 -0700560 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400561 terminal.scrollPort_.setUserCssUrl(v);
562 },
563
564 'user-css-text': function(v) {
565 terminal.scrollPort_.setUserCssText(v);
566 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400567
568 'word-break-match-left': function(v) {
569 terminal.primaryScreen_.wordBreakMatchLeft = v;
570 terminal.alternateScreen_.wordBreakMatchLeft = v;
571 },
572
573 'word-break-match-right': function(v) {
574 terminal.primaryScreen_.wordBreakMatchRight = v;
575 terminal.alternateScreen_.wordBreakMatchRight = v;
576 },
577
578 'word-break-match-middle': function(v) {
579 terminal.primaryScreen_.wordBreakMatchMiddle = v;
580 terminal.alternateScreen_.wordBreakMatchMiddle = v;
581 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400582
583 'allow-images-inline': function(v) {
584 terminal.allowImagesInline = v;
585 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700586 });
rginda30f20f62012-04-05 16:36:19 -0700587
Robert Ginda57f03b42012-09-13 11:02:48 -0700588 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800589 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700590
591 if (opt_callback)
592 opt_callback();
593 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800594};
595
Rob Spies56953412014-04-28 14:09:47 -0700596
597/**
598 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500599 *
600 * @return {hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700601 */
602hterm.Terminal.prototype.getPrefs = function() {
603 return this.prefs_;
604};
605
Robert Gindaa063b202014-07-21 11:08:25 -0700606/**
607 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500608 *
609 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700610 */
611hterm.Terminal.prototype.setBracketedPaste = function(state) {
612 this.options_.bracketedPaste = state;
613};
Rob Spies56953412014-04-28 14:09:47 -0700614
rginda8e92a692012-05-20 19:37:20 -0700615/**
616 * Set the color for the cursor.
617 *
618 * If you want this setting to persist, set it through prefs_, rather than
619 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500620 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500621 * @param {string=} color The color to set. If not defined, we reset to the
622 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700623 */
624hterm.Terminal.prototype.setCursorColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500625 if (color === undefined)
626 color = this.prefs_.get('cursor-color');
627
Robert Ginda830583c2013-08-07 13:20:46 -0700628 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700629 this.cursorNode_.style.backgroundColor = color;
630 this.cursorNode_.style.borderColor = color;
631};
632
633/**
634 * Return the current cursor color as a string.
Evan Jones2600d4f2016-12-06 09:29:36 -0500635 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700636 */
637hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700638 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700639};
640
641/**
rgindad5613292012-06-19 15:40:37 -0700642 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500643 *
644 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700645 */
646hterm.Terminal.prototype.setSelectionEnabled = function(state) {
647 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700648};
649
650/**
rginda8e92a692012-05-20 19:37:20 -0700651 * Set the background color.
652 *
653 * If you want this setting to persist, set it through prefs_, rather than
654 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500655 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500656 * @param {string=} color The color to set. If not defined, we reset to the
657 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700658 */
659hterm.Terminal.prototype.setBackgroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500660 if (color === undefined)
661 color = this.prefs_.get('background-color');
662
rgindacbbd7482012-06-13 15:06:16 -0700663 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700664 this.primaryScreen_.textAttributes.setDefaults(
665 this.foregroundColor_, this.backgroundColor_);
666 this.alternateScreen_.textAttributes.setDefaults(
667 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700668 this.scrollPort_.setBackgroundColor(color);
669};
670
rginda9f5222b2012-03-05 11:53:28 -0800671/**
672 * Return the current terminal background color.
673 *
674 * Intended for use by other classes, so we don't have to expose the entire
675 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500676 *
677 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800678 */
679hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700680 return this.backgroundColor_;
681};
682
683/**
684 * Set the foreground color.
685 *
686 * If you want this setting to persist, set it through prefs_, rather than
687 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500688 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500689 * @param {string=} color The color to set. If not defined, we reset to the
690 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700691 */
692hterm.Terminal.prototype.setForegroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500693 if (color === undefined)
694 color = this.prefs_.get('foreground-color');
695
rgindacbbd7482012-06-13 15:06:16 -0700696 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700697 this.primaryScreen_.textAttributes.setDefaults(
698 this.foregroundColor_, this.backgroundColor_);
699 this.alternateScreen_.textAttributes.setDefaults(
700 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700701 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800702};
703
704/**
705 * Return the current terminal foreground color.
706 *
707 * Intended for use by other classes, so we don't have to expose the entire
708 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500709 *
710 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800711 */
712hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700713 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800714};
715
716/**
rginda87b86462011-12-14 13:48:03 -0800717 * Create a new instance of a terminal command and run it with a given
718 * argument string.
719 *
720 * @param {function} commandClass The constructor for a terminal command.
721 * @param {string} argString The argument string to pass to the command.
722 */
723hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700724 var environment = this.prefs_.get('environment');
725 if (typeof environment != 'object' || environment == null)
726 environment = {};
727
rginda87b86462011-12-14 13:48:03 -0800728 var self = this;
729 this.command = new commandClass(
730 { argString: argString || '',
731 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700732 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800733 onExit: function(code) {
734 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800735 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700736 if (self.prefs_.get('close-on-exit'))
737 window.close();
rginda87b86462011-12-14 13:48:03 -0800738 }
739 });
740
rgindafeaf3142012-01-31 15:14:20 -0800741 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800742 this.command.run();
743};
744
745/**
rgindafeaf3142012-01-31 15:14:20 -0800746 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500747 *
748 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800749 */
750hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700751 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800752};
753
754/**
755 * Install the keyboard handler for this terminal.
756 *
757 * This will prevent the browser from seeing any keystrokes sent to the
758 * terminal.
759 */
760hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700761 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400762};
rgindafeaf3142012-01-31 15:14:20 -0800763
764/**
765 * Uninstall the keyboard handler for this terminal.
766 */
767hterm.Terminal.prototype.uninstallKeyboard = function() {
768 this.keyboard.installKeyboard(null);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400769};
rgindafeaf3142012-01-31 15:14:20 -0800770
771/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400772 * Set a CSS variable.
773 *
774 * Normally this is used to set variables in the hterm namespace.
775 *
776 * @param {string} name The variable to set.
777 * @param {string} value The value to assign to the variable.
778 * @param {string?} opt_prefix The variable namespace/prefix to use.
779 */
780hterm.Terminal.prototype.setCssVar = function(name, value,
781 opt_prefix='--hterm-') {
782 this.document_.documentElement.style.setProperty(
783 `${opt_prefix}${name}`, value);
784};
785
786/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500787 * Get a CSS variable.
788 *
789 * Normally this is used to get variables in the hterm namespace.
790 *
791 * @param {string} name The variable to read.
792 * @param {string?} opt_prefix The variable namespace/prefix to use.
793 * @return {string} The current setting for this variable.
794 */
795hterm.Terminal.prototype.getCssVar = function(name, opt_prefix='--hterm-') {
796 return this.document_.documentElement.style.getPropertyValue(
797 `${opt_prefix}${name}`);
798};
799
800/**
rginda35c456b2012-02-09 17:29:05 -0800801 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800802 *
803 * Call setFontSize(0) to reset to the default font size.
804 *
805 * This function does not modify the font-size preference.
806 *
807 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800808 */
809hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500810 if (px <= 0)
rginda9f5222b2012-03-05 11:53:28 -0800811 px = this.prefs_.get('font-size');
812
rginda35c456b2012-02-09 17:29:05 -0800813 this.scrollPort_.setFontSize(px);
Mike Frysingercce97c42017-08-05 01:11:22 -0400814 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
815 this.setCssVar('charsize-height',
816 this.scrollPort_.characterSize.height + 'px');
rginda35c456b2012-02-09 17:29:05 -0800817};
818
819/**
820 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500821 *
822 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800823 */
824hterm.Terminal.prototype.getFontSize = function() {
825 return this.scrollPort_.getFontSize();
826};
827
828/**
rginda8e92a692012-05-20 19:37:20 -0700829 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500830 *
831 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700832 */
833hterm.Terminal.prototype.getFontFamily = function() {
834 return this.scrollPort_.getFontFamily();
835};
836
837/**
rginda35c456b2012-02-09 17:29:05 -0800838 * Set the CSS "font-family" for this terminal.
839 */
rginda9f5222b2012-03-05 11:53:28 -0800840hterm.Terminal.prototype.syncFontFamily = function() {
841 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
842 this.prefs_.get('font-smoothing'));
843 this.syncBoldSafeState();
844};
845
rginda4bba5e12012-06-20 16:15:30 -0700846/**
847 * Set this.mousePasteButton based on the mouse-paste-button pref,
848 * autodetecting if necessary.
849 */
850hterm.Terminal.prototype.syncMousePasteButton = function() {
851 var button = this.prefs_.get('mouse-paste-button');
852 if (typeof button == 'number') {
853 this.mousePasteButton = button;
854 return;
855 }
856
Mike Frysingeree81a002017-12-12 16:14:53 -0500857 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400858 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700859 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400860 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700861 }
862};
863
864/**
865 * Enable or disable bold based on the enable-bold pref, autodetecting if
866 * necessary.
867 */
rginda9f5222b2012-03-05 11:53:28 -0800868hterm.Terminal.prototype.syncBoldSafeState = function() {
869 var enableBold = this.prefs_.get('enable-bold');
870 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700871 this.primaryScreen_.textAttributes.enableBold = enableBold;
872 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800873 return;
874 }
875
rgindaf7521392012-02-28 17:20:34 -0800876 var normalSize = this.scrollPort_.measureCharacterSize();
877 var boldSize = this.scrollPort_.measureCharacterSize('bold');
878
879 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800880 if (!isBoldSafe) {
881 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700882 'from normal. Font family is: ' +
883 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800884 }
rginda9f5222b2012-03-05 11:53:28 -0800885
Robert Gindaed016262012-10-26 16:27:09 -0700886 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
887 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800888};
889
890/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500891 * Control text blinking behavior.
892 *
893 * @param {boolean=} state Whether to enable support for blinking text.
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400894 */
Mike Frysinger261597c2017-12-28 01:14:21 -0500895hterm.Terminal.prototype.setTextBlink = function(state) {
896 if (state === undefined)
897 state = this.prefs_.get('enable-blink');
898 this.setCssVar('blink-node-duration', state ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400899};
900
901/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400902 * Set the mouse cursor style based on the current terminal mode.
903 */
904hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400905 this.setCssVar('mouse-cursor-style',
906 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
907 'var(--hterm-mouse-cursor-text)' :
908 'var(--hterm-mouse-cursor-pointer)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400909};
910
911/**
rginda87b86462011-12-14 13:48:03 -0800912 * Return a copy of the current cursor position.
913 *
914 * @return {hterm.RowCol} The RowCol object representing the current position.
915 */
916hterm.Terminal.prototype.saveCursor = function() {
917 return this.screen_.cursorPosition.clone();
918};
919
Evan Jones2600d4f2016-12-06 09:29:36 -0500920/**
921 * Return the current text attributes.
922 *
923 * @return {string}
924 */
rgindaa19afe22012-01-25 15:40:22 -0800925hterm.Terminal.prototype.getTextAttributes = function() {
926 return this.screen_.textAttributes;
927};
928
Evan Jones2600d4f2016-12-06 09:29:36 -0500929/**
930 * Set the text attributes.
931 *
932 * @param {string} textAttributes The attributes to set.
933 */
rginda1a09aa02012-06-18 21:11:25 -0700934hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
935 this.screen_.textAttributes = textAttributes;
936};
937
rginda87b86462011-12-14 13:48:03 -0800938/**
rgindaf522ce02012-04-17 17:49:17 -0700939 * Return the current browser zoom factor applied to the terminal.
940 *
941 * @return {number} The current browser zoom factor.
942 */
943hterm.Terminal.prototype.getZoomFactor = function() {
944 return this.scrollPort_.characterSize.zoomFactor;
945};
946
947/**
rginda9846e2f2012-01-27 13:53:33 -0800948 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500949 *
950 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800951 */
952hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800953 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800954};
955
956/**
rginda87b86462011-12-14 13:48:03 -0800957 * Restore a previously saved cursor position.
958 *
959 * @param {hterm.RowCol} cursor The position to restore.
960 */
961hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700962 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
963 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800964 this.screen_.setCursorPosition(row, column);
965 if (cursor.column > column ||
966 cursor.column == column && cursor.overflow) {
967 this.screen_.cursorPosition.overflow = true;
968 }
rginda87b86462011-12-14 13:48:03 -0800969};
970
971/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400972 * Clear the cursor's overflow flag.
973 */
974hterm.Terminal.prototype.clearCursorOverflow = function() {
975 this.screen_.cursorPosition.overflow = false;
976};
977
978/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800979 * Save the current cursor state to the corresponding screens.
980 *
981 * See the hterm.Screen.CursorState class for more details.
982 *
983 * @param {boolean=} both If true, update both screens, else only update the
984 * current screen.
985 */
986hterm.Terminal.prototype.saveCursorAndState = function(both) {
987 if (both) {
988 this.primaryScreen_.saveCursorAndState(this.vt);
989 this.alternateScreen_.saveCursorAndState(this.vt);
990 } else
991 this.screen_.saveCursorAndState(this.vt);
992};
993
994/**
995 * Restore the saved cursor state in the corresponding screens.
996 *
997 * See the hterm.Screen.CursorState class for more details.
998 *
999 * @param {boolean=} both If true, update both screens, else only update the
1000 * current screen.
1001 */
1002hterm.Terminal.prototype.restoreCursorAndState = function(both) {
1003 if (both) {
1004 this.primaryScreen_.restoreCursorAndState(this.vt);
1005 this.alternateScreen_.restoreCursorAndState(this.vt);
1006 } else
1007 this.screen_.restoreCursorAndState(this.vt);
1008};
1009
1010/**
Robert Ginda830583c2013-08-07 13:20:46 -07001011 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001012 *
1013 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -07001014 */
1015hterm.Terminal.prototype.setCursorShape = function(shape) {
1016 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001017 this.restyleCursor_();
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001018};
Robert Ginda830583c2013-08-07 13:20:46 -07001019
1020/**
1021 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001022 *
1023 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -07001024 */
1025hterm.Terminal.prototype.getCursorShape = function() {
1026 return this.cursorShape_;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001027};
Robert Ginda830583c2013-08-07 13:20:46 -07001028
1029/**
rginda87b86462011-12-14 13:48:03 -08001030 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001031 *
1032 * @param {number} columnCount
rginda87b86462011-12-14 13:48:03 -08001033 */
1034hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001035 if (columnCount == null) {
1036 this.div_.style.width = '100%';
1037 return;
1038 }
1039
Robert Ginda26806d12014-07-24 13:44:07 -07001040 this.div_.style.width = Math.ceil(
1041 this.scrollPort_.characterSize.width *
1042 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001043 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001044 this.scheduleSyncCursorPosition_();
1045};
rginda87b86462011-12-14 13:48:03 -08001046
rgindac9bc5502012-01-18 11:48:44 -08001047/**
rginda35c456b2012-02-09 17:29:05 -08001048 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001049 *
1050 * @param {number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001051 */
1052hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001053 if (rowCount == null) {
1054 this.div_.style.height = '100%';
1055 return;
1056 }
1057
rginda35c456b2012-02-09 17:29:05 -08001058 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -07001059 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -08001060 this.realizeSize_(this.screenSize.width, rowCount);
1061 this.scheduleSyncCursorPosition_();
1062};
1063
1064/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001065 * Deal with terminal size changes.
1066 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001067 * @param {number} columnCount The number of columns.
1068 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001069 */
1070hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
1071 if (columnCount != this.screenSize.width)
1072 this.realizeWidth_(columnCount);
1073
1074 if (rowCount != this.screenSize.height)
1075 this.realizeHeight_(rowCount);
1076
1077 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -07001078 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001079};
1080
1081/**
rgindac9bc5502012-01-18 11:48:44 -08001082 * Deal with terminal width changes.
1083 *
1084 * This function does what needs to be done when the terminal width changes
1085 * out from under us. It happens here rather than in onResize_() because this
1086 * code may need to run synchronously to handle programmatic changes of
1087 * terminal width.
1088 *
1089 * Relying on the browser to send us an async resize event means we may not be
1090 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001091 *
1092 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001093 */
1094hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001095 if (columnCount <= 0)
1096 throw new Error('Attempt to realize bad width: ' + columnCount);
1097
rgindac9bc5502012-01-18 11:48:44 -08001098 var deltaColumns = columnCount - this.screen_.getWidth();
1099
rginda87b86462011-12-14 13:48:03 -08001100 this.screenSize.width = columnCount;
1101 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001102
1103 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -04001104 if (this.defaultTabStops)
1105 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -08001106 } else {
1107 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -04001108 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -08001109 break;
1110
1111 this.tabStops_.pop();
1112 }
1113 }
1114
1115 this.screen_.setColumnCount(this.screenSize.width);
1116};
1117
1118/**
1119 * Deal with terminal height changes.
1120 *
1121 * This function does what needs to be done when the terminal height changes
1122 * out from under us. It happens here rather than in onResize_() because this
1123 * code may need to run synchronously to handle programmatic changes of
1124 * terminal height.
1125 *
1126 * Relying on the browser to send us an async resize event means we may not be
1127 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001128 *
1129 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001130 */
1131hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001132 if (rowCount <= 0)
1133 throw new Error('Attempt to realize bad height: ' + rowCount);
1134
rgindac9bc5502012-01-18 11:48:44 -08001135 var deltaRows = rowCount - this.screen_.getHeight();
1136
1137 this.screenSize.height = rowCount;
1138
1139 var cursor = this.saveCursor();
1140
1141 if (deltaRows < 0) {
1142 // Screen got smaller.
1143 deltaRows *= -1;
1144 while (deltaRows) {
1145 var lastRow = this.getRowCount() - 1;
1146 if (lastRow - this.scrollbackRows_.length == cursor.row)
1147 break;
1148
1149 if (this.getRowText(lastRow))
1150 break;
1151
1152 this.screen_.popRow();
1153 deltaRows--;
1154 }
1155
1156 var ary = this.screen_.shiftRows(deltaRows);
1157 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1158
1159 // We just removed rows from the top of the screen, we need to update
1160 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001161 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001162 } else if (deltaRows > 0) {
1163 // Screen got larger.
1164
1165 if (deltaRows <= this.scrollbackRows_.length) {
1166 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1167 var rows = this.scrollbackRows_.splice(
1168 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1169 this.screen_.unshiftRows(rows);
1170 deltaRows -= scrollbackCount;
1171 cursor.row += scrollbackCount;
1172 }
1173
1174 if (deltaRows)
1175 this.appendRows_(deltaRows);
1176 }
1177
rginda35c456b2012-02-09 17:29:05 -08001178 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001179 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001180};
1181
1182/**
1183 * Scroll the terminal to the top of the scrollback buffer.
1184 */
1185hterm.Terminal.prototype.scrollHome = function() {
1186 this.scrollPort_.scrollRowToTop(0);
1187};
1188
1189/**
1190 * Scroll the terminal to the end.
1191 */
1192hterm.Terminal.prototype.scrollEnd = function() {
1193 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1194};
1195
1196/**
1197 * Scroll the terminal one page up (minus one line) relative to the current
1198 * position.
1199 */
1200hterm.Terminal.prototype.scrollPageUp = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001201 this.scrollPort_.scrollPageUp();
rginda87b86462011-12-14 13:48:03 -08001202};
1203
1204/**
1205 * Scroll the terminal one page down (minus one line) relative to the current
1206 * position.
1207 */
1208hterm.Terminal.prototype.scrollPageDown = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001209 this.scrollPort_.scrollPageDown();
rginda8ba33642011-12-14 12:31:31 -08001210};
1211
rgindac9bc5502012-01-18 11:48:44 -08001212/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001213 * Scroll the terminal one line up relative to the current position.
1214 */
1215hterm.Terminal.prototype.scrollLineUp = function() {
1216 var i = this.scrollPort_.getTopRowIndex();
1217 this.scrollPort_.scrollRowToTop(i - 1);
1218};
1219
1220/**
1221 * Scroll the terminal one line down relative to the current position.
1222 */
1223hterm.Terminal.prototype.scrollLineDown = function() {
1224 var i = this.scrollPort_.getTopRowIndex();
1225 this.scrollPort_.scrollRowToTop(i + 1);
1226};
1227
1228/**
Robert Ginda40932892012-12-10 17:26:40 -08001229 * Clear primary screen, secondary screen, and the scrollback buffer.
1230 */
1231hterm.Terminal.prototype.wipeContents = function() {
1232 this.scrollbackRows_.length = 0;
1233 this.scrollPort_.resetCache();
1234
1235 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
1236 var bottom = screen.getHeight();
1237 if (bottom > 0) {
1238 this.renumberRows_(0, bottom);
1239 this.clearHome(screen);
1240 }
1241 }.bind(this));
1242
1243 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001244 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001245};
1246
1247/**
rgindac9bc5502012-01-18 11:48:44 -08001248 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001249 *
1250 * Perform a full reset to the default values listed in
1251 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001252 */
rginda87b86462011-12-14 13:48:03 -08001253hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001254 this.vt.reset();
1255
rgindac9bc5502012-01-18 11:48:44 -08001256 this.clearAllTabStops();
1257 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001258
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001259 const resetScreen = (screen) => {
1260 // We want to make sure to reset the attributes before we clear the screen.
1261 // The attributes might be used to initialize default/empty rows.
1262 screen.textAttributes.reset();
1263 screen.textAttributes.resetColorPalette();
1264 this.clearHome(screen);
1265 screen.saveCursorAndState(this.vt);
1266 };
1267 resetScreen(this.primaryScreen_);
1268 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001269
Mike Frysinger84301d02017-11-29 13:28:46 -08001270 // Reset terminal options to their default values.
1271 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001272 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1273
Mike Frysinger84301d02017-11-29 13:28:46 -08001274 this.setVTScrollRegion(null, null);
1275
1276 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001277};
1278
rgindac9bc5502012-01-18 11:48:44 -08001279/**
1280 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001281 *
1282 * Perform a soft reset to the default values listed in
1283 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001284 */
rginda0f5c0292012-01-13 11:00:13 -08001285hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001286 this.vt.reset();
1287
rgindab8bc8932012-04-27 12:45:03 -07001288 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001289 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001290
Brad Townb62dfdc2015-03-16 19:07:15 -07001291 // We show the cursor on soft reset but do not alter the blink state.
1292 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1293
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001294 const resetScreen = (screen) => {
1295 // Xterm also resets the color palette on soft reset, even though it doesn't
1296 // seem to be documented anywhere.
1297 screen.textAttributes.reset();
1298 screen.textAttributes.resetColorPalette();
1299 screen.saveCursorAndState(this.vt);
1300 };
1301 resetScreen(this.primaryScreen_);
1302 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001303
rgindab8bc8932012-04-27 12:45:03 -07001304 // The xterm man page explicitly says this will happen on soft reset.
1305 this.setVTScrollRegion(null, null);
1306
1307 // Xterm also shows the cursor on soft reset, but does not alter the blink
1308 // state.
rgindaa19afe22012-01-25 15:40:22 -08001309 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001310};
1311
rgindac9bc5502012-01-18 11:48:44 -08001312/**
1313 * Move the cursor forward to the next tab stop, or to the last column
1314 * if no more tab stops are set.
1315 */
1316hterm.Terminal.prototype.forwardTabStop = function() {
1317 var column = this.screen_.cursorPosition.column;
1318
1319 for (var i = 0; i < this.tabStops_.length; i++) {
1320 if (this.tabStops_[i] > column) {
1321 this.setCursorColumn(this.tabStops_[i]);
1322 return;
1323 }
1324 }
1325
David Benjamin66e954d2012-05-05 21:08:12 -04001326 // xterm does not clear the overflow flag on HT or CHT.
1327 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001328 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001329 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001330};
1331
rgindac9bc5502012-01-18 11:48:44 -08001332/**
1333 * Move the cursor backward to the previous tab stop, or to the first column
1334 * if no previous tab stops are set.
1335 */
1336hterm.Terminal.prototype.backwardTabStop = function() {
1337 var column = this.screen_.cursorPosition.column;
1338
1339 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1340 if (this.tabStops_[i] < column) {
1341 this.setCursorColumn(this.tabStops_[i]);
1342 return;
1343 }
1344 }
1345
1346 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001347};
1348
rgindac9bc5502012-01-18 11:48:44 -08001349/**
1350 * Set a tab stop at the given column.
1351 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001352 * @param {integer} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001353 */
1354hterm.Terminal.prototype.setTabStop = function(column) {
1355 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1356 if (this.tabStops_[i] == column)
1357 return;
1358
1359 if (this.tabStops_[i] < column) {
1360 this.tabStops_.splice(i + 1, 0, column);
1361 return;
1362 }
1363 }
1364
1365 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001366};
1367
rgindac9bc5502012-01-18 11:48:44 -08001368/**
1369 * Clear the tab stop at the current cursor position.
1370 *
1371 * No effect if there is no tab stop at the current cursor position.
1372 */
1373hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1374 var column = this.screen_.cursorPosition.column;
1375
1376 var i = this.tabStops_.indexOf(column);
1377 if (i == -1)
1378 return;
1379
1380 this.tabStops_.splice(i, 1);
1381};
1382
1383/**
1384 * Clear all tab stops.
1385 */
1386hterm.Terminal.prototype.clearAllTabStops = function() {
1387 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001388 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001389};
1390
1391/**
1392 * Set up the default tab stops, starting from a given column.
1393 *
1394 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001395 * from the specified column, or 0 if no column is provided. It also flags
1396 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001397 *
1398 * This does not clear the existing tab stops first, use clearAllTabStops
1399 * for that.
1400 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001401 * @param {integer} opt_start Optional starting zero based starting column, useful
rgindac9bc5502012-01-18 11:48:44 -08001402 * for filling out missing tab stops when the terminal is resized.
1403 */
1404hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1405 var start = opt_start || 0;
1406 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001407 // Round start up to a default tab stop.
1408 start = start - 1 - ((start - 1) % w) + w;
1409 for (var i = start; i < this.screenSize.width; i += w) {
1410 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001411 }
David Benjamin66e954d2012-05-05 21:08:12 -04001412
1413 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001414};
1415
rginda6d397402012-01-17 10:58:29 -08001416/**
rginda8ba33642011-12-14 12:31:31 -08001417 * Interpret a sequence of characters.
1418 *
1419 * Incomplete escape sequences are buffered until the next call.
1420 *
1421 * @param {string} str Sequence of characters to interpret or pass through.
1422 */
1423hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001424 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001425 this.scheduleSyncCursorPosition_();
1426};
1427
1428/**
1429 * Take over the given DIV for use as the terminal display.
1430 *
1431 * @param {HTMLDivElement} div The div to use as the terminal display.
1432 */
1433hterm.Terminal.prototype.decorate = function(div) {
Mike Frysinger5768a9d2017-12-26 12:57:44 -05001434 const charset = div.ownerDocument.characterSet.toLowerCase();
1435 if (charset != 'utf-8') {
1436 console.warn(`Document encoding should be set to utf-8, not "${charset}";` +
1437 ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`);
1438 }
1439
rginda87b86462011-12-14 13:48:03 -08001440 this.div_ = div;
1441
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001442 this.accessibilityReader_ = new hterm.AccessibilityReader(div);
1443
rginda8ba33642011-12-14 12:31:31 -08001444 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001445 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001446 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1447 this.scrollPort_.setBackgroundPosition(
1448 this.prefs_.get('background-position'));
Mike Frysinger08bad432017-04-24 00:50:54 -04001449 this.scrollPort_.setUserCssUrl(this.prefs_.get('user-css'));
1450 this.scrollPort_.setUserCssText(this.prefs_.get('user-css-text'));
Raymes Khoury177aec72018-06-26 10:58:53 +10001451 this.scrollPort_.setAccessibilityReader(this.accessibilityReader_);
rginda30f20f62012-04-05 16:36:19 -07001452
rginda0918b652012-04-04 11:26:24 -07001453 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001454
rginda9f5222b2012-03-05 11:53:28 -08001455 this.setFontSize(this.prefs_.get('font-size'));
1456 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001457
David Reveman8f552492012-03-28 12:18:41 -04001458 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001459 this.setScrollWheelMoveMultipler(
1460 this.prefs_.get('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001461
rginda8ba33642011-12-14 12:31:31 -08001462 this.document_ = this.scrollPort_.getDocument();
1463
Evan Jones5f9df812016-12-06 09:38:58 -05001464 this.document_.body.oncontextmenu = function() { return false; };
rginda4bba5e12012-06-20 16:15:30 -07001465
1466 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001467 var screenNode = this.scrollPort_.getScreenNode();
1468 screenNode.addEventListener('mousedown', onMouse);
1469 screenNode.addEventListener('mouseup', onMouse);
1470 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001471 this.scrollPort_.onScrollWheel = onMouse;
1472
Mike Frysinger02ded6d2018-06-21 14:25:20 -04001473 screenNode.addEventListener('keydown', this.onKeyboardActivity_.bind(this));
1474
Toni Barzic0bfa8922013-11-22 11:18:35 -08001475 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001476 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001477 // Listen for mousedown events on the screenNode as in FF the focus
1478 // events don't bubble.
1479 screenNode.addEventListener('mousedown', function() {
1480 setTimeout(this.onFocusChange_.bind(this, true));
1481 }.bind(this));
1482
Toni Barzic0bfa8922013-11-22 11:18:35 -08001483 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001484 'blur', this.onFocusChange_.bind(this, false));
1485
1486 var style = this.document_.createElement('style');
1487 style.textContent =
1488 ('.cursor-node[focus="false"] {' +
1489 ' box-sizing: border-box;' +
1490 ' background-color: transparent !important;' +
1491 ' border-width: 2px;' +
1492 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001493 '}' +
1494 '.wc-node {' +
1495 ' display: inline-block;' +
1496 ' text-align: center;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001497 ' width: calc(var(--hterm-charsize-width) * 2);' +
Mike Frysinger4036f6e2017-05-31 14:02:55 -04001498 ' line-height: var(--hterm-charsize-height);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001499 '}' +
1500 ':root {' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001501 ' --hterm-charsize-width: ' + this.scrollPort_.characterSize.width + 'px;' +
1502 ' --hterm-charsize-height: ' + this.scrollPort_.characterSize.height + 'px;' +
Mike Frysingera27c0502017-08-23 22:37:10 -04001503 // Default position hides the cursor for when the window is initializing.
1504 ' --hterm-cursor-offset-col: -1;' +
1505 ' --hterm-cursor-offset-row: -1;' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001506 ' --hterm-blink-node-duration: 0.7s;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001507 ' --hterm-mouse-cursor-text: text;' +
1508 ' --hterm-mouse-cursor-pointer: default;' +
1509 ' --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001510 '}' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001511 '.uri-node:hover {' +
1512 ' text-decoration: underline;' +
Mike Frysingerb74a6472018-06-22 13:37:08 -04001513 ' cursor: var(--hterm-mouse-cursor-pointer), pointer;' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001514 '}' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001515 '@keyframes blink {' +
1516 ' from { opacity: 1.0; }' +
1517 ' to { opacity: 0.0; }' +
1518 '}' +
1519 '.blink-node {' +
1520 ' animation-name: blink;' +
1521 ' animation-duration: var(--hterm-blink-node-duration);' +
1522 ' animation-iteration-count: infinite;' +
1523 ' animation-timing-function: ease-in-out;' +
1524 ' animation-direction: alternate;' +
rginda8e92a692012-05-20 19:37:20 -07001525 '}');
Mike Frysingerb74a6472018-06-22 13:37:08 -04001526 // Insert this stock style as the first node so that any user styles will
1527 // override w/out having to use !important everywhere. The rules above mix
1528 // runtime variables with default ones designed to be overridden by the user,
1529 // but we can wait for a concrete case from the users to determine the best
1530 // way to split the sheet up to before & after the user-css settings.
1531 this.document_.head.insertBefore(style, this.document_.head.firstChild);
rginda8e92a692012-05-20 19:37:20 -07001532
rginda8ba33642011-12-14 12:31:31 -08001533 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001534 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001535 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001536 this.cursorNode_.style.cssText =
1537 ('position: absolute;' +
Mike Frysinger44c32202017-08-05 01:13:09 -04001538 'left: calc(var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));' +
1539 'top: calc(var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));' +
Mike Frysingerd60f4c22018-03-15 21:25:30 -07001540 'display: ' + (this.options_.cursorVisible ? '' : 'none') + ';' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001541 'width: var(--hterm-charsize-width);' +
1542 'height: var(--hterm-charsize-height);' +
Rob Spies06533ba2014-04-24 11:20:37 -07001543 '-webkit-transition: opacity, background-color 100ms linear;' +
1544 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001545
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001546 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001547 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1548 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001549
rginda8ba33642011-12-14 12:31:31 -08001550 this.document_.body.appendChild(this.cursorNode_);
1551
rgindad5613292012-06-19 15:40:37 -07001552 // When 'enableMouseDragScroll' is off we reposition this element directly
1553 // under the mouse cursor after a click. This makes Chrome associate
1554 // subsequent mousemove events with the scroll-blocker. Since the
1555 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1556 // events do not cause the scrollport to scroll.
1557 //
1558 // It's a hack, but it's the cleanest way I could find.
1559 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001560 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
Raymes Khoury6dce2f82018-04-12 15:38:58 +10001561 this.scrollBlockerNode_.setAttribute('aria-hidden', 'true');
rgindad5613292012-06-19 15:40:37 -07001562 this.scrollBlockerNode_.style.cssText =
1563 ('position: absolute;' +
1564 'top: -99px;' +
1565 'display: block;' +
1566 'width: 10px;' +
1567 'height: 10px;');
1568 this.document_.body.appendChild(this.scrollBlockerNode_);
1569
rgindad5613292012-06-19 15:40:37 -07001570 this.scrollPort_.onScrollWheel = onMouse;
1571 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1572 ].forEach(function(event) {
1573 this.scrollBlockerNode_.addEventListener(event, onMouse);
1574 this.cursorNode_.addEventListener(event, onMouse);
1575 this.document_.addEventListener(event, onMouse);
1576 }.bind(this));
1577
1578 this.cursorNode_.addEventListener('mousedown', function() {
1579 setTimeout(this.focus.bind(this));
1580 }.bind(this));
1581
rginda8ba33642011-12-14 12:31:31 -08001582 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001583
rginda87b86462011-12-14 13:48:03 -08001584 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001585 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001586};
1587
rginda0918b652012-04-04 11:26:24 -07001588/**
1589 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001590 *
1591 * @return {HTMLDocument}
rginda0918b652012-04-04 11:26:24 -07001592 */
rginda87b86462011-12-14 13:48:03 -08001593hterm.Terminal.prototype.getDocument = function() {
1594 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001595};
1596
1597/**
rginda0918b652012-04-04 11:26:24 -07001598 * Focus the terminal.
1599 */
1600hterm.Terminal.prototype.focus = function() {
1601 this.scrollPort_.focus();
1602};
1603
1604/**
rginda8ba33642011-12-14 12:31:31 -08001605 * Return the HTML Element for a given row index.
1606 *
1607 * This is a method from the RowProvider interface. The ScrollPort uses
1608 * it to fetch rows on demand as they are scrolled into view.
1609 *
1610 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1611 * pairs to conserve memory.
1612 *
1613 * @param {integer} index The zero-based row index, measured relative to the
1614 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001615 * largest indices.
rginda8ba33642011-12-14 12:31:31 -08001616 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1617 */
1618hterm.Terminal.prototype.getRowNode = function(index) {
1619 if (index < this.scrollbackRows_.length)
1620 return this.scrollbackRows_[index];
1621
1622 var screenIndex = index - this.scrollbackRows_.length;
1623 return this.screen_.rowsArray[screenIndex];
1624};
1625
1626/**
1627 * Return the text content for a given range of rows.
1628 *
1629 * This is a method from the RowProvider interface. The ScrollPort uses
1630 * it to fetch text content on demand when the user attempts to copy their
1631 * selection to the clipboard.
1632 *
1633 * @param {integer} start The zero-based row index to start from, measured
1634 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001635 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001636 * @param {integer} end The zero-based row index to end on, measured
1637 * relative to the start of the scrollback buffer.
1638 * @return {string} A single string containing the text value of the range of
1639 * rows. Lines will be newline delimited, with no trailing newline.
1640 */
1641hterm.Terminal.prototype.getRowsText = function(start, end) {
1642 var ary = [];
1643 for (var i = start; i < end; i++) {
1644 var node = this.getRowNode(i);
1645 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001646 if (i < end - 1 && !node.getAttribute('line-overflow'))
1647 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001648 }
1649
rgindaa09e7332012-08-17 12:49:51 -07001650 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001651};
1652
1653/**
1654 * Return the text content for a given row.
1655 *
1656 * This is a method from the RowProvider interface. The ScrollPort uses
1657 * it to fetch text content on demand when the user attempts to copy their
1658 * selection to the clipboard.
1659 *
1660 * @param {integer} index The zero-based row index to return, measured
1661 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001662 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001663 * @return {string} A string containing the text value of the selected row.
1664 */
1665hterm.Terminal.prototype.getRowText = function(index) {
1666 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001667 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001668};
1669
1670/**
1671 * Return the total number of rows in the addressable screen and in the
1672 * scrollback buffer of this terminal.
1673 *
1674 * This is a method from the RowProvider interface. The ScrollPort uses
1675 * it to compute the size of the scrollbar.
1676 *
1677 * @return {integer} The number of rows in this terminal.
1678 */
1679hterm.Terminal.prototype.getRowCount = function() {
1680 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1681};
1682
1683/**
1684 * Create DOM nodes for new rows and append them to the end of the terminal.
1685 *
1686 * This is the only correct way to add a new DOM node for a row. Notice that
1687 * the new row is appended to the bottom of the list of rows, and does not
1688 * require renumbering (of the rowIndex property) of previous rows.
1689 *
1690 * If you think you want a new blank row somewhere in the middle of the
1691 * terminal, look into moveRows_().
1692 *
1693 * This method does not pay attention to vtScrollTop/Bottom, since you should
1694 * be using moveRows() in cases where they would matter.
1695 *
1696 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001697 *
1698 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001699 */
1700hterm.Terminal.prototype.appendRows_ = function(count) {
1701 var cursorRow = this.screen_.rowsArray.length;
1702 var offset = this.scrollbackRows_.length + cursorRow;
1703 for (var i = 0; i < count; i++) {
1704 var row = this.document_.createElement('x-row');
1705 row.appendChild(this.document_.createTextNode(''));
1706 row.rowIndex = offset + i;
1707 this.screen_.pushRow(row);
1708 }
1709
1710 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1711 if (extraRows > 0) {
1712 var ary = this.screen_.shiftRows(extraRows);
1713 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001714 if (this.scrollPort_.isScrolledEnd)
1715 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001716 }
1717
1718 if (cursorRow >= this.screen_.rowsArray.length)
1719 cursorRow = this.screen_.rowsArray.length - 1;
1720
rginda87b86462011-12-14 13:48:03 -08001721 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001722};
1723
1724/**
1725 * Relocate rows from one part of the addressable screen to another.
1726 *
1727 * This is used to recycle rows during VT scrolls (those which are driven
1728 * by VT commands, rather than by the user manipulating the scrollbar.)
1729 *
1730 * In this case, the blank lines scrolled into the scroll region are made of
1731 * the nodes we scrolled off. These have their rowIndex properties carefully
1732 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001733 *
1734 * @param {number} fromIndex The start index.
1735 * @param {number} count The number of rows to move.
1736 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001737 */
1738hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1739 var ary = this.screen_.removeRows(fromIndex, count);
1740 this.screen_.insertRows(toIndex, ary);
1741
1742 var start, end;
1743 if (fromIndex < toIndex) {
1744 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001745 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001746 } else {
1747 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001748 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001749 }
1750
1751 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001752 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001753};
1754
1755/**
1756 * Renumber the rowIndex property of the given range of rows.
1757 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001758 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001759 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001760 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001761 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001762 *
1763 * @param {number} start The start index.
1764 * @param {number} end The end index.
1765 * @param {hterm.Screen} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001766 */
Robert Ginda40932892012-12-10 17:26:40 -08001767hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1768 var screen = opt_screen || this.screen_;
1769
rginda8ba33642011-12-14 12:31:31 -08001770 var offset = this.scrollbackRows_.length;
1771 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001772 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001773 }
1774};
1775
1776/**
1777 * Print a string to the terminal.
1778 *
1779 * This respects the current insert and wraparound modes. It will add new lines
1780 * to the end of the terminal, scrolling off the top into the scrollback buffer
1781 * if necessary.
1782 *
1783 * The string is *not* parsed for escape codes. Use the interpret() method if
1784 * that's what you're after.
1785 *
1786 * @param{string} str The string to print.
1787 */
1788hterm.Terminal.prototype.print = function(str) {
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001789 // Basic accessibility output for the screen reader.
Raymes Khoury177aec72018-06-26 10:58:53 +10001790 this.accessibilityReader_.announce(str);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001791
rgindaa9abdd82012-08-06 18:05:09 -07001792 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001793
Ricky Liang48f05cb2013-12-31 23:35:29 +08001794 var strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04001795 // Fun edge case: If the string only contains zero width codepoints (like
1796 // combining characters), we make sure to iterate at least once below.
1797 if (strWidth == 0 && str)
1798 strWidth = 1;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001799
1800 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001801 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1802 this.screen_.commitLineOverflow();
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001803 this.newLine(true);
rgindaa09e7332012-08-17 12:49:51 -07001804 }
rgindaa19afe22012-01-25 15:40:22 -08001805
Ricky Liang48f05cb2013-12-31 23:35:29 +08001806 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001807 var didOverflow = false;
1808 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001809
rgindaa9abdd82012-08-06 18:05:09 -07001810 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1811 didOverflow = true;
1812 count = this.screenSize.width - this.screen_.cursorPosition.column;
1813 }
rgindaa19afe22012-01-25 15:40:22 -08001814
rgindaa9abdd82012-08-06 18:05:09 -07001815 if (didOverflow && !this.options_.wraparound) {
1816 // If the string overflowed the line but wraparound is off, then the
1817 // last printed character should be the last of the string.
1818 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001819 substr = lib.wc.substr(str, startOffset, count - 1) +
1820 lib.wc.substr(str, strWidth - 1);
1821 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001822 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001823 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001824 }
rgindaa19afe22012-01-25 15:40:22 -08001825
Ricky Liang48f05cb2013-12-31 23:35:29 +08001826 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1827 for (var i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001828 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
1829 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001830
1831 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001832 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001833 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001834 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001835 }
1836 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001837 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07001838 }
1839
1840 this.screen_.maybeClipCurrentRow();
1841 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001842 }
rginda8ba33642011-12-14 12:31:31 -08001843
1844 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001845
rginda9f5222b2012-03-05 11:53:28 -08001846 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001847 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001848};
1849
1850/**
rginda87b86462011-12-14 13:48:03 -08001851 * Set the VT scroll region.
1852 *
rginda87b86462011-12-14 13:48:03 -08001853 * This also resets the cursor position to the absolute (0, 0) position, since
1854 * that's what xterm appears to do.
1855 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001856 * Setting the scroll region to the full height of the terminal will clear
1857 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1858 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1859 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1860 * continue to work as most users would expect.
1861 *
rginda87b86462011-12-14 13:48:03 -08001862 * @param {integer} scrollTop The zero-based top of the scroll region.
1863 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1864 * inclusive.
1865 */
1866hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001867 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001868 this.vtScrollTop_ = null;
1869 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001870 } else {
1871 this.vtScrollTop_ = scrollTop;
1872 this.vtScrollBottom_ = scrollBottom;
1873 }
rginda87b86462011-12-14 13:48:03 -08001874};
1875
1876/**
rginda8ba33642011-12-14 12:31:31 -08001877 * Return the top row index according to the VT.
1878 *
1879 * This will return 0 unless the terminal has been told to restrict scrolling
1880 * to some lower row. It is used for some VT cursor positioning and scrolling
1881 * commands.
1882 *
1883 * @return {integer} The topmost row in the terminal's scroll region.
1884 */
1885hterm.Terminal.prototype.getVTScrollTop = function() {
1886 if (this.vtScrollTop_ != null)
1887 return this.vtScrollTop_;
1888
1889 return 0;
rginda87b86462011-12-14 13:48:03 -08001890};
rginda8ba33642011-12-14 12:31:31 -08001891
1892/**
1893 * Return the bottom row index according to the VT.
1894 *
1895 * This will return the height of the terminal unless the it has been told to
1896 * restrict scrolling to some higher row. It is used for some VT cursor
1897 * positioning and scrolling commands.
1898 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001899 * @return {integer} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001900 */
1901hterm.Terminal.prototype.getVTScrollBottom = function() {
1902 if (this.vtScrollBottom_ != null)
1903 return this.vtScrollBottom_;
1904
rginda87b86462011-12-14 13:48:03 -08001905 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001906};
rginda8ba33642011-12-14 12:31:31 -08001907
1908/**
1909 * Process a '\n' character.
1910 *
1911 * If the cursor is on the final row of the terminal this will append a new
1912 * blank row to the screen and scroll the topmost row into the scrollback
1913 * buffer.
1914 *
1915 * Otherwise, this moves the cursor to column zero of the next row.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001916 *
1917 * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of
1918 * the terminal.
rginda8ba33642011-12-14 12:31:31 -08001919 */
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001920hterm.Terminal.prototype.newLine = function(dueToOverflow = false) {
1921 if (!dueToOverflow)
1922 this.accessibilityReader_.newLine();
1923
Robert Ginda9937abc2013-07-25 16:09:23 -07001924 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1925 this.screen_.rowsArray.length - 1);
1926
1927 if (this.vtScrollBottom_ != null) {
1928 // A VT Scroll region is active, we never append new rows.
1929 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1930 // We're at the end of the VT Scroll Region, perform a VT scroll.
1931 this.vtScrollUp(1);
1932 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1933 } else if (cursorAtEndOfScreen) {
1934 // We're at the end of the screen, the only thing to do is put the
1935 // cursor to column 0.
1936 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1937 } else {
1938 // Anywhere else, advance the cursor row, and reset the column.
1939 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1940 }
1941 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001942 // We're at the end of the screen. Append a new row to the terminal,
1943 // shifting the top row into the scrollback.
1944 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001945 } else {
rginda87b86462011-12-14 13:48:03 -08001946 // Anywhere else in the screen just moves the cursor.
1947 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001948 }
1949};
1950
1951/**
1952 * Like newLine(), except maintain the cursor column.
1953 */
1954hterm.Terminal.prototype.lineFeed = function() {
1955 var column = this.screen_.cursorPosition.column;
1956 this.newLine();
1957 this.setCursorColumn(column);
1958};
1959
1960/**
rginda87b86462011-12-14 13:48:03 -08001961 * If autoCarriageReturn is set then newLine(), else lineFeed().
1962 */
1963hterm.Terminal.prototype.formFeed = function() {
1964 if (this.options_.autoCarriageReturn) {
1965 this.newLine();
1966 } else {
1967 this.lineFeed();
1968 }
1969};
1970
1971/**
1972 * Move the cursor up one row, possibly inserting a blank line.
1973 *
1974 * The cursor column is not changed.
1975 */
1976hterm.Terminal.prototype.reverseLineFeed = function() {
1977 var scrollTop = this.getVTScrollTop();
1978 var currentRow = this.screen_.cursorPosition.row;
1979
1980 if (currentRow == scrollTop) {
1981 this.insertLines(1);
1982 } else {
1983 this.setAbsoluteCursorRow(currentRow - 1);
1984 }
1985};
1986
1987/**
rginda8ba33642011-12-14 12:31:31 -08001988 * Replace all characters to the left of the current cursor with the space
1989 * character.
1990 *
1991 * TODO(rginda): This should probably *remove* the characters (not just replace
1992 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001993 * position.
rginda8ba33642011-12-14 12:31:31 -08001994 */
1995hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001996 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001997 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04001998 const count = cursor.column + 1;
1999 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08002000 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002001};
2002
2003/**
David Benjamin684a9b72012-05-01 17:19:58 -04002004 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08002005 *
2006 * The cursor position is unchanged.
2007 *
Robert Gindaf2547f12012-10-25 20:36:21 -07002008 * If the current background color is not the default background color this
2009 * will insert spaces rather than delete. This is unfortunate because the
2010 * trailing space will affect text selection, but it's difficult to come up
2011 * with a way to style empty space that wouldn't trip up the hterm.Screen
2012 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07002013 *
2014 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
2015 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2016 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002017 *
2018 * @param {number} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002019 */
2020hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002021 if (this.screen_.cursorPosition.overflow)
2022 return;
2023
Robert Ginda7fd57082012-09-25 14:41:47 -07002024 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
2025 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002026
2027 if (this.screen_.textAttributes.background ===
2028 this.screen_.textAttributes.DEFAULT_COLOR) {
2029 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002030 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002031 this.screen_.cursorPosition.column + count) {
2032 this.screen_.deleteChars(count);
2033 this.clearCursorOverflow();
2034 return;
2035 }
2036 }
2037
rginda87b86462011-12-14 13:48:03 -08002038 var cursor = this.saveCursor();
Mike Frysinger6380bed2017-08-24 18:46:39 -04002039 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08002040 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002041 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002042};
2043
2044/**
2045 * Erase the current line.
2046 *
2047 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002048 */
2049hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08002050 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002051 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002052 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002053 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002054};
2055
2056/**
David Benjamina08d78f2012-05-05 00:28:49 -04002057 * Erase all characters from the start of the screen to the current cursor
2058 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002059 *
2060 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002061 */
2062hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08002063 var cursor = this.saveCursor();
2064
2065 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002066
David Benjamina08d78f2012-05-05 00:28:49 -04002067 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002068 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002069 this.screen_.clearCursorRow();
2070 }
2071
rginda87b86462011-12-14 13:48:03 -08002072 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002073 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002074};
2075
2076/**
2077 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002078 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002079 *
2080 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002081 */
2082hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08002083 var cursor = this.saveCursor();
2084
2085 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002086
David Benjamina08d78f2012-05-05 00:28:49 -04002087 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08002088 for (var i = cursor.row + 1; i <= bottom; i++) {
2089 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002090 this.screen_.clearCursorRow();
2091 }
2092
rginda87b86462011-12-14 13:48:03 -08002093 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002094 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002095};
2096
2097/**
2098 * Fill the terminal with a given character.
2099 *
2100 * This methods does not respect the VT scroll region.
2101 *
2102 * @param {string} ch The character to use for the fill.
2103 */
2104hterm.Terminal.prototype.fill = function(ch) {
2105 var cursor = this.saveCursor();
2106
2107 this.setAbsoluteCursorPosition(0, 0);
2108 for (var row = 0; row < this.screenSize.height; row++) {
2109 for (var col = 0; col < this.screenSize.width; col++) {
2110 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002111 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002112 }
2113 }
2114
2115 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002116};
2117
2118/**
rginda9ea433c2012-03-16 11:57:00 -07002119 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002120 *
rginda9ea433c2012-03-16 11:57:00 -07002121 * This does not respect the scroll region.
2122 *
2123 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2124 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002125 */
rginda9ea433c2012-03-16 11:57:00 -07002126hterm.Terminal.prototype.clearHome = function(opt_screen) {
2127 var screen = opt_screen || this.screen_;
2128 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002129
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002130 this.accessibilityReader_.clear();
2131
rginda11057d52012-04-25 12:29:56 -07002132 if (bottom == 0) {
2133 // Empty screen, nothing to do.
2134 return;
2135 }
2136
rgindae4d29232012-01-19 10:47:13 -08002137 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002138 screen.setCursorPosition(i, 0);
2139 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002140 }
2141
rginda9ea433c2012-03-16 11:57:00 -07002142 screen.setCursorPosition(0, 0);
2143};
2144
2145/**
2146 * Erase the entire display without changing the cursor position.
2147 *
2148 * The cursor position is unchanged. This does not respect the scroll
2149 * region.
2150 *
2151 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2152 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002153 */
2154hterm.Terminal.prototype.clear = function(opt_screen) {
2155 var screen = opt_screen || this.screen_;
2156 var cursor = screen.cursorPosition.clone();
2157 this.clearHome(screen);
2158 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002159};
2160
2161/**
2162 * VT command to insert lines at the current cursor row.
2163 *
2164 * This respects the current scroll region. Rows pushed off the bottom are
2165 * lost (they won't show up in the scrollback buffer).
2166 *
rginda8ba33642011-12-14 12:31:31 -08002167 * @param {integer} count The number of lines to insert.
2168 */
2169hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002170 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002171
2172 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002173 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002174
Robert Ginda579186b2012-09-26 11:40:04 -07002175 // The moveCount is the number of rows we need to relocate to make room for
2176 // the new row(s). The count is the distance to move them.
2177 var moveCount = bottom - cursorRow - count + 1;
2178 if (moveCount)
2179 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002180
Robert Ginda579186b2012-09-26 11:40:04 -07002181 for (var i = count - 1; i >= 0; i--) {
2182 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002183 this.screen_.clearCursorRow();
2184 }
rginda8ba33642011-12-14 12:31:31 -08002185};
2186
2187/**
2188 * VT command to delete lines at the current cursor row.
2189 *
2190 * New rows are added to the bottom of scroll region to take their place. New
2191 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002192 *
2193 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002194 */
2195hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002196 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002197
rginda87b86462011-12-14 13:48:03 -08002198 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002199 var bottom = this.getVTScrollBottom();
2200
rginda87b86462011-12-14 13:48:03 -08002201 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002202 count = Math.min(count, maxCount);
2203
rginda87b86462011-12-14 13:48:03 -08002204 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002205 if (count != maxCount)
2206 this.moveRows_(top, count, moveStart);
2207
2208 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002209 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002210 this.screen_.clearCursorRow();
2211 }
2212
rginda87b86462011-12-14 13:48:03 -08002213 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002214 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002215};
2216
2217/**
2218 * Inserts the given number of spaces at the current cursor position.
2219 *
rginda87b86462011-12-14 13:48:03 -08002220 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002221 *
2222 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002223 */
2224hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002225 var cursor = this.saveCursor();
2226
rgindacbbd7482012-06-13 15:06:16 -07002227 var ws = lib.f.getWhitespace(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002228 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002229 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002230
2231 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002232 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002233};
2234
2235/**
2236 * Forward-delete the specified number of characters starting at the cursor
2237 * position.
2238 *
2239 * @param {integer} count The number of characters to delete.
2240 */
2241hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002242 var deleted = this.screen_.deleteChars(count);
2243 if (deleted && !this.screen_.textAttributes.isDefault()) {
2244 var cursor = this.saveCursor();
2245 this.setCursorColumn(this.screenSize.width - deleted);
2246 this.screen_.insertString(lib.f.getWhitespace(deleted));
2247 this.restoreCursor(cursor);
2248 }
2249
David Benjamin54e8bf62012-06-01 22:31:40 -04002250 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002251};
2252
2253/**
2254 * Shift rows in the scroll region upwards by a given number of lines.
2255 *
2256 * New rows are inserted at the bottom of the scroll region to fill the
2257 * vacated rows. The new rows not filled out with the current text attributes.
2258 *
2259 * This function does not affect the scrollback rows at all. Rows shifted
2260 * off the top are lost.
2261 *
rginda87b86462011-12-14 13:48:03 -08002262 * The cursor position is not altered.
2263 *
rginda8ba33642011-12-14 12:31:31 -08002264 * @param {integer} count The number of rows to scroll.
2265 */
2266hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002267 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002268
rginda87b86462011-12-14 13:48:03 -08002269 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002270 this.deleteLines(count);
2271
rginda87b86462011-12-14 13:48:03 -08002272 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002273};
2274
2275/**
2276 * Shift rows below the cursor down by a given number of lines.
2277 *
2278 * This function respects the current scroll region.
2279 *
2280 * New rows are inserted at the top of the scroll region to fill the
2281 * vacated rows. The new rows not filled out with the current text attributes.
2282 *
2283 * This function does not affect the scrollback rows at all. Rows shifted
2284 * off the bottom are lost.
2285 *
2286 * @param {integer} count The number of rows to scroll.
2287 */
2288hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08002289 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002290
rginda87b86462011-12-14 13:48:03 -08002291 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08002292 this.insertLines(opt_count);
2293
rginda87b86462011-12-14 13:48:03 -08002294 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002295};
2296
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002297/**
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002298 * Enable accessibility-friendly features that have a performance impact.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002299 *
2300 * This will generate additional DOM nodes in an aria-live region that will
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002301 * cause Assitive Technology to announce the output of the terminal. It also
2302 * enables other features that aid assistive technology. All the features gated
2303 * behind this flag have a performance impact on the terminal which is why they
2304 * are made optional.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002305 *
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002306 * @param {boolean} enabled Whether to enable accessibility-friendly features.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002307 */
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002308hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) {
Raymes Khoury177aec72018-06-26 10:58:53 +10002309 this.accessibilityReader_.setAccessibilityEnabled(enabled);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002310};
rginda87b86462011-12-14 13:48:03 -08002311
rginda8ba33642011-12-14 12:31:31 -08002312/**
2313 * Set the cursor position.
2314 *
2315 * The cursor row is relative to the scroll region if the terminal has
2316 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2317 *
2318 * @param {integer} row The new zero-based cursor row.
2319 * @param {integer} row The new zero-based cursor column.
2320 */
2321hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2322 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002323 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002324 } else {
rginda87b86462011-12-14 13:48:03 -08002325 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002326 }
rginda87b86462011-12-14 13:48:03 -08002327};
rginda8ba33642011-12-14 12:31:31 -08002328
Evan Jones2600d4f2016-12-06 09:29:36 -05002329/**
2330 * Move the cursor relative to its current position.
2331 *
2332 * @param {number} row
2333 * @param {number} column
2334 */
rginda87b86462011-12-14 13:48:03 -08002335hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2336 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002337 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2338 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002339 this.screen_.setCursorPosition(row, column);
2340};
2341
Evan Jones2600d4f2016-12-06 09:29:36 -05002342/**
2343 * Move the cursor to the specified position.
2344 *
2345 * @param {number} row
2346 * @param {number} column
2347 */
rginda87b86462011-12-14 13:48:03 -08002348hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002349 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2350 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002351 this.screen_.setCursorPosition(row, column);
2352};
2353
2354/**
2355 * Set the cursor column.
2356 *
2357 * @param {integer} column The new zero-based cursor column.
2358 */
2359hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002360 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002361};
2362
2363/**
2364 * Return the cursor column.
2365 *
2366 * @return {integer} The zero-based cursor column.
2367 */
2368hterm.Terminal.prototype.getCursorColumn = function() {
2369 return this.screen_.cursorPosition.column;
2370};
2371
2372/**
2373 * Set the cursor row.
2374 *
2375 * The cursor row is relative to the scroll region if the terminal has
2376 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2377 *
2378 * @param {integer} row The new cursor row.
2379 */
rginda87b86462011-12-14 13:48:03 -08002380hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2381 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002382};
2383
2384/**
2385 * Return the cursor row.
2386 *
2387 * @return {integer} The zero-based cursor row.
2388 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002389hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002390 return this.screen_.cursorPosition.row;
2391};
2392
2393/**
2394 * Request that the ScrollPort redraw itself soon.
2395 *
2396 * The redraw will happen asynchronously, soon after the call stack winds down.
2397 * Multiple calls will be coalesced into a single redraw.
2398 */
2399hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002400 if (this.timeouts_.redraw)
2401 return;
rginda8ba33642011-12-14 12:31:31 -08002402
2403 var self = this;
rginda87b86462011-12-14 13:48:03 -08002404 this.timeouts_.redraw = setTimeout(function() {
2405 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002406 self.scrollPort_.redraw_();
2407 }, 0);
2408};
2409
2410/**
2411 * Request that the ScrollPort be scrolled to the bottom.
2412 *
2413 * The scroll will happen asynchronously, soon after the call stack winds down.
2414 * Multiple calls will be coalesced into a single scroll.
2415 *
2416 * This affects the scrollbar position of the ScrollPort, and has nothing to
2417 * do with the VT scroll commands.
2418 */
2419hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2420 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002421 return;
rginda8ba33642011-12-14 12:31:31 -08002422
2423 var self = this;
2424 this.timeouts_.scrollDown = setTimeout(function() {
2425 delete self.timeouts_.scrollDown;
2426 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2427 }, 10);
2428};
2429
2430/**
2431 * Move the cursor up a specified number of rows.
2432 *
2433 * @param {integer} count The number of rows to move the cursor.
2434 */
2435hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002436 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002437};
2438
2439/**
2440 * Move the cursor down a specified number of rows.
2441 *
2442 * @param {integer} count The number of rows to move the cursor.
2443 */
2444hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002445 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002446 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2447 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2448 this.screenSize.height - 1);
2449
rgindacbbd7482012-06-13 15:06:16 -07002450 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002451 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002452 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002453};
2454
2455/**
2456 * Move the cursor left a specified number of columns.
2457 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002458 * If reverse wraparound mode is enabled and the previous row wrapped into
2459 * the current row then we back up through the wraparound as well.
2460 *
rginda8ba33642011-12-14 12:31:31 -08002461 * @param {integer} count The number of columns to move the cursor.
2462 */
2463hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002464 count = count || 1;
2465
2466 if (count < 1)
2467 return;
2468
2469 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002470 if (this.options_.reverseWraparound) {
2471 if (this.screen_.cursorPosition.overflow) {
2472 // If this cursor is in the right margin, consume one count to get it
2473 // back to the last column. This only applies when we're in reverse
2474 // wraparound mode.
2475 count--;
2476 this.clearCursorOverflow();
2477
2478 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002479 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002480 }
2481
Robert Gindabfb32622014-07-17 13:20:27 -07002482 var newRow = this.screen_.cursorPosition.row;
2483 var newColumn = currentColumn - count;
2484 if (newColumn < 0) {
2485 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2486 if (newRow < 0) {
2487 // xterm also wraps from row 0 to the last row.
2488 newRow = this.screenSize.height + newRow % this.screenSize.height;
2489 }
2490 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2491 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002492
Robert Gindabfb32622014-07-17 13:20:27 -07002493 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2494
2495 } else {
2496 var newColumn = Math.max(currentColumn - count, 0);
2497 this.setCursorColumn(newColumn);
2498 }
rginda8ba33642011-12-14 12:31:31 -08002499};
2500
2501/**
2502 * Move the cursor right a specified number of columns.
2503 *
2504 * @param {integer} count The number of columns to move the cursor.
2505 */
2506hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002507 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002508
2509 if (count < 1)
2510 return;
2511
rgindacbbd7482012-06-13 15:06:16 -07002512 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002513 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002514 this.setCursorColumn(column);
2515};
2516
2517/**
2518 * Reverse the foreground and background colors of the terminal.
2519 *
2520 * This only affects text that was drawn with no attributes.
2521 *
2522 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2523 * been drawn with attributes that happen to coincide with the default
2524 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002525 *
2526 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002527 */
2528hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002529 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002530 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002531 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2532 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002533 } else {
rginda9f5222b2012-03-05 11:53:28 -08002534 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2535 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002536 }
2537};
2538
2539/**
rginda87b86462011-12-14 13:48:03 -08002540 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002541 *
2542 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002543 */
2544hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002545 this.cursorNode_.style.backgroundColor =
2546 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002547
2548 var self = this;
2549 setTimeout(function() {
Matheus Fernandes2d733082017-09-11 06:43:01 -04002550 self.restyleCursor_();
rginda6d397402012-01-17 10:58:29 -08002551 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002552
Michael Kelly485ecd12014-06-09 11:41:56 -04002553 // bellSquelchTimeout_ affects both audio and notification bells.
2554 if (this.bellSquelchTimeout_)
2555 return;
2556
Robert Ginda92e18102013-03-14 13:56:37 -07002557 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002558 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002559 this.bellSequelchTimeout_ = setTimeout(function() {
2560 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002561 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002562 } else {
2563 delete this.bellSquelchTimeout_;
2564 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002565
2566 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002567 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002568 this.bellNotificationList_.push(n);
2569 // TODO: Should we try to raise the window here?
2570 n.onclick = function() { self.closeBellNotifications_(); };
2571 }
rginda87b86462011-12-14 13:48:03 -08002572};
2573
2574/**
rginda8ba33642011-12-14 12:31:31 -08002575 * Set the origin mode bit.
2576 *
2577 * If origin mode is on, certain VT cursor and scrolling commands measure their
2578 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2579 * to the top of the addressable screen.
2580 *
2581 * Defaults to off.
2582 *
2583 * @param {boolean} state True to set origin mode, false to unset.
2584 */
2585hterm.Terminal.prototype.setOriginMode = function(state) {
2586 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002587 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002588};
2589
2590/**
2591 * Set the insert mode bit.
2592 *
2593 * If insert mode is on, existing text beyond the cursor position will be
2594 * shifted right to make room for new text. Otherwise, new text overwrites
2595 * any existing text.
2596 *
2597 * Defaults to off.
2598 *
2599 * @param {boolean} state True to set insert mode, false to unset.
2600 */
2601hterm.Terminal.prototype.setInsertMode = function(state) {
2602 this.options_.insertMode = state;
2603};
2604
2605/**
rginda87b86462011-12-14 13:48:03 -08002606 * Set the auto carriage return bit.
2607 *
2608 * If auto carriage return is on then a formfeed character is interpreted
2609 * as a newline, otherwise it's the same as a linefeed. The difference boils
2610 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002611 *
2612 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002613 */
2614hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2615 this.options_.autoCarriageReturn = state;
2616};
2617
2618/**
rginda8ba33642011-12-14 12:31:31 -08002619 * Set the wraparound mode bit.
2620 *
2621 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2622 * to the start of the following row. Otherwise, the cursor is clamped to the
2623 * end of the screen and attempts to write past it are ignored.
2624 *
2625 * Defaults to on.
2626 *
2627 * @param {boolean} state True to set wraparound mode, false to unset.
2628 */
2629hterm.Terminal.prototype.setWraparound = function(state) {
2630 this.options_.wraparound = state;
2631};
2632
2633/**
2634 * Set the reverse-wraparound mode bit.
2635 *
2636 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2637 * to the end of the previous row. Otherwise, the cursor is clamped to column
2638 * 0.
2639 *
2640 * Defaults to off.
2641 *
2642 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2643 */
2644hterm.Terminal.prototype.setReverseWraparound = function(state) {
2645 this.options_.reverseWraparound = state;
2646};
2647
2648/**
2649 * Selects between the primary and alternate screens.
2650 *
2651 * If alternate mode is on, the alternate screen is active. Otherwise the
2652 * primary screen is active.
2653 *
2654 * Swapping screens has no effect on the scrollback buffer.
2655 *
2656 * Each screen maintains its own cursor position.
2657 *
2658 * Defaults to off.
2659 *
2660 * @param {boolean} state True to set alternate mode, false to unset.
2661 */
2662hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002663 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002664 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2665
rginda35c456b2012-02-09 17:29:05 -08002666 if (this.screen_.rowsArray.length &&
2667 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2668 // If the screen changed sizes while we were away, our rowIndexes may
2669 // be incorrect.
2670 var offset = this.scrollbackRows_.length;
2671 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002672 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002673 ary[i].rowIndex = offset + i;
2674 }
2675 }
rginda8ba33642011-12-14 12:31:31 -08002676
rginda35c456b2012-02-09 17:29:05 -08002677 this.realizeWidth_(this.screenSize.width);
2678 this.realizeHeight_(this.screenSize.height);
2679 this.scrollPort_.syncScrollHeight();
2680 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002681
rginda6d397402012-01-17 10:58:29 -08002682 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002683 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002684};
2685
2686/**
2687 * Set the cursor-blink mode bit.
2688 *
2689 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2690 * a visible cursor does not blink.
2691 *
2692 * You should make sure to turn blinking off if you're going to dispose of a
2693 * terminal, otherwise you'll leak a timeout.
2694 *
2695 * Defaults to on.
2696 *
2697 * @param {boolean} state True to set cursor-blink mode, false to unset.
2698 */
2699hterm.Terminal.prototype.setCursorBlink = function(state) {
2700 this.options_.cursorBlink = state;
2701
2702 if (!state && this.timeouts_.cursorBlink) {
2703 clearTimeout(this.timeouts_.cursorBlink);
2704 delete this.timeouts_.cursorBlink;
2705 }
2706
2707 if (this.options_.cursorVisible)
2708 this.setCursorVisible(true);
2709};
2710
2711/**
2712 * Set the cursor-visible mode bit.
2713 *
2714 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2715 *
2716 * Defaults to on.
2717 *
2718 * @param {boolean} state True to set cursor-visible mode, false to unset.
2719 */
2720hterm.Terminal.prototype.setCursorVisible = function(state) {
2721 this.options_.cursorVisible = state;
2722
2723 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002724 if (this.timeouts_.cursorBlink) {
2725 clearTimeout(this.timeouts_.cursorBlink);
2726 delete this.timeouts_.cursorBlink;
2727 }
rginda87b86462011-12-14 13:48:03 -08002728 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002729 return;
2730 }
2731
rginda87b86462011-12-14 13:48:03 -08002732 this.syncCursorPosition_();
2733
2734 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002735
2736 if (this.options_.cursorBlink) {
2737 if (this.timeouts_.cursorBlink)
2738 return;
2739
Robert Gindaea2183e2014-07-17 09:51:51 -07002740 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002741 } else {
2742 if (this.timeouts_.cursorBlink) {
2743 clearTimeout(this.timeouts_.cursorBlink);
2744 delete this.timeouts_.cursorBlink;
2745 }
2746 }
2747};
2748
2749/**
rginda87b86462011-12-14 13:48:03 -08002750 * Synchronizes the visible cursor and document selection with the current
2751 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002752 */
2753hterm.Terminal.prototype.syncCursorPosition_ = function() {
2754 var topRowIndex = this.scrollPort_.getTopRowIndex();
2755 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2756 var cursorRowIndex = this.scrollbackRows_.length +
2757 this.screen_.cursorPosition.row;
2758
2759 if (cursorRowIndex > bottomRowIndex) {
2760 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04002761 this.setCssVar('cursor-offset-row', '-1');
rginda8ba33642011-12-14 12:31:31 -08002762 return;
2763 }
2764
Robert Gindab837c052014-08-11 11:17:51 -07002765 if (this.options_.cursorVisible &&
2766 this.cursorNode_.style.display == 'none') {
2767 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2768 this.cursorNode_.style.display = '';
2769 }
2770
Mike Frysinger44c32202017-08-05 01:13:09 -04002771 // Position the cursor using CSS variable math. If we do the math in JS,
2772 // the float math will end up being more precise than the CSS which will
2773 // cause the cursor tracking to be off.
2774 this.setCssVar(
2775 'cursor-offset-row',
2776 `${cursorRowIndex - topRowIndex} + ` +
2777 `${this.scrollPort_.visibleRowTopMargin}px`);
2778 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08002779
2780 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04002781 '(' + this.screen_.cursorPosition.column +
2782 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08002783 ')');
2784
2785 // Update the caret for a11y purposes.
2786 var selection = this.document_.getSelection();
2787 if (selection && selection.isCollapsed)
2788 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002789};
2790
Robert Gindafb1be6a2013-12-11 11:56:22 -08002791/**
2792 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2793 * and character cell dimensions.
2794 */
Robert Ginda830583c2013-08-07 13:20:46 -07002795hterm.Terminal.prototype.restyleCursor_ = function() {
2796 var shape = this.cursorShape_;
2797
2798 if (this.cursorNode_.getAttribute('focus') == 'false') {
2799 // Always show a block cursor when unfocused.
2800 shape = hterm.Terminal.cursorShape.BLOCK;
2801 }
2802
2803 var style = this.cursorNode_.style;
2804
2805 switch (shape) {
2806 case hterm.Terminal.cursorShape.BEAM:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002807 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002808 style.backgroundColor = 'transparent';
2809 style.borderBottomStyle = null;
2810 style.borderLeftStyle = 'solid';
2811 break;
2812
2813 case hterm.Terminal.cursorShape.UNDERLINE:
2814 style.height = this.scrollPort_.characterSize.baseline + 'px';
2815 style.backgroundColor = 'transparent';
2816 style.borderBottomStyle = 'solid';
2817 // correct the size to put it exactly at the baseline
2818 style.borderLeftStyle = null;
2819 break;
2820
2821 default:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002822 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002823 style.backgroundColor = this.cursorColor_;
2824 style.borderBottomStyle = null;
2825 style.borderLeftStyle = null;
2826 break;
2827 }
2828};
2829
rginda8ba33642011-12-14 12:31:31 -08002830/**
2831 * Synchronizes the visible cursor with the current cursor coordinates.
2832 *
2833 * The sync will happen asynchronously, soon after the call stack winds down.
2834 * Multiple calls will be coalesced into a single sync.
2835 */
2836hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2837 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002838 return;
rginda8ba33642011-12-14 12:31:31 -08002839
2840 var self = this;
2841 this.timeouts_.syncCursor = setTimeout(function() {
2842 self.syncCursorPosition_();
2843 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002844 }, 0);
2845};
2846
rgindacc2996c2012-02-24 14:59:31 -08002847/**
rgindaf522ce02012-04-17 17:49:17 -07002848 * Show or hide the zoom warning.
2849 *
2850 * The zoom warning is a message warning the user that their browser zoom must
2851 * be set to 100% in order for hterm to function properly.
2852 *
2853 * @param {boolean} state True to show the message, false to hide it.
2854 */
2855hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2856 if (!this.zoomWarningNode_) {
2857 if (!state)
2858 return;
2859
2860 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002861 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07002862 this.zoomWarningNode_.style.cssText = (
2863 'color: black;' +
2864 'background-color: #ff2222;' +
2865 'font-size: large;' +
2866 'border-radius: 8px;' +
2867 'opacity: 0.75;' +
2868 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2869 'top: 0.5em;' +
2870 'right: 1.2em;' +
2871 'position: absolute;' +
2872 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002873 '-webkit-user-select: none;' +
2874 '-moz-text-size-adjust: none;' +
2875 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002876
2877 this.zoomWarningNode_.addEventListener('click', function(e) {
2878 this.parentNode.removeChild(this);
2879 });
rgindaf522ce02012-04-17 17:49:17 -07002880 }
2881
Robert Gindab4839c22013-02-28 16:52:10 -08002882 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2883 hterm.zoomWarningMessage,
2884 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2885
rgindaf522ce02012-04-17 17:49:17 -07002886 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2887
2888 if (state) {
2889 if (!this.zoomWarningNode_.parentNode)
2890 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2891 } else if (this.zoomWarningNode_.parentNode) {
2892 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2893 }
2894};
2895
2896/**
rgindacc2996c2012-02-24 14:59:31 -08002897 * Show the terminal overlay for a given amount of time.
2898 *
2899 * The terminal overlay appears in inverse video in a large font, centered
2900 * over the terminal. You should probably keep the overlay message brief,
2901 * since it's in a large font and you probably aren't going to check the size
2902 * of the terminal first.
2903 *
2904 * @param {string} msg The text (not HTML) message to display in the overlay.
2905 * @param {number} opt_timeout The amount of time to wait before fading out
2906 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2907 * stay up forever (or until the next overlay).
2908 */
2909hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002910 if (!this.overlayNode_) {
2911 if (!this.div_)
2912 return;
2913
2914 this.overlayNode_ = this.document_.createElement('div');
2915 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002916 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002917 'font-size: xx-large;' +
2918 'opacity: 0.75;' +
2919 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2920 'position: absolute;' +
2921 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002922 '-webkit-transition: opacity 180ms ease-in;' +
2923 '-moz-user-select: none;' +
2924 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002925
2926 this.overlayNode_.addEventListener('mousedown', function(e) {
2927 e.preventDefault();
2928 e.stopPropagation();
2929 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002930 }
2931
rginda9f5222b2012-03-05 11:53:28 -08002932 this.overlayNode_.style.color = this.prefs_.get('background-color');
2933 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2934 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2935
rgindaf0090c92012-02-10 14:58:52 -08002936 this.overlayNode_.textContent = msg;
2937 this.overlayNode_.style.opacity = '0.75';
2938
2939 if (!this.overlayNode_.parentNode)
2940 this.div_.appendChild(this.overlayNode_);
2941
Robert Ginda97769282013-02-01 15:30:30 -08002942 var divSize = hterm.getClientSize(this.div_);
2943 var overlaySize = hterm.getClientSize(this.overlayNode_);
2944
Robert Ginda8a59f762014-07-23 11:29:55 -07002945 this.overlayNode_.style.top =
2946 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08002947 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07002948 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08002949
rgindaf0090c92012-02-10 14:58:52 -08002950 if (this.overlayTimeout_)
2951 clearTimeout(this.overlayTimeout_);
2952
rgindacc2996c2012-02-24 14:59:31 -08002953 if (opt_timeout === null)
2954 return;
2955
Mike Frysingerb6cfded2017-09-18 00:39:31 -04002956 this.overlayTimeout_ = setTimeout(() => {
2957 this.overlayNode_.style.opacity = '0';
2958 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
2959 }, opt_timeout || 1500);
2960};
2961
2962/**
2963 * Hide the terminal overlay immediately.
2964 *
2965 * Useful when we show an overlay for an event with an unknown end time.
2966 */
2967hterm.Terminal.prototype.hideOverlay = function() {
2968 if (this.overlayTimeout_)
2969 clearTimeout(this.overlayTimeout_);
2970 this.overlayTimeout_ = null;
2971
2972 if (this.overlayNode_.parentNode)
2973 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
2974 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08002975};
2976
rginda4bba5e12012-06-20 16:15:30 -07002977/**
2978 * Paste from the system clipboard to the terminal.
2979 */
2980hterm.Terminal.prototype.paste = function() {
Mike Frysinger4628ad22017-07-20 02:44:20 -04002981 return hterm.pasteFromClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002982};
2983
2984/**
2985 * Copy a string to the system clipboard.
2986 *
2987 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05002988 *
2989 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07002990 */
2991hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002992 if (this.prefs_.get('enable-clipboard-notice'))
2993 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2994
rgindaa09e7332012-08-17 12:49:51 -07002995 var copySource = this.document_.createElement('pre');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002996 copySource.id = 'hterm:copy-to-clipboard-source';
rginda4bba5e12012-06-20 16:15:30 -07002997 copySource.textContent = str;
2998 copySource.style.cssText = (
2999 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003000 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07003001 'position: absolute;' +
3002 'top: -99px');
3003
3004 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07003005
rginda4bba5e12012-06-20 16:15:30 -07003006 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07003007 var anchorNode = selection.anchorNode;
3008 var anchorOffset = selection.anchorOffset;
3009 var focusNode = selection.focusNode;
3010 var focusOffset = selection.focusOffset;
3011
rginda4bba5e12012-06-20 16:15:30 -07003012 selection.selectAllChildren(copySource);
3013
rgindaa09e7332012-08-17 12:49:51 -07003014 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07003015
Rob Spies56953412014-04-28 14:09:47 -07003016 // IE doesn't support selection.extend. This means that the selection
3017 // won't return on IE.
Robert Ginda6f4f0aa2014-07-28 10:21:39 -07003018 if (selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07003019 selection.collapse(anchorNode, anchorOffset);
3020 selection.extend(focusNode, focusOffset);
3021 }
rgindafaa74742012-08-21 13:34:03 -07003022
rginda4bba5e12012-06-20 16:15:30 -07003023 copySource.parentNode.removeChild(copySource);
3024};
3025
Evan Jones2600d4f2016-12-06 09:29:36 -05003026/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003027 * Display an image.
3028 *
3029 * @param {Object} options The image to display.
3030 * @param {string=} options.name A human readable string for the image.
3031 * @param {string|number=} options.size The size (in bytes).
3032 * @param {boolean=} options.preserveAspectRatio Whether to preserve aspect.
3033 * @param {boolean=} options.inline Whether to display the image inline.
3034 * @param {string|number=} options.width The width of the image.
3035 * @param {string|number=} options.height The height of the image.
3036 * @param {string=} options.align Direction to align the image.
3037 * @param {string} options.uri The source URI for the image.
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003038 * @param {function=} onLoad Callback when loading finishes.
3039 * @param {function(Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003040 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003041hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003042 // Make sure we're actually given a resource to display.
3043 if (options.uri === undefined)
3044 return;
3045
3046 // Set up the defaults to simplify code below.
3047 if (!options.name)
3048 options.name = '';
3049
3050 // Has the user approved image display yet?
3051 if (this.allowImagesInline !== true) {
3052 this.newLine();
3053 const row = this.getRowNode(this.scrollbackRows_.length +
3054 this.getCursorRow() - 1);
3055
3056 if (this.allowImagesInline === false) {
3057 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3058 'Inline Images Disabled');
3059 return;
3060 }
3061
3062 // Show a prompt.
3063 let button;
3064 const span = this.document_.createElement('span');
3065 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3066 span.style.fontWeight = 'bold';
3067 span.style.borderWidth = '1px';
3068 span.style.borderStyle = 'dashed';
3069 button = this.document_.createElement('span');
3070 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3071 button.style.marginLeft = '1em';
3072 button.style.borderWidth = '1px';
3073 button.style.borderStyle = 'solid';
3074 button.addEventListener('click', () => {
3075 this.prefs_.set('allow-images-inline', false);
3076 });
3077 span.appendChild(button);
3078 button = this.document_.createElement('span');
3079 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3080 'allow this session');
3081 button.style.marginLeft = '1em';
3082 button.style.borderWidth = '1px';
3083 button.style.borderStyle = 'solid';
3084 button.addEventListener('click', () => {
3085 this.allowImagesInline = true;
3086 });
3087 span.appendChild(button);
3088 button = this.document_.createElement('span');
3089 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3090 button.style.marginLeft = '1em';
3091 button.style.borderWidth = '1px';
3092 button.style.borderStyle = 'solid';
3093 button.addEventListener('click', () => {
3094 this.prefs_.set('allow-images-inline', true);
3095 });
3096 span.appendChild(button);
3097
3098 row.appendChild(span);
3099 return;
3100 }
3101
3102 // See if we should show this object directly, or download it.
3103 if (options.inline) {
3104 const io = this.io.push();
3105 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
3106 'Loading $1 ...'), null);
3107
3108 // While we're loading the image, eat all the user's input.
3109 io.onVTKeystroke = io.sendString = () => {};
3110
3111 // Initialize this new image.
3112 const img = this.document_.createElement('img');
3113 img.src = options.uri;
3114 img.title = img.alt = options.name;
3115
3116 // Attach the image to the page to let it load/render. It won't stay here.
3117 // This is needed so it's visible and the DOM can calculate the height. If
3118 // the image is hidden or not in the DOM, the height is always 0.
3119 this.document_.body.appendChild(img);
3120
3121 // Wait for the image to finish loading before we try moving it to the
3122 // right place in the terminal.
3123 img.onload = () => {
3124 // Now that we have the image dimensions, figure out how to show it.
3125 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
3126 img.style.maxWidth = `${this.document_.body.clientWidth}px`;
3127 img.style.maxHeight = `${this.document_.body.clientHeight}px`;
3128
3129 // Parse a width/height specification.
3130 const parseDim = (dim, maxDim, cssVar) => {
3131 if (!dim || dim == 'auto')
3132 return '';
3133
3134 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3135 if (ary) {
3136 if (ary[2] == '%')
3137 return maxDim * parseInt(ary[1]) / 100 + 'px';
3138 else if (ary[2] == 'px')
3139 return dim;
3140 else
3141 return `calc(${dim} * var(${cssVar}))`;
3142 }
3143
3144 return '';
3145 };
3146 img.style.width =
3147 parseDim(options.width, this.document_.body.clientWidth,
3148 '--hterm-charsize-width');
3149 img.style.height =
3150 parseDim(options.height, this.document_.body.clientHeight,
3151 '--hterm-charsize-height');
3152
3153 // Figure out how many rows the image occupies, then add that many.
3154 // XXX: This count will be inaccurate if the font size changes on us.
3155 const padRows = Math.ceil(img.clientHeight /
3156 this.scrollPort_.characterSize.height);
3157 for (let i = 0; i < padRows; ++i)
3158 this.newLine();
3159
3160 // Update the max height in case the user shrinks the character size.
3161 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3162
3163 // Move the image to the last row. This way when we scroll up, it doesn't
3164 // disappear when the first row gets clipped. It will disappear when we
3165 // scroll down and the last row is clipped ...
3166 this.document_.body.removeChild(img);
3167 // Create a wrapper node so we can do an absolute in a relative position.
3168 // This helps with rounding errors between JS & CSS counts.
3169 const div = this.document_.createElement('div');
3170 div.style.position = 'relative';
3171 div.style.textAlign = options.align;
3172 img.style.position = 'absolute';
3173 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3174 div.appendChild(img);
3175 const row = this.getRowNode(this.scrollbackRows_.length +
3176 this.getCursorRow() - 1);
3177 row.appendChild(div);
3178
3179 io.hideOverlay();
3180 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003181
3182 if (onLoad)
3183 onLoad();
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003184 };
3185
3186 // If we got a malformed image, give up.
3187 img.onerror = (e) => {
3188 this.document_.body.removeChild(img);
3189 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
Mike Frysingere14a8c42018-03-10 00:17:30 -08003190 'Loading $1 failed'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003191 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003192
3193 if (onError)
3194 onError(e);
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003195 };
3196 } else {
3197 // We can't use chrome.downloads.download as that requires "downloads"
3198 // permissions, and that works only in extensions, not apps.
3199 const a = this.document_.createElement('a');
3200 a.href = options.uri;
3201 a.download = options.name;
3202 this.document_.body.appendChild(a);
3203 a.click();
3204 a.remove();
3205 }
3206};
3207
3208/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003209 * Returns the selected text, or null if no text is selected.
3210 *
3211 * @return {string|null}
3212 */
rgindaa09e7332012-08-17 12:49:51 -07003213hterm.Terminal.prototype.getSelectionText = function() {
3214 var selection = this.scrollPort_.selection;
3215 selection.sync();
3216
3217 if (selection.isCollapsed)
3218 return null;
3219
rgindaa09e7332012-08-17 12:49:51 -07003220 // Start offset measures from the beginning of the line.
3221 var startOffset = selection.startOffset;
3222 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003223
Raymes Khoury334625a2018-06-25 10:29:40 +10003224 // If an x-row isn't selected, |node| will be null.
3225 if (!node)
3226 return null;
3227
Robert Gindafdbb3f22012-09-06 20:23:06 -07003228 if (node.nodeName != 'X-ROW') {
3229 // If the selection doesn't start on an x-row node, then it must be
3230 // somewhere inside the x-row. Add any characters from previous siblings
3231 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003232
3233 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3234 // If node is the text node in a styled span, move up to the span node.
3235 node = node.parentNode;
3236 }
3237
Robert Gindafdbb3f22012-09-06 20:23:06 -07003238 while (node.previousSibling) {
3239 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003240 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003241 }
rgindaa09e7332012-08-17 12:49:51 -07003242 }
3243
3244 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08003245 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
3246 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003247 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003248
Robert Gindafdbb3f22012-09-06 20:23:06 -07003249 if (node.nodeName != 'X-ROW') {
3250 // If the selection doesn't end on an x-row node, then it must be
3251 // somewhere inside the x-row. Add any characters from following siblings
3252 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003253
3254 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3255 // If node is the text node in a styled span, move up to the span node.
3256 node = node.parentNode;
3257 }
3258
Robert Gindafdbb3f22012-09-06 20:23:06 -07003259 while (node.nextSibling) {
3260 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003261 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003262 }
rgindaa09e7332012-08-17 12:49:51 -07003263 }
3264
3265 var rv = this.getRowsText(selection.startRow.rowIndex,
3266 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003267 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003268};
3269
rginda4bba5e12012-06-20 16:15:30 -07003270/**
3271 * Copy the current selection to the system clipboard, then clear it after a
3272 * short delay.
3273 */
3274hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07003275 var text = this.getSelectionText();
3276 if (text != null)
3277 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07003278};
3279
rgindaf0090c92012-02-10 14:58:52 -08003280hterm.Terminal.prototype.overlaySize = function() {
3281 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
3282};
3283
rginda87b86462011-12-14 13:48:03 -08003284/**
3285 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3286 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003287 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003288 */
3289hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08003290 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08003291 this.scrollPort_.scrollRowToBottom(this.getRowCount());
3292
Robert Ginda8cb7d902013-06-20 14:37:18 -07003293 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08003294};
3295
3296/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003297 * Open the selected url.
3298 */
3299hterm.Terminal.prototype.openSelectedUrl_ = function() {
3300 var str = this.getSelectionText();
3301
3302 // If there is no selection, try and expand wherever they clicked.
3303 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003304 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003305 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003306
3307 // If clicking in empty space, return.
3308 if (str == null)
3309 return;
Mike Frysinger70b94692017-01-26 18:57:50 -10003310 }
3311
3312 // Make sure URL is valid before opening.
3313 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
3314 return;
Mike Frysinger43472622017-06-26 18:11:07 -04003315
3316 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003317 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003318 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3319 // We have to whitelist a few protocols that lack authorities and thus
3320 // never use the //. Like mailto.
3321 switch (str.split(':', 1)[0]) {
3322 case 'mailto':
3323 break;
3324 default:
3325 str = 'http://' + str;
3326 break;
3327 }
3328 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003329
Mike Frysinger720fa832017-10-23 01:15:52 -04003330 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003331};
Mike Frysinger70b94692017-01-26 18:57:50 -10003332
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003333/**
3334 * Manage the automatic mouse hiding behavior while typing.
3335 *
3336 * @param {boolean=} v Whether to enable automatic hiding.
3337 */
3338hterm.Terminal.prototype.setAutomaticMouseHiding = function(v=null) {
3339 // Since Chrome OS & macOS do this by default everywhere, we don't need to.
3340 // Linux & Windows seem to leave this to specific applications to manage.
3341 if (v === null)
3342 v = (hterm.os != 'cros' && hterm.os != 'mac');
3343
3344 this.mouseHideWhileTyping_ = !!v;
3345};
3346
3347/**
3348 * Handler for monitoring user keyboard activity.
3349 *
3350 * This isn't for processing the keystrokes directly, but for updating any
3351 * state that might toggle based on the user using the keyboard at all.
3352 *
3353 * @param {KeyboardEvent} e The keyboard event that triggered us.
3354 */
3355hterm.Terminal.prototype.onKeyboardActivity_ = function(e) {
3356 // When the user starts typing, hide the mouse cursor.
3357 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_)
3358 this.setCssVar('mouse-cursor-style', 'none');
3359};
Mike Frysinger70b94692017-01-26 18:57:50 -10003360
3361/**
rgindad5613292012-06-19 15:40:37 -07003362 * Add the terminalRow and terminalColumn properties to mouse events and
3363 * then forward on to onMouse().
3364 *
3365 * The terminalRow and terminalColumn properties contain the (row, column)
3366 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003367 *
3368 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003369 */
3370hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003371 if (e.processedByTerminalHandler_) {
3372 // We register our event handlers on the document, as well as the cursor
3373 // and the scroll blocker. Mouse events that occur on the cursor or
3374 // scroll blocker will also appear on the document, but we don't want to
3375 // process them twice.
3376 //
3377 // We can't just prevent bubbling because that has other side effects, so
3378 // we decorate the event object with this property instead.
3379 return;
3380 }
3381
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003382 var reportMouseEvents = (!this.defeatMouseReports_ &&
3383 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3384
rgindafaa74742012-08-21 13:34:03 -07003385 e.processedByTerminalHandler_ = true;
3386
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003387 // Handle auto hiding of mouse cursor while typing.
3388 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
3389 // Make sure the mouse cursor is visible.
3390 this.syncMouseStyle();
3391 // This debounce isn't perfect, but should work well enough for such a
3392 // simple implementation. If the user moved the mouse, we enabled this
3393 // debounce, and then moved the mouse just before the timeout, we wouldn't
3394 // debounce that later movement.
3395 this.mouseHideDelay_ = setTimeout(() => this.mouseHideDelay_ = null, 1000);
3396 }
3397
Robert Gindaeda48db2014-07-17 09:25:30 -07003398 // One based row/column stored on the mouse event.
3399 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
3400 this.scrollPort_.characterSize.height) + 1;
3401 e.terminalColumn = parseInt(e.clientX /
3402 this.scrollPort_.characterSize.width) + 1;
3403
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003404 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3405 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003406 return;
3407 }
3408
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003409 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003410 // If the cursor is visible and we're not sending mouse events to the
3411 // host app, then we want to hide the terminal cursor when the mouse
3412 // cursor is over top. This keeps the terminal cursor from interfering
3413 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003414 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3415 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3416 this.cursorNode_.style.display = 'none';
3417 } else if (this.cursorNode_.style.display == 'none') {
3418 this.cursorNode_.style.display = '';
3419 }
3420 }
rgindad5613292012-06-19 15:40:37 -07003421
Robert Ginda928cf632014-03-05 15:07:41 -08003422 if (e.type == 'mousedown') {
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003423 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003424 // If VT mouse reporting is disabled, or has been defeated with
3425 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003426 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003427 this.setSelectionEnabled(true);
3428 } else {
3429 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003430 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003431 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003432 this.setSelectionEnabled(false);
3433 e.preventDefault();
3434 }
3435 }
3436
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003437 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003438 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003439 this.screen_.expandSelection(this.document_.getSelection());
John Lin2aad22e2018-03-16 13:58:11 +08003440 if (this.copyOnSelect)
3441 this.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07003442 }
3443
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003444 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003445 // Debounce this event with the dblclick event. If you try to doubleclick
3446 // a URL to open it, Chrome will fire click then dblclick, but we won't
3447 // have expanded the selection text at the first click event.
3448 clearTimeout(this.timeouts_.openUrl);
3449 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3450 500);
3451 return;
3452 }
3453
Mike Frysinger847577f2017-05-23 23:25:57 -04003454 if (e.type == 'mousedown') {
3455 if ((this.mouseRightClickPaste && e.button == 2 /* right button */) ||
Mike Frysinger2edd3612017-05-24 00:54:39 -04003456 e.button == this.mousePasteButton) {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003457 if (!this.paste())
Mike Frysinger05a57f02017-08-27 17:48:55 -04003458 console.warn('Could not paste manually due to web restrictions');
Mike Frysinger847577f2017-05-23 23:25:57 -04003459 }
3460 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003461
Mike Frysinger2edd3612017-05-24 00:54:39 -04003462 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003463 !this.document_.getSelection().isCollapsed) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003464 this.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003465 }
3466
3467 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3468 this.scrollBlockerNode_.engaged) {
3469 // Disengage the scroll-blocker after one of these events.
3470 this.scrollBlockerNode_.engaged = false;
3471 this.scrollBlockerNode_.style.top = '-99px';
3472 }
3473
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003474 // Emulate arrow key presses via scroll wheel events.
3475 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3476 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003477 if (e.type == 'wheel') {
3478 var delta = this.scrollPort_.scrollWheelDelta(e);
3479 var lines = lib.f.smartFloorDivide(
3480 Math.abs(delta), this.scrollPort_.characterSize.height);
3481
3482 var data = '\x1bO' + (delta < 0 ? 'B' : 'A');
3483 this.io.sendString(data.repeat(lines));
3484
3485 e.preventDefault();
3486 }
3487 }
Robert Ginda928cf632014-03-05 15:07:41 -08003488 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003489 if (!this.scrollBlockerNode_.engaged) {
3490 if (e.type == 'mousedown') {
3491 // Move the scroll-blocker into place if we want to keep the scrollport
3492 // from scrolling.
3493 this.scrollBlockerNode_.engaged = true;
3494 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3495 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3496 } else if (e.type == 'mousemove') {
3497 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3498 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003499 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003500 e.preventDefault();
3501 }
3502 }
Robert Ginda928cf632014-03-05 15:07:41 -08003503
3504 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003505 }
3506
Robert Ginda928cf632014-03-05 15:07:41 -08003507 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3508 // Restore this on mouseup in case it was temporarily defeated with a
3509 // alt-mousedown. Only do this when the selection is empty so that
3510 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003511 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003512 }
rgindad5613292012-06-19 15:40:37 -07003513};
3514
3515/**
3516 * Clients should override this if they care to know about mouse events.
3517 *
3518 * The event parameter will be a normal DOM mouse click event with additional
3519 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003520 *
3521 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003522 */
3523hterm.Terminal.prototype.onMouse = function(e) { };
3524
3525/**
rginda8e92a692012-05-20 19:37:20 -07003526 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003527 *
3528 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003529 */
Rob Spies06533ba2014-04-24 11:20:37 -07003530hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3531 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003532 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003533
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003534 if (this.reportFocus)
3535 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003536
Michael Kelly485ecd12014-06-09 11:41:56 -04003537 if (focused === true)
3538 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003539};
3540
3541/**
rginda8ba33642011-12-14 12:31:31 -08003542 * React when the ScrollPort is scrolled.
3543 */
3544hterm.Terminal.prototype.onScroll_ = function() {
3545 this.scheduleSyncCursorPosition_();
3546};
3547
3548/**
rginda9846e2f2012-01-27 13:53:33 -08003549 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003550 *
3551 * @param {Event} e The DOM paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003552 */
3553hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07003554 var data = e.text.replace(/\n/mg, '\r');
Connor Hegartyf525ceb2014-09-03 13:36:03 -07003555 data = this.keyboard.encode(data);
Mike Frysingere8c32c82018-03-11 14:57:28 -07003556 if (this.options_.bracketedPaste) {
3557 // We strip out most escape sequences as they can cause issues (like
3558 // inserting an \x1b[201~ midstream). We pass through whitespace
3559 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
3560 // This matches xterm behavior.
3561 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
3562 data = '\x1b[200~' + filter(data) + '\x1b[201~';
3563 }
Robert Gindaa063b202014-07-21 11:08:25 -07003564
3565 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003566};
3567
3568/**
rgindaa09e7332012-08-17 12:49:51 -07003569 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003570 *
3571 * @param {Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003572 */
3573hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003574 if (!this.useDefaultWindowCopy) {
3575 e.preventDefault();
3576 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3577 }
rgindaa09e7332012-08-17 12:49:51 -07003578};
3579
3580/**
rginda8ba33642011-12-14 12:31:31 -08003581 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003582 *
3583 * Note: This function should not directly contain code that alters the internal
3584 * state of the terminal. That kind of code belongs in realizeWidth or
3585 * realizeHeight, so that it can be executed synchronously in the case of a
3586 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003587 */
3588hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003589 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003590 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003591 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003592 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003593
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003594 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003595 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003596 // gets removed from the document or during the initial load, and we can't
3597 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003598 // This can also happen if called before the scrollPort calculates the
3599 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003600 return;
3601 }
3602
rgindaa8ba17d2012-08-15 14:41:10 -07003603 var isNewSize = (columnCount != this.screenSize.width ||
3604 rowCount != this.screenSize.height);
3605
3606 // We do this even if the size didn't change, just to be sure everything is
3607 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003608 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003609 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003610
3611 if (isNewSize)
3612 this.overlaySize();
3613
Robert Gindafb1be6a2013-12-11 11:56:22 -08003614 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003615 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08003616};
3617
3618/**
3619 * Service the cursor blink timeout.
3620 */
3621hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003622 if (!this.options_.cursorBlink) {
3623 delete this.timeouts_.cursorBlink;
3624 return;
3625 }
3626
Robert Ginda830583c2013-08-07 13:20:46 -07003627 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3628 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003629 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003630 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3631 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003632 } else {
rginda87b86462011-12-14 13:48:03 -08003633 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003634 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3635 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003636 }
3637};
David Reveman8f552492012-03-28 12:18:41 -04003638
3639/**
3640 * Set the scrollbar-visible mode bit.
3641 *
3642 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3643 * Otherwise it will not.
3644 *
3645 * Defaults to on.
3646 *
3647 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3648 */
3649hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3650 this.scrollPort_.setScrollbarVisible(state);
3651};
Michael Kelly485ecd12014-06-09 11:41:56 -04003652
3653/**
Rob Spies49039e52014-12-17 13:40:04 -08003654 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003655 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003656 *
3657 * Defaults to 1.
3658 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003659 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003660 */
3661hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3662 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3663};
3664
3665/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003666 * Close all web notifications created by terminal bells.
3667 */
3668hterm.Terminal.prototype.closeBellNotifications_ = function() {
3669 this.bellNotificationList_.forEach(function(n) {
3670 n.close();
3671 });
3672 this.bellNotificationList_.length = 0;
3673};