blob: e6c8e3588f74c63fbcb23eb570655550804c5d9a [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
118 // Whether command output should be rendered for Assistive Technology.
119 // This isn't always enabled because it has an impact on performance.
120 this.accessibilityEnabled_ = false;
121
Michael Kelly485ecd12014-06-09 11:41:56 -0400122 // All terminal bell notifications that have been generated (not necessarily
123 // shown).
124 this.bellNotificationList_ = [];
125
126 // Whether we have permission to display notifications.
127 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400128
rginda6d397402012-01-17 10:58:29 -0800129 // Cursor position and attributes saved with DECSC.
130 this.savedOptions_ = {};
131
rginda8ba33642011-12-14 12:31:31 -0800132 // The current mode bits for the terminal.
133 this.options_ = new hterm.Options();
134
135 // Timeouts we might need to clear.
136 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800137
138 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800139 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800140
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800141 this.saveCursorAndState(true);
142
Zhu Qunying30d40712017-03-14 16:27:00 -0700143 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800144 this.keyboard = new hterm.Keyboard(this);
145
rginda87b86462011-12-14 13:48:03 -0800146 // General IO interface that can be given to third parties without exposing
147 // the entire terminal object.
148 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800149
rgindad5613292012-06-19 15:40:37 -0700150 // True if mouse-click-drag should scroll the terminal.
151 this.enableMouseDragScroll = true;
152
Robert Ginda57f03b42012-09-13 11:02:48 -0700153 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400154 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700155 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700156
Zhu Qunying30d40712017-03-14 16:27:00 -0700157 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700158 this.useDefaultWindowCopy = false;
159
160 this.clearSelectionAfterCopy = true;
161
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400162 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800163 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700164
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400165 // Whether we allow images to be shown.
166 this.allowImagesInline = null;
167
Gabriel Holodake8a09be2017-10-10 01:07:11 -0400168 this.reportFocus = false;
169
Robert Ginda57f03b42012-09-13 11:02:48 -0700170 this.setProfile(opt_profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500171 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800172};
173
174/**
Robert Ginda830583c2013-08-07 13:20:46 -0700175 * Possible cursor shapes.
176 */
177hterm.Terminal.cursorShape = {
178 BLOCK: 'BLOCK',
179 BEAM: 'BEAM',
180 UNDERLINE: 'UNDERLINE'
181};
182
183/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700184 * Clients should override this to be notified when the terminal is ready
185 * for use.
186 *
187 * The terminal initialization is asynchronous, and shouldn't be used before
188 * this method is called.
189 */
190hterm.Terminal.prototype.onTerminalReady = function() { };
191
192/**
rginda35c456b2012-02-09 17:29:05 -0800193 * Default tab with of 8 to match xterm.
194 */
195hterm.Terminal.prototype.tabWidth = 8;
196
197/**
rginda9f5222b2012-03-05 11:53:28 -0800198 * Select a preference profile.
199 *
200 * This will load the terminal preferences for the given profile name and
201 * associate subsequent preference changes with the new preference profile.
202 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500203 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800204 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700205 * @param {function} opt_callback Optional callback to invoke when the profile
206 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800207 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700208hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
209 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800210
Robert Ginda57f03b42012-09-13 11:02:48 -0700211 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800212
Robert Ginda57f03b42012-09-13 11:02:48 -0700213 if (this.prefs_)
214 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800215
Robert Ginda57f03b42012-09-13 11:02:48 -0700216 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
217 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800218 'alt-gr-mode': function(v) {
219 if (v == null) {
220 if (navigator.language.toLowerCase() == 'en-us') {
221 v = 'none';
222 } else {
223 v = 'right-alt';
224 }
225 } else if (typeof v == 'string') {
226 v = v.toLowerCase();
227 } else {
228 v = 'none';
229 }
230
231 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v))
232 v = 'none';
233
234 terminal.keyboard.altGrMode = v;
235 },
236
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700237 'alt-backspace-is-meta-backspace': function(v) {
238 terminal.keyboard.altBackspaceIsMetaBackspace = v;
239 },
240
Robert Ginda57f03b42012-09-13 11:02:48 -0700241 'alt-is-meta': function(v) {
242 terminal.keyboard.altIsMeta = v;
243 },
244
245 'alt-sends-what': function(v) {
246 if (!/^(escape|8-bit|browser-key)$/.test(v))
247 v = 'escape';
248
249 terminal.keyboard.altSendsWhat = v;
250 },
251
252 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800253 var ary = v.match(/^lib-resource:(\S+)/);
254 if (ary) {
255 terminal.bellAudio_.setAttribute('src',
256 lib.resource.getDataUrl(ary[1]));
257 } else {
258 terminal.bellAudio_.setAttribute('src', v);
259 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700260 },
261
Michael Kelly485ecd12014-06-09 11:41:56 -0400262 'desktop-notification-bell': function(v) {
263 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700264 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400265 Notification.permission === 'granted';
266 if (!terminal.desktopNotificationBell_) {
267 // Note: We don't call Notification.requestPermission here because
268 // Chrome requires the call be the result of a user action (such as an
269 // onclick handler), and pref listeners are run asynchronously.
270 //
271 // A way of working around this would be to display a dialog in the
272 // terminal with a "click-to-request-permission" button.
273 console.warn('desktop-notification-bell is true but we do not have ' +
274 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400275 }
276 } else {
277 terminal.desktopNotificationBell_ = false;
278 }
279 },
280
Robert Ginda57f03b42012-09-13 11:02:48 -0700281 'background-color': function(v) {
282 terminal.setBackgroundColor(v);
283 },
284
285 'background-image': function(v) {
286 terminal.scrollPort_.setBackgroundImage(v);
287 },
288
289 'background-size': function(v) {
290 terminal.scrollPort_.setBackgroundSize(v);
291 },
292
293 'background-position': function(v) {
294 terminal.scrollPort_.setBackgroundPosition(v);
295 },
296
297 'backspace-sends-backspace': function(v) {
298 terminal.keyboard.backspaceSendsBackspace = v;
299 },
300
Brad Town18654b62015-03-12 00:27:45 -0700301 'character-map-overrides': function(v) {
302 if (!(v == null || v instanceof Object)) {
303 console.warn('Preference character-map-modifications is not an ' +
304 'object: ' + v);
305 return;
306 }
307
Mike Frysinger095d4062017-06-14 00:29:48 -0700308 terminal.vt.characterMaps.reset();
309 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700310 },
311
Robert Ginda57f03b42012-09-13 11:02:48 -0700312 'cursor-blink': function(v) {
313 terminal.setCursorBlink(!!v);
314 },
315
Robert Gindaea2183e2014-07-17 09:51:51 -0700316 'cursor-blink-cycle': function(v) {
317 if (v instanceof Array &&
318 typeof v[0] == 'number' &&
319 typeof v[1] == 'number') {
320 terminal.cursorBlinkCycle_ = v;
321 } else if (typeof v == 'number') {
322 terminal.cursorBlinkCycle_ = [v, v];
323 } else {
324 // Fast blink indicates an error.
325 terminal.cursorBlinkCycle_ = [100, 100];
326 }
327 },
328
Robert Ginda57f03b42012-09-13 11:02:48 -0700329 'cursor-color': function(v) {
330 terminal.setCursorColor(v);
331 },
332
333 'color-palette-overrides': function(v) {
334 if (!(v == null || v instanceof Object || v instanceof Array)) {
335 console.warn('Preference color-palette-overrides is not an array or ' +
336 'object: ' + v);
337 return;
rginda9f5222b2012-03-05 11:53:28 -0800338 }
rginda9f5222b2012-03-05 11:53:28 -0800339
Robert Ginda57f03b42012-09-13 11:02:48 -0700340 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700341
Robert Ginda57f03b42012-09-13 11:02:48 -0700342 if (v) {
343 for (var key in v) {
344 var i = parseInt(key);
345 if (isNaN(i) || i < 0 || i > 255) {
346 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
347 continue;
348 }
349
350 if (v[i]) {
351 var rgb = lib.colors.normalizeCSS(v[i]);
352 if (rgb)
353 lib.colors.colorPalette[i] = rgb;
354 }
355 }
rginda30f20f62012-04-05 16:36:19 -0700356 }
rginda30f20f62012-04-05 16:36:19 -0700357
Evan Jones5f9df812016-12-06 09:38:58 -0500358 terminal.primaryScreen_.textAttributes.resetColorPalette();
Robert Ginda57f03b42012-09-13 11:02:48 -0700359 terminal.alternateScreen_.textAttributes.resetColorPalette();
360 },
rginda30f20f62012-04-05 16:36:19 -0700361
Robert Ginda57f03b42012-09-13 11:02:48 -0700362 'copy-on-select': function(v) {
363 terminal.copyOnSelect = !!v;
364 },
rginda9f5222b2012-03-05 11:53:28 -0800365
Rob Spies0bec09b2014-06-06 15:58:09 -0700366 'use-default-window-copy': function(v) {
367 terminal.useDefaultWindowCopy = !!v;
368 },
369
370 'clear-selection-after-copy': function(v) {
371 terminal.clearSelectionAfterCopy = !!v;
372 },
373
Robert Ginda7e5e9522014-03-14 12:23:58 -0700374 'ctrl-plus-minus-zero-zoom': function(v) {
375 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
376 },
377
Robert Gindafb5a3f92014-05-13 14:12:00 -0700378 'ctrl-c-copy': function(v) {
379 terminal.keyboard.ctrlCCopy = v;
380 },
381
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100382 'ctrl-v-paste': function(v) {
383 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700384 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100385 },
386
Masaya Suzuki273aa982014-05-31 07:25:55 +0900387 'east-asian-ambiguous-as-two-column': function(v) {
388 lib.wc.regardCjkAmbiguous = v;
389 },
390
Robert Ginda57f03b42012-09-13 11:02:48 -0700391 'enable-8-bit-control': function(v) {
392 terminal.vt.enable8BitControl = !!v;
393 },
rginda30f20f62012-04-05 16:36:19 -0700394
Robert Ginda57f03b42012-09-13 11:02:48 -0700395 'enable-bold': function(v) {
396 terminal.syncBoldSafeState();
397 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400398
Robert Ginda3e278d72014-03-25 13:18:51 -0700399 'enable-bold-as-bright': function(v) {
400 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
401 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
402 },
403
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400404 'enable-blink': function(v) {
Mike Frysinger261597c2017-12-28 01:14:21 -0500405 terminal.setTextBlink(!!v);
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400406 },
407
Robert Ginda57f03b42012-09-13 11:02:48 -0700408 'enable-clipboard-write': function(v) {
409 terminal.vt.enableClipboardWrite = !!v;
410 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400411
Robert Ginda3755e752013-05-31 13:34:09 -0700412 'enable-dec12': function(v) {
413 terminal.vt.enableDec12 = !!v;
414 },
415
Robert Ginda57f03b42012-09-13 11:02:48 -0700416 'font-family': function(v) {
417 terminal.syncFontFamily();
418 },
rginda30f20f62012-04-05 16:36:19 -0700419
Robert Ginda57f03b42012-09-13 11:02:48 -0700420 'font-size': function(v) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500421 v = parseInt(v);
422 if (v <= 0) {
423 console.error(`Invalid font size: ${v}`);
424 return;
425 }
426
Robert Ginda57f03b42012-09-13 11:02:48 -0700427 terminal.setFontSize(v);
428 },
rginda9875d902012-08-20 16:21:57 -0700429
Robert Ginda57f03b42012-09-13 11:02:48 -0700430 'font-smoothing': function(v) {
431 terminal.syncFontFamily();
432 },
rgindade84e382012-04-20 15:39:31 -0700433
Robert Ginda57f03b42012-09-13 11:02:48 -0700434 'foreground-color': function(v) {
435 terminal.setForegroundColor(v);
436 },
rginda30f20f62012-04-05 16:36:19 -0700437
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400438 'hide-mouse-while-typing': function(v) {
439 terminal.setAutomaticMouseHiding(v);
440 },
441
Robert Ginda57f03b42012-09-13 11:02:48 -0700442 'home-keys-scroll': function(v) {
443 terminal.keyboard.homeKeysScroll = v;
444 },
rginda4bba5e12012-06-20 16:15:30 -0700445
Robert Gindaa8165692015-06-15 14:46:31 -0700446 'keybindings': function(v) {
447 terminal.keyboard.bindings.clear();
448
449 if (!v)
450 return;
451
452 if (!(v instanceof Object)) {
453 console.error('Error in keybindings preference: Expected object');
454 return;
455 }
456
457 try {
458 terminal.keyboard.bindings.addBindings(v);
459 } catch (ex) {
460 console.error('Error in keybindings preference: ' + ex);
461 }
462 },
463
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700464 'media-keys-are-fkeys': function(v) {
465 terminal.keyboard.mediaKeysAreFKeys = v;
466 },
467
Robert Ginda57f03b42012-09-13 11:02:48 -0700468 'meta-sends-escape': function(v) {
469 terminal.keyboard.metaSendsEscape = v;
470 },
rginda30f20f62012-04-05 16:36:19 -0700471
Mike Frysinger847577f2017-05-23 23:25:57 -0400472 'mouse-right-click-paste': function(v) {
473 terminal.mouseRightClickPaste = v;
474 },
475
Robert Ginda57f03b42012-09-13 11:02:48 -0700476 'mouse-paste-button': function(v) {
477 terminal.syncMousePasteButton();
478 },
rgindaa8ba17d2012-08-15 14:41:10 -0700479
Robert Gindae76aa9f2014-03-14 12:29:12 -0700480 'page-keys-scroll': function(v) {
481 terminal.keyboard.pageKeysScroll = v;
482 },
483
Robert Ginda40932892012-12-10 17:26:40 -0800484 'pass-alt-number': function(v) {
485 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800486 // Let Alt-1..9 pass to the browser (to control tab switching) on
487 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500488 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800489 }
490
491 terminal.passAltNumber = v;
492 },
493
494 'pass-ctrl-number': function(v) {
495 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800496 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
497 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500498 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800499 }
500
501 terminal.passCtrlNumber = v;
502 },
503
504 'pass-meta-number': function(v) {
505 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800506 // Let Meta-1..9 pass to the browser (to control tab switching) on
507 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500508 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800509 }
510
511 terminal.passMetaNumber = v;
512 },
513
Marius Schilder77857b32014-05-14 16:21:26 -0700514 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700515 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700516 },
517
Robert Ginda8cb7d902013-06-20 14:37:18 -0700518 'receive-encoding': function(v) {
519 if (!(/^(utf-8|raw)$/).test(v)) {
520 console.warn('Invalid value for "receive-encoding": ' + v);
521 v = 'utf-8';
522 }
523
524 terminal.vt.characterEncoding = v;
525 },
526
Robert Ginda57f03b42012-09-13 11:02:48 -0700527 'scroll-on-keystroke': function(v) {
528 terminal.scrollOnKeystroke_ = v;
529 },
rginda9f5222b2012-03-05 11:53:28 -0800530
Robert Ginda57f03b42012-09-13 11:02:48 -0700531 'scroll-on-output': function(v) {
532 terminal.scrollOnOutput_ = v;
533 },
rginda30f20f62012-04-05 16:36:19 -0700534
Robert Ginda57f03b42012-09-13 11:02:48 -0700535 'scrollbar-visible': function(v) {
536 terminal.setScrollbarVisible(v);
537 },
rginda9f5222b2012-03-05 11:53:28 -0800538
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400539 'scroll-wheel-may-send-arrow-keys': function(v) {
540 terminal.scrollWheelArrowKeys_ = v;
541 },
542
Rob Spies49039e52014-12-17 13:40:04 -0800543 'scroll-wheel-move-multiplier': function(v) {
544 terminal.setScrollWheelMoveMultipler(v);
545 },
546
Robert Ginda8cb7d902013-06-20 14:37:18 -0700547 'send-encoding': function(v) {
548 if (!(/^(utf-8|raw)$/).test(v)) {
549 console.warn('Invalid value for "send-encoding": ' + v);
550 v = 'utf-8';
551 }
552
553 terminal.keyboard.characterEncoding = v;
554 },
555
Robert Ginda57f03b42012-09-13 11:02:48 -0700556 'shift-insert-paste': function(v) {
557 terminal.keyboard.shiftInsertPaste = v;
558 },
rginda9f5222b2012-03-05 11:53:28 -0800559
Mike Frysingera7768922017-07-28 15:00:12 -0400560 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400561 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400562 },
563
Robert Gindae76aa9f2014-03-14 12:29:12 -0700564 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400565 terminal.scrollPort_.setUserCssUrl(v);
566 },
567
568 'user-css-text': function(v) {
569 terminal.scrollPort_.setUserCssText(v);
570 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400571
572 'word-break-match-left': function(v) {
573 terminal.primaryScreen_.wordBreakMatchLeft = v;
574 terminal.alternateScreen_.wordBreakMatchLeft = v;
575 },
576
577 'word-break-match-right': function(v) {
578 terminal.primaryScreen_.wordBreakMatchRight = v;
579 terminal.alternateScreen_.wordBreakMatchRight = v;
580 },
581
582 'word-break-match-middle': function(v) {
583 terminal.primaryScreen_.wordBreakMatchMiddle = v;
584 terminal.alternateScreen_.wordBreakMatchMiddle = v;
585 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400586
587 'allow-images-inline': function(v) {
588 terminal.allowImagesInline = v;
589 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700590 });
rginda30f20f62012-04-05 16:36:19 -0700591
Robert Ginda57f03b42012-09-13 11:02:48 -0700592 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800593 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700594
595 if (opt_callback)
596 opt_callback();
597 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800598};
599
Rob Spies56953412014-04-28 14:09:47 -0700600
601/**
602 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500603 *
604 * @return {hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700605 */
606hterm.Terminal.prototype.getPrefs = function() {
607 return this.prefs_;
608};
609
Robert Gindaa063b202014-07-21 11:08:25 -0700610/**
611 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500612 *
613 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700614 */
615hterm.Terminal.prototype.setBracketedPaste = function(state) {
616 this.options_.bracketedPaste = state;
617};
Rob Spies56953412014-04-28 14:09:47 -0700618
rginda8e92a692012-05-20 19:37:20 -0700619/**
620 * Set the color for the cursor.
621 *
622 * If you want this setting to persist, set it through prefs_, rather than
623 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500624 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500625 * @param {string=} color The color to set. If not defined, we reset to the
626 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700627 */
628hterm.Terminal.prototype.setCursorColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500629 if (color === undefined)
630 color = this.prefs_.get('cursor-color');
631
Robert Ginda830583c2013-08-07 13:20:46 -0700632 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700633 this.cursorNode_.style.backgroundColor = color;
634 this.cursorNode_.style.borderColor = color;
635};
636
637/**
638 * Return the current cursor color as a string.
Evan Jones2600d4f2016-12-06 09:29:36 -0500639 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700640 */
641hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700642 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700643};
644
645/**
rgindad5613292012-06-19 15:40:37 -0700646 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500647 *
648 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700649 */
650hterm.Terminal.prototype.setSelectionEnabled = function(state) {
651 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700652};
653
654/**
rginda8e92a692012-05-20 19:37:20 -0700655 * Set the background color.
656 *
657 * If you want this setting to persist, set it through prefs_, rather than
658 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500659 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500660 * @param {string=} color The color to set. If not defined, we reset to the
661 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700662 */
663hterm.Terminal.prototype.setBackgroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500664 if (color === undefined)
665 color = this.prefs_.get('background-color');
666
rgindacbbd7482012-06-13 15:06:16 -0700667 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700668 this.primaryScreen_.textAttributes.setDefaults(
669 this.foregroundColor_, this.backgroundColor_);
670 this.alternateScreen_.textAttributes.setDefaults(
671 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700672 this.scrollPort_.setBackgroundColor(color);
673};
674
rginda9f5222b2012-03-05 11:53:28 -0800675/**
676 * Return the current terminal background color.
677 *
678 * Intended for use by other classes, so we don't have to expose the entire
679 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500680 *
681 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800682 */
683hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700684 return this.backgroundColor_;
685};
686
687/**
688 * Set the foreground color.
689 *
690 * If you want this setting to persist, set it through prefs_, rather than
691 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500692 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500693 * @param {string=} color The color to set. If not defined, we reset to the
694 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700695 */
696hterm.Terminal.prototype.setForegroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500697 if (color === undefined)
698 color = this.prefs_.get('foreground-color');
699
rgindacbbd7482012-06-13 15:06:16 -0700700 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700701 this.primaryScreen_.textAttributes.setDefaults(
702 this.foregroundColor_, this.backgroundColor_);
703 this.alternateScreen_.textAttributes.setDefaults(
704 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700705 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800706};
707
708/**
709 * Return the current terminal foreground color.
710 *
711 * Intended for use by other classes, so we don't have to expose the entire
712 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500713 *
714 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800715 */
716hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700717 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800718};
719
720/**
rginda87b86462011-12-14 13:48:03 -0800721 * Create a new instance of a terminal command and run it with a given
722 * argument string.
723 *
724 * @param {function} commandClass The constructor for a terminal command.
725 * @param {string} argString The argument string to pass to the command.
726 */
727hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700728 var environment = this.prefs_.get('environment');
729 if (typeof environment != 'object' || environment == null)
730 environment = {};
731
rginda87b86462011-12-14 13:48:03 -0800732 var self = this;
733 this.command = new commandClass(
734 { argString: argString || '',
735 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700736 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800737 onExit: function(code) {
738 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800739 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700740 if (self.prefs_.get('close-on-exit'))
741 window.close();
rginda87b86462011-12-14 13:48:03 -0800742 }
743 });
744
rgindafeaf3142012-01-31 15:14:20 -0800745 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800746 this.command.run();
747};
748
749/**
rgindafeaf3142012-01-31 15:14:20 -0800750 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500751 *
752 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800753 */
754hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700755 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800756};
757
758/**
759 * Install the keyboard handler for this terminal.
760 *
761 * This will prevent the browser from seeing any keystrokes sent to the
762 * terminal.
763 */
764hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700765 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400766};
rgindafeaf3142012-01-31 15:14:20 -0800767
768/**
769 * Uninstall the keyboard handler for this terminal.
770 */
771hterm.Terminal.prototype.uninstallKeyboard = function() {
772 this.keyboard.installKeyboard(null);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400773};
rgindafeaf3142012-01-31 15:14:20 -0800774
775/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400776 * Set a CSS variable.
777 *
778 * Normally this is used to set variables in the hterm namespace.
779 *
780 * @param {string} name The variable to set.
781 * @param {string} value The value to assign to the variable.
782 * @param {string?} opt_prefix The variable namespace/prefix to use.
783 */
784hterm.Terminal.prototype.setCssVar = function(name, value,
785 opt_prefix='--hterm-') {
786 this.document_.documentElement.style.setProperty(
787 `${opt_prefix}${name}`, value);
788};
789
790/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500791 * Get a CSS variable.
792 *
793 * Normally this is used to get variables in the hterm namespace.
794 *
795 * @param {string} name The variable to read.
796 * @param {string?} opt_prefix The variable namespace/prefix to use.
797 * @return {string} The current setting for this variable.
798 */
799hterm.Terminal.prototype.getCssVar = function(name, opt_prefix='--hterm-') {
800 return this.document_.documentElement.style.getPropertyValue(
801 `${opt_prefix}${name}`);
802};
803
804/**
rginda35c456b2012-02-09 17:29:05 -0800805 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800806 *
807 * Call setFontSize(0) to reset to the default font size.
808 *
809 * This function does not modify the font-size preference.
810 *
811 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800812 */
813hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500814 if (px <= 0)
rginda9f5222b2012-03-05 11:53:28 -0800815 px = this.prefs_.get('font-size');
816
rginda35c456b2012-02-09 17:29:05 -0800817 this.scrollPort_.setFontSize(px);
Mike Frysingercce97c42017-08-05 01:11:22 -0400818 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
819 this.setCssVar('charsize-height',
820 this.scrollPort_.characterSize.height + 'px');
rginda35c456b2012-02-09 17:29:05 -0800821};
822
823/**
824 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500825 *
826 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800827 */
828hterm.Terminal.prototype.getFontSize = function() {
829 return this.scrollPort_.getFontSize();
830};
831
832/**
rginda8e92a692012-05-20 19:37:20 -0700833 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500834 *
835 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700836 */
837hterm.Terminal.prototype.getFontFamily = function() {
838 return this.scrollPort_.getFontFamily();
839};
840
841/**
rginda35c456b2012-02-09 17:29:05 -0800842 * Set the CSS "font-family" for this terminal.
843 */
rginda9f5222b2012-03-05 11:53:28 -0800844hterm.Terminal.prototype.syncFontFamily = function() {
845 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
846 this.prefs_.get('font-smoothing'));
847 this.syncBoldSafeState();
848};
849
rginda4bba5e12012-06-20 16:15:30 -0700850/**
851 * Set this.mousePasteButton based on the mouse-paste-button pref,
852 * autodetecting if necessary.
853 */
854hterm.Terminal.prototype.syncMousePasteButton = function() {
855 var button = this.prefs_.get('mouse-paste-button');
856 if (typeof button == 'number') {
857 this.mousePasteButton = button;
858 return;
859 }
860
Mike Frysingeree81a002017-12-12 16:14:53 -0500861 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400862 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700863 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400864 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700865 }
866};
867
868/**
869 * Enable or disable bold based on the enable-bold pref, autodetecting if
870 * necessary.
871 */
rginda9f5222b2012-03-05 11:53:28 -0800872hterm.Terminal.prototype.syncBoldSafeState = function() {
873 var enableBold = this.prefs_.get('enable-bold');
874 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700875 this.primaryScreen_.textAttributes.enableBold = enableBold;
876 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800877 return;
878 }
879
rgindaf7521392012-02-28 17:20:34 -0800880 var normalSize = this.scrollPort_.measureCharacterSize();
881 var boldSize = this.scrollPort_.measureCharacterSize('bold');
882
883 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800884 if (!isBoldSafe) {
885 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700886 'from normal. Font family is: ' +
887 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800888 }
rginda9f5222b2012-03-05 11:53:28 -0800889
Robert Gindaed016262012-10-26 16:27:09 -0700890 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
891 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800892};
893
894/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500895 * Control text blinking behavior.
896 *
897 * @param {boolean=} state Whether to enable support for blinking text.
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400898 */
Mike Frysinger261597c2017-12-28 01:14:21 -0500899hterm.Terminal.prototype.setTextBlink = function(state) {
900 if (state === undefined)
901 state = this.prefs_.get('enable-blink');
902 this.setCssVar('blink-node-duration', state ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400903};
904
905/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400906 * Set the mouse cursor style based on the current terminal mode.
907 */
908hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400909 this.setCssVar('mouse-cursor-style',
910 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
911 'var(--hterm-mouse-cursor-text)' :
912 'var(--hterm-mouse-cursor-pointer)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400913};
914
915/**
rginda87b86462011-12-14 13:48:03 -0800916 * Return a copy of the current cursor position.
917 *
918 * @return {hterm.RowCol} The RowCol object representing the current position.
919 */
920hterm.Terminal.prototype.saveCursor = function() {
921 return this.screen_.cursorPosition.clone();
922};
923
Evan Jones2600d4f2016-12-06 09:29:36 -0500924/**
925 * Return the current text attributes.
926 *
927 * @return {string}
928 */
rgindaa19afe22012-01-25 15:40:22 -0800929hterm.Terminal.prototype.getTextAttributes = function() {
930 return this.screen_.textAttributes;
931};
932
Evan Jones2600d4f2016-12-06 09:29:36 -0500933/**
934 * Set the text attributes.
935 *
936 * @param {string} textAttributes The attributes to set.
937 */
rginda1a09aa02012-06-18 21:11:25 -0700938hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
939 this.screen_.textAttributes = textAttributes;
940};
941
rginda87b86462011-12-14 13:48:03 -0800942/**
rgindaf522ce02012-04-17 17:49:17 -0700943 * Return the current browser zoom factor applied to the terminal.
944 *
945 * @return {number} The current browser zoom factor.
946 */
947hterm.Terminal.prototype.getZoomFactor = function() {
948 return this.scrollPort_.characterSize.zoomFactor;
949};
950
951/**
rginda9846e2f2012-01-27 13:53:33 -0800952 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500953 *
954 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800955 */
956hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800957 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800958};
959
960/**
rginda87b86462011-12-14 13:48:03 -0800961 * Restore a previously saved cursor position.
962 *
963 * @param {hterm.RowCol} cursor The position to restore.
964 */
965hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700966 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
967 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800968 this.screen_.setCursorPosition(row, column);
969 if (cursor.column > column ||
970 cursor.column == column && cursor.overflow) {
971 this.screen_.cursorPosition.overflow = true;
972 }
rginda87b86462011-12-14 13:48:03 -0800973};
974
975/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400976 * Clear the cursor's overflow flag.
977 */
978hterm.Terminal.prototype.clearCursorOverflow = function() {
979 this.screen_.cursorPosition.overflow = false;
980};
981
982/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800983 * Save the current cursor state to the corresponding screens.
984 *
985 * See the hterm.Screen.CursorState class for more details.
986 *
987 * @param {boolean=} both If true, update both screens, else only update the
988 * current screen.
989 */
990hterm.Terminal.prototype.saveCursorAndState = function(both) {
991 if (both) {
992 this.primaryScreen_.saveCursorAndState(this.vt);
993 this.alternateScreen_.saveCursorAndState(this.vt);
994 } else
995 this.screen_.saveCursorAndState(this.vt);
996};
997
998/**
999 * Restore the saved cursor state in the corresponding screens.
1000 *
1001 * See the hterm.Screen.CursorState class for more details.
1002 *
1003 * @param {boolean=} both If true, update both screens, else only update the
1004 * current screen.
1005 */
1006hterm.Terminal.prototype.restoreCursorAndState = function(both) {
1007 if (both) {
1008 this.primaryScreen_.restoreCursorAndState(this.vt);
1009 this.alternateScreen_.restoreCursorAndState(this.vt);
1010 } else
1011 this.screen_.restoreCursorAndState(this.vt);
1012};
1013
1014/**
Robert Ginda830583c2013-08-07 13:20:46 -07001015 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001016 *
1017 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -07001018 */
1019hterm.Terminal.prototype.setCursorShape = function(shape) {
1020 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001021 this.restyleCursor_();
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001022};
Robert Ginda830583c2013-08-07 13:20:46 -07001023
1024/**
1025 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001026 *
1027 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -07001028 */
1029hterm.Terminal.prototype.getCursorShape = function() {
1030 return this.cursorShape_;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001031};
Robert Ginda830583c2013-08-07 13:20:46 -07001032
1033/**
rginda87b86462011-12-14 13:48:03 -08001034 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001035 *
1036 * @param {number} columnCount
rginda87b86462011-12-14 13:48:03 -08001037 */
1038hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001039 if (columnCount == null) {
1040 this.div_.style.width = '100%';
1041 return;
1042 }
1043
Robert Ginda26806d12014-07-24 13:44:07 -07001044 this.div_.style.width = Math.ceil(
1045 this.scrollPort_.characterSize.width *
1046 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001047 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001048 this.scheduleSyncCursorPosition_();
1049};
rginda87b86462011-12-14 13:48:03 -08001050
rgindac9bc5502012-01-18 11:48:44 -08001051/**
rginda35c456b2012-02-09 17:29:05 -08001052 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001053 *
1054 * @param {number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001055 */
1056hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001057 if (rowCount == null) {
1058 this.div_.style.height = '100%';
1059 return;
1060 }
1061
rginda35c456b2012-02-09 17:29:05 -08001062 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -07001063 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -08001064 this.realizeSize_(this.screenSize.width, rowCount);
1065 this.scheduleSyncCursorPosition_();
1066};
1067
1068/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001069 * Deal with terminal size changes.
1070 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001071 * @param {number} columnCount The number of columns.
1072 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001073 */
1074hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
1075 if (columnCount != this.screenSize.width)
1076 this.realizeWidth_(columnCount);
1077
1078 if (rowCount != this.screenSize.height)
1079 this.realizeHeight_(rowCount);
1080
1081 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -07001082 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001083};
1084
1085/**
rgindac9bc5502012-01-18 11:48:44 -08001086 * Deal with terminal width changes.
1087 *
1088 * This function does what needs to be done when the terminal width changes
1089 * out from under us. It happens here rather than in onResize_() because this
1090 * code may need to run synchronously to handle programmatic changes of
1091 * terminal width.
1092 *
1093 * Relying on the browser to send us an async resize event means we may not be
1094 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001095 *
1096 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001097 */
1098hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001099 if (columnCount <= 0)
1100 throw new Error('Attempt to realize bad width: ' + columnCount);
1101
rgindac9bc5502012-01-18 11:48:44 -08001102 var deltaColumns = columnCount - this.screen_.getWidth();
1103
rginda87b86462011-12-14 13:48:03 -08001104 this.screenSize.width = columnCount;
1105 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001106
1107 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -04001108 if (this.defaultTabStops)
1109 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -08001110 } else {
1111 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -04001112 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -08001113 break;
1114
1115 this.tabStops_.pop();
1116 }
1117 }
1118
1119 this.screen_.setColumnCount(this.screenSize.width);
1120};
1121
1122/**
1123 * Deal with terminal height changes.
1124 *
1125 * This function does what needs to be done when the terminal height changes
1126 * out from under us. It happens here rather than in onResize_() because this
1127 * code may need to run synchronously to handle programmatic changes of
1128 * terminal height.
1129 *
1130 * Relying on the browser to send us an async resize event means we may not be
1131 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001132 *
1133 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001134 */
1135hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001136 if (rowCount <= 0)
1137 throw new Error('Attempt to realize bad height: ' + rowCount);
1138
rgindac9bc5502012-01-18 11:48:44 -08001139 var deltaRows = rowCount - this.screen_.getHeight();
1140
1141 this.screenSize.height = rowCount;
1142
1143 var cursor = this.saveCursor();
1144
1145 if (deltaRows < 0) {
1146 // Screen got smaller.
1147 deltaRows *= -1;
1148 while (deltaRows) {
1149 var lastRow = this.getRowCount() - 1;
1150 if (lastRow - this.scrollbackRows_.length == cursor.row)
1151 break;
1152
1153 if (this.getRowText(lastRow))
1154 break;
1155
1156 this.screen_.popRow();
1157 deltaRows--;
1158 }
1159
1160 var ary = this.screen_.shiftRows(deltaRows);
1161 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1162
1163 // We just removed rows from the top of the screen, we need to update
1164 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001165 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001166 } else if (deltaRows > 0) {
1167 // Screen got larger.
1168
1169 if (deltaRows <= this.scrollbackRows_.length) {
1170 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1171 var rows = this.scrollbackRows_.splice(
1172 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1173 this.screen_.unshiftRows(rows);
1174 deltaRows -= scrollbackCount;
1175 cursor.row += scrollbackCount;
1176 }
1177
1178 if (deltaRows)
1179 this.appendRows_(deltaRows);
1180 }
1181
rginda35c456b2012-02-09 17:29:05 -08001182 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001183 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001184};
1185
1186/**
1187 * Scroll the terminal to the top of the scrollback buffer.
1188 */
1189hterm.Terminal.prototype.scrollHome = function() {
1190 this.scrollPort_.scrollRowToTop(0);
1191};
1192
1193/**
1194 * Scroll the terminal to the end.
1195 */
1196hterm.Terminal.prototype.scrollEnd = function() {
1197 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1198};
1199
1200/**
1201 * Scroll the terminal one page up (minus one line) relative to the current
1202 * position.
1203 */
1204hterm.Terminal.prototype.scrollPageUp = function() {
1205 var i = this.scrollPort_.getTopRowIndex();
1206 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
1207};
1208
1209/**
1210 * Scroll the terminal one page down (minus one line) relative to the current
1211 * position.
1212 */
1213hterm.Terminal.prototype.scrollPageDown = function() {
1214 var i = this.scrollPort_.getTopRowIndex();
1215 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -08001216};
1217
rgindac9bc5502012-01-18 11:48:44 -08001218/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001219 * Scroll the terminal one line up relative to the current position.
1220 */
1221hterm.Terminal.prototype.scrollLineUp = function() {
1222 var i = this.scrollPort_.getTopRowIndex();
1223 this.scrollPort_.scrollRowToTop(i - 1);
1224};
1225
1226/**
1227 * Scroll the terminal one line down relative to the current position.
1228 */
1229hterm.Terminal.prototype.scrollLineDown = function() {
1230 var i = this.scrollPort_.getTopRowIndex();
1231 this.scrollPort_.scrollRowToTop(i + 1);
1232};
1233
1234/**
Robert Ginda40932892012-12-10 17:26:40 -08001235 * Clear primary screen, secondary screen, and the scrollback buffer.
1236 */
1237hterm.Terminal.prototype.wipeContents = function() {
1238 this.scrollbackRows_.length = 0;
1239 this.scrollPort_.resetCache();
1240
1241 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
1242 var bottom = screen.getHeight();
1243 if (bottom > 0) {
1244 this.renumberRows_(0, bottom);
1245 this.clearHome(screen);
1246 }
1247 }.bind(this));
1248
1249 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001250 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001251};
1252
1253/**
rgindac9bc5502012-01-18 11:48:44 -08001254 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001255 *
1256 * Perform a full reset to the default values listed in
1257 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001258 */
rginda87b86462011-12-14 13:48:03 -08001259hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001260 this.vt.reset();
1261
rgindac9bc5502012-01-18 11:48:44 -08001262 this.clearAllTabStops();
1263 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001264
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001265 const resetScreen = (screen) => {
1266 // We want to make sure to reset the attributes before we clear the screen.
1267 // The attributes might be used to initialize default/empty rows.
1268 screen.textAttributes.reset();
1269 screen.textAttributes.resetColorPalette();
1270 this.clearHome(screen);
1271 screen.saveCursorAndState(this.vt);
1272 };
1273 resetScreen(this.primaryScreen_);
1274 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001275
Mike Frysinger84301d02017-11-29 13:28:46 -08001276 // Reset terminal options to their default values.
1277 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001278 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1279
Mike Frysinger84301d02017-11-29 13:28:46 -08001280 this.setVTScrollRegion(null, null);
1281
1282 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001283};
1284
rgindac9bc5502012-01-18 11:48:44 -08001285/**
1286 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001287 *
1288 * Perform a soft reset to the default values listed in
1289 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001290 */
rginda0f5c0292012-01-13 11:00:13 -08001291hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001292 this.vt.reset();
1293
rgindab8bc8932012-04-27 12:45:03 -07001294 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001295 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001296
Brad Townb62dfdc2015-03-16 19:07:15 -07001297 // We show the cursor on soft reset but do not alter the blink state.
1298 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1299
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001300 const resetScreen = (screen) => {
1301 // Xterm also resets the color palette on soft reset, even though it doesn't
1302 // seem to be documented anywhere.
1303 screen.textAttributes.reset();
1304 screen.textAttributes.resetColorPalette();
1305 screen.saveCursorAndState(this.vt);
1306 };
1307 resetScreen(this.primaryScreen_);
1308 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001309
rgindab8bc8932012-04-27 12:45:03 -07001310 // The xterm man page explicitly says this will happen on soft reset.
1311 this.setVTScrollRegion(null, null);
1312
1313 // Xterm also shows the cursor on soft reset, but does not alter the blink
1314 // state.
rgindaa19afe22012-01-25 15:40:22 -08001315 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001316};
1317
rgindac9bc5502012-01-18 11:48:44 -08001318/**
1319 * Move the cursor forward to the next tab stop, or to the last column
1320 * if no more tab stops are set.
1321 */
1322hterm.Terminal.prototype.forwardTabStop = function() {
1323 var column = this.screen_.cursorPosition.column;
1324
1325 for (var i = 0; i < this.tabStops_.length; i++) {
1326 if (this.tabStops_[i] > column) {
1327 this.setCursorColumn(this.tabStops_[i]);
1328 return;
1329 }
1330 }
1331
David Benjamin66e954d2012-05-05 21:08:12 -04001332 // xterm does not clear the overflow flag on HT or CHT.
1333 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001334 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001335 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001336};
1337
rgindac9bc5502012-01-18 11:48:44 -08001338/**
1339 * Move the cursor backward to the previous tab stop, or to the first column
1340 * if no previous tab stops are set.
1341 */
1342hterm.Terminal.prototype.backwardTabStop = function() {
1343 var column = this.screen_.cursorPosition.column;
1344
1345 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1346 if (this.tabStops_[i] < column) {
1347 this.setCursorColumn(this.tabStops_[i]);
1348 return;
1349 }
1350 }
1351
1352 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001353};
1354
rgindac9bc5502012-01-18 11:48:44 -08001355/**
1356 * Set a tab stop at the given column.
1357 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001358 * @param {integer} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001359 */
1360hterm.Terminal.prototype.setTabStop = function(column) {
1361 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1362 if (this.tabStops_[i] == column)
1363 return;
1364
1365 if (this.tabStops_[i] < column) {
1366 this.tabStops_.splice(i + 1, 0, column);
1367 return;
1368 }
1369 }
1370
1371 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001372};
1373
rgindac9bc5502012-01-18 11:48:44 -08001374/**
1375 * Clear the tab stop at the current cursor position.
1376 *
1377 * No effect if there is no tab stop at the current cursor position.
1378 */
1379hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1380 var column = this.screen_.cursorPosition.column;
1381
1382 var i = this.tabStops_.indexOf(column);
1383 if (i == -1)
1384 return;
1385
1386 this.tabStops_.splice(i, 1);
1387};
1388
1389/**
1390 * Clear all tab stops.
1391 */
1392hterm.Terminal.prototype.clearAllTabStops = function() {
1393 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001394 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001395};
1396
1397/**
1398 * Set up the default tab stops, starting from a given column.
1399 *
1400 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001401 * from the specified column, or 0 if no column is provided. It also flags
1402 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001403 *
1404 * This does not clear the existing tab stops first, use clearAllTabStops
1405 * for that.
1406 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001407 * @param {integer} opt_start Optional starting zero based starting column, useful
rgindac9bc5502012-01-18 11:48:44 -08001408 * for filling out missing tab stops when the terminal is resized.
1409 */
1410hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1411 var start = opt_start || 0;
1412 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001413 // Round start up to a default tab stop.
1414 start = start - 1 - ((start - 1) % w) + w;
1415 for (var i = start; i < this.screenSize.width; i += w) {
1416 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001417 }
David Benjamin66e954d2012-05-05 21:08:12 -04001418
1419 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001420};
1421
rginda6d397402012-01-17 10:58:29 -08001422/**
rginda8ba33642011-12-14 12:31:31 -08001423 * Interpret a sequence of characters.
1424 *
1425 * Incomplete escape sequences are buffered until the next call.
1426 *
1427 * @param {string} str Sequence of characters to interpret or pass through.
1428 */
1429hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001430 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001431 this.scheduleSyncCursorPosition_();
1432};
1433
1434/**
1435 * Take over the given DIV for use as the terminal display.
1436 *
1437 * @param {HTMLDivElement} div The div to use as the terminal display.
1438 */
1439hterm.Terminal.prototype.decorate = function(div) {
Mike Frysinger5768a9d2017-12-26 12:57:44 -05001440 const charset = div.ownerDocument.characterSet.toLowerCase();
1441 if (charset != 'utf-8') {
1442 console.warn(`Document encoding should be set to utf-8, not "${charset}";` +
1443 ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`);
1444 }
1445
rginda87b86462011-12-14 13:48:03 -08001446 this.div_ = div;
1447
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001448 this.accessibilityReader_ = new hterm.AccessibilityReader(div);
1449
rginda8ba33642011-12-14 12:31:31 -08001450 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001451 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001452 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1453 this.scrollPort_.setBackgroundPosition(
1454 this.prefs_.get('background-position'));
Mike Frysinger08bad432017-04-24 00:50:54 -04001455 this.scrollPort_.setUserCssUrl(this.prefs_.get('user-css'));
1456 this.scrollPort_.setUserCssText(this.prefs_.get('user-css-text'));
rginda30f20f62012-04-05 16:36:19 -07001457
rginda0918b652012-04-04 11:26:24 -07001458 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001459
rginda9f5222b2012-03-05 11:53:28 -08001460 this.setFontSize(this.prefs_.get('font-size'));
1461 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001462
David Reveman8f552492012-03-28 12:18:41 -04001463 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001464 this.setScrollWheelMoveMultipler(
1465 this.prefs_.get('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001466
rginda8ba33642011-12-14 12:31:31 -08001467 this.document_ = this.scrollPort_.getDocument();
1468
Evan Jones5f9df812016-12-06 09:38:58 -05001469 this.document_.body.oncontextmenu = function() { return false; };
rginda4bba5e12012-06-20 16:15:30 -07001470
1471 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001472 var screenNode = this.scrollPort_.getScreenNode();
1473 screenNode.addEventListener('mousedown', onMouse);
1474 screenNode.addEventListener('mouseup', onMouse);
1475 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001476 this.scrollPort_.onScrollWheel = onMouse;
1477
Mike Frysinger02ded6d2018-06-21 14:25:20 -04001478 screenNode.addEventListener('keydown', this.onKeyboardActivity_.bind(this));
1479
Toni Barzic0bfa8922013-11-22 11:18:35 -08001480 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001481 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001482 // Listen for mousedown events on the screenNode as in FF the focus
1483 // events don't bubble.
1484 screenNode.addEventListener('mousedown', function() {
1485 setTimeout(this.onFocusChange_.bind(this, true));
1486 }.bind(this));
1487
Toni Barzic0bfa8922013-11-22 11:18:35 -08001488 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001489 'blur', this.onFocusChange_.bind(this, false));
1490
1491 var style = this.document_.createElement('style');
1492 style.textContent =
1493 ('.cursor-node[focus="false"] {' +
1494 ' box-sizing: border-box;' +
1495 ' background-color: transparent !important;' +
1496 ' border-width: 2px;' +
1497 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001498 '}' +
1499 '.wc-node {' +
1500 ' display: inline-block;' +
1501 ' text-align: center;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001502 ' width: calc(var(--hterm-charsize-width) * 2);' +
Mike Frysinger4036f6e2017-05-31 14:02:55 -04001503 ' line-height: var(--hterm-charsize-height);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001504 '}' +
1505 ':root {' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001506 ' --hterm-charsize-width: ' + this.scrollPort_.characterSize.width + 'px;' +
1507 ' --hterm-charsize-height: ' + this.scrollPort_.characterSize.height + 'px;' +
Mike Frysingera27c0502017-08-23 22:37:10 -04001508 // Default position hides the cursor for when the window is initializing.
1509 ' --hterm-cursor-offset-col: -1;' +
1510 ' --hterm-cursor-offset-row: -1;' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001511 ' --hterm-blink-node-duration: 0.7s;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001512 ' --hterm-mouse-cursor-text: text;' +
1513 ' --hterm-mouse-cursor-pointer: default;' +
1514 ' --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001515 '}' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001516 '.uri-node:hover {' +
1517 ' text-decoration: underline;' +
1518 ' cursor: pointer;' +
1519 '}' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001520 '@keyframes blink {' +
1521 ' from { opacity: 1.0; }' +
1522 ' to { opacity: 0.0; }' +
1523 '}' +
1524 '.blink-node {' +
1525 ' animation-name: blink;' +
1526 ' animation-duration: var(--hterm-blink-node-duration);' +
1527 ' animation-iteration-count: infinite;' +
1528 ' animation-timing-function: ease-in-out;' +
1529 ' animation-direction: alternate;' +
rginda8e92a692012-05-20 19:37:20 -07001530 '}');
1531 this.document_.head.appendChild(style);
1532
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 Khouryf1c61ba2018-05-28 14:05:38 +10001790 if (this.accessibilityEnabled_) {
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001791 this.accessibilityReader_.announce(str);
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001792 }
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001793
rgindaa9abdd82012-08-06 18:05:09 -07001794 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001795
Ricky Liang48f05cb2013-12-31 23:35:29 +08001796 var strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04001797 // Fun edge case: If the string only contains zero width codepoints (like
1798 // combining characters), we make sure to iterate at least once below.
1799 if (strWidth == 0 && str)
1800 strWidth = 1;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001801
1802 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001803 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1804 this.screen_.commitLineOverflow();
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001805 this.newLine(true);
rgindaa09e7332012-08-17 12:49:51 -07001806 }
rgindaa19afe22012-01-25 15:40:22 -08001807
Ricky Liang48f05cb2013-12-31 23:35:29 +08001808 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001809 var didOverflow = false;
1810 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001811
rgindaa9abdd82012-08-06 18:05:09 -07001812 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1813 didOverflow = true;
1814 count = this.screenSize.width - this.screen_.cursorPosition.column;
1815 }
rgindaa19afe22012-01-25 15:40:22 -08001816
rgindaa9abdd82012-08-06 18:05:09 -07001817 if (didOverflow && !this.options_.wraparound) {
1818 // If the string overflowed the line but wraparound is off, then the
1819 // last printed character should be the last of the string.
1820 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001821 substr = lib.wc.substr(str, startOffset, count - 1) +
1822 lib.wc.substr(str, strWidth - 1);
1823 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001824 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001825 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001826 }
rgindaa19afe22012-01-25 15:40:22 -08001827
Ricky Liang48f05cb2013-12-31 23:35:29 +08001828 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1829 for (var i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001830 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
1831 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001832
1833 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001834 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001835 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001836 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001837 }
1838 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001839 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07001840 }
1841
1842 this.screen_.maybeClipCurrentRow();
1843 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001844 }
rginda8ba33642011-12-14 12:31:31 -08001845
1846 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001847
rginda9f5222b2012-03-05 11:53:28 -08001848 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001849 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001850};
1851
1852/**
rginda87b86462011-12-14 13:48:03 -08001853 * Set the VT scroll region.
1854 *
rginda87b86462011-12-14 13:48:03 -08001855 * This also resets the cursor position to the absolute (0, 0) position, since
1856 * that's what xterm appears to do.
1857 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001858 * Setting the scroll region to the full height of the terminal will clear
1859 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1860 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1861 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1862 * continue to work as most users would expect.
1863 *
rginda87b86462011-12-14 13:48:03 -08001864 * @param {integer} scrollTop The zero-based top of the scroll region.
1865 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1866 * inclusive.
1867 */
1868hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001869 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001870 this.vtScrollTop_ = null;
1871 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001872 } else {
1873 this.vtScrollTop_ = scrollTop;
1874 this.vtScrollBottom_ = scrollBottom;
1875 }
rginda87b86462011-12-14 13:48:03 -08001876};
1877
1878/**
rginda8ba33642011-12-14 12:31:31 -08001879 * Return the top row index according to the VT.
1880 *
1881 * This will return 0 unless the terminal has been told to restrict scrolling
1882 * to some lower row. It is used for some VT cursor positioning and scrolling
1883 * commands.
1884 *
1885 * @return {integer} The topmost row in the terminal's scroll region.
1886 */
1887hterm.Terminal.prototype.getVTScrollTop = function() {
1888 if (this.vtScrollTop_ != null)
1889 return this.vtScrollTop_;
1890
1891 return 0;
rginda87b86462011-12-14 13:48:03 -08001892};
rginda8ba33642011-12-14 12:31:31 -08001893
1894/**
1895 * Return the bottom row index according to the VT.
1896 *
1897 * This will return the height of the terminal unless the it has been told to
1898 * restrict scrolling to some higher row. It is used for some VT cursor
1899 * positioning and scrolling commands.
1900 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001901 * @return {integer} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001902 */
1903hterm.Terminal.prototype.getVTScrollBottom = function() {
1904 if (this.vtScrollBottom_ != null)
1905 return this.vtScrollBottom_;
1906
rginda87b86462011-12-14 13:48:03 -08001907 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001908};
rginda8ba33642011-12-14 12:31:31 -08001909
1910/**
1911 * Process a '\n' character.
1912 *
1913 * If the cursor is on the final row of the terminal this will append a new
1914 * blank row to the screen and scroll the topmost row into the scrollback
1915 * buffer.
1916 *
1917 * Otherwise, this moves the cursor to column zero of the next row.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001918 *
1919 * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of
1920 * the terminal.
rginda8ba33642011-12-14 12:31:31 -08001921 */
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001922hterm.Terminal.prototype.newLine = function(dueToOverflow = false) {
1923 if (!dueToOverflow)
1924 this.accessibilityReader_.newLine();
1925
Robert Ginda9937abc2013-07-25 16:09:23 -07001926 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1927 this.screen_.rowsArray.length - 1);
1928
1929 if (this.vtScrollBottom_ != null) {
1930 // A VT Scroll region is active, we never append new rows.
1931 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1932 // We're at the end of the VT Scroll Region, perform a VT scroll.
1933 this.vtScrollUp(1);
1934 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1935 } else if (cursorAtEndOfScreen) {
1936 // We're at the end of the screen, the only thing to do is put the
1937 // cursor to column 0.
1938 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1939 } else {
1940 // Anywhere else, advance the cursor row, and reset the column.
1941 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1942 }
1943 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001944 // We're at the end of the screen. Append a new row to the terminal,
1945 // shifting the top row into the scrollback.
1946 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001947 } else {
rginda87b86462011-12-14 13:48:03 -08001948 // Anywhere else in the screen just moves the cursor.
1949 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001950 }
1951};
1952
1953/**
1954 * Like newLine(), except maintain the cursor column.
1955 */
1956hterm.Terminal.prototype.lineFeed = function() {
1957 var column = this.screen_.cursorPosition.column;
1958 this.newLine();
1959 this.setCursorColumn(column);
1960};
1961
1962/**
rginda87b86462011-12-14 13:48:03 -08001963 * If autoCarriageReturn is set then newLine(), else lineFeed().
1964 */
1965hterm.Terminal.prototype.formFeed = function() {
1966 if (this.options_.autoCarriageReturn) {
1967 this.newLine();
1968 } else {
1969 this.lineFeed();
1970 }
1971};
1972
1973/**
1974 * Move the cursor up one row, possibly inserting a blank line.
1975 *
1976 * The cursor column is not changed.
1977 */
1978hterm.Terminal.prototype.reverseLineFeed = function() {
1979 var scrollTop = this.getVTScrollTop();
1980 var currentRow = this.screen_.cursorPosition.row;
1981
1982 if (currentRow == scrollTop) {
1983 this.insertLines(1);
1984 } else {
1985 this.setAbsoluteCursorRow(currentRow - 1);
1986 }
1987};
1988
1989/**
rginda8ba33642011-12-14 12:31:31 -08001990 * Replace all characters to the left of the current cursor with the space
1991 * character.
1992 *
1993 * TODO(rginda): This should probably *remove* the characters (not just replace
1994 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001995 * position.
rginda8ba33642011-12-14 12:31:31 -08001996 */
1997hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001998 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001999 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002000 const count = cursor.column + 1;
2001 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08002002 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002003};
2004
2005/**
David Benjamin684a9b72012-05-01 17:19:58 -04002006 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08002007 *
2008 * The cursor position is unchanged.
2009 *
Robert Gindaf2547f12012-10-25 20:36:21 -07002010 * If the current background color is not the default background color this
2011 * will insert spaces rather than delete. This is unfortunate because the
2012 * trailing space will affect text selection, but it's difficult to come up
2013 * with a way to style empty space that wouldn't trip up the hterm.Screen
2014 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07002015 *
2016 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
2017 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2018 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002019 *
2020 * @param {number} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002021 */
2022hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002023 if (this.screen_.cursorPosition.overflow)
2024 return;
2025
Robert Ginda7fd57082012-09-25 14:41:47 -07002026 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
2027 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002028
2029 if (this.screen_.textAttributes.background ===
2030 this.screen_.textAttributes.DEFAULT_COLOR) {
2031 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002032 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002033 this.screen_.cursorPosition.column + count) {
2034 this.screen_.deleteChars(count);
2035 this.clearCursorOverflow();
2036 return;
2037 }
2038 }
2039
rginda87b86462011-12-14 13:48:03 -08002040 var cursor = this.saveCursor();
Mike Frysinger6380bed2017-08-24 18:46:39 -04002041 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08002042 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002043 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002044};
2045
2046/**
2047 * Erase the current line.
2048 *
2049 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002050 */
2051hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08002052 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002053 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002054 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002055 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002056};
2057
2058/**
David Benjamina08d78f2012-05-05 00:28:49 -04002059 * Erase all characters from the start of the screen to the current cursor
2060 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002061 *
2062 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002063 */
2064hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08002065 var cursor = this.saveCursor();
2066
2067 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002068
David Benjamina08d78f2012-05-05 00:28:49 -04002069 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002070 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002071 this.screen_.clearCursorRow();
2072 }
2073
rginda87b86462011-12-14 13:48:03 -08002074 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002075 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002076};
2077
2078/**
2079 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002080 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002081 *
2082 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002083 */
2084hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08002085 var cursor = this.saveCursor();
2086
2087 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002088
David Benjamina08d78f2012-05-05 00:28:49 -04002089 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08002090 for (var i = cursor.row + 1; i <= bottom; i++) {
2091 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002092 this.screen_.clearCursorRow();
2093 }
2094
rginda87b86462011-12-14 13:48:03 -08002095 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002096 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002097};
2098
2099/**
2100 * Fill the terminal with a given character.
2101 *
2102 * This methods does not respect the VT scroll region.
2103 *
2104 * @param {string} ch The character to use for the fill.
2105 */
2106hterm.Terminal.prototype.fill = function(ch) {
2107 var cursor = this.saveCursor();
2108
2109 this.setAbsoluteCursorPosition(0, 0);
2110 for (var row = 0; row < this.screenSize.height; row++) {
2111 for (var col = 0; col < this.screenSize.width; col++) {
2112 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002113 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002114 }
2115 }
2116
2117 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002118};
2119
2120/**
rginda9ea433c2012-03-16 11:57:00 -07002121 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002122 *
rginda9ea433c2012-03-16 11:57:00 -07002123 * This does not respect the scroll region.
2124 *
2125 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2126 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002127 */
rginda9ea433c2012-03-16 11:57:00 -07002128hterm.Terminal.prototype.clearHome = function(opt_screen) {
2129 var screen = opt_screen || this.screen_;
2130 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002131
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002132 this.accessibilityReader_.clear();
2133
rginda11057d52012-04-25 12:29:56 -07002134 if (bottom == 0) {
2135 // Empty screen, nothing to do.
2136 return;
2137 }
2138
rgindae4d29232012-01-19 10:47:13 -08002139 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002140 screen.setCursorPosition(i, 0);
2141 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002142 }
2143
rginda9ea433c2012-03-16 11:57:00 -07002144 screen.setCursorPosition(0, 0);
2145};
2146
2147/**
2148 * Erase the entire display without changing the cursor position.
2149 *
2150 * The cursor position is unchanged. This does not respect the scroll
2151 * region.
2152 *
2153 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2154 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002155 */
2156hterm.Terminal.prototype.clear = function(opt_screen) {
2157 var screen = opt_screen || this.screen_;
2158 var cursor = screen.cursorPosition.clone();
2159 this.clearHome(screen);
2160 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002161};
2162
2163/**
2164 * VT command to insert lines at the current cursor row.
2165 *
2166 * This respects the current scroll region. Rows pushed off the bottom are
2167 * lost (they won't show up in the scrollback buffer).
2168 *
rginda8ba33642011-12-14 12:31:31 -08002169 * @param {integer} count The number of lines to insert.
2170 */
2171hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002172 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002173
2174 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002175 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002176
Robert Ginda579186b2012-09-26 11:40:04 -07002177 // The moveCount is the number of rows we need to relocate to make room for
2178 // the new row(s). The count is the distance to move them.
2179 var moveCount = bottom - cursorRow - count + 1;
2180 if (moveCount)
2181 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002182
Robert Ginda579186b2012-09-26 11:40:04 -07002183 for (var i = count - 1; i >= 0; i--) {
2184 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002185 this.screen_.clearCursorRow();
2186 }
rginda8ba33642011-12-14 12:31:31 -08002187};
2188
2189/**
2190 * VT command to delete lines at the current cursor row.
2191 *
2192 * New rows are added to the bottom of scroll region to take their place. New
2193 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002194 *
2195 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002196 */
2197hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002198 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002199
rginda87b86462011-12-14 13:48:03 -08002200 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002201 var bottom = this.getVTScrollBottom();
2202
rginda87b86462011-12-14 13:48:03 -08002203 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002204 count = Math.min(count, maxCount);
2205
rginda87b86462011-12-14 13:48:03 -08002206 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002207 if (count != maxCount)
2208 this.moveRows_(top, count, moveStart);
2209
2210 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002211 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002212 this.screen_.clearCursorRow();
2213 }
2214
rginda87b86462011-12-14 13:48:03 -08002215 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002216 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002217};
2218
2219/**
2220 * Inserts the given number of spaces at the current cursor position.
2221 *
rginda87b86462011-12-14 13:48:03 -08002222 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002223 *
2224 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002225 */
2226hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002227 var cursor = this.saveCursor();
2228
rgindacbbd7482012-06-13 15:06:16 -07002229 var ws = lib.f.getWhitespace(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002230 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002231 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002232
2233 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002234 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002235};
2236
2237/**
2238 * Forward-delete the specified number of characters starting at the cursor
2239 * position.
2240 *
2241 * @param {integer} count The number of characters to delete.
2242 */
2243hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002244 var deleted = this.screen_.deleteChars(count);
2245 if (deleted && !this.screen_.textAttributes.isDefault()) {
2246 var cursor = this.saveCursor();
2247 this.setCursorColumn(this.screenSize.width - deleted);
2248 this.screen_.insertString(lib.f.getWhitespace(deleted));
2249 this.restoreCursor(cursor);
2250 }
2251
David Benjamin54e8bf62012-06-01 22:31:40 -04002252 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002253};
2254
2255/**
2256 * Shift rows in the scroll region upwards by a given number of lines.
2257 *
2258 * New rows are inserted at the bottom of the scroll region to fill the
2259 * vacated rows. The new rows not filled out with the current text attributes.
2260 *
2261 * This function does not affect the scrollback rows at all. Rows shifted
2262 * off the top are lost.
2263 *
rginda87b86462011-12-14 13:48:03 -08002264 * The cursor position is not altered.
2265 *
rginda8ba33642011-12-14 12:31:31 -08002266 * @param {integer} count The number of rows to scroll.
2267 */
2268hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002269 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002270
rginda87b86462011-12-14 13:48:03 -08002271 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002272 this.deleteLines(count);
2273
rginda87b86462011-12-14 13:48:03 -08002274 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002275};
2276
2277/**
2278 * Shift rows below the cursor down by a given number of lines.
2279 *
2280 * This function respects the current scroll region.
2281 *
2282 * New rows are inserted at the top of the scroll region to fill the
2283 * vacated rows. The new rows not filled out with the current text attributes.
2284 *
2285 * This function does not affect the scrollback rows at all. Rows shifted
2286 * off the bottom are lost.
2287 *
2288 * @param {integer} count The number of rows to scroll.
2289 */
2290hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08002291 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002292
rginda87b86462011-12-14 13:48:03 -08002293 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08002294 this.insertLines(opt_count);
2295
rginda87b86462011-12-14 13:48:03 -08002296 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002297};
2298
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002299/**
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002300 * Enable accessibility-friendly features that have a performance impact.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002301 *
2302 * This will generate additional DOM nodes in an aria-live region that will
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002303 * cause Assitive Technology to announce the output of the terminal. It also
2304 * enables other features that aid assistive technology. All the features gated
2305 * behind this flag have a performance impact on the terminal which is why they
2306 * are made optional.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002307 *
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002308 * @param {boolean} enabled Whether to enable accessibility-friendly features.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002309 */
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002310hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) {
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002311 this.accessibilityEnabled_ = enabled;
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002312 this.scrollPort_.setAccessibilityEnabled(enabled);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002313};
rginda87b86462011-12-14 13:48:03 -08002314
rginda8ba33642011-12-14 12:31:31 -08002315/**
2316 * Set the cursor position.
2317 *
2318 * The cursor row is relative to the scroll region if the terminal has
2319 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2320 *
2321 * @param {integer} row The new zero-based cursor row.
2322 * @param {integer} row The new zero-based cursor column.
2323 */
2324hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2325 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002326 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002327 } else {
rginda87b86462011-12-14 13:48:03 -08002328 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002329 }
rginda87b86462011-12-14 13:48:03 -08002330};
rginda8ba33642011-12-14 12:31:31 -08002331
Evan Jones2600d4f2016-12-06 09:29:36 -05002332/**
2333 * Move the cursor relative to its current position.
2334 *
2335 * @param {number} row
2336 * @param {number} column
2337 */
rginda87b86462011-12-14 13:48:03 -08002338hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2339 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002340 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2341 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002342 this.screen_.setCursorPosition(row, column);
2343};
2344
Evan Jones2600d4f2016-12-06 09:29:36 -05002345/**
2346 * Move the cursor to the specified position.
2347 *
2348 * @param {number} row
2349 * @param {number} column
2350 */
rginda87b86462011-12-14 13:48:03 -08002351hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002352 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2353 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002354 this.screen_.setCursorPosition(row, column);
2355};
2356
2357/**
2358 * Set the cursor column.
2359 *
2360 * @param {integer} column The new zero-based cursor column.
2361 */
2362hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002363 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002364};
2365
2366/**
2367 * Return the cursor column.
2368 *
2369 * @return {integer} The zero-based cursor column.
2370 */
2371hterm.Terminal.prototype.getCursorColumn = function() {
2372 return this.screen_.cursorPosition.column;
2373};
2374
2375/**
2376 * Set the cursor row.
2377 *
2378 * The cursor row is relative to the scroll region if the terminal has
2379 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2380 *
2381 * @param {integer} row The new cursor row.
2382 */
rginda87b86462011-12-14 13:48:03 -08002383hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2384 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002385};
2386
2387/**
2388 * Return the cursor row.
2389 *
2390 * @return {integer} The zero-based cursor row.
2391 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002392hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002393 return this.screen_.cursorPosition.row;
2394};
2395
2396/**
2397 * Request that the ScrollPort redraw itself soon.
2398 *
2399 * The redraw will happen asynchronously, soon after the call stack winds down.
2400 * Multiple calls will be coalesced into a single redraw.
2401 */
2402hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002403 if (this.timeouts_.redraw)
2404 return;
rginda8ba33642011-12-14 12:31:31 -08002405
2406 var self = this;
rginda87b86462011-12-14 13:48:03 -08002407 this.timeouts_.redraw = setTimeout(function() {
2408 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002409 self.scrollPort_.redraw_();
2410 }, 0);
2411};
2412
2413/**
2414 * Request that the ScrollPort be scrolled to the bottom.
2415 *
2416 * The scroll will happen asynchronously, soon after the call stack winds down.
2417 * Multiple calls will be coalesced into a single scroll.
2418 *
2419 * This affects the scrollbar position of the ScrollPort, and has nothing to
2420 * do with the VT scroll commands.
2421 */
2422hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2423 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002424 return;
rginda8ba33642011-12-14 12:31:31 -08002425
2426 var self = this;
2427 this.timeouts_.scrollDown = setTimeout(function() {
2428 delete self.timeouts_.scrollDown;
2429 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2430 }, 10);
2431};
2432
2433/**
2434 * Move the cursor up a specified number of rows.
2435 *
2436 * @param {integer} count The number of rows to move the cursor.
2437 */
2438hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002439 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002440};
2441
2442/**
2443 * Move the cursor down a specified number of rows.
2444 *
2445 * @param {integer} count The number of rows to move the cursor.
2446 */
2447hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002448 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002449 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2450 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2451 this.screenSize.height - 1);
2452
rgindacbbd7482012-06-13 15:06:16 -07002453 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002454 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002455 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002456};
2457
2458/**
2459 * Move the cursor left a specified number of columns.
2460 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002461 * If reverse wraparound mode is enabled and the previous row wrapped into
2462 * the current row then we back up through the wraparound as well.
2463 *
rginda8ba33642011-12-14 12:31:31 -08002464 * @param {integer} count The number of columns to move the cursor.
2465 */
2466hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002467 count = count || 1;
2468
2469 if (count < 1)
2470 return;
2471
2472 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002473 if (this.options_.reverseWraparound) {
2474 if (this.screen_.cursorPosition.overflow) {
2475 // If this cursor is in the right margin, consume one count to get it
2476 // back to the last column. This only applies when we're in reverse
2477 // wraparound mode.
2478 count--;
2479 this.clearCursorOverflow();
2480
2481 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002482 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002483 }
2484
Robert Gindabfb32622014-07-17 13:20:27 -07002485 var newRow = this.screen_.cursorPosition.row;
2486 var newColumn = currentColumn - count;
2487 if (newColumn < 0) {
2488 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2489 if (newRow < 0) {
2490 // xterm also wraps from row 0 to the last row.
2491 newRow = this.screenSize.height + newRow % this.screenSize.height;
2492 }
2493 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2494 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002495
Robert Gindabfb32622014-07-17 13:20:27 -07002496 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2497
2498 } else {
2499 var newColumn = Math.max(currentColumn - count, 0);
2500 this.setCursorColumn(newColumn);
2501 }
rginda8ba33642011-12-14 12:31:31 -08002502};
2503
2504/**
2505 * Move the cursor right a specified number of columns.
2506 *
2507 * @param {integer} count The number of columns to move the cursor.
2508 */
2509hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002510 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002511
2512 if (count < 1)
2513 return;
2514
rgindacbbd7482012-06-13 15:06:16 -07002515 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002516 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002517 this.setCursorColumn(column);
2518};
2519
2520/**
2521 * Reverse the foreground and background colors of the terminal.
2522 *
2523 * This only affects text that was drawn with no attributes.
2524 *
2525 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2526 * been drawn with attributes that happen to coincide with the default
2527 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002528 *
2529 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002530 */
2531hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002532 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002533 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002534 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2535 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002536 } else {
rginda9f5222b2012-03-05 11:53:28 -08002537 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2538 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002539 }
2540};
2541
2542/**
rginda87b86462011-12-14 13:48:03 -08002543 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002544 *
2545 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002546 */
2547hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002548 this.cursorNode_.style.backgroundColor =
2549 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002550
2551 var self = this;
2552 setTimeout(function() {
Matheus Fernandes2d733082017-09-11 06:43:01 -04002553 self.restyleCursor_();
rginda6d397402012-01-17 10:58:29 -08002554 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002555
Michael Kelly485ecd12014-06-09 11:41:56 -04002556 // bellSquelchTimeout_ affects both audio and notification bells.
2557 if (this.bellSquelchTimeout_)
2558 return;
2559
Robert Ginda92e18102013-03-14 13:56:37 -07002560 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002561 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002562 this.bellSequelchTimeout_ = setTimeout(function() {
2563 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002564 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002565 } else {
2566 delete this.bellSquelchTimeout_;
2567 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002568
2569 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002570 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002571 this.bellNotificationList_.push(n);
2572 // TODO: Should we try to raise the window here?
2573 n.onclick = function() { self.closeBellNotifications_(); };
2574 }
rginda87b86462011-12-14 13:48:03 -08002575};
2576
2577/**
rginda8ba33642011-12-14 12:31:31 -08002578 * Set the origin mode bit.
2579 *
2580 * If origin mode is on, certain VT cursor and scrolling commands measure their
2581 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2582 * to the top of the addressable screen.
2583 *
2584 * Defaults to off.
2585 *
2586 * @param {boolean} state True to set origin mode, false to unset.
2587 */
2588hterm.Terminal.prototype.setOriginMode = function(state) {
2589 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002590 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002591};
2592
2593/**
2594 * Set the insert mode bit.
2595 *
2596 * If insert mode is on, existing text beyond the cursor position will be
2597 * shifted right to make room for new text. Otherwise, new text overwrites
2598 * any existing text.
2599 *
2600 * Defaults to off.
2601 *
2602 * @param {boolean} state True to set insert mode, false to unset.
2603 */
2604hterm.Terminal.prototype.setInsertMode = function(state) {
2605 this.options_.insertMode = state;
2606};
2607
2608/**
rginda87b86462011-12-14 13:48:03 -08002609 * Set the auto carriage return bit.
2610 *
2611 * If auto carriage return is on then a formfeed character is interpreted
2612 * as a newline, otherwise it's the same as a linefeed. The difference boils
2613 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002614 *
2615 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002616 */
2617hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2618 this.options_.autoCarriageReturn = state;
2619};
2620
2621/**
rginda8ba33642011-12-14 12:31:31 -08002622 * Set the wraparound mode bit.
2623 *
2624 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2625 * to the start of the following row. Otherwise, the cursor is clamped to the
2626 * end of the screen and attempts to write past it are ignored.
2627 *
2628 * Defaults to on.
2629 *
2630 * @param {boolean} state True to set wraparound mode, false to unset.
2631 */
2632hterm.Terminal.prototype.setWraparound = function(state) {
2633 this.options_.wraparound = state;
2634};
2635
2636/**
2637 * Set the reverse-wraparound mode bit.
2638 *
2639 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2640 * to the end of the previous row. Otherwise, the cursor is clamped to column
2641 * 0.
2642 *
2643 * Defaults to off.
2644 *
2645 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2646 */
2647hterm.Terminal.prototype.setReverseWraparound = function(state) {
2648 this.options_.reverseWraparound = state;
2649};
2650
2651/**
2652 * Selects between the primary and alternate screens.
2653 *
2654 * If alternate mode is on, the alternate screen is active. Otherwise the
2655 * primary screen is active.
2656 *
2657 * Swapping screens has no effect on the scrollback buffer.
2658 *
2659 * Each screen maintains its own cursor position.
2660 *
2661 * Defaults to off.
2662 *
2663 * @param {boolean} state True to set alternate mode, false to unset.
2664 */
2665hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002666 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002667 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2668
rginda35c456b2012-02-09 17:29:05 -08002669 if (this.screen_.rowsArray.length &&
2670 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2671 // If the screen changed sizes while we were away, our rowIndexes may
2672 // be incorrect.
2673 var offset = this.scrollbackRows_.length;
2674 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002675 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002676 ary[i].rowIndex = offset + i;
2677 }
2678 }
rginda8ba33642011-12-14 12:31:31 -08002679
rginda35c456b2012-02-09 17:29:05 -08002680 this.realizeWidth_(this.screenSize.width);
2681 this.realizeHeight_(this.screenSize.height);
2682 this.scrollPort_.syncScrollHeight();
2683 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002684
rginda6d397402012-01-17 10:58:29 -08002685 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002686 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002687};
2688
2689/**
2690 * Set the cursor-blink mode bit.
2691 *
2692 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2693 * a visible cursor does not blink.
2694 *
2695 * You should make sure to turn blinking off if you're going to dispose of a
2696 * terminal, otherwise you'll leak a timeout.
2697 *
2698 * Defaults to on.
2699 *
2700 * @param {boolean} state True to set cursor-blink mode, false to unset.
2701 */
2702hterm.Terminal.prototype.setCursorBlink = function(state) {
2703 this.options_.cursorBlink = state;
2704
2705 if (!state && this.timeouts_.cursorBlink) {
2706 clearTimeout(this.timeouts_.cursorBlink);
2707 delete this.timeouts_.cursorBlink;
2708 }
2709
2710 if (this.options_.cursorVisible)
2711 this.setCursorVisible(true);
2712};
2713
2714/**
2715 * Set the cursor-visible mode bit.
2716 *
2717 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2718 *
2719 * Defaults to on.
2720 *
2721 * @param {boolean} state True to set cursor-visible mode, false to unset.
2722 */
2723hterm.Terminal.prototype.setCursorVisible = function(state) {
2724 this.options_.cursorVisible = state;
2725
2726 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002727 if (this.timeouts_.cursorBlink) {
2728 clearTimeout(this.timeouts_.cursorBlink);
2729 delete this.timeouts_.cursorBlink;
2730 }
rginda87b86462011-12-14 13:48:03 -08002731 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002732 return;
2733 }
2734
rginda87b86462011-12-14 13:48:03 -08002735 this.syncCursorPosition_();
2736
2737 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002738
2739 if (this.options_.cursorBlink) {
2740 if (this.timeouts_.cursorBlink)
2741 return;
2742
Robert Gindaea2183e2014-07-17 09:51:51 -07002743 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002744 } else {
2745 if (this.timeouts_.cursorBlink) {
2746 clearTimeout(this.timeouts_.cursorBlink);
2747 delete this.timeouts_.cursorBlink;
2748 }
2749 }
2750};
2751
2752/**
rginda87b86462011-12-14 13:48:03 -08002753 * Synchronizes the visible cursor and document selection with the current
2754 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002755 */
2756hterm.Terminal.prototype.syncCursorPosition_ = function() {
2757 var topRowIndex = this.scrollPort_.getTopRowIndex();
2758 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2759 var cursorRowIndex = this.scrollbackRows_.length +
2760 this.screen_.cursorPosition.row;
2761
2762 if (cursorRowIndex > bottomRowIndex) {
2763 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04002764 this.setCssVar('cursor-offset-row', '-1');
rginda8ba33642011-12-14 12:31:31 -08002765 return;
2766 }
2767
Robert Gindab837c052014-08-11 11:17:51 -07002768 if (this.options_.cursorVisible &&
2769 this.cursorNode_.style.display == 'none') {
2770 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2771 this.cursorNode_.style.display = '';
2772 }
2773
Mike Frysinger44c32202017-08-05 01:13:09 -04002774 // Position the cursor using CSS variable math. If we do the math in JS,
2775 // the float math will end up being more precise than the CSS which will
2776 // cause the cursor tracking to be off.
2777 this.setCssVar(
2778 'cursor-offset-row',
2779 `${cursorRowIndex - topRowIndex} + ` +
2780 `${this.scrollPort_.visibleRowTopMargin}px`);
2781 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08002782
2783 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04002784 '(' + this.screen_.cursorPosition.column +
2785 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08002786 ')');
2787
2788 // Update the caret for a11y purposes.
2789 var selection = this.document_.getSelection();
2790 if (selection && selection.isCollapsed)
2791 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002792};
2793
Robert Gindafb1be6a2013-12-11 11:56:22 -08002794/**
2795 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2796 * and character cell dimensions.
2797 */
Robert Ginda830583c2013-08-07 13:20:46 -07002798hterm.Terminal.prototype.restyleCursor_ = function() {
2799 var shape = this.cursorShape_;
2800
2801 if (this.cursorNode_.getAttribute('focus') == 'false') {
2802 // Always show a block cursor when unfocused.
2803 shape = hterm.Terminal.cursorShape.BLOCK;
2804 }
2805
2806 var style = this.cursorNode_.style;
2807
2808 switch (shape) {
2809 case hterm.Terminal.cursorShape.BEAM:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002810 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002811 style.backgroundColor = 'transparent';
2812 style.borderBottomStyle = null;
2813 style.borderLeftStyle = 'solid';
2814 break;
2815
2816 case hterm.Terminal.cursorShape.UNDERLINE:
2817 style.height = this.scrollPort_.characterSize.baseline + 'px';
2818 style.backgroundColor = 'transparent';
2819 style.borderBottomStyle = 'solid';
2820 // correct the size to put it exactly at the baseline
2821 style.borderLeftStyle = null;
2822 break;
2823
2824 default:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002825 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002826 style.backgroundColor = this.cursorColor_;
2827 style.borderBottomStyle = null;
2828 style.borderLeftStyle = null;
2829 break;
2830 }
2831};
2832
rginda8ba33642011-12-14 12:31:31 -08002833/**
2834 * Synchronizes the visible cursor with the current cursor coordinates.
2835 *
2836 * The sync will happen asynchronously, soon after the call stack winds down.
2837 * Multiple calls will be coalesced into a single sync.
2838 */
2839hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2840 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002841 return;
rginda8ba33642011-12-14 12:31:31 -08002842
2843 var self = this;
2844 this.timeouts_.syncCursor = setTimeout(function() {
2845 self.syncCursorPosition_();
2846 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002847 }, 0);
2848};
2849
rgindacc2996c2012-02-24 14:59:31 -08002850/**
rgindaf522ce02012-04-17 17:49:17 -07002851 * Show or hide the zoom warning.
2852 *
2853 * The zoom warning is a message warning the user that their browser zoom must
2854 * be set to 100% in order for hterm to function properly.
2855 *
2856 * @param {boolean} state True to show the message, false to hide it.
2857 */
2858hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2859 if (!this.zoomWarningNode_) {
2860 if (!state)
2861 return;
2862
2863 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002864 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07002865 this.zoomWarningNode_.style.cssText = (
2866 'color: black;' +
2867 'background-color: #ff2222;' +
2868 'font-size: large;' +
2869 'border-radius: 8px;' +
2870 'opacity: 0.75;' +
2871 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2872 'top: 0.5em;' +
2873 'right: 1.2em;' +
2874 'position: absolute;' +
2875 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002876 '-webkit-user-select: none;' +
2877 '-moz-text-size-adjust: none;' +
2878 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002879
2880 this.zoomWarningNode_.addEventListener('click', function(e) {
2881 this.parentNode.removeChild(this);
2882 });
rgindaf522ce02012-04-17 17:49:17 -07002883 }
2884
Robert Gindab4839c22013-02-28 16:52:10 -08002885 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2886 hterm.zoomWarningMessage,
2887 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2888
rgindaf522ce02012-04-17 17:49:17 -07002889 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2890
2891 if (state) {
2892 if (!this.zoomWarningNode_.parentNode)
2893 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2894 } else if (this.zoomWarningNode_.parentNode) {
2895 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2896 }
2897};
2898
2899/**
rgindacc2996c2012-02-24 14:59:31 -08002900 * Show the terminal overlay for a given amount of time.
2901 *
2902 * The terminal overlay appears in inverse video in a large font, centered
2903 * over the terminal. You should probably keep the overlay message brief,
2904 * since it's in a large font and you probably aren't going to check the size
2905 * of the terminal first.
2906 *
2907 * @param {string} msg The text (not HTML) message to display in the overlay.
2908 * @param {number} opt_timeout The amount of time to wait before fading out
2909 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2910 * stay up forever (or until the next overlay).
2911 */
2912hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002913 if (!this.overlayNode_) {
2914 if (!this.div_)
2915 return;
2916
2917 this.overlayNode_ = this.document_.createElement('div');
2918 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002919 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002920 'font-size: xx-large;' +
2921 'opacity: 0.75;' +
2922 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2923 'position: absolute;' +
2924 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002925 '-webkit-transition: opacity 180ms ease-in;' +
2926 '-moz-user-select: none;' +
2927 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002928
2929 this.overlayNode_.addEventListener('mousedown', function(e) {
2930 e.preventDefault();
2931 e.stopPropagation();
2932 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002933 }
2934
rginda9f5222b2012-03-05 11:53:28 -08002935 this.overlayNode_.style.color = this.prefs_.get('background-color');
2936 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2937 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2938
rgindaf0090c92012-02-10 14:58:52 -08002939 this.overlayNode_.textContent = msg;
2940 this.overlayNode_.style.opacity = '0.75';
2941
2942 if (!this.overlayNode_.parentNode)
2943 this.div_.appendChild(this.overlayNode_);
2944
Robert Ginda97769282013-02-01 15:30:30 -08002945 var divSize = hterm.getClientSize(this.div_);
2946 var overlaySize = hterm.getClientSize(this.overlayNode_);
2947
Robert Ginda8a59f762014-07-23 11:29:55 -07002948 this.overlayNode_.style.top =
2949 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08002950 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07002951 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08002952
rgindaf0090c92012-02-10 14:58:52 -08002953 if (this.overlayTimeout_)
2954 clearTimeout(this.overlayTimeout_);
2955
rgindacc2996c2012-02-24 14:59:31 -08002956 if (opt_timeout === null)
2957 return;
2958
Mike Frysingerb6cfded2017-09-18 00:39:31 -04002959 this.overlayTimeout_ = setTimeout(() => {
2960 this.overlayNode_.style.opacity = '0';
2961 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
2962 }, opt_timeout || 1500);
2963};
2964
2965/**
2966 * Hide the terminal overlay immediately.
2967 *
2968 * Useful when we show an overlay for an event with an unknown end time.
2969 */
2970hterm.Terminal.prototype.hideOverlay = function() {
2971 if (this.overlayTimeout_)
2972 clearTimeout(this.overlayTimeout_);
2973 this.overlayTimeout_ = null;
2974
2975 if (this.overlayNode_.parentNode)
2976 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
2977 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08002978};
2979
rginda4bba5e12012-06-20 16:15:30 -07002980/**
2981 * Paste from the system clipboard to the terminal.
2982 */
2983hterm.Terminal.prototype.paste = function() {
Mike Frysinger4628ad22017-07-20 02:44:20 -04002984 return hterm.pasteFromClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002985};
2986
2987/**
2988 * Copy a string to the system clipboard.
2989 *
2990 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05002991 *
2992 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07002993 */
2994hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002995 if (this.prefs_.get('enable-clipboard-notice'))
2996 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2997
rgindaa09e7332012-08-17 12:49:51 -07002998 var copySource = this.document_.createElement('pre');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002999 copySource.id = 'hterm:copy-to-clipboard-source';
rginda4bba5e12012-06-20 16:15:30 -07003000 copySource.textContent = str;
3001 copySource.style.cssText = (
3002 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003003 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07003004 'position: absolute;' +
3005 'top: -99px');
3006
3007 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07003008
rginda4bba5e12012-06-20 16:15:30 -07003009 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07003010 var anchorNode = selection.anchorNode;
3011 var anchorOffset = selection.anchorOffset;
3012 var focusNode = selection.focusNode;
3013 var focusOffset = selection.focusOffset;
3014
rginda4bba5e12012-06-20 16:15:30 -07003015 selection.selectAllChildren(copySource);
3016
rgindaa09e7332012-08-17 12:49:51 -07003017 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07003018
Rob Spies56953412014-04-28 14:09:47 -07003019 // IE doesn't support selection.extend. This means that the selection
3020 // won't return on IE.
Robert Ginda6f4f0aa2014-07-28 10:21:39 -07003021 if (selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07003022 selection.collapse(anchorNode, anchorOffset);
3023 selection.extend(focusNode, focusOffset);
3024 }
rgindafaa74742012-08-21 13:34:03 -07003025
rginda4bba5e12012-06-20 16:15:30 -07003026 copySource.parentNode.removeChild(copySource);
3027};
3028
Evan Jones2600d4f2016-12-06 09:29:36 -05003029/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003030 * Display an image.
3031 *
3032 * @param {Object} options The image to display.
3033 * @param {string=} options.name A human readable string for the image.
3034 * @param {string|number=} options.size The size (in bytes).
3035 * @param {boolean=} options.preserveAspectRatio Whether to preserve aspect.
3036 * @param {boolean=} options.inline Whether to display the image inline.
3037 * @param {string|number=} options.width The width of the image.
3038 * @param {string|number=} options.height The height of the image.
3039 * @param {string=} options.align Direction to align the image.
3040 * @param {string} options.uri The source URI for the image.
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003041 * @param {function=} onLoad Callback when loading finishes.
3042 * @param {function(Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003043 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003044hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003045 // Make sure we're actually given a resource to display.
3046 if (options.uri === undefined)
3047 return;
3048
3049 // Set up the defaults to simplify code below.
3050 if (!options.name)
3051 options.name = '';
3052
3053 // Has the user approved image display yet?
3054 if (this.allowImagesInline !== true) {
3055 this.newLine();
3056 const row = this.getRowNode(this.scrollbackRows_.length +
3057 this.getCursorRow() - 1);
3058
3059 if (this.allowImagesInline === false) {
3060 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3061 'Inline Images Disabled');
3062 return;
3063 }
3064
3065 // Show a prompt.
3066 let button;
3067 const span = this.document_.createElement('span');
3068 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3069 span.style.fontWeight = 'bold';
3070 span.style.borderWidth = '1px';
3071 span.style.borderStyle = 'dashed';
3072 button = this.document_.createElement('span');
3073 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3074 button.style.marginLeft = '1em';
3075 button.style.borderWidth = '1px';
3076 button.style.borderStyle = 'solid';
3077 button.addEventListener('click', () => {
3078 this.prefs_.set('allow-images-inline', false);
3079 });
3080 span.appendChild(button);
3081 button = this.document_.createElement('span');
3082 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3083 'allow this session');
3084 button.style.marginLeft = '1em';
3085 button.style.borderWidth = '1px';
3086 button.style.borderStyle = 'solid';
3087 button.addEventListener('click', () => {
3088 this.allowImagesInline = true;
3089 });
3090 span.appendChild(button);
3091 button = this.document_.createElement('span');
3092 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3093 button.style.marginLeft = '1em';
3094 button.style.borderWidth = '1px';
3095 button.style.borderStyle = 'solid';
3096 button.addEventListener('click', () => {
3097 this.prefs_.set('allow-images-inline', true);
3098 });
3099 span.appendChild(button);
3100
3101 row.appendChild(span);
3102 return;
3103 }
3104
3105 // See if we should show this object directly, or download it.
3106 if (options.inline) {
3107 const io = this.io.push();
3108 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
3109 'Loading $1 ...'), null);
3110
3111 // While we're loading the image, eat all the user's input.
3112 io.onVTKeystroke = io.sendString = () => {};
3113
3114 // Initialize this new image.
3115 const img = this.document_.createElement('img');
3116 img.src = options.uri;
3117 img.title = img.alt = options.name;
3118
3119 // Attach the image to the page to let it load/render. It won't stay here.
3120 // This is needed so it's visible and the DOM can calculate the height. If
3121 // the image is hidden or not in the DOM, the height is always 0.
3122 this.document_.body.appendChild(img);
3123
3124 // Wait for the image to finish loading before we try moving it to the
3125 // right place in the terminal.
3126 img.onload = () => {
3127 // Now that we have the image dimensions, figure out how to show it.
3128 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
3129 img.style.maxWidth = `${this.document_.body.clientWidth}px`;
3130 img.style.maxHeight = `${this.document_.body.clientHeight}px`;
3131
3132 // Parse a width/height specification.
3133 const parseDim = (dim, maxDim, cssVar) => {
3134 if (!dim || dim == 'auto')
3135 return '';
3136
3137 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3138 if (ary) {
3139 if (ary[2] == '%')
3140 return maxDim * parseInt(ary[1]) / 100 + 'px';
3141 else if (ary[2] == 'px')
3142 return dim;
3143 else
3144 return `calc(${dim} * var(${cssVar}))`;
3145 }
3146
3147 return '';
3148 };
3149 img.style.width =
3150 parseDim(options.width, this.document_.body.clientWidth,
3151 '--hterm-charsize-width');
3152 img.style.height =
3153 parseDim(options.height, this.document_.body.clientHeight,
3154 '--hterm-charsize-height');
3155
3156 // Figure out how many rows the image occupies, then add that many.
3157 // XXX: This count will be inaccurate if the font size changes on us.
3158 const padRows = Math.ceil(img.clientHeight /
3159 this.scrollPort_.characterSize.height);
3160 for (let i = 0; i < padRows; ++i)
3161 this.newLine();
3162
3163 // Update the max height in case the user shrinks the character size.
3164 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3165
3166 // Move the image to the last row. This way when we scroll up, it doesn't
3167 // disappear when the first row gets clipped. It will disappear when we
3168 // scroll down and the last row is clipped ...
3169 this.document_.body.removeChild(img);
3170 // Create a wrapper node so we can do an absolute in a relative position.
3171 // This helps with rounding errors between JS & CSS counts.
3172 const div = this.document_.createElement('div');
3173 div.style.position = 'relative';
3174 div.style.textAlign = options.align;
3175 img.style.position = 'absolute';
3176 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3177 div.appendChild(img);
3178 const row = this.getRowNode(this.scrollbackRows_.length +
3179 this.getCursorRow() - 1);
3180 row.appendChild(div);
3181
3182 io.hideOverlay();
3183 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003184
3185 if (onLoad)
3186 onLoad();
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003187 };
3188
3189 // If we got a malformed image, give up.
3190 img.onerror = (e) => {
3191 this.document_.body.removeChild(img);
3192 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
Mike Frysingere14a8c42018-03-10 00:17:30 -08003193 'Loading $1 failed'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003194 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003195
3196 if (onError)
3197 onError(e);
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003198 };
3199 } else {
3200 // We can't use chrome.downloads.download as that requires "downloads"
3201 // permissions, and that works only in extensions, not apps.
3202 const a = this.document_.createElement('a');
3203 a.href = options.uri;
3204 a.download = options.name;
3205 this.document_.body.appendChild(a);
3206 a.click();
3207 a.remove();
3208 }
3209};
3210
3211/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003212 * Returns the selected text, or null if no text is selected.
3213 *
3214 * @return {string|null}
3215 */
rgindaa09e7332012-08-17 12:49:51 -07003216hterm.Terminal.prototype.getSelectionText = function() {
3217 var selection = this.scrollPort_.selection;
3218 selection.sync();
3219
3220 if (selection.isCollapsed)
3221 return null;
3222
3223
3224 // Start offset measures from the beginning of the line.
3225 var startOffset = selection.startOffset;
3226 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003227
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};