blob: 71cc6322dac851cd1a758aa3b7f3c2fbfa0b82bf [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;' +
Mike Frysingerb74a6472018-06-22 13:37:08 -04001518 ' cursor: var(--hterm-mouse-cursor-pointer), pointer;' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001519 '}' +
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 '}');
Mike Frysingerb74a6472018-06-22 13:37:08 -04001531 // Insert this stock style as the first node so that any user styles will
1532 // override w/out having to use !important everywhere. The rules above mix
1533 // runtime variables with default ones designed to be overridden by the user,
1534 // but we can wait for a concrete case from the users to determine the best
1535 // way to split the sheet up to before & after the user-css settings.
1536 this.document_.head.insertBefore(style, this.document_.head.firstChild);
rginda8e92a692012-05-20 19:37:20 -07001537
rginda8ba33642011-12-14 12:31:31 -08001538 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001539 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001540 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001541 this.cursorNode_.style.cssText =
1542 ('position: absolute;' +
Mike Frysinger44c32202017-08-05 01:13:09 -04001543 'left: calc(var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));' +
1544 'top: calc(var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));' +
Mike Frysingerd60f4c22018-03-15 21:25:30 -07001545 'display: ' + (this.options_.cursorVisible ? '' : 'none') + ';' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001546 'width: var(--hterm-charsize-width);' +
1547 'height: var(--hterm-charsize-height);' +
Rob Spies06533ba2014-04-24 11:20:37 -07001548 '-webkit-transition: opacity, background-color 100ms linear;' +
1549 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001550
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001551 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001552 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1553 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001554
rginda8ba33642011-12-14 12:31:31 -08001555 this.document_.body.appendChild(this.cursorNode_);
1556
rgindad5613292012-06-19 15:40:37 -07001557 // When 'enableMouseDragScroll' is off we reposition this element directly
1558 // under the mouse cursor after a click. This makes Chrome associate
1559 // subsequent mousemove events with the scroll-blocker. Since the
1560 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1561 // events do not cause the scrollport to scroll.
1562 //
1563 // It's a hack, but it's the cleanest way I could find.
1564 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001565 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
Raymes Khoury6dce2f82018-04-12 15:38:58 +10001566 this.scrollBlockerNode_.setAttribute('aria-hidden', 'true');
rgindad5613292012-06-19 15:40:37 -07001567 this.scrollBlockerNode_.style.cssText =
1568 ('position: absolute;' +
1569 'top: -99px;' +
1570 'display: block;' +
1571 'width: 10px;' +
1572 'height: 10px;');
1573 this.document_.body.appendChild(this.scrollBlockerNode_);
1574
rgindad5613292012-06-19 15:40:37 -07001575 this.scrollPort_.onScrollWheel = onMouse;
1576 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1577 ].forEach(function(event) {
1578 this.scrollBlockerNode_.addEventListener(event, onMouse);
1579 this.cursorNode_.addEventListener(event, onMouse);
1580 this.document_.addEventListener(event, onMouse);
1581 }.bind(this));
1582
1583 this.cursorNode_.addEventListener('mousedown', function() {
1584 setTimeout(this.focus.bind(this));
1585 }.bind(this));
1586
rginda8ba33642011-12-14 12:31:31 -08001587 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001588
rginda87b86462011-12-14 13:48:03 -08001589 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001590 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001591};
1592
rginda0918b652012-04-04 11:26:24 -07001593/**
1594 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001595 *
1596 * @return {HTMLDocument}
rginda0918b652012-04-04 11:26:24 -07001597 */
rginda87b86462011-12-14 13:48:03 -08001598hterm.Terminal.prototype.getDocument = function() {
1599 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001600};
1601
1602/**
rginda0918b652012-04-04 11:26:24 -07001603 * Focus the terminal.
1604 */
1605hterm.Terminal.prototype.focus = function() {
1606 this.scrollPort_.focus();
1607};
1608
1609/**
rginda8ba33642011-12-14 12:31:31 -08001610 * Return the HTML Element for a given row index.
1611 *
1612 * This is a method from the RowProvider interface. The ScrollPort uses
1613 * it to fetch rows on demand as they are scrolled into view.
1614 *
1615 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1616 * pairs to conserve memory.
1617 *
1618 * @param {integer} index The zero-based row index, measured relative to the
1619 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001620 * largest indices.
rginda8ba33642011-12-14 12:31:31 -08001621 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1622 */
1623hterm.Terminal.prototype.getRowNode = function(index) {
1624 if (index < this.scrollbackRows_.length)
1625 return this.scrollbackRows_[index];
1626
1627 var screenIndex = index - this.scrollbackRows_.length;
1628 return this.screen_.rowsArray[screenIndex];
1629};
1630
1631/**
1632 * Return the text content for a given range of rows.
1633 *
1634 * This is a method from the RowProvider interface. The ScrollPort uses
1635 * it to fetch text content on demand when the user attempts to copy their
1636 * selection to the clipboard.
1637 *
1638 * @param {integer} start The zero-based row index to start from, measured
1639 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001640 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001641 * @param {integer} end The zero-based row index to end on, measured
1642 * relative to the start of the scrollback buffer.
1643 * @return {string} A single string containing the text value of the range of
1644 * rows. Lines will be newline delimited, with no trailing newline.
1645 */
1646hterm.Terminal.prototype.getRowsText = function(start, end) {
1647 var ary = [];
1648 for (var i = start; i < end; i++) {
1649 var node = this.getRowNode(i);
1650 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001651 if (i < end - 1 && !node.getAttribute('line-overflow'))
1652 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001653 }
1654
rgindaa09e7332012-08-17 12:49:51 -07001655 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001656};
1657
1658/**
1659 * Return the text content for a given row.
1660 *
1661 * This is a method from the RowProvider interface. The ScrollPort uses
1662 * it to fetch text content on demand when the user attempts to copy their
1663 * selection to the clipboard.
1664 *
1665 * @param {integer} index The zero-based row index to return, measured
1666 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001667 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001668 * @return {string} A string containing the text value of the selected row.
1669 */
1670hterm.Terminal.prototype.getRowText = function(index) {
1671 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001672 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001673};
1674
1675/**
1676 * Return the total number of rows in the addressable screen and in the
1677 * scrollback buffer of this terminal.
1678 *
1679 * This is a method from the RowProvider interface. The ScrollPort uses
1680 * it to compute the size of the scrollbar.
1681 *
1682 * @return {integer} The number of rows in this terminal.
1683 */
1684hterm.Terminal.prototype.getRowCount = function() {
1685 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1686};
1687
1688/**
1689 * Create DOM nodes for new rows and append them to the end of the terminal.
1690 *
1691 * This is the only correct way to add a new DOM node for a row. Notice that
1692 * the new row is appended to the bottom of the list of rows, and does not
1693 * require renumbering (of the rowIndex property) of previous rows.
1694 *
1695 * If you think you want a new blank row somewhere in the middle of the
1696 * terminal, look into moveRows_().
1697 *
1698 * This method does not pay attention to vtScrollTop/Bottom, since you should
1699 * be using moveRows() in cases where they would matter.
1700 *
1701 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001702 *
1703 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001704 */
1705hterm.Terminal.prototype.appendRows_ = function(count) {
1706 var cursorRow = this.screen_.rowsArray.length;
1707 var offset = this.scrollbackRows_.length + cursorRow;
1708 for (var i = 0; i < count; i++) {
1709 var row = this.document_.createElement('x-row');
1710 row.appendChild(this.document_.createTextNode(''));
1711 row.rowIndex = offset + i;
1712 this.screen_.pushRow(row);
1713 }
1714
1715 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1716 if (extraRows > 0) {
1717 var ary = this.screen_.shiftRows(extraRows);
1718 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001719 if (this.scrollPort_.isScrolledEnd)
1720 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001721 }
1722
1723 if (cursorRow >= this.screen_.rowsArray.length)
1724 cursorRow = this.screen_.rowsArray.length - 1;
1725
rginda87b86462011-12-14 13:48:03 -08001726 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001727};
1728
1729/**
1730 * Relocate rows from one part of the addressable screen to another.
1731 *
1732 * This is used to recycle rows during VT scrolls (those which are driven
1733 * by VT commands, rather than by the user manipulating the scrollbar.)
1734 *
1735 * In this case, the blank lines scrolled into the scroll region are made of
1736 * the nodes we scrolled off. These have their rowIndex properties carefully
1737 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001738 *
1739 * @param {number} fromIndex The start index.
1740 * @param {number} count The number of rows to move.
1741 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001742 */
1743hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1744 var ary = this.screen_.removeRows(fromIndex, count);
1745 this.screen_.insertRows(toIndex, ary);
1746
1747 var start, end;
1748 if (fromIndex < toIndex) {
1749 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001750 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001751 } else {
1752 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001753 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001754 }
1755
1756 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001757 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001758};
1759
1760/**
1761 * Renumber the rowIndex property of the given range of rows.
1762 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001763 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001764 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001765 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001766 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001767 *
1768 * @param {number} start The start index.
1769 * @param {number} end The end index.
1770 * @param {hterm.Screen} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001771 */
Robert Ginda40932892012-12-10 17:26:40 -08001772hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1773 var screen = opt_screen || this.screen_;
1774
rginda8ba33642011-12-14 12:31:31 -08001775 var offset = this.scrollbackRows_.length;
1776 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001777 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001778 }
1779};
1780
1781/**
1782 * Print a string to the terminal.
1783 *
1784 * This respects the current insert and wraparound modes. It will add new lines
1785 * to the end of the terminal, scrolling off the top into the scrollback buffer
1786 * if necessary.
1787 *
1788 * The string is *not* parsed for escape codes. Use the interpret() method if
1789 * that's what you're after.
1790 *
1791 * @param{string} str The string to print.
1792 */
1793hterm.Terminal.prototype.print = function(str) {
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001794 // Basic accessibility output for the screen reader.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001795 if (this.accessibilityEnabled_) {
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001796 this.accessibilityReader_.announce(str);
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001797 }
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001798
rgindaa9abdd82012-08-06 18:05:09 -07001799 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001800
Ricky Liang48f05cb2013-12-31 23:35:29 +08001801 var strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04001802 // Fun edge case: If the string only contains zero width codepoints (like
1803 // combining characters), we make sure to iterate at least once below.
1804 if (strWidth == 0 && str)
1805 strWidth = 1;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001806
1807 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001808 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1809 this.screen_.commitLineOverflow();
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001810 this.newLine(true);
rgindaa09e7332012-08-17 12:49:51 -07001811 }
rgindaa19afe22012-01-25 15:40:22 -08001812
Ricky Liang48f05cb2013-12-31 23:35:29 +08001813 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001814 var didOverflow = false;
1815 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001816
rgindaa9abdd82012-08-06 18:05:09 -07001817 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1818 didOverflow = true;
1819 count = this.screenSize.width - this.screen_.cursorPosition.column;
1820 }
rgindaa19afe22012-01-25 15:40:22 -08001821
rgindaa9abdd82012-08-06 18:05:09 -07001822 if (didOverflow && !this.options_.wraparound) {
1823 // If the string overflowed the line but wraparound is off, then the
1824 // last printed character should be the last of the string.
1825 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001826 substr = lib.wc.substr(str, startOffset, count - 1) +
1827 lib.wc.substr(str, strWidth - 1);
1828 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001829 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001830 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001831 }
rgindaa19afe22012-01-25 15:40:22 -08001832
Ricky Liang48f05cb2013-12-31 23:35:29 +08001833 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1834 for (var i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001835 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
1836 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001837
1838 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001839 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001840 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001841 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001842 }
1843 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001844 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07001845 }
1846
1847 this.screen_.maybeClipCurrentRow();
1848 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001849 }
rginda8ba33642011-12-14 12:31:31 -08001850
1851 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001852
rginda9f5222b2012-03-05 11:53:28 -08001853 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001854 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001855};
1856
1857/**
rginda87b86462011-12-14 13:48:03 -08001858 * Set the VT scroll region.
1859 *
rginda87b86462011-12-14 13:48:03 -08001860 * This also resets the cursor position to the absolute (0, 0) position, since
1861 * that's what xterm appears to do.
1862 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001863 * Setting the scroll region to the full height of the terminal will clear
1864 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1865 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1866 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1867 * continue to work as most users would expect.
1868 *
rginda87b86462011-12-14 13:48:03 -08001869 * @param {integer} scrollTop The zero-based top of the scroll region.
1870 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1871 * inclusive.
1872 */
1873hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001874 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001875 this.vtScrollTop_ = null;
1876 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001877 } else {
1878 this.vtScrollTop_ = scrollTop;
1879 this.vtScrollBottom_ = scrollBottom;
1880 }
rginda87b86462011-12-14 13:48:03 -08001881};
1882
1883/**
rginda8ba33642011-12-14 12:31:31 -08001884 * Return the top row index according to the VT.
1885 *
1886 * This will return 0 unless the terminal has been told to restrict scrolling
1887 * to some lower row. It is used for some VT cursor positioning and scrolling
1888 * commands.
1889 *
1890 * @return {integer} The topmost row in the terminal's scroll region.
1891 */
1892hterm.Terminal.prototype.getVTScrollTop = function() {
1893 if (this.vtScrollTop_ != null)
1894 return this.vtScrollTop_;
1895
1896 return 0;
rginda87b86462011-12-14 13:48:03 -08001897};
rginda8ba33642011-12-14 12:31:31 -08001898
1899/**
1900 * Return the bottom row index according to the VT.
1901 *
1902 * This will return the height of the terminal unless the it has been told to
1903 * restrict scrolling to some higher row. It is used for some VT cursor
1904 * positioning and scrolling commands.
1905 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001906 * @return {integer} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001907 */
1908hterm.Terminal.prototype.getVTScrollBottom = function() {
1909 if (this.vtScrollBottom_ != null)
1910 return this.vtScrollBottom_;
1911
rginda87b86462011-12-14 13:48:03 -08001912 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001913};
rginda8ba33642011-12-14 12:31:31 -08001914
1915/**
1916 * Process a '\n' character.
1917 *
1918 * If the cursor is on the final row of the terminal this will append a new
1919 * blank row to the screen and scroll the topmost row into the scrollback
1920 * buffer.
1921 *
1922 * Otherwise, this moves the cursor to column zero of the next row.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001923 *
1924 * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of
1925 * the terminal.
rginda8ba33642011-12-14 12:31:31 -08001926 */
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001927hterm.Terminal.prototype.newLine = function(dueToOverflow = false) {
1928 if (!dueToOverflow)
1929 this.accessibilityReader_.newLine();
1930
Robert Ginda9937abc2013-07-25 16:09:23 -07001931 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1932 this.screen_.rowsArray.length - 1);
1933
1934 if (this.vtScrollBottom_ != null) {
1935 // A VT Scroll region is active, we never append new rows.
1936 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1937 // We're at the end of the VT Scroll Region, perform a VT scroll.
1938 this.vtScrollUp(1);
1939 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1940 } else if (cursorAtEndOfScreen) {
1941 // We're at the end of the screen, the only thing to do is put the
1942 // cursor to column 0.
1943 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1944 } else {
1945 // Anywhere else, advance the cursor row, and reset the column.
1946 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1947 }
1948 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001949 // We're at the end of the screen. Append a new row to the terminal,
1950 // shifting the top row into the scrollback.
1951 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001952 } else {
rginda87b86462011-12-14 13:48:03 -08001953 // Anywhere else in the screen just moves the cursor.
1954 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001955 }
1956};
1957
1958/**
1959 * Like newLine(), except maintain the cursor column.
1960 */
1961hterm.Terminal.prototype.lineFeed = function() {
1962 var column = this.screen_.cursorPosition.column;
1963 this.newLine();
1964 this.setCursorColumn(column);
1965};
1966
1967/**
rginda87b86462011-12-14 13:48:03 -08001968 * If autoCarriageReturn is set then newLine(), else lineFeed().
1969 */
1970hterm.Terminal.prototype.formFeed = function() {
1971 if (this.options_.autoCarriageReturn) {
1972 this.newLine();
1973 } else {
1974 this.lineFeed();
1975 }
1976};
1977
1978/**
1979 * Move the cursor up one row, possibly inserting a blank line.
1980 *
1981 * The cursor column is not changed.
1982 */
1983hterm.Terminal.prototype.reverseLineFeed = function() {
1984 var scrollTop = this.getVTScrollTop();
1985 var currentRow = this.screen_.cursorPosition.row;
1986
1987 if (currentRow == scrollTop) {
1988 this.insertLines(1);
1989 } else {
1990 this.setAbsoluteCursorRow(currentRow - 1);
1991 }
1992};
1993
1994/**
rginda8ba33642011-12-14 12:31:31 -08001995 * Replace all characters to the left of the current cursor with the space
1996 * character.
1997 *
1998 * TODO(rginda): This should probably *remove* the characters (not just replace
1999 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07002000 * position.
rginda8ba33642011-12-14 12:31:31 -08002001 */
2002hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08002003 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002004 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002005 const count = cursor.column + 1;
2006 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08002007 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002008};
2009
2010/**
David Benjamin684a9b72012-05-01 17:19:58 -04002011 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08002012 *
2013 * The cursor position is unchanged.
2014 *
Robert Gindaf2547f12012-10-25 20:36:21 -07002015 * If the current background color is not the default background color this
2016 * will insert spaces rather than delete. This is unfortunate because the
2017 * trailing space will affect text selection, but it's difficult to come up
2018 * with a way to style empty space that wouldn't trip up the hterm.Screen
2019 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07002020 *
2021 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
2022 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2023 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002024 *
2025 * @param {number} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002026 */
2027hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002028 if (this.screen_.cursorPosition.overflow)
2029 return;
2030
Robert Ginda7fd57082012-09-25 14:41:47 -07002031 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
2032 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002033
2034 if (this.screen_.textAttributes.background ===
2035 this.screen_.textAttributes.DEFAULT_COLOR) {
2036 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002037 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002038 this.screen_.cursorPosition.column + count) {
2039 this.screen_.deleteChars(count);
2040 this.clearCursorOverflow();
2041 return;
2042 }
2043 }
2044
rginda87b86462011-12-14 13:48:03 -08002045 var cursor = this.saveCursor();
Mike Frysinger6380bed2017-08-24 18:46:39 -04002046 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08002047 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002048 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002049};
2050
2051/**
2052 * Erase the current line.
2053 *
2054 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002055 */
2056hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08002057 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002058 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002059 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002060 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002061};
2062
2063/**
David Benjamina08d78f2012-05-05 00:28:49 -04002064 * Erase all characters from the start of the screen to the current cursor
2065 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002066 *
2067 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002068 */
2069hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08002070 var cursor = this.saveCursor();
2071
2072 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002073
David Benjamina08d78f2012-05-05 00:28:49 -04002074 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002075 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002076 this.screen_.clearCursorRow();
2077 }
2078
rginda87b86462011-12-14 13:48:03 -08002079 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002080 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002081};
2082
2083/**
2084 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002085 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002086 *
2087 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002088 */
2089hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08002090 var cursor = this.saveCursor();
2091
2092 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002093
David Benjamina08d78f2012-05-05 00:28:49 -04002094 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08002095 for (var i = cursor.row + 1; i <= bottom; i++) {
2096 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002097 this.screen_.clearCursorRow();
2098 }
2099
rginda87b86462011-12-14 13:48:03 -08002100 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002101 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002102};
2103
2104/**
2105 * Fill the terminal with a given character.
2106 *
2107 * This methods does not respect the VT scroll region.
2108 *
2109 * @param {string} ch The character to use for the fill.
2110 */
2111hterm.Terminal.prototype.fill = function(ch) {
2112 var cursor = this.saveCursor();
2113
2114 this.setAbsoluteCursorPosition(0, 0);
2115 for (var row = 0; row < this.screenSize.height; row++) {
2116 for (var col = 0; col < this.screenSize.width; col++) {
2117 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002118 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002119 }
2120 }
2121
2122 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002123};
2124
2125/**
rginda9ea433c2012-03-16 11:57:00 -07002126 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002127 *
rginda9ea433c2012-03-16 11:57:00 -07002128 * This does not respect the scroll region.
2129 *
2130 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2131 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002132 */
rginda9ea433c2012-03-16 11:57:00 -07002133hterm.Terminal.prototype.clearHome = function(opt_screen) {
2134 var screen = opt_screen || this.screen_;
2135 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002136
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002137 this.accessibilityReader_.clear();
2138
rginda11057d52012-04-25 12:29:56 -07002139 if (bottom == 0) {
2140 // Empty screen, nothing to do.
2141 return;
2142 }
2143
rgindae4d29232012-01-19 10:47:13 -08002144 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002145 screen.setCursorPosition(i, 0);
2146 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002147 }
2148
rginda9ea433c2012-03-16 11:57:00 -07002149 screen.setCursorPosition(0, 0);
2150};
2151
2152/**
2153 * Erase the entire display without changing the cursor position.
2154 *
2155 * The cursor position is unchanged. This does not respect the scroll
2156 * region.
2157 *
2158 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2159 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002160 */
2161hterm.Terminal.prototype.clear = function(opt_screen) {
2162 var screen = opt_screen || this.screen_;
2163 var cursor = screen.cursorPosition.clone();
2164 this.clearHome(screen);
2165 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002166};
2167
2168/**
2169 * VT command to insert lines at the current cursor row.
2170 *
2171 * This respects the current scroll region. Rows pushed off the bottom are
2172 * lost (they won't show up in the scrollback buffer).
2173 *
rginda8ba33642011-12-14 12:31:31 -08002174 * @param {integer} count The number of lines to insert.
2175 */
2176hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002177 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002178
2179 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002180 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002181
Robert Ginda579186b2012-09-26 11:40:04 -07002182 // The moveCount is the number of rows we need to relocate to make room for
2183 // the new row(s). The count is the distance to move them.
2184 var moveCount = bottom - cursorRow - count + 1;
2185 if (moveCount)
2186 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002187
Robert Ginda579186b2012-09-26 11:40:04 -07002188 for (var i = count - 1; i >= 0; i--) {
2189 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002190 this.screen_.clearCursorRow();
2191 }
rginda8ba33642011-12-14 12:31:31 -08002192};
2193
2194/**
2195 * VT command to delete lines at the current cursor row.
2196 *
2197 * New rows are added to the bottom of scroll region to take their place. New
2198 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002199 *
2200 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002201 */
2202hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002203 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002204
rginda87b86462011-12-14 13:48:03 -08002205 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002206 var bottom = this.getVTScrollBottom();
2207
rginda87b86462011-12-14 13:48:03 -08002208 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002209 count = Math.min(count, maxCount);
2210
rginda87b86462011-12-14 13:48:03 -08002211 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002212 if (count != maxCount)
2213 this.moveRows_(top, count, moveStart);
2214
2215 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002216 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002217 this.screen_.clearCursorRow();
2218 }
2219
rginda87b86462011-12-14 13:48:03 -08002220 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002221 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002222};
2223
2224/**
2225 * Inserts the given number of spaces at the current cursor position.
2226 *
rginda87b86462011-12-14 13:48:03 -08002227 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002228 *
2229 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002230 */
2231hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002232 var cursor = this.saveCursor();
2233
rgindacbbd7482012-06-13 15:06:16 -07002234 var ws = lib.f.getWhitespace(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002235 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002236 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002237
2238 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002239 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002240};
2241
2242/**
2243 * Forward-delete the specified number of characters starting at the cursor
2244 * position.
2245 *
2246 * @param {integer} count The number of characters to delete.
2247 */
2248hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002249 var deleted = this.screen_.deleteChars(count);
2250 if (deleted && !this.screen_.textAttributes.isDefault()) {
2251 var cursor = this.saveCursor();
2252 this.setCursorColumn(this.screenSize.width - deleted);
2253 this.screen_.insertString(lib.f.getWhitespace(deleted));
2254 this.restoreCursor(cursor);
2255 }
2256
David Benjamin54e8bf62012-06-01 22:31:40 -04002257 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002258};
2259
2260/**
2261 * Shift rows in the scroll region upwards by a given number of lines.
2262 *
2263 * New rows are inserted at the bottom of the scroll region to fill the
2264 * vacated rows. The new rows not filled out with the current text attributes.
2265 *
2266 * This function does not affect the scrollback rows at all. Rows shifted
2267 * off the top are lost.
2268 *
rginda87b86462011-12-14 13:48:03 -08002269 * The cursor position is not altered.
2270 *
rginda8ba33642011-12-14 12:31:31 -08002271 * @param {integer} count The number of rows to scroll.
2272 */
2273hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002274 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002275
rginda87b86462011-12-14 13:48:03 -08002276 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002277 this.deleteLines(count);
2278
rginda87b86462011-12-14 13:48:03 -08002279 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002280};
2281
2282/**
2283 * Shift rows below the cursor down by a given number of lines.
2284 *
2285 * This function respects the current scroll region.
2286 *
2287 * New rows are inserted at the top of the scroll region to fill the
2288 * vacated rows. The new rows not filled out with the current text attributes.
2289 *
2290 * This function does not affect the scrollback rows at all. Rows shifted
2291 * off the bottom are lost.
2292 *
2293 * @param {integer} count The number of rows to scroll.
2294 */
2295hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08002296 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002297
rginda87b86462011-12-14 13:48:03 -08002298 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08002299 this.insertLines(opt_count);
2300
rginda87b86462011-12-14 13:48:03 -08002301 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002302};
2303
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002304/**
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002305 * Enable accessibility-friendly features that have a performance impact.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002306 *
2307 * This will generate additional DOM nodes in an aria-live region that will
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002308 * cause Assitive Technology to announce the output of the terminal. It also
2309 * enables other features that aid assistive technology. All the features gated
2310 * behind this flag have a performance impact on the terminal which is why they
2311 * are made optional.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002312 *
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002313 * @param {boolean} enabled Whether to enable accessibility-friendly features.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002314 */
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002315hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) {
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002316 this.accessibilityEnabled_ = enabled;
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002317 this.scrollPort_.setAccessibilityEnabled(enabled);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002318};
rginda87b86462011-12-14 13:48:03 -08002319
rginda8ba33642011-12-14 12:31:31 -08002320/**
2321 * Set the cursor position.
2322 *
2323 * The cursor row is relative to the scroll region if the terminal has
2324 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2325 *
2326 * @param {integer} row The new zero-based cursor row.
2327 * @param {integer} row The new zero-based cursor column.
2328 */
2329hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2330 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002331 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002332 } else {
rginda87b86462011-12-14 13:48:03 -08002333 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002334 }
rginda87b86462011-12-14 13:48:03 -08002335};
rginda8ba33642011-12-14 12:31:31 -08002336
Evan Jones2600d4f2016-12-06 09:29:36 -05002337/**
2338 * Move the cursor relative to its current position.
2339 *
2340 * @param {number} row
2341 * @param {number} column
2342 */
rginda87b86462011-12-14 13:48:03 -08002343hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2344 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002345 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2346 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002347 this.screen_.setCursorPosition(row, column);
2348};
2349
Evan Jones2600d4f2016-12-06 09:29:36 -05002350/**
2351 * Move the cursor to the specified position.
2352 *
2353 * @param {number} row
2354 * @param {number} column
2355 */
rginda87b86462011-12-14 13:48:03 -08002356hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002357 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2358 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002359 this.screen_.setCursorPosition(row, column);
2360};
2361
2362/**
2363 * Set the cursor column.
2364 *
2365 * @param {integer} column The new zero-based cursor column.
2366 */
2367hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002368 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002369};
2370
2371/**
2372 * Return the cursor column.
2373 *
2374 * @return {integer} The zero-based cursor column.
2375 */
2376hterm.Terminal.prototype.getCursorColumn = function() {
2377 return this.screen_.cursorPosition.column;
2378};
2379
2380/**
2381 * Set the cursor row.
2382 *
2383 * The cursor row is relative to the scroll region if the terminal has
2384 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2385 *
2386 * @param {integer} row The new cursor row.
2387 */
rginda87b86462011-12-14 13:48:03 -08002388hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2389 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002390};
2391
2392/**
2393 * Return the cursor row.
2394 *
2395 * @return {integer} The zero-based cursor row.
2396 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002397hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002398 return this.screen_.cursorPosition.row;
2399};
2400
2401/**
2402 * Request that the ScrollPort redraw itself soon.
2403 *
2404 * The redraw will happen asynchronously, soon after the call stack winds down.
2405 * Multiple calls will be coalesced into a single redraw.
2406 */
2407hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002408 if (this.timeouts_.redraw)
2409 return;
rginda8ba33642011-12-14 12:31:31 -08002410
2411 var self = this;
rginda87b86462011-12-14 13:48:03 -08002412 this.timeouts_.redraw = setTimeout(function() {
2413 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002414 self.scrollPort_.redraw_();
2415 }, 0);
2416};
2417
2418/**
2419 * Request that the ScrollPort be scrolled to the bottom.
2420 *
2421 * The scroll will happen asynchronously, soon after the call stack winds down.
2422 * Multiple calls will be coalesced into a single scroll.
2423 *
2424 * This affects the scrollbar position of the ScrollPort, and has nothing to
2425 * do with the VT scroll commands.
2426 */
2427hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2428 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002429 return;
rginda8ba33642011-12-14 12:31:31 -08002430
2431 var self = this;
2432 this.timeouts_.scrollDown = setTimeout(function() {
2433 delete self.timeouts_.scrollDown;
2434 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2435 }, 10);
2436};
2437
2438/**
2439 * Move the cursor up a specified number of rows.
2440 *
2441 * @param {integer} count The number of rows to move the cursor.
2442 */
2443hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002444 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002445};
2446
2447/**
2448 * Move the cursor down a specified number of rows.
2449 *
2450 * @param {integer} count The number of rows to move the cursor.
2451 */
2452hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002453 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002454 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2455 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2456 this.screenSize.height - 1);
2457
rgindacbbd7482012-06-13 15:06:16 -07002458 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002459 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002460 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002461};
2462
2463/**
2464 * Move the cursor left a specified number of columns.
2465 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002466 * If reverse wraparound mode is enabled and the previous row wrapped into
2467 * the current row then we back up through the wraparound as well.
2468 *
rginda8ba33642011-12-14 12:31:31 -08002469 * @param {integer} count The number of columns to move the cursor.
2470 */
2471hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002472 count = count || 1;
2473
2474 if (count < 1)
2475 return;
2476
2477 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002478 if (this.options_.reverseWraparound) {
2479 if (this.screen_.cursorPosition.overflow) {
2480 // If this cursor is in the right margin, consume one count to get it
2481 // back to the last column. This only applies when we're in reverse
2482 // wraparound mode.
2483 count--;
2484 this.clearCursorOverflow();
2485
2486 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002487 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002488 }
2489
Robert Gindabfb32622014-07-17 13:20:27 -07002490 var newRow = this.screen_.cursorPosition.row;
2491 var newColumn = currentColumn - count;
2492 if (newColumn < 0) {
2493 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2494 if (newRow < 0) {
2495 // xterm also wraps from row 0 to the last row.
2496 newRow = this.screenSize.height + newRow % this.screenSize.height;
2497 }
2498 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2499 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002500
Robert Gindabfb32622014-07-17 13:20:27 -07002501 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2502
2503 } else {
2504 var newColumn = Math.max(currentColumn - count, 0);
2505 this.setCursorColumn(newColumn);
2506 }
rginda8ba33642011-12-14 12:31:31 -08002507};
2508
2509/**
2510 * Move the cursor right a specified number of columns.
2511 *
2512 * @param {integer} count The number of columns to move the cursor.
2513 */
2514hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002515 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002516
2517 if (count < 1)
2518 return;
2519
rgindacbbd7482012-06-13 15:06:16 -07002520 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002521 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002522 this.setCursorColumn(column);
2523};
2524
2525/**
2526 * Reverse the foreground and background colors of the terminal.
2527 *
2528 * This only affects text that was drawn with no attributes.
2529 *
2530 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2531 * been drawn with attributes that happen to coincide with the default
2532 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002533 *
2534 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002535 */
2536hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002537 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002538 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002539 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2540 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002541 } else {
rginda9f5222b2012-03-05 11:53:28 -08002542 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2543 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002544 }
2545};
2546
2547/**
rginda87b86462011-12-14 13:48:03 -08002548 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002549 *
2550 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002551 */
2552hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002553 this.cursorNode_.style.backgroundColor =
2554 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002555
2556 var self = this;
2557 setTimeout(function() {
Matheus Fernandes2d733082017-09-11 06:43:01 -04002558 self.restyleCursor_();
rginda6d397402012-01-17 10:58:29 -08002559 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002560
Michael Kelly485ecd12014-06-09 11:41:56 -04002561 // bellSquelchTimeout_ affects both audio and notification bells.
2562 if (this.bellSquelchTimeout_)
2563 return;
2564
Robert Ginda92e18102013-03-14 13:56:37 -07002565 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002566 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002567 this.bellSequelchTimeout_ = setTimeout(function() {
2568 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002569 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002570 } else {
2571 delete this.bellSquelchTimeout_;
2572 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002573
2574 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002575 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002576 this.bellNotificationList_.push(n);
2577 // TODO: Should we try to raise the window here?
2578 n.onclick = function() { self.closeBellNotifications_(); };
2579 }
rginda87b86462011-12-14 13:48:03 -08002580};
2581
2582/**
rginda8ba33642011-12-14 12:31:31 -08002583 * Set the origin mode bit.
2584 *
2585 * If origin mode is on, certain VT cursor and scrolling commands measure their
2586 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2587 * to the top of the addressable screen.
2588 *
2589 * Defaults to off.
2590 *
2591 * @param {boolean} state True to set origin mode, false to unset.
2592 */
2593hterm.Terminal.prototype.setOriginMode = function(state) {
2594 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002595 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002596};
2597
2598/**
2599 * Set the insert mode bit.
2600 *
2601 * If insert mode is on, existing text beyond the cursor position will be
2602 * shifted right to make room for new text. Otherwise, new text overwrites
2603 * any existing text.
2604 *
2605 * Defaults to off.
2606 *
2607 * @param {boolean} state True to set insert mode, false to unset.
2608 */
2609hterm.Terminal.prototype.setInsertMode = function(state) {
2610 this.options_.insertMode = state;
2611};
2612
2613/**
rginda87b86462011-12-14 13:48:03 -08002614 * Set the auto carriage return bit.
2615 *
2616 * If auto carriage return is on then a formfeed character is interpreted
2617 * as a newline, otherwise it's the same as a linefeed. The difference boils
2618 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002619 *
2620 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002621 */
2622hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2623 this.options_.autoCarriageReturn = state;
2624};
2625
2626/**
rginda8ba33642011-12-14 12:31:31 -08002627 * Set the wraparound mode bit.
2628 *
2629 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2630 * to the start of the following row. Otherwise, the cursor is clamped to the
2631 * end of the screen and attempts to write past it are ignored.
2632 *
2633 * Defaults to on.
2634 *
2635 * @param {boolean} state True to set wraparound mode, false to unset.
2636 */
2637hterm.Terminal.prototype.setWraparound = function(state) {
2638 this.options_.wraparound = state;
2639};
2640
2641/**
2642 * Set the reverse-wraparound mode bit.
2643 *
2644 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2645 * to the end of the previous row. Otherwise, the cursor is clamped to column
2646 * 0.
2647 *
2648 * Defaults to off.
2649 *
2650 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2651 */
2652hterm.Terminal.prototype.setReverseWraparound = function(state) {
2653 this.options_.reverseWraparound = state;
2654};
2655
2656/**
2657 * Selects between the primary and alternate screens.
2658 *
2659 * If alternate mode is on, the alternate screen is active. Otherwise the
2660 * primary screen is active.
2661 *
2662 * Swapping screens has no effect on the scrollback buffer.
2663 *
2664 * Each screen maintains its own cursor position.
2665 *
2666 * Defaults to off.
2667 *
2668 * @param {boolean} state True to set alternate mode, false to unset.
2669 */
2670hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002671 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002672 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2673
rginda35c456b2012-02-09 17:29:05 -08002674 if (this.screen_.rowsArray.length &&
2675 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2676 // If the screen changed sizes while we were away, our rowIndexes may
2677 // be incorrect.
2678 var offset = this.scrollbackRows_.length;
2679 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002680 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002681 ary[i].rowIndex = offset + i;
2682 }
2683 }
rginda8ba33642011-12-14 12:31:31 -08002684
rginda35c456b2012-02-09 17:29:05 -08002685 this.realizeWidth_(this.screenSize.width);
2686 this.realizeHeight_(this.screenSize.height);
2687 this.scrollPort_.syncScrollHeight();
2688 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002689
rginda6d397402012-01-17 10:58:29 -08002690 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002691 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002692};
2693
2694/**
2695 * Set the cursor-blink mode bit.
2696 *
2697 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2698 * a visible cursor does not blink.
2699 *
2700 * You should make sure to turn blinking off if you're going to dispose of a
2701 * terminal, otherwise you'll leak a timeout.
2702 *
2703 * Defaults to on.
2704 *
2705 * @param {boolean} state True to set cursor-blink mode, false to unset.
2706 */
2707hterm.Terminal.prototype.setCursorBlink = function(state) {
2708 this.options_.cursorBlink = state;
2709
2710 if (!state && this.timeouts_.cursorBlink) {
2711 clearTimeout(this.timeouts_.cursorBlink);
2712 delete this.timeouts_.cursorBlink;
2713 }
2714
2715 if (this.options_.cursorVisible)
2716 this.setCursorVisible(true);
2717};
2718
2719/**
2720 * Set the cursor-visible mode bit.
2721 *
2722 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2723 *
2724 * Defaults to on.
2725 *
2726 * @param {boolean} state True to set cursor-visible mode, false to unset.
2727 */
2728hterm.Terminal.prototype.setCursorVisible = function(state) {
2729 this.options_.cursorVisible = state;
2730
2731 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002732 if (this.timeouts_.cursorBlink) {
2733 clearTimeout(this.timeouts_.cursorBlink);
2734 delete this.timeouts_.cursorBlink;
2735 }
rginda87b86462011-12-14 13:48:03 -08002736 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002737 return;
2738 }
2739
rginda87b86462011-12-14 13:48:03 -08002740 this.syncCursorPosition_();
2741
2742 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002743
2744 if (this.options_.cursorBlink) {
2745 if (this.timeouts_.cursorBlink)
2746 return;
2747
Robert Gindaea2183e2014-07-17 09:51:51 -07002748 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002749 } else {
2750 if (this.timeouts_.cursorBlink) {
2751 clearTimeout(this.timeouts_.cursorBlink);
2752 delete this.timeouts_.cursorBlink;
2753 }
2754 }
2755};
2756
2757/**
rginda87b86462011-12-14 13:48:03 -08002758 * Synchronizes the visible cursor and document selection with the current
2759 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002760 */
2761hterm.Terminal.prototype.syncCursorPosition_ = function() {
2762 var topRowIndex = this.scrollPort_.getTopRowIndex();
2763 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2764 var cursorRowIndex = this.scrollbackRows_.length +
2765 this.screen_.cursorPosition.row;
2766
2767 if (cursorRowIndex > bottomRowIndex) {
2768 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04002769 this.setCssVar('cursor-offset-row', '-1');
rginda8ba33642011-12-14 12:31:31 -08002770 return;
2771 }
2772
Robert Gindab837c052014-08-11 11:17:51 -07002773 if (this.options_.cursorVisible &&
2774 this.cursorNode_.style.display == 'none') {
2775 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2776 this.cursorNode_.style.display = '';
2777 }
2778
Mike Frysinger44c32202017-08-05 01:13:09 -04002779 // Position the cursor using CSS variable math. If we do the math in JS,
2780 // the float math will end up being more precise than the CSS which will
2781 // cause the cursor tracking to be off.
2782 this.setCssVar(
2783 'cursor-offset-row',
2784 `${cursorRowIndex - topRowIndex} + ` +
2785 `${this.scrollPort_.visibleRowTopMargin}px`);
2786 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08002787
2788 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04002789 '(' + this.screen_.cursorPosition.column +
2790 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08002791 ')');
2792
2793 // Update the caret for a11y purposes.
2794 var selection = this.document_.getSelection();
2795 if (selection && selection.isCollapsed)
2796 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002797};
2798
Robert Gindafb1be6a2013-12-11 11:56:22 -08002799/**
2800 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2801 * and character cell dimensions.
2802 */
Robert Ginda830583c2013-08-07 13:20:46 -07002803hterm.Terminal.prototype.restyleCursor_ = function() {
2804 var shape = this.cursorShape_;
2805
2806 if (this.cursorNode_.getAttribute('focus') == 'false') {
2807 // Always show a block cursor when unfocused.
2808 shape = hterm.Terminal.cursorShape.BLOCK;
2809 }
2810
2811 var style = this.cursorNode_.style;
2812
2813 switch (shape) {
2814 case hterm.Terminal.cursorShape.BEAM:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002815 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002816 style.backgroundColor = 'transparent';
2817 style.borderBottomStyle = null;
2818 style.borderLeftStyle = 'solid';
2819 break;
2820
2821 case hterm.Terminal.cursorShape.UNDERLINE:
2822 style.height = this.scrollPort_.characterSize.baseline + 'px';
2823 style.backgroundColor = 'transparent';
2824 style.borderBottomStyle = 'solid';
2825 // correct the size to put it exactly at the baseline
2826 style.borderLeftStyle = null;
2827 break;
2828
2829 default:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002830 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002831 style.backgroundColor = this.cursorColor_;
2832 style.borderBottomStyle = null;
2833 style.borderLeftStyle = null;
2834 break;
2835 }
2836};
2837
rginda8ba33642011-12-14 12:31:31 -08002838/**
2839 * Synchronizes the visible cursor with the current cursor coordinates.
2840 *
2841 * The sync will happen asynchronously, soon after the call stack winds down.
2842 * Multiple calls will be coalesced into a single sync.
2843 */
2844hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2845 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002846 return;
rginda8ba33642011-12-14 12:31:31 -08002847
2848 var self = this;
2849 this.timeouts_.syncCursor = setTimeout(function() {
2850 self.syncCursorPosition_();
2851 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002852 }, 0);
2853};
2854
rgindacc2996c2012-02-24 14:59:31 -08002855/**
rgindaf522ce02012-04-17 17:49:17 -07002856 * Show or hide the zoom warning.
2857 *
2858 * The zoom warning is a message warning the user that their browser zoom must
2859 * be set to 100% in order for hterm to function properly.
2860 *
2861 * @param {boolean} state True to show the message, false to hide it.
2862 */
2863hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2864 if (!this.zoomWarningNode_) {
2865 if (!state)
2866 return;
2867
2868 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002869 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07002870 this.zoomWarningNode_.style.cssText = (
2871 'color: black;' +
2872 'background-color: #ff2222;' +
2873 'font-size: large;' +
2874 'border-radius: 8px;' +
2875 'opacity: 0.75;' +
2876 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2877 'top: 0.5em;' +
2878 'right: 1.2em;' +
2879 'position: absolute;' +
2880 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002881 '-webkit-user-select: none;' +
2882 '-moz-text-size-adjust: none;' +
2883 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002884
2885 this.zoomWarningNode_.addEventListener('click', function(e) {
2886 this.parentNode.removeChild(this);
2887 });
rgindaf522ce02012-04-17 17:49:17 -07002888 }
2889
Robert Gindab4839c22013-02-28 16:52:10 -08002890 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2891 hterm.zoomWarningMessage,
2892 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2893
rgindaf522ce02012-04-17 17:49:17 -07002894 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2895
2896 if (state) {
2897 if (!this.zoomWarningNode_.parentNode)
2898 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2899 } else if (this.zoomWarningNode_.parentNode) {
2900 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2901 }
2902};
2903
2904/**
rgindacc2996c2012-02-24 14:59:31 -08002905 * Show the terminal overlay for a given amount of time.
2906 *
2907 * The terminal overlay appears in inverse video in a large font, centered
2908 * over the terminal. You should probably keep the overlay message brief,
2909 * since it's in a large font and you probably aren't going to check the size
2910 * of the terminal first.
2911 *
2912 * @param {string} msg The text (not HTML) message to display in the overlay.
2913 * @param {number} opt_timeout The amount of time to wait before fading out
2914 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2915 * stay up forever (or until the next overlay).
2916 */
2917hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002918 if (!this.overlayNode_) {
2919 if (!this.div_)
2920 return;
2921
2922 this.overlayNode_ = this.document_.createElement('div');
2923 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002924 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002925 'font-size: xx-large;' +
2926 'opacity: 0.75;' +
2927 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2928 'position: absolute;' +
2929 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002930 '-webkit-transition: opacity 180ms ease-in;' +
2931 '-moz-user-select: none;' +
2932 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002933
2934 this.overlayNode_.addEventListener('mousedown', function(e) {
2935 e.preventDefault();
2936 e.stopPropagation();
2937 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002938 }
2939
rginda9f5222b2012-03-05 11:53:28 -08002940 this.overlayNode_.style.color = this.prefs_.get('background-color');
2941 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2942 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2943
rgindaf0090c92012-02-10 14:58:52 -08002944 this.overlayNode_.textContent = msg;
2945 this.overlayNode_.style.opacity = '0.75';
2946
2947 if (!this.overlayNode_.parentNode)
2948 this.div_.appendChild(this.overlayNode_);
2949
Robert Ginda97769282013-02-01 15:30:30 -08002950 var divSize = hterm.getClientSize(this.div_);
2951 var overlaySize = hterm.getClientSize(this.overlayNode_);
2952
Robert Ginda8a59f762014-07-23 11:29:55 -07002953 this.overlayNode_.style.top =
2954 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08002955 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07002956 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08002957
rgindaf0090c92012-02-10 14:58:52 -08002958 if (this.overlayTimeout_)
2959 clearTimeout(this.overlayTimeout_);
2960
rgindacc2996c2012-02-24 14:59:31 -08002961 if (opt_timeout === null)
2962 return;
2963
Mike Frysingerb6cfded2017-09-18 00:39:31 -04002964 this.overlayTimeout_ = setTimeout(() => {
2965 this.overlayNode_.style.opacity = '0';
2966 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
2967 }, opt_timeout || 1500);
2968};
2969
2970/**
2971 * Hide the terminal overlay immediately.
2972 *
2973 * Useful when we show an overlay for an event with an unknown end time.
2974 */
2975hterm.Terminal.prototype.hideOverlay = function() {
2976 if (this.overlayTimeout_)
2977 clearTimeout(this.overlayTimeout_);
2978 this.overlayTimeout_ = null;
2979
2980 if (this.overlayNode_.parentNode)
2981 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
2982 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08002983};
2984
rginda4bba5e12012-06-20 16:15:30 -07002985/**
2986 * Paste from the system clipboard to the terminal.
2987 */
2988hterm.Terminal.prototype.paste = function() {
Mike Frysinger4628ad22017-07-20 02:44:20 -04002989 return hterm.pasteFromClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002990};
2991
2992/**
2993 * Copy a string to the system clipboard.
2994 *
2995 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05002996 *
2997 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07002998 */
2999hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003000 if (this.prefs_.get('enable-clipboard-notice'))
3001 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
3002
rgindaa09e7332012-08-17 12:49:51 -07003003 var copySource = this.document_.createElement('pre');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04003004 copySource.id = 'hterm:copy-to-clipboard-source';
rginda4bba5e12012-06-20 16:15:30 -07003005 copySource.textContent = str;
3006 copySource.style.cssText = (
3007 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003008 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07003009 'position: absolute;' +
3010 'top: -99px');
3011
3012 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07003013
rginda4bba5e12012-06-20 16:15:30 -07003014 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07003015 var anchorNode = selection.anchorNode;
3016 var anchorOffset = selection.anchorOffset;
3017 var focusNode = selection.focusNode;
3018 var focusOffset = selection.focusOffset;
3019
rginda4bba5e12012-06-20 16:15:30 -07003020 selection.selectAllChildren(copySource);
3021
rgindaa09e7332012-08-17 12:49:51 -07003022 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07003023
Rob Spies56953412014-04-28 14:09:47 -07003024 // IE doesn't support selection.extend. This means that the selection
3025 // won't return on IE.
Robert Ginda6f4f0aa2014-07-28 10:21:39 -07003026 if (selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07003027 selection.collapse(anchorNode, anchorOffset);
3028 selection.extend(focusNode, focusOffset);
3029 }
rgindafaa74742012-08-21 13:34:03 -07003030
rginda4bba5e12012-06-20 16:15:30 -07003031 copySource.parentNode.removeChild(copySource);
3032};
3033
Evan Jones2600d4f2016-12-06 09:29:36 -05003034/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003035 * Display an image.
3036 *
3037 * @param {Object} options The image to display.
3038 * @param {string=} options.name A human readable string for the image.
3039 * @param {string|number=} options.size The size (in bytes).
3040 * @param {boolean=} options.preserveAspectRatio Whether to preserve aspect.
3041 * @param {boolean=} options.inline Whether to display the image inline.
3042 * @param {string|number=} options.width The width of the image.
3043 * @param {string|number=} options.height The height of the image.
3044 * @param {string=} options.align Direction to align the image.
3045 * @param {string} options.uri The source URI for the image.
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003046 * @param {function=} onLoad Callback when loading finishes.
3047 * @param {function(Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003048 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003049hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003050 // Make sure we're actually given a resource to display.
3051 if (options.uri === undefined)
3052 return;
3053
3054 // Set up the defaults to simplify code below.
3055 if (!options.name)
3056 options.name = '';
3057
3058 // Has the user approved image display yet?
3059 if (this.allowImagesInline !== true) {
3060 this.newLine();
3061 const row = this.getRowNode(this.scrollbackRows_.length +
3062 this.getCursorRow() - 1);
3063
3064 if (this.allowImagesInline === false) {
3065 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3066 'Inline Images Disabled');
3067 return;
3068 }
3069
3070 // Show a prompt.
3071 let button;
3072 const span = this.document_.createElement('span');
3073 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3074 span.style.fontWeight = 'bold';
3075 span.style.borderWidth = '1px';
3076 span.style.borderStyle = 'dashed';
3077 button = this.document_.createElement('span');
3078 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3079 button.style.marginLeft = '1em';
3080 button.style.borderWidth = '1px';
3081 button.style.borderStyle = 'solid';
3082 button.addEventListener('click', () => {
3083 this.prefs_.set('allow-images-inline', false);
3084 });
3085 span.appendChild(button);
3086 button = this.document_.createElement('span');
3087 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3088 'allow this session');
3089 button.style.marginLeft = '1em';
3090 button.style.borderWidth = '1px';
3091 button.style.borderStyle = 'solid';
3092 button.addEventListener('click', () => {
3093 this.allowImagesInline = true;
3094 });
3095 span.appendChild(button);
3096 button = this.document_.createElement('span');
3097 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3098 button.style.marginLeft = '1em';
3099 button.style.borderWidth = '1px';
3100 button.style.borderStyle = 'solid';
3101 button.addEventListener('click', () => {
3102 this.prefs_.set('allow-images-inline', true);
3103 });
3104 span.appendChild(button);
3105
3106 row.appendChild(span);
3107 return;
3108 }
3109
3110 // See if we should show this object directly, or download it.
3111 if (options.inline) {
3112 const io = this.io.push();
3113 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
3114 'Loading $1 ...'), null);
3115
3116 // While we're loading the image, eat all the user's input.
3117 io.onVTKeystroke = io.sendString = () => {};
3118
3119 // Initialize this new image.
3120 const img = this.document_.createElement('img');
3121 img.src = options.uri;
3122 img.title = img.alt = options.name;
3123
3124 // Attach the image to the page to let it load/render. It won't stay here.
3125 // This is needed so it's visible and the DOM can calculate the height. If
3126 // the image is hidden or not in the DOM, the height is always 0.
3127 this.document_.body.appendChild(img);
3128
3129 // Wait for the image to finish loading before we try moving it to the
3130 // right place in the terminal.
3131 img.onload = () => {
3132 // Now that we have the image dimensions, figure out how to show it.
3133 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
3134 img.style.maxWidth = `${this.document_.body.clientWidth}px`;
3135 img.style.maxHeight = `${this.document_.body.clientHeight}px`;
3136
3137 // Parse a width/height specification.
3138 const parseDim = (dim, maxDim, cssVar) => {
3139 if (!dim || dim == 'auto')
3140 return '';
3141
3142 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3143 if (ary) {
3144 if (ary[2] == '%')
3145 return maxDim * parseInt(ary[1]) / 100 + 'px';
3146 else if (ary[2] == 'px')
3147 return dim;
3148 else
3149 return `calc(${dim} * var(${cssVar}))`;
3150 }
3151
3152 return '';
3153 };
3154 img.style.width =
3155 parseDim(options.width, this.document_.body.clientWidth,
3156 '--hterm-charsize-width');
3157 img.style.height =
3158 parseDim(options.height, this.document_.body.clientHeight,
3159 '--hterm-charsize-height');
3160
3161 // Figure out how many rows the image occupies, then add that many.
3162 // XXX: This count will be inaccurate if the font size changes on us.
3163 const padRows = Math.ceil(img.clientHeight /
3164 this.scrollPort_.characterSize.height);
3165 for (let i = 0; i < padRows; ++i)
3166 this.newLine();
3167
3168 // Update the max height in case the user shrinks the character size.
3169 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3170
3171 // Move the image to the last row. This way when we scroll up, it doesn't
3172 // disappear when the first row gets clipped. It will disappear when we
3173 // scroll down and the last row is clipped ...
3174 this.document_.body.removeChild(img);
3175 // Create a wrapper node so we can do an absolute in a relative position.
3176 // This helps with rounding errors between JS & CSS counts.
3177 const div = this.document_.createElement('div');
3178 div.style.position = 'relative';
3179 div.style.textAlign = options.align;
3180 img.style.position = 'absolute';
3181 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3182 div.appendChild(img);
3183 const row = this.getRowNode(this.scrollbackRows_.length +
3184 this.getCursorRow() - 1);
3185 row.appendChild(div);
3186
3187 io.hideOverlay();
3188 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003189
3190 if (onLoad)
3191 onLoad();
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003192 };
3193
3194 // If we got a malformed image, give up.
3195 img.onerror = (e) => {
3196 this.document_.body.removeChild(img);
3197 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
Mike Frysingere14a8c42018-03-10 00:17:30 -08003198 'Loading $1 failed'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003199 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003200
3201 if (onError)
3202 onError(e);
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003203 };
3204 } else {
3205 // We can't use chrome.downloads.download as that requires "downloads"
3206 // permissions, and that works only in extensions, not apps.
3207 const a = this.document_.createElement('a');
3208 a.href = options.uri;
3209 a.download = options.name;
3210 this.document_.body.appendChild(a);
3211 a.click();
3212 a.remove();
3213 }
3214};
3215
3216/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003217 * Returns the selected text, or null if no text is selected.
3218 *
3219 * @return {string|null}
3220 */
rgindaa09e7332012-08-17 12:49:51 -07003221hterm.Terminal.prototype.getSelectionText = function() {
3222 var selection = this.scrollPort_.selection;
3223 selection.sync();
3224
3225 if (selection.isCollapsed)
3226 return null;
3227
3228
3229 // Start offset measures from the beginning of the line.
3230 var startOffset = selection.startOffset;
3231 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003232
Robert Gindafdbb3f22012-09-06 20:23:06 -07003233 if (node.nodeName != 'X-ROW') {
3234 // If the selection doesn't start on an x-row node, then it must be
3235 // somewhere inside the x-row. Add any characters from previous siblings
3236 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003237
3238 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3239 // If node is the text node in a styled span, move up to the span node.
3240 node = node.parentNode;
3241 }
3242
Robert Gindafdbb3f22012-09-06 20:23:06 -07003243 while (node.previousSibling) {
3244 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003245 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003246 }
rgindaa09e7332012-08-17 12:49:51 -07003247 }
3248
3249 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08003250 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
3251 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003252 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003253
Robert Gindafdbb3f22012-09-06 20:23:06 -07003254 if (node.nodeName != 'X-ROW') {
3255 // If the selection doesn't end on an x-row node, then it must be
3256 // somewhere inside the x-row. Add any characters from following siblings
3257 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003258
3259 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3260 // If node is the text node in a styled span, move up to the span node.
3261 node = node.parentNode;
3262 }
3263
Robert Gindafdbb3f22012-09-06 20:23:06 -07003264 while (node.nextSibling) {
3265 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003266 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003267 }
rgindaa09e7332012-08-17 12:49:51 -07003268 }
3269
3270 var rv = this.getRowsText(selection.startRow.rowIndex,
3271 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003272 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003273};
3274
rginda4bba5e12012-06-20 16:15:30 -07003275/**
3276 * Copy the current selection to the system clipboard, then clear it after a
3277 * short delay.
3278 */
3279hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07003280 var text = this.getSelectionText();
3281 if (text != null)
3282 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07003283};
3284
rgindaf0090c92012-02-10 14:58:52 -08003285hterm.Terminal.prototype.overlaySize = function() {
3286 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
3287};
3288
rginda87b86462011-12-14 13:48:03 -08003289/**
3290 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3291 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003292 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003293 */
3294hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08003295 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08003296 this.scrollPort_.scrollRowToBottom(this.getRowCount());
3297
Robert Ginda8cb7d902013-06-20 14:37:18 -07003298 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08003299};
3300
3301/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003302 * Open the selected url.
3303 */
3304hterm.Terminal.prototype.openSelectedUrl_ = function() {
3305 var str = this.getSelectionText();
3306
3307 // If there is no selection, try and expand wherever they clicked.
3308 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003309 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003310 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003311
3312 // If clicking in empty space, return.
3313 if (str == null)
3314 return;
Mike Frysinger70b94692017-01-26 18:57:50 -10003315 }
3316
3317 // Make sure URL is valid before opening.
3318 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
3319 return;
Mike Frysinger43472622017-06-26 18:11:07 -04003320
3321 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003322 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003323 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3324 // We have to whitelist a few protocols that lack authorities and thus
3325 // never use the //. Like mailto.
3326 switch (str.split(':', 1)[0]) {
3327 case 'mailto':
3328 break;
3329 default:
3330 str = 'http://' + str;
3331 break;
3332 }
3333 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003334
Mike Frysinger720fa832017-10-23 01:15:52 -04003335 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003336};
Mike Frysinger70b94692017-01-26 18:57:50 -10003337
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003338/**
3339 * Manage the automatic mouse hiding behavior while typing.
3340 *
3341 * @param {boolean=} v Whether to enable automatic hiding.
3342 */
3343hterm.Terminal.prototype.setAutomaticMouseHiding = function(v=null) {
3344 // Since Chrome OS & macOS do this by default everywhere, we don't need to.
3345 // Linux & Windows seem to leave this to specific applications to manage.
3346 if (v === null)
3347 v = (hterm.os != 'cros' && hterm.os != 'mac');
3348
3349 this.mouseHideWhileTyping_ = !!v;
3350};
3351
3352/**
3353 * Handler for monitoring user keyboard activity.
3354 *
3355 * This isn't for processing the keystrokes directly, but for updating any
3356 * state that might toggle based on the user using the keyboard at all.
3357 *
3358 * @param {KeyboardEvent} e The keyboard event that triggered us.
3359 */
3360hterm.Terminal.prototype.onKeyboardActivity_ = function(e) {
3361 // When the user starts typing, hide the mouse cursor.
3362 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_)
3363 this.setCssVar('mouse-cursor-style', 'none');
3364};
Mike Frysinger70b94692017-01-26 18:57:50 -10003365
3366/**
rgindad5613292012-06-19 15:40:37 -07003367 * Add the terminalRow and terminalColumn properties to mouse events and
3368 * then forward on to onMouse().
3369 *
3370 * The terminalRow and terminalColumn properties contain the (row, column)
3371 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003372 *
3373 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003374 */
3375hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003376 if (e.processedByTerminalHandler_) {
3377 // We register our event handlers on the document, as well as the cursor
3378 // and the scroll blocker. Mouse events that occur on the cursor or
3379 // scroll blocker will also appear on the document, but we don't want to
3380 // process them twice.
3381 //
3382 // We can't just prevent bubbling because that has other side effects, so
3383 // we decorate the event object with this property instead.
3384 return;
3385 }
3386
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003387 var reportMouseEvents = (!this.defeatMouseReports_ &&
3388 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3389
rgindafaa74742012-08-21 13:34:03 -07003390 e.processedByTerminalHandler_ = true;
3391
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003392 // Handle auto hiding of mouse cursor while typing.
3393 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
3394 // Make sure the mouse cursor is visible.
3395 this.syncMouseStyle();
3396 // This debounce isn't perfect, but should work well enough for such a
3397 // simple implementation. If the user moved the mouse, we enabled this
3398 // debounce, and then moved the mouse just before the timeout, we wouldn't
3399 // debounce that later movement.
3400 this.mouseHideDelay_ = setTimeout(() => this.mouseHideDelay_ = null, 1000);
3401 }
3402
Robert Gindaeda48db2014-07-17 09:25:30 -07003403 // One based row/column stored on the mouse event.
3404 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
3405 this.scrollPort_.characterSize.height) + 1;
3406 e.terminalColumn = parseInt(e.clientX /
3407 this.scrollPort_.characterSize.width) + 1;
3408
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003409 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3410 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003411 return;
3412 }
3413
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003414 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003415 // If the cursor is visible and we're not sending mouse events to the
3416 // host app, then we want to hide the terminal cursor when the mouse
3417 // cursor is over top. This keeps the terminal cursor from interfering
3418 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003419 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3420 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3421 this.cursorNode_.style.display = 'none';
3422 } else if (this.cursorNode_.style.display == 'none') {
3423 this.cursorNode_.style.display = '';
3424 }
3425 }
rgindad5613292012-06-19 15:40:37 -07003426
Robert Ginda928cf632014-03-05 15:07:41 -08003427 if (e.type == 'mousedown') {
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003428 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003429 // If VT mouse reporting is disabled, or has been defeated with
3430 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003431 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003432 this.setSelectionEnabled(true);
3433 } else {
3434 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003435 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003436 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003437 this.setSelectionEnabled(false);
3438 e.preventDefault();
3439 }
3440 }
3441
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003442 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003443 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003444 this.screen_.expandSelection(this.document_.getSelection());
John Lin2aad22e2018-03-16 13:58:11 +08003445 if (this.copyOnSelect)
3446 this.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07003447 }
3448
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003449 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003450 // Debounce this event with the dblclick event. If you try to doubleclick
3451 // a URL to open it, Chrome will fire click then dblclick, but we won't
3452 // have expanded the selection text at the first click event.
3453 clearTimeout(this.timeouts_.openUrl);
3454 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3455 500);
3456 return;
3457 }
3458
Mike Frysinger847577f2017-05-23 23:25:57 -04003459 if (e.type == 'mousedown') {
3460 if ((this.mouseRightClickPaste && e.button == 2 /* right button */) ||
Mike Frysinger2edd3612017-05-24 00:54:39 -04003461 e.button == this.mousePasteButton) {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003462 if (!this.paste())
Mike Frysinger05a57f02017-08-27 17:48:55 -04003463 console.warn('Could not paste manually due to web restrictions');
Mike Frysinger847577f2017-05-23 23:25:57 -04003464 }
3465 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003466
Mike Frysinger2edd3612017-05-24 00:54:39 -04003467 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003468 !this.document_.getSelection().isCollapsed) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003469 this.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003470 }
3471
3472 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3473 this.scrollBlockerNode_.engaged) {
3474 // Disengage the scroll-blocker after one of these events.
3475 this.scrollBlockerNode_.engaged = false;
3476 this.scrollBlockerNode_.style.top = '-99px';
3477 }
3478
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003479 // Emulate arrow key presses via scroll wheel events.
3480 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3481 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003482 if (e.type == 'wheel') {
3483 var delta = this.scrollPort_.scrollWheelDelta(e);
3484 var lines = lib.f.smartFloorDivide(
3485 Math.abs(delta), this.scrollPort_.characterSize.height);
3486
3487 var data = '\x1bO' + (delta < 0 ? 'B' : 'A');
3488 this.io.sendString(data.repeat(lines));
3489
3490 e.preventDefault();
3491 }
3492 }
Robert Ginda928cf632014-03-05 15:07:41 -08003493 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003494 if (!this.scrollBlockerNode_.engaged) {
3495 if (e.type == 'mousedown') {
3496 // Move the scroll-blocker into place if we want to keep the scrollport
3497 // from scrolling.
3498 this.scrollBlockerNode_.engaged = true;
3499 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3500 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3501 } else if (e.type == 'mousemove') {
3502 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3503 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003504 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003505 e.preventDefault();
3506 }
3507 }
Robert Ginda928cf632014-03-05 15:07:41 -08003508
3509 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003510 }
3511
Robert Ginda928cf632014-03-05 15:07:41 -08003512 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3513 // Restore this on mouseup in case it was temporarily defeated with a
3514 // alt-mousedown. Only do this when the selection is empty so that
3515 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003516 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003517 }
rgindad5613292012-06-19 15:40:37 -07003518};
3519
3520/**
3521 * Clients should override this if they care to know about mouse events.
3522 *
3523 * The event parameter will be a normal DOM mouse click event with additional
3524 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003525 *
3526 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003527 */
3528hterm.Terminal.prototype.onMouse = function(e) { };
3529
3530/**
rginda8e92a692012-05-20 19:37:20 -07003531 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003532 *
3533 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003534 */
Rob Spies06533ba2014-04-24 11:20:37 -07003535hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3536 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003537 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003538
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003539 if (this.reportFocus)
3540 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003541
Michael Kelly485ecd12014-06-09 11:41:56 -04003542 if (focused === true)
3543 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003544};
3545
3546/**
rginda8ba33642011-12-14 12:31:31 -08003547 * React when the ScrollPort is scrolled.
3548 */
3549hterm.Terminal.prototype.onScroll_ = function() {
3550 this.scheduleSyncCursorPosition_();
3551};
3552
3553/**
rginda9846e2f2012-01-27 13:53:33 -08003554 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003555 *
3556 * @param {Event} e The DOM paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003557 */
3558hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07003559 var data = e.text.replace(/\n/mg, '\r');
Connor Hegartyf525ceb2014-09-03 13:36:03 -07003560 data = this.keyboard.encode(data);
Mike Frysingere8c32c82018-03-11 14:57:28 -07003561 if (this.options_.bracketedPaste) {
3562 // We strip out most escape sequences as they can cause issues (like
3563 // inserting an \x1b[201~ midstream). We pass through whitespace
3564 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
3565 // This matches xterm behavior.
3566 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
3567 data = '\x1b[200~' + filter(data) + '\x1b[201~';
3568 }
Robert Gindaa063b202014-07-21 11:08:25 -07003569
3570 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003571};
3572
3573/**
rgindaa09e7332012-08-17 12:49:51 -07003574 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003575 *
3576 * @param {Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003577 */
3578hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003579 if (!this.useDefaultWindowCopy) {
3580 e.preventDefault();
3581 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3582 }
rgindaa09e7332012-08-17 12:49:51 -07003583};
3584
3585/**
rginda8ba33642011-12-14 12:31:31 -08003586 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003587 *
3588 * Note: This function should not directly contain code that alters the internal
3589 * state of the terminal. That kind of code belongs in realizeWidth or
3590 * realizeHeight, so that it can be executed synchronously in the case of a
3591 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003592 */
3593hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003594 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003595 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003596 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003597 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003598
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003599 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003600 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003601 // gets removed from the document or during the initial load, and we can't
3602 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003603 // This can also happen if called before the scrollPort calculates the
3604 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003605 return;
3606 }
3607
rgindaa8ba17d2012-08-15 14:41:10 -07003608 var isNewSize = (columnCount != this.screenSize.width ||
3609 rowCount != this.screenSize.height);
3610
3611 // We do this even if the size didn't change, just to be sure everything is
3612 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003613 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003614 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003615
3616 if (isNewSize)
3617 this.overlaySize();
3618
Robert Gindafb1be6a2013-12-11 11:56:22 -08003619 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003620 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08003621};
3622
3623/**
3624 * Service the cursor blink timeout.
3625 */
3626hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003627 if (!this.options_.cursorBlink) {
3628 delete this.timeouts_.cursorBlink;
3629 return;
3630 }
3631
Robert Ginda830583c2013-08-07 13:20:46 -07003632 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3633 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003634 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003635 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3636 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003637 } else {
rginda87b86462011-12-14 13:48:03 -08003638 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003639 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3640 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003641 }
3642};
David Reveman8f552492012-03-28 12:18:41 -04003643
3644/**
3645 * Set the scrollbar-visible mode bit.
3646 *
3647 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3648 * Otherwise it will not.
3649 *
3650 * Defaults to on.
3651 *
3652 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3653 */
3654hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3655 this.scrollPort_.setScrollbarVisible(state);
3656};
Michael Kelly485ecd12014-06-09 11:41:56 -04003657
3658/**
Rob Spies49039e52014-12-17 13:40:04 -08003659 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003660 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003661 *
3662 * Defaults to 1.
3663 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003664 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003665 */
3666hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3667 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3668};
3669
3670/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003671 * Close all web notifications created by terminal bells.
3672 */
3673hterm.Terminal.prototype.closeBellNotifications_ = function() {
3674 this.bellNotificationList_.forEach(function(n) {
3675 n.close();
3676 });
3677 this.bellNotificationList_.length = 0;
3678};