blob: 12fb3edeac86a079adde3a023a514df2c0adad85 [file] [log] [blame]
rginda87b86462011-12-14 13:48:03 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rginda8ba33642011-12-14 12:31:31 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rgindacbbd7482012-06-13 15:06:16 -07005'use strict';
6
Masaya Suzuki273aa982014-05-31 07:25:55 +09007lib.rtdep('lib.colors', 'lib.PreferenceManager', 'lib.resource', 'lib.wc',
Raymes Khoury3e44bc92018-05-17 10:54:23 +10008 'lib.f', 'hterm.AccessibilityReader', 'hterm.Keyboard',
9 'hterm.Options', 'hterm.PreferenceManager', 'hterm.Screen',
10 'hterm.ScrollPort', 'hterm.Size', 'hterm.TextAttributes', 'hterm.VT');
rgindacbbd7482012-06-13 15:06:16 -070011
rginda8ba33642011-12-14 12:31:31 -080012/**
13 * Constructor for the Terminal class.
14 *
15 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
16 * classes to provide the complete terminal functionality.
17 *
18 * There are a number of lower-level Terminal methods that can be called
19 * directly to manipulate the cursor, text, scroll region, and other terminal
20 * attributes. However, the primary method is interpret(), which parses VT
21 * escape sequences and invokes the appropriate Terminal methods.
22 *
23 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
24 *
25 * TODO(rginda): Eventually we're going to need to support characters which are
26 * displayed twice as wide as standard latin characters. This is to support
27 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080028 *
Robert Ginda57f03b42012-09-13 11:02:48 -070029 * @param {string} opt_profileId Optional preference profile name. If not
rginda9f5222b2012-03-05 11:53:28 -080030 * provided, defaults to 'default'.
rginda8ba33642011-12-14 12:31:31 -080031 */
Robert Ginda57f03b42012-09-13 11:02:48 -070032hterm.Terminal = function(opt_profileId) {
33 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080034
rginda8ba33642011-12-14 12:31:31 -080035 // Two screen instances.
36 this.primaryScreen_ = new hterm.Screen();
37 this.alternateScreen_ = new hterm.Screen();
38
39 // The "current" screen.
40 this.screen_ = this.primaryScreen_;
41
rginda8ba33642011-12-14 12:31:31 -080042 // The local notion of the screen size. ScreenBuffers also have a size which
43 // indicates their present size. During size changes, the two may disagree.
44 // Also, the inactive screen's size is not altered until it is made the active
45 // screen.
46 this.screenSize = new hterm.Size(0, 0);
47
rginda8ba33642011-12-14 12:31:31 -080048 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080049 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080050 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
51 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080052 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070053 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080054
rginda87b86462011-12-14 13:48:03 -080055 // The div that contains this terminal.
56 this.div_ = null;
57
rgindac9bc5502012-01-18 11:48:44 -080058 // The document that contains the scrollPort. Defaulted to the global
59 // document here so that the terminal is functional even if it hasn't been
60 // inserted into a document yet, but re-set in decorate().
61 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080062
rginda8ba33642011-12-14 12:31:31 -080063 // The rows that have scrolled off screen and are no longer addressable.
64 this.scrollbackRows_ = [];
65
rgindac9bc5502012-01-18 11:48:44 -080066 // Saved tab stops.
67 this.tabStops_ = [];
68
David Benjamin66e954d2012-05-05 21:08:12 -040069 // Keep track of whether default tab stops have been erased; after a TBC
70 // clears all tab stops, defaults aren't restored on resize until a reset.
71 this.defaultTabStops = true;
72
rginda8ba33642011-12-14 12:31:31 -080073 // The VT's notion of the top and bottom rows. Used during some VT
74 // cursor positioning and scrolling commands.
75 this.vtScrollTop_ = null;
76 this.vtScrollBottom_ = null;
77
78 // The DIV element for the visible cursor.
79 this.cursorNode_ = null;
80
Robert Ginda830583c2013-08-07 13:20:46 -070081 // The current cursor shape of the terminal.
82 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
83
84 // The current color of the cursor.
85 this.cursorColor_ = null;
86
Robert Gindaea2183e2014-07-17 09:51:51 -070087 // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded.
88 this.cursorBlinkCycle_ = [100, 100];
89
90 // Pre-bound onCursorBlink_ handler, so we don't have to do this for each
91 // cursor on/off servicing.
92 this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
93
rginda9f5222b2012-03-05 11:53:28 -080094 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070095 // each output and keystroke. They are initialized by the preference manager.
Robert Ginda8cb7d902013-06-20 14:37:18 -070096 this.backgroundColor_ = null;
97 this.foregroundColor_ = null;
Robert Ginda57f03b42012-09-13 11:02:48 -070098 this.scrollOnOutput_ = null;
99 this.scrollOnKeystroke_ = null;
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400100 this.scrollWheelArrowKeys_ = null;
rginda9f5222b2012-03-05 11:53:28 -0800101
Robert Ginda6aec7eb2015-06-16 10:31:30 -0700102 // True if we should override mouse event reporting to allow local selection.
103 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -0800104
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400105 // Whether to auto hide the mouse cursor when typing.
106 this.setAutomaticMouseHiding();
107 // Timer to keep mouse visible while it's being used.
108 this.mouseHideDelay_ = null;
109
rgindaf0090c92012-02-10 14:58:52 -0800110 // Terminal bell sound.
111 this.bellAudio_ = this.document_.createElement('audio');
Mike Frysingerd826f1a2017-07-06 16:20:06 -0400112 this.bellAudio_.id = 'hterm:bell-audio';
rgindaf0090c92012-02-10 14:58:52 -0800113 this.bellAudio_.setAttribute('preload', 'auto');
114
Raymes Khoury3e44bc92018-05-17 10:54:23 +1000115 // The AccessibilityReader object for announcing command output.
116 this.accessibilityReader_ = null;
117
Michael Kelly485ecd12014-06-09 11:41:56 -0400118 // All terminal bell notifications that have been generated (not necessarily
119 // shown).
120 this.bellNotificationList_ = [];
121
122 // Whether we have permission to display notifications.
123 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400124
rginda6d397402012-01-17 10:58:29 -0800125 // Cursor position and attributes saved with DECSC.
126 this.savedOptions_ = {};
127
rginda8ba33642011-12-14 12:31:31 -0800128 // The current mode bits for the terminal.
129 this.options_ = new hterm.Options();
130
131 // Timeouts we might need to clear.
132 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800133
134 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800135 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800136
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800137 this.saveCursorAndState(true);
138
Zhu Qunying30d40712017-03-14 16:27:00 -0700139 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800140 this.keyboard = new hterm.Keyboard(this);
141
rginda87b86462011-12-14 13:48:03 -0800142 // General IO interface that can be given to third parties without exposing
143 // the entire terminal object.
144 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800145
rgindad5613292012-06-19 15:40:37 -0700146 // True if mouse-click-drag should scroll the terminal.
147 this.enableMouseDragScroll = true;
148
Robert Ginda57f03b42012-09-13 11:02:48 -0700149 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400150 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700151 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700152
Zhu Qunying30d40712017-03-14 16:27:00 -0700153 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700154 this.useDefaultWindowCopy = false;
155
156 this.clearSelectionAfterCopy = true;
157
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400158 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800159 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700160
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400161 // Whether we allow images to be shown.
162 this.allowImagesInline = null;
163
Gabriel Holodake8a09be2017-10-10 01:07:11 -0400164 this.reportFocus = false;
165
Robert Ginda57f03b42012-09-13 11:02:48 -0700166 this.setProfile(opt_profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500167 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800168};
169
170/**
Robert Ginda830583c2013-08-07 13:20:46 -0700171 * Possible cursor shapes.
172 */
173hterm.Terminal.cursorShape = {
174 BLOCK: 'BLOCK',
175 BEAM: 'BEAM',
176 UNDERLINE: 'UNDERLINE'
177};
178
179/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700180 * Clients should override this to be notified when the terminal is ready
181 * for use.
182 *
183 * The terminal initialization is asynchronous, and shouldn't be used before
184 * this method is called.
185 */
186hterm.Terminal.prototype.onTerminalReady = function() { };
187
188/**
rginda35c456b2012-02-09 17:29:05 -0800189 * Default tab with of 8 to match xterm.
190 */
191hterm.Terminal.prototype.tabWidth = 8;
192
193/**
rginda9f5222b2012-03-05 11:53:28 -0800194 * Select a preference profile.
195 *
196 * This will load the terminal preferences for the given profile name and
197 * associate subsequent preference changes with the new preference profile.
198 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500199 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800200 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700201 * @param {function} opt_callback Optional callback to invoke when the profile
202 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800203 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700204hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
205 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800206
Robert Ginda57f03b42012-09-13 11:02:48 -0700207 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800208
Robert Ginda57f03b42012-09-13 11:02:48 -0700209 if (this.prefs_)
210 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800211
Robert Ginda57f03b42012-09-13 11:02:48 -0700212 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
213 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800214 'alt-gr-mode': function(v) {
215 if (v == null) {
216 if (navigator.language.toLowerCase() == 'en-us') {
217 v = 'none';
218 } else {
219 v = 'right-alt';
220 }
221 } else if (typeof v == 'string') {
222 v = v.toLowerCase();
223 } else {
224 v = 'none';
225 }
226
227 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v))
228 v = 'none';
229
230 terminal.keyboard.altGrMode = v;
231 },
232
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700233 'alt-backspace-is-meta-backspace': function(v) {
234 terminal.keyboard.altBackspaceIsMetaBackspace = v;
235 },
236
Robert Ginda57f03b42012-09-13 11:02:48 -0700237 'alt-is-meta': function(v) {
238 terminal.keyboard.altIsMeta = v;
239 },
240
241 'alt-sends-what': function(v) {
242 if (!/^(escape|8-bit|browser-key)$/.test(v))
243 v = 'escape';
244
245 terminal.keyboard.altSendsWhat = v;
246 },
247
248 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800249 var ary = v.match(/^lib-resource:(\S+)/);
250 if (ary) {
251 terminal.bellAudio_.setAttribute('src',
252 lib.resource.getDataUrl(ary[1]));
253 } else {
254 terminal.bellAudio_.setAttribute('src', v);
255 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700256 },
257
Michael Kelly485ecd12014-06-09 11:41:56 -0400258 'desktop-notification-bell': function(v) {
259 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700260 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400261 Notification.permission === 'granted';
262 if (!terminal.desktopNotificationBell_) {
263 // Note: We don't call Notification.requestPermission here because
264 // Chrome requires the call be the result of a user action (such as an
265 // onclick handler), and pref listeners are run asynchronously.
266 //
267 // A way of working around this would be to display a dialog in the
268 // terminal with a "click-to-request-permission" button.
269 console.warn('desktop-notification-bell is true but we do not have ' +
270 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400271 }
272 } else {
273 terminal.desktopNotificationBell_ = false;
274 }
275 },
276
Robert Ginda57f03b42012-09-13 11:02:48 -0700277 'background-color': function(v) {
278 terminal.setBackgroundColor(v);
279 },
280
281 'background-image': function(v) {
282 terminal.scrollPort_.setBackgroundImage(v);
283 },
284
285 'background-size': function(v) {
286 terminal.scrollPort_.setBackgroundSize(v);
287 },
288
289 'background-position': function(v) {
290 terminal.scrollPort_.setBackgroundPosition(v);
291 },
292
293 'backspace-sends-backspace': function(v) {
294 terminal.keyboard.backspaceSendsBackspace = v;
295 },
296
Brad Town18654b62015-03-12 00:27:45 -0700297 'character-map-overrides': function(v) {
298 if (!(v == null || v instanceof Object)) {
299 console.warn('Preference character-map-modifications is not an ' +
300 'object: ' + v);
301 return;
302 }
303
Mike Frysinger095d4062017-06-14 00:29:48 -0700304 terminal.vt.characterMaps.reset();
305 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700306 },
307
Robert Ginda57f03b42012-09-13 11:02:48 -0700308 'cursor-blink': function(v) {
309 terminal.setCursorBlink(!!v);
310 },
311
Robert Gindaea2183e2014-07-17 09:51:51 -0700312 'cursor-blink-cycle': function(v) {
313 if (v instanceof Array &&
314 typeof v[0] == 'number' &&
315 typeof v[1] == 'number') {
316 terminal.cursorBlinkCycle_ = v;
317 } else if (typeof v == 'number') {
318 terminal.cursorBlinkCycle_ = [v, v];
319 } else {
320 // Fast blink indicates an error.
321 terminal.cursorBlinkCycle_ = [100, 100];
322 }
323 },
324
Robert Ginda57f03b42012-09-13 11:02:48 -0700325 'cursor-color': function(v) {
326 terminal.setCursorColor(v);
327 },
328
329 'color-palette-overrides': function(v) {
330 if (!(v == null || v instanceof Object || v instanceof Array)) {
331 console.warn('Preference color-palette-overrides is not an array or ' +
332 'object: ' + v);
333 return;
rginda9f5222b2012-03-05 11:53:28 -0800334 }
rginda9f5222b2012-03-05 11:53:28 -0800335
Robert Ginda57f03b42012-09-13 11:02:48 -0700336 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700337
Robert Ginda57f03b42012-09-13 11:02:48 -0700338 if (v) {
339 for (var key in v) {
340 var i = parseInt(key);
341 if (isNaN(i) || i < 0 || i > 255) {
342 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
343 continue;
344 }
345
346 if (v[i]) {
347 var rgb = lib.colors.normalizeCSS(v[i]);
348 if (rgb)
349 lib.colors.colorPalette[i] = rgb;
350 }
351 }
rginda30f20f62012-04-05 16:36:19 -0700352 }
rginda30f20f62012-04-05 16:36:19 -0700353
Evan Jones5f9df812016-12-06 09:38:58 -0500354 terminal.primaryScreen_.textAttributes.resetColorPalette();
Robert Ginda57f03b42012-09-13 11:02:48 -0700355 terminal.alternateScreen_.textAttributes.resetColorPalette();
356 },
rginda30f20f62012-04-05 16:36:19 -0700357
Robert Ginda57f03b42012-09-13 11:02:48 -0700358 'copy-on-select': function(v) {
359 terminal.copyOnSelect = !!v;
360 },
rginda9f5222b2012-03-05 11:53:28 -0800361
Rob Spies0bec09b2014-06-06 15:58:09 -0700362 'use-default-window-copy': function(v) {
363 terminal.useDefaultWindowCopy = !!v;
364 },
365
366 'clear-selection-after-copy': function(v) {
367 terminal.clearSelectionAfterCopy = !!v;
368 },
369
Robert Ginda7e5e9522014-03-14 12:23:58 -0700370 'ctrl-plus-minus-zero-zoom': function(v) {
371 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
372 },
373
Robert Gindafb5a3f92014-05-13 14:12:00 -0700374 'ctrl-c-copy': function(v) {
375 terminal.keyboard.ctrlCCopy = v;
376 },
377
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100378 'ctrl-v-paste': function(v) {
379 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700380 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100381 },
382
Masaya Suzuki273aa982014-05-31 07:25:55 +0900383 'east-asian-ambiguous-as-two-column': function(v) {
384 lib.wc.regardCjkAmbiguous = v;
385 },
386
Robert Ginda57f03b42012-09-13 11:02:48 -0700387 'enable-8-bit-control': function(v) {
388 terminal.vt.enable8BitControl = !!v;
389 },
rginda30f20f62012-04-05 16:36:19 -0700390
Robert Ginda57f03b42012-09-13 11:02:48 -0700391 'enable-bold': function(v) {
392 terminal.syncBoldSafeState();
393 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400394
Robert Ginda3e278d72014-03-25 13:18:51 -0700395 'enable-bold-as-bright': function(v) {
396 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
397 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
398 },
399
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400400 'enable-blink': function(v) {
Mike Frysinger261597c2017-12-28 01:14:21 -0500401 terminal.setTextBlink(!!v);
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400402 },
403
Robert Ginda57f03b42012-09-13 11:02:48 -0700404 'enable-clipboard-write': function(v) {
405 terminal.vt.enableClipboardWrite = !!v;
406 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400407
Robert Ginda3755e752013-05-31 13:34:09 -0700408 'enable-dec12': function(v) {
409 terminal.vt.enableDec12 = !!v;
410 },
411
Robert Ginda57f03b42012-09-13 11:02:48 -0700412 'font-family': function(v) {
413 terminal.syncFontFamily();
414 },
rginda30f20f62012-04-05 16:36:19 -0700415
Robert Ginda57f03b42012-09-13 11:02:48 -0700416 'font-size': function(v) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500417 v = parseInt(v);
418 if (v <= 0) {
419 console.error(`Invalid font size: ${v}`);
420 return;
421 }
422
Robert Ginda57f03b42012-09-13 11:02:48 -0700423 terminal.setFontSize(v);
424 },
rginda9875d902012-08-20 16:21:57 -0700425
Robert Ginda57f03b42012-09-13 11:02:48 -0700426 'font-smoothing': function(v) {
427 terminal.syncFontFamily();
428 },
rgindade84e382012-04-20 15:39:31 -0700429
Robert Ginda57f03b42012-09-13 11:02:48 -0700430 'foreground-color': function(v) {
431 terminal.setForegroundColor(v);
432 },
rginda30f20f62012-04-05 16:36:19 -0700433
Mike Frysinger02ded6d2018-06-21 14:25:20 -0400434 'hide-mouse-while-typing': function(v) {
435 terminal.setAutomaticMouseHiding(v);
436 },
437
Robert Ginda57f03b42012-09-13 11:02:48 -0700438 'home-keys-scroll': function(v) {
439 terminal.keyboard.homeKeysScroll = v;
440 },
rginda4bba5e12012-06-20 16:15:30 -0700441
Robert Gindaa8165692015-06-15 14:46:31 -0700442 'keybindings': function(v) {
443 terminal.keyboard.bindings.clear();
444
445 if (!v)
446 return;
447
448 if (!(v instanceof Object)) {
449 console.error('Error in keybindings preference: Expected object');
450 return;
451 }
452
453 try {
454 terminal.keyboard.bindings.addBindings(v);
455 } catch (ex) {
456 console.error('Error in keybindings preference: ' + ex);
457 }
458 },
459
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700460 'media-keys-are-fkeys': function(v) {
461 terminal.keyboard.mediaKeysAreFKeys = v;
462 },
463
Robert Ginda57f03b42012-09-13 11:02:48 -0700464 'meta-sends-escape': function(v) {
465 terminal.keyboard.metaSendsEscape = v;
466 },
rginda30f20f62012-04-05 16:36:19 -0700467
Mike Frysinger847577f2017-05-23 23:25:57 -0400468 'mouse-right-click-paste': function(v) {
469 terminal.mouseRightClickPaste = v;
470 },
471
Robert Ginda57f03b42012-09-13 11:02:48 -0700472 'mouse-paste-button': function(v) {
473 terminal.syncMousePasteButton();
474 },
rgindaa8ba17d2012-08-15 14:41:10 -0700475
Robert Gindae76aa9f2014-03-14 12:29:12 -0700476 'page-keys-scroll': function(v) {
477 terminal.keyboard.pageKeysScroll = v;
478 },
479
Robert Ginda40932892012-12-10 17:26:40 -0800480 'pass-alt-number': function(v) {
481 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800482 // Let Alt-1..9 pass to the browser (to control tab switching) on
483 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500484 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800485 }
486
487 terminal.passAltNumber = v;
488 },
489
490 'pass-ctrl-number': function(v) {
491 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800492 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
493 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500494 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800495 }
496
497 terminal.passCtrlNumber = v;
498 },
499
500 'pass-meta-number': function(v) {
501 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800502 // Let Meta-1..9 pass to the browser (to control tab switching) on
503 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500504 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800505 }
506
507 terminal.passMetaNumber = v;
508 },
509
Marius Schilder77857b32014-05-14 16:21:26 -0700510 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700511 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700512 },
513
Robert Ginda8cb7d902013-06-20 14:37:18 -0700514 'receive-encoding': function(v) {
515 if (!(/^(utf-8|raw)$/).test(v)) {
516 console.warn('Invalid value for "receive-encoding": ' + v);
517 v = 'utf-8';
518 }
519
520 terminal.vt.characterEncoding = v;
521 },
522
Robert Ginda57f03b42012-09-13 11:02:48 -0700523 'scroll-on-keystroke': function(v) {
524 terminal.scrollOnKeystroke_ = v;
525 },
rginda9f5222b2012-03-05 11:53:28 -0800526
Robert Ginda57f03b42012-09-13 11:02:48 -0700527 'scroll-on-output': function(v) {
528 terminal.scrollOnOutput_ = v;
529 },
rginda30f20f62012-04-05 16:36:19 -0700530
Robert Ginda57f03b42012-09-13 11:02:48 -0700531 'scrollbar-visible': function(v) {
532 terminal.setScrollbarVisible(v);
533 },
rginda9f5222b2012-03-05 11:53:28 -0800534
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400535 'scroll-wheel-may-send-arrow-keys': function(v) {
536 terminal.scrollWheelArrowKeys_ = v;
537 },
538
Rob Spies49039e52014-12-17 13:40:04 -0800539 'scroll-wheel-move-multiplier': function(v) {
540 terminal.setScrollWheelMoveMultipler(v);
541 },
542
Robert Ginda8cb7d902013-06-20 14:37:18 -0700543 'send-encoding': function(v) {
544 if (!(/^(utf-8|raw)$/).test(v)) {
545 console.warn('Invalid value for "send-encoding": ' + v);
546 v = 'utf-8';
547 }
548
549 terminal.keyboard.characterEncoding = v;
550 },
551
Robert Ginda57f03b42012-09-13 11:02:48 -0700552 'shift-insert-paste': function(v) {
553 terminal.keyboard.shiftInsertPaste = v;
554 },
rginda9f5222b2012-03-05 11:53:28 -0800555
Mike Frysingera7768922017-07-28 15:00:12 -0400556 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400557 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400558 },
559
Robert Gindae76aa9f2014-03-14 12:29:12 -0700560 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400561 terminal.scrollPort_.setUserCssUrl(v);
562 },
563
564 'user-css-text': function(v) {
565 terminal.scrollPort_.setUserCssText(v);
566 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400567
568 'word-break-match-left': function(v) {
569 terminal.primaryScreen_.wordBreakMatchLeft = v;
570 terminal.alternateScreen_.wordBreakMatchLeft = v;
571 },
572
573 'word-break-match-right': function(v) {
574 terminal.primaryScreen_.wordBreakMatchRight = v;
575 terminal.alternateScreen_.wordBreakMatchRight = v;
576 },
577
578 'word-break-match-middle': function(v) {
579 terminal.primaryScreen_.wordBreakMatchMiddle = v;
580 terminal.alternateScreen_.wordBreakMatchMiddle = v;
581 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400582
583 'allow-images-inline': function(v) {
584 terminal.allowImagesInline = v;
585 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700586 });
rginda30f20f62012-04-05 16:36:19 -0700587
Robert Ginda57f03b42012-09-13 11:02:48 -0700588 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800589 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700590
591 if (opt_callback)
592 opt_callback();
593 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800594};
595
Rob Spies56953412014-04-28 14:09:47 -0700596
597/**
598 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500599 *
600 * @return {hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700601 */
602hterm.Terminal.prototype.getPrefs = function() {
603 return this.prefs_;
604};
605
Robert Gindaa063b202014-07-21 11:08:25 -0700606/**
607 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500608 *
609 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700610 */
611hterm.Terminal.prototype.setBracketedPaste = function(state) {
612 this.options_.bracketedPaste = state;
613};
Rob Spies56953412014-04-28 14:09:47 -0700614
rginda8e92a692012-05-20 19:37:20 -0700615/**
616 * Set the color for the cursor.
617 *
618 * If you want this setting to persist, set it through prefs_, rather than
619 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500620 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500621 * @param {string=} color The color to set. If not defined, we reset to the
622 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700623 */
624hterm.Terminal.prototype.setCursorColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500625 if (color === undefined)
626 color = this.prefs_.get('cursor-color');
627
Robert Ginda830583c2013-08-07 13:20:46 -0700628 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700629 this.cursorNode_.style.backgroundColor = color;
630 this.cursorNode_.style.borderColor = color;
631};
632
633/**
634 * Return the current cursor color as a string.
Evan Jones2600d4f2016-12-06 09:29:36 -0500635 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700636 */
637hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700638 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700639};
640
641/**
rgindad5613292012-06-19 15:40:37 -0700642 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500643 *
644 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700645 */
646hterm.Terminal.prototype.setSelectionEnabled = function(state) {
647 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700648};
649
650/**
rginda8e92a692012-05-20 19:37:20 -0700651 * Set the background color.
652 *
653 * If you want this setting to persist, set it through prefs_, rather than
654 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500655 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500656 * @param {string=} color The color to set. If not defined, we reset to the
657 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700658 */
659hterm.Terminal.prototype.setBackgroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500660 if (color === undefined)
661 color = this.prefs_.get('background-color');
662
rgindacbbd7482012-06-13 15:06:16 -0700663 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700664 this.primaryScreen_.textAttributes.setDefaults(
665 this.foregroundColor_, this.backgroundColor_);
666 this.alternateScreen_.textAttributes.setDefaults(
667 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700668 this.scrollPort_.setBackgroundColor(color);
669};
670
rginda9f5222b2012-03-05 11:53:28 -0800671/**
672 * Return the current terminal background color.
673 *
674 * Intended for use by other classes, so we don't have to expose the entire
675 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500676 *
677 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800678 */
679hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700680 return this.backgroundColor_;
681};
682
683/**
684 * Set the foreground color.
685 *
686 * If you want this setting to persist, set it through prefs_, rather than
687 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500688 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500689 * @param {string=} color The color to set. If not defined, we reset to the
690 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700691 */
692hterm.Terminal.prototype.setForegroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500693 if (color === undefined)
694 color = this.prefs_.get('foreground-color');
695
rgindacbbd7482012-06-13 15:06:16 -0700696 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700697 this.primaryScreen_.textAttributes.setDefaults(
698 this.foregroundColor_, this.backgroundColor_);
699 this.alternateScreen_.textAttributes.setDefaults(
700 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700701 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800702};
703
704/**
705 * Return the current terminal foreground color.
706 *
707 * Intended for use by other classes, so we don't have to expose the entire
708 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500709 *
710 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800711 */
712hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700713 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800714};
715
716/**
rginda87b86462011-12-14 13:48:03 -0800717 * Create a new instance of a terminal command and run it with a given
718 * argument string.
719 *
720 * @param {function} commandClass The constructor for a terminal command.
721 * @param {string} argString The argument string to pass to the command.
722 */
723hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700724 var environment = this.prefs_.get('environment');
725 if (typeof environment != 'object' || environment == null)
726 environment = {};
727
rginda87b86462011-12-14 13:48:03 -0800728 var self = this;
729 this.command = new commandClass(
730 { argString: argString || '',
731 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700732 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800733 onExit: function(code) {
734 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800735 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700736 if (self.prefs_.get('close-on-exit'))
737 window.close();
rginda87b86462011-12-14 13:48:03 -0800738 }
739 });
740
rgindafeaf3142012-01-31 15:14:20 -0800741 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800742 this.command.run();
743};
744
745/**
rgindafeaf3142012-01-31 15:14:20 -0800746 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500747 *
748 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800749 */
750hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700751 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800752};
753
754/**
755 * Install the keyboard handler for this terminal.
756 *
757 * This will prevent the browser from seeing any keystrokes sent to the
758 * terminal.
759 */
760hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700761 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400762};
rgindafeaf3142012-01-31 15:14:20 -0800763
764/**
765 * Uninstall the keyboard handler for this terminal.
766 */
767hterm.Terminal.prototype.uninstallKeyboard = function() {
768 this.keyboard.installKeyboard(null);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400769};
rgindafeaf3142012-01-31 15:14:20 -0800770
771/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400772 * Set a CSS variable.
773 *
774 * Normally this is used to set variables in the hterm namespace.
775 *
776 * @param {string} name The variable to set.
777 * @param {string} value The value to assign to the variable.
778 * @param {string?} opt_prefix The variable namespace/prefix to use.
779 */
780hterm.Terminal.prototype.setCssVar = function(name, value,
781 opt_prefix='--hterm-') {
782 this.document_.documentElement.style.setProperty(
783 `${opt_prefix}${name}`, value);
784};
785
786/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500787 * Get a CSS variable.
788 *
789 * Normally this is used to get variables in the hterm namespace.
790 *
791 * @param {string} name The variable to read.
792 * @param {string?} opt_prefix The variable namespace/prefix to use.
793 * @return {string} The current setting for this variable.
794 */
795hterm.Terminal.prototype.getCssVar = function(name, opt_prefix='--hterm-') {
796 return this.document_.documentElement.style.getPropertyValue(
797 `${opt_prefix}${name}`);
798};
799
800/**
rginda35c456b2012-02-09 17:29:05 -0800801 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800802 *
803 * Call setFontSize(0) to reset to the default font size.
804 *
805 * This function does not modify the font-size preference.
806 *
807 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800808 */
809hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500810 if (px <= 0)
rginda9f5222b2012-03-05 11:53:28 -0800811 px = this.prefs_.get('font-size');
812
rginda35c456b2012-02-09 17:29:05 -0800813 this.scrollPort_.setFontSize(px);
Mike Frysingercce97c42017-08-05 01:11:22 -0400814 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
815 this.setCssVar('charsize-height',
816 this.scrollPort_.characterSize.height + 'px');
rginda35c456b2012-02-09 17:29:05 -0800817};
818
819/**
820 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500821 *
822 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800823 */
824hterm.Terminal.prototype.getFontSize = function() {
825 return this.scrollPort_.getFontSize();
826};
827
828/**
rginda8e92a692012-05-20 19:37:20 -0700829 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500830 *
831 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700832 */
833hterm.Terminal.prototype.getFontFamily = function() {
834 return this.scrollPort_.getFontFamily();
835};
836
837/**
rginda35c456b2012-02-09 17:29:05 -0800838 * Set the CSS "font-family" for this terminal.
839 */
rginda9f5222b2012-03-05 11:53:28 -0800840hterm.Terminal.prototype.syncFontFamily = function() {
841 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
842 this.prefs_.get('font-smoothing'));
843 this.syncBoldSafeState();
844};
845
rginda4bba5e12012-06-20 16:15:30 -0700846/**
847 * Set this.mousePasteButton based on the mouse-paste-button pref,
848 * autodetecting if necessary.
849 */
850hterm.Terminal.prototype.syncMousePasteButton = function() {
851 var button = this.prefs_.get('mouse-paste-button');
852 if (typeof button == 'number') {
853 this.mousePasteButton = button;
854 return;
855 }
856
Mike Frysingeree81a002017-12-12 16:14:53 -0500857 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400858 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700859 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400860 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700861 }
862};
863
864/**
865 * Enable or disable bold based on the enable-bold pref, autodetecting if
866 * necessary.
867 */
rginda9f5222b2012-03-05 11:53:28 -0800868hterm.Terminal.prototype.syncBoldSafeState = function() {
869 var enableBold = this.prefs_.get('enable-bold');
870 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700871 this.primaryScreen_.textAttributes.enableBold = enableBold;
872 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800873 return;
874 }
875
rgindaf7521392012-02-28 17:20:34 -0800876 var normalSize = this.scrollPort_.measureCharacterSize();
877 var boldSize = this.scrollPort_.measureCharacterSize('bold');
878
879 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800880 if (!isBoldSafe) {
881 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700882 'from normal. Font family is: ' +
883 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800884 }
rginda9f5222b2012-03-05 11:53:28 -0800885
Robert Gindaed016262012-10-26 16:27:09 -0700886 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
887 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800888};
889
890/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500891 * Control text blinking behavior.
892 *
893 * @param {boolean=} state Whether to enable support for blinking text.
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400894 */
Mike Frysinger261597c2017-12-28 01:14:21 -0500895hterm.Terminal.prototype.setTextBlink = function(state) {
896 if (state === undefined)
897 state = this.prefs_.get('enable-blink');
898 this.setCssVar('blink-node-duration', state ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400899};
900
901/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400902 * Set the mouse cursor style based on the current terminal mode.
903 */
904hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400905 this.setCssVar('mouse-cursor-style',
906 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
907 'var(--hterm-mouse-cursor-text)' :
908 'var(--hterm-mouse-cursor-pointer)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400909};
910
911/**
rginda87b86462011-12-14 13:48:03 -0800912 * Return a copy of the current cursor position.
913 *
914 * @return {hterm.RowCol} The RowCol object representing the current position.
915 */
916hterm.Terminal.prototype.saveCursor = function() {
917 return this.screen_.cursorPosition.clone();
918};
919
Evan Jones2600d4f2016-12-06 09:29:36 -0500920/**
921 * Return the current text attributes.
922 *
923 * @return {string}
924 */
rgindaa19afe22012-01-25 15:40:22 -0800925hterm.Terminal.prototype.getTextAttributes = function() {
926 return this.screen_.textAttributes;
927};
928
Evan Jones2600d4f2016-12-06 09:29:36 -0500929/**
930 * Set the text attributes.
931 *
932 * @param {string} textAttributes The attributes to set.
933 */
rginda1a09aa02012-06-18 21:11:25 -0700934hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
935 this.screen_.textAttributes = textAttributes;
936};
937
rginda87b86462011-12-14 13:48:03 -0800938/**
rgindaf522ce02012-04-17 17:49:17 -0700939 * Return the current browser zoom factor applied to the terminal.
940 *
941 * @return {number} The current browser zoom factor.
942 */
943hterm.Terminal.prototype.getZoomFactor = function() {
944 return this.scrollPort_.characterSize.zoomFactor;
945};
946
947/**
rginda9846e2f2012-01-27 13:53:33 -0800948 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500949 *
950 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800951 */
952hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800953 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800954};
955
956/**
rginda87b86462011-12-14 13:48:03 -0800957 * Restore a previously saved cursor position.
958 *
959 * @param {hterm.RowCol} cursor The position to restore.
960 */
961hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700962 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
963 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800964 this.screen_.setCursorPosition(row, column);
965 if (cursor.column > column ||
966 cursor.column == column && cursor.overflow) {
967 this.screen_.cursorPosition.overflow = true;
968 }
rginda87b86462011-12-14 13:48:03 -0800969};
970
971/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400972 * Clear the cursor's overflow flag.
973 */
974hterm.Terminal.prototype.clearCursorOverflow = function() {
975 this.screen_.cursorPosition.overflow = false;
976};
977
978/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800979 * Save the current cursor state to the corresponding screens.
980 *
981 * See the hterm.Screen.CursorState class for more details.
982 *
983 * @param {boolean=} both If true, update both screens, else only update the
984 * current screen.
985 */
986hterm.Terminal.prototype.saveCursorAndState = function(both) {
987 if (both) {
988 this.primaryScreen_.saveCursorAndState(this.vt);
989 this.alternateScreen_.saveCursorAndState(this.vt);
990 } else
991 this.screen_.saveCursorAndState(this.vt);
992};
993
994/**
995 * Restore the saved cursor state in the corresponding screens.
996 *
997 * See the hterm.Screen.CursorState class for more details.
998 *
999 * @param {boolean=} both If true, update both screens, else only update the
1000 * current screen.
1001 */
1002hterm.Terminal.prototype.restoreCursorAndState = function(both) {
1003 if (both) {
1004 this.primaryScreen_.restoreCursorAndState(this.vt);
1005 this.alternateScreen_.restoreCursorAndState(this.vt);
1006 } else
1007 this.screen_.restoreCursorAndState(this.vt);
1008};
1009
1010/**
Robert Ginda830583c2013-08-07 13:20:46 -07001011 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001012 *
1013 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -07001014 */
1015hterm.Terminal.prototype.setCursorShape = function(shape) {
1016 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001017 this.restyleCursor_();
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001018};
Robert Ginda830583c2013-08-07 13:20:46 -07001019
1020/**
1021 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001022 *
1023 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -07001024 */
1025hterm.Terminal.prototype.getCursorShape = function() {
1026 return this.cursorShape_;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001027};
Robert Ginda830583c2013-08-07 13:20:46 -07001028
1029/**
rginda87b86462011-12-14 13:48:03 -08001030 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001031 *
1032 * @param {number} columnCount
rginda87b86462011-12-14 13:48:03 -08001033 */
1034hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001035 if (columnCount == null) {
1036 this.div_.style.width = '100%';
1037 return;
1038 }
1039
Robert Ginda26806d12014-07-24 13:44:07 -07001040 this.div_.style.width = Math.ceil(
1041 this.scrollPort_.characterSize.width *
1042 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001043 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001044 this.scheduleSyncCursorPosition_();
1045};
rginda87b86462011-12-14 13:48:03 -08001046
rgindac9bc5502012-01-18 11:48:44 -08001047/**
rginda35c456b2012-02-09 17:29:05 -08001048 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001049 *
1050 * @param {number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001051 */
1052hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001053 if (rowCount == null) {
1054 this.div_.style.height = '100%';
1055 return;
1056 }
1057
rginda35c456b2012-02-09 17:29:05 -08001058 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -07001059 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -08001060 this.realizeSize_(this.screenSize.width, rowCount);
1061 this.scheduleSyncCursorPosition_();
1062};
1063
1064/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001065 * Deal with terminal size changes.
1066 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001067 * @param {number} columnCount The number of columns.
1068 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001069 */
1070hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
1071 if (columnCount != this.screenSize.width)
1072 this.realizeWidth_(columnCount);
1073
1074 if (rowCount != this.screenSize.height)
1075 this.realizeHeight_(rowCount);
1076
1077 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -07001078 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001079};
1080
1081/**
rgindac9bc5502012-01-18 11:48:44 -08001082 * Deal with terminal width changes.
1083 *
1084 * This function does what needs to be done when the terminal width changes
1085 * out from under us. It happens here rather than in onResize_() because this
1086 * code may need to run synchronously to handle programmatic changes of
1087 * terminal width.
1088 *
1089 * Relying on the browser to send us an async resize event means we may not be
1090 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001091 *
1092 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001093 */
1094hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001095 if (columnCount <= 0)
1096 throw new Error('Attempt to realize bad width: ' + columnCount);
1097
rgindac9bc5502012-01-18 11:48:44 -08001098 var deltaColumns = columnCount - this.screen_.getWidth();
1099
rginda87b86462011-12-14 13:48:03 -08001100 this.screenSize.width = columnCount;
1101 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001102
1103 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -04001104 if (this.defaultTabStops)
1105 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -08001106 } else {
1107 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -04001108 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -08001109 break;
1110
1111 this.tabStops_.pop();
1112 }
1113 }
1114
1115 this.screen_.setColumnCount(this.screenSize.width);
1116};
1117
1118/**
1119 * Deal with terminal height changes.
1120 *
1121 * This function does what needs to be done when the terminal height changes
1122 * out from under us. It happens here rather than in onResize_() because this
1123 * code may need to run synchronously to handle programmatic changes of
1124 * terminal height.
1125 *
1126 * Relying on the browser to send us an async resize event means we may not be
1127 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001128 *
1129 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001130 */
1131hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001132 if (rowCount <= 0)
1133 throw new Error('Attempt to realize bad height: ' + rowCount);
1134
rgindac9bc5502012-01-18 11:48:44 -08001135 var deltaRows = rowCount - this.screen_.getHeight();
1136
1137 this.screenSize.height = rowCount;
1138
1139 var cursor = this.saveCursor();
1140
1141 if (deltaRows < 0) {
1142 // Screen got smaller.
1143 deltaRows *= -1;
1144 while (deltaRows) {
1145 var lastRow = this.getRowCount() - 1;
1146 if (lastRow - this.scrollbackRows_.length == cursor.row)
1147 break;
1148
1149 if (this.getRowText(lastRow))
1150 break;
1151
1152 this.screen_.popRow();
1153 deltaRows--;
1154 }
1155
1156 var ary = this.screen_.shiftRows(deltaRows);
1157 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1158
1159 // We just removed rows from the top of the screen, we need to update
1160 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001161 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001162 } else if (deltaRows > 0) {
1163 // Screen got larger.
1164
1165 if (deltaRows <= this.scrollbackRows_.length) {
1166 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1167 var rows = this.scrollbackRows_.splice(
1168 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1169 this.screen_.unshiftRows(rows);
1170 deltaRows -= scrollbackCount;
1171 cursor.row += scrollbackCount;
1172 }
1173
1174 if (deltaRows)
1175 this.appendRows_(deltaRows);
1176 }
1177
rginda35c456b2012-02-09 17:29:05 -08001178 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001179 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001180};
1181
1182/**
1183 * Scroll the terminal to the top of the scrollback buffer.
1184 */
1185hterm.Terminal.prototype.scrollHome = function() {
1186 this.scrollPort_.scrollRowToTop(0);
1187};
1188
1189/**
1190 * Scroll the terminal to the end.
1191 */
1192hterm.Terminal.prototype.scrollEnd = function() {
1193 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1194};
1195
1196/**
1197 * Scroll the terminal one page up (minus one line) relative to the current
1198 * position.
1199 */
1200hterm.Terminal.prototype.scrollPageUp = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001201 this.scrollPort_.scrollPageUp();
rginda87b86462011-12-14 13:48:03 -08001202};
1203
1204/**
1205 * Scroll the terminal one page down (minus one line) relative to the current
1206 * position.
1207 */
1208hterm.Terminal.prototype.scrollPageDown = function() {
Raymes Khoury177aec72018-06-26 10:58:53 +10001209 this.scrollPort_.scrollPageDown();
rginda8ba33642011-12-14 12:31:31 -08001210};
1211
rgindac9bc5502012-01-18 11:48:44 -08001212/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001213 * Scroll the terminal one line up relative to the current position.
1214 */
1215hterm.Terminal.prototype.scrollLineUp = function() {
1216 var i = this.scrollPort_.getTopRowIndex();
1217 this.scrollPort_.scrollRowToTop(i - 1);
1218};
1219
1220/**
1221 * Scroll the terminal one line down relative to the current position.
1222 */
1223hterm.Terminal.prototype.scrollLineDown = function() {
1224 var i = this.scrollPort_.getTopRowIndex();
1225 this.scrollPort_.scrollRowToTop(i + 1);
1226};
1227
1228/**
Robert Ginda40932892012-12-10 17:26:40 -08001229 * Clear primary screen, secondary screen, and the scrollback buffer.
1230 */
1231hterm.Terminal.prototype.wipeContents = function() {
1232 this.scrollbackRows_.length = 0;
1233 this.scrollPort_.resetCache();
1234
1235 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
1236 var bottom = screen.getHeight();
1237 if (bottom > 0) {
1238 this.renumberRows_(0, bottom);
1239 this.clearHome(screen);
1240 }
1241 }.bind(this));
1242
1243 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001244 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001245};
1246
1247/**
rgindac9bc5502012-01-18 11:48:44 -08001248 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001249 *
1250 * Perform a full reset to the default values listed in
1251 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001252 */
rginda87b86462011-12-14 13:48:03 -08001253hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001254 this.vt.reset();
1255
rgindac9bc5502012-01-18 11:48:44 -08001256 this.clearAllTabStops();
1257 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001258
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001259 const resetScreen = (screen) => {
1260 // We want to make sure to reset the attributes before we clear the screen.
1261 // The attributes might be used to initialize default/empty rows.
1262 screen.textAttributes.reset();
1263 screen.textAttributes.resetColorPalette();
1264 this.clearHome(screen);
1265 screen.saveCursorAndState(this.vt);
1266 };
1267 resetScreen(this.primaryScreen_);
1268 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001269
Mike Frysinger84301d02017-11-29 13:28:46 -08001270 // Reset terminal options to their default values.
1271 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001272 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1273
Mike Frysinger84301d02017-11-29 13:28:46 -08001274 this.setVTScrollRegion(null, null);
1275
1276 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001277};
1278
rgindac9bc5502012-01-18 11:48:44 -08001279/**
1280 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001281 *
1282 * Perform a soft reset to the default values listed in
1283 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001284 */
rginda0f5c0292012-01-13 11:00:13 -08001285hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001286 this.vt.reset();
1287
rgindab8bc8932012-04-27 12:45:03 -07001288 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001289 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001290
Brad Townb62dfdc2015-03-16 19:07:15 -07001291 // We show the cursor on soft reset but do not alter the blink state.
1292 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1293
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001294 const resetScreen = (screen) => {
1295 // Xterm also resets the color palette on soft reset, even though it doesn't
1296 // seem to be documented anywhere.
1297 screen.textAttributes.reset();
1298 screen.textAttributes.resetColorPalette();
1299 screen.saveCursorAndState(this.vt);
1300 };
1301 resetScreen(this.primaryScreen_);
1302 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001303
rgindab8bc8932012-04-27 12:45:03 -07001304 // The xterm man page explicitly says this will happen on soft reset.
1305 this.setVTScrollRegion(null, null);
1306
1307 // Xterm also shows the cursor on soft reset, but does not alter the blink
1308 // state.
rgindaa19afe22012-01-25 15:40:22 -08001309 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001310};
1311
rgindac9bc5502012-01-18 11:48:44 -08001312/**
1313 * Move the cursor forward to the next tab stop, or to the last column
1314 * if no more tab stops are set.
1315 */
1316hterm.Terminal.prototype.forwardTabStop = function() {
1317 var column = this.screen_.cursorPosition.column;
1318
1319 for (var i = 0; i < this.tabStops_.length; i++) {
1320 if (this.tabStops_[i] > column) {
1321 this.setCursorColumn(this.tabStops_[i]);
1322 return;
1323 }
1324 }
1325
David Benjamin66e954d2012-05-05 21:08:12 -04001326 // xterm does not clear the overflow flag on HT or CHT.
1327 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001328 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001329 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001330};
1331
rgindac9bc5502012-01-18 11:48:44 -08001332/**
1333 * Move the cursor backward to the previous tab stop, or to the first column
1334 * if no previous tab stops are set.
1335 */
1336hterm.Terminal.prototype.backwardTabStop = function() {
1337 var column = this.screen_.cursorPosition.column;
1338
1339 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1340 if (this.tabStops_[i] < column) {
1341 this.setCursorColumn(this.tabStops_[i]);
1342 return;
1343 }
1344 }
1345
1346 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001347};
1348
rgindac9bc5502012-01-18 11:48:44 -08001349/**
1350 * Set a tab stop at the given column.
1351 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001352 * @param {integer} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001353 */
1354hterm.Terminal.prototype.setTabStop = function(column) {
1355 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1356 if (this.tabStops_[i] == column)
1357 return;
1358
1359 if (this.tabStops_[i] < column) {
1360 this.tabStops_.splice(i + 1, 0, column);
1361 return;
1362 }
1363 }
1364
1365 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001366};
1367
rgindac9bc5502012-01-18 11:48:44 -08001368/**
1369 * Clear the tab stop at the current cursor position.
1370 *
1371 * No effect if there is no tab stop at the current cursor position.
1372 */
1373hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1374 var column = this.screen_.cursorPosition.column;
1375
1376 var i = this.tabStops_.indexOf(column);
1377 if (i == -1)
1378 return;
1379
1380 this.tabStops_.splice(i, 1);
1381};
1382
1383/**
1384 * Clear all tab stops.
1385 */
1386hterm.Terminal.prototype.clearAllTabStops = function() {
1387 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001388 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001389};
1390
1391/**
1392 * Set up the default tab stops, starting from a given column.
1393 *
1394 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001395 * from the specified column, or 0 if no column is provided. It also flags
1396 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001397 *
1398 * This does not clear the existing tab stops first, use clearAllTabStops
1399 * for that.
1400 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001401 * @param {integer} opt_start Optional starting zero based starting column, useful
rgindac9bc5502012-01-18 11:48:44 -08001402 * for filling out missing tab stops when the terminal is resized.
1403 */
1404hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1405 var start = opt_start || 0;
1406 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001407 // Round start up to a default tab stop.
1408 start = start - 1 - ((start - 1) % w) + w;
1409 for (var i = start; i < this.screenSize.width; i += w) {
1410 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001411 }
David Benjamin66e954d2012-05-05 21:08:12 -04001412
1413 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001414};
1415
rginda6d397402012-01-17 10:58:29 -08001416/**
rginda8ba33642011-12-14 12:31:31 -08001417 * Interpret a sequence of characters.
1418 *
1419 * Incomplete escape sequences are buffered until the next call.
1420 *
1421 * @param {string} str Sequence of characters to interpret or pass through.
1422 */
1423hterm.Terminal.prototype.interpret = function(str) {
rginda8ba33642011-12-14 12:31:31 -08001424 this.scheduleSyncCursorPosition_();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001425 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001426};
1427
1428/**
1429 * Take over the given DIV for use as the terminal display.
1430 *
1431 * @param {HTMLDivElement} div The div to use as the terminal display.
1432 */
1433hterm.Terminal.prototype.decorate = function(div) {
Mike Frysinger5768a9d2017-12-26 12:57:44 -05001434 const charset = div.ownerDocument.characterSet.toLowerCase();
1435 if (charset != 'utf-8') {
1436 console.warn(`Document encoding should be set to utf-8, not "${charset}";` +
1437 ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`);
1438 }
1439
rginda87b86462011-12-14 13:48:03 -08001440 this.div_ = div;
1441
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001442 this.accessibilityReader_ = new hterm.AccessibilityReader(div);
1443
rginda8ba33642011-12-14 12:31:31 -08001444 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001445 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001446 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1447 this.scrollPort_.setBackgroundPosition(
1448 this.prefs_.get('background-position'));
Mike Frysinger08bad432017-04-24 00:50:54 -04001449 this.scrollPort_.setUserCssUrl(this.prefs_.get('user-css'));
1450 this.scrollPort_.setUserCssText(this.prefs_.get('user-css-text'));
Raymes Khoury177aec72018-06-26 10:58:53 +10001451 this.scrollPort_.setAccessibilityReader(this.accessibilityReader_);
rginda30f20f62012-04-05 16:36:19 -07001452
rginda0918b652012-04-04 11:26:24 -07001453 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001454
rginda9f5222b2012-03-05 11:53:28 -08001455 this.setFontSize(this.prefs_.get('font-size'));
1456 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001457
David Reveman8f552492012-03-28 12:18:41 -04001458 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001459 this.setScrollWheelMoveMultipler(
1460 this.prefs_.get('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001461
rginda8ba33642011-12-14 12:31:31 -08001462 this.document_ = this.scrollPort_.getDocument();
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001463 this.accessibilityReader_.decorate(this.document_);
rginda8ba33642011-12-14 12:31:31 -08001464
Evan Jones5f9df812016-12-06 09:38:58 -05001465 this.document_.body.oncontextmenu = function() { return false; };
rginda4bba5e12012-06-20 16:15:30 -07001466
1467 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001468 var screenNode = this.scrollPort_.getScreenNode();
1469 screenNode.addEventListener('mousedown', onMouse);
1470 screenNode.addEventListener('mouseup', onMouse);
1471 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001472 this.scrollPort_.onScrollWheel = onMouse;
1473
Mike Frysinger02ded6d2018-06-21 14:25:20 -04001474 screenNode.addEventListener('keydown', this.onKeyboardActivity_.bind(this));
1475
Toni Barzic0bfa8922013-11-22 11:18:35 -08001476 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001477 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001478 // Listen for mousedown events on the screenNode as in FF the focus
1479 // events don't bubble.
1480 screenNode.addEventListener('mousedown', function() {
1481 setTimeout(this.onFocusChange_.bind(this, true));
1482 }.bind(this));
1483
Toni Barzic0bfa8922013-11-22 11:18:35 -08001484 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001485 'blur', this.onFocusChange_.bind(this, false));
1486
1487 var style = this.document_.createElement('style');
1488 style.textContent =
1489 ('.cursor-node[focus="false"] {' +
1490 ' box-sizing: border-box;' +
1491 ' background-color: transparent !important;' +
1492 ' border-width: 2px;' +
1493 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001494 '}' +
1495 '.wc-node {' +
1496 ' display: inline-block;' +
1497 ' text-align: center;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001498 ' width: calc(var(--hterm-charsize-width) * 2);' +
Mike Frysinger4036f6e2017-05-31 14:02:55 -04001499 ' line-height: var(--hterm-charsize-height);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001500 '}' +
1501 ':root {' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001502 ' --hterm-charsize-width: ' + this.scrollPort_.characterSize.width + 'px;' +
1503 ' --hterm-charsize-height: ' + this.scrollPort_.characterSize.height + 'px;' +
Mike Frysingera27c0502017-08-23 22:37:10 -04001504 // Default position hides the cursor for when the window is initializing.
1505 ' --hterm-cursor-offset-col: -1;' +
1506 ' --hterm-cursor-offset-row: -1;' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001507 ' --hterm-blink-node-duration: 0.7s;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001508 ' --hterm-mouse-cursor-text: text;' +
1509 ' --hterm-mouse-cursor-pointer: default;' +
1510 ' --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001511 '}' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001512 '.uri-node:hover {' +
1513 ' text-decoration: underline;' +
Mike Frysingerb74a6472018-06-22 13:37:08 -04001514 ' cursor: var(--hterm-mouse-cursor-pointer), pointer;' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001515 '}' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001516 '@keyframes blink {' +
1517 ' from { opacity: 1.0; }' +
1518 ' to { opacity: 0.0; }' +
1519 '}' +
1520 '.blink-node {' +
1521 ' animation-name: blink;' +
1522 ' animation-duration: var(--hterm-blink-node-duration);' +
1523 ' animation-iteration-count: infinite;' +
1524 ' animation-timing-function: ease-in-out;' +
1525 ' animation-direction: alternate;' +
rginda8e92a692012-05-20 19:37:20 -07001526 '}');
Mike Frysingerb74a6472018-06-22 13:37:08 -04001527 // Insert this stock style as the first node so that any user styles will
1528 // override w/out having to use !important everywhere. The rules above mix
1529 // runtime variables with default ones designed to be overridden by the user,
1530 // but we can wait for a concrete case from the users to determine the best
1531 // way to split the sheet up to before & after the user-css settings.
1532 this.document_.head.insertBefore(style, this.document_.head.firstChild);
rginda8e92a692012-05-20 19:37:20 -07001533
rginda8ba33642011-12-14 12:31:31 -08001534 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001535 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001536 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001537 this.cursorNode_.style.cssText =
1538 ('position: absolute;' +
Mike Frysinger44c32202017-08-05 01:13:09 -04001539 'left: calc(var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));' +
1540 'top: calc(var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));' +
Mike Frysingerd60f4c22018-03-15 21:25:30 -07001541 'display: ' + (this.options_.cursorVisible ? '' : 'none') + ';' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001542 'width: var(--hterm-charsize-width);' +
1543 'height: var(--hterm-charsize-height);' +
Rob Spies06533ba2014-04-24 11:20:37 -07001544 '-webkit-transition: opacity, background-color 100ms linear;' +
1545 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001546
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001547 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001548 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1549 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001550
rginda8ba33642011-12-14 12:31:31 -08001551 this.document_.body.appendChild(this.cursorNode_);
1552
rgindad5613292012-06-19 15:40:37 -07001553 // When 'enableMouseDragScroll' is off we reposition this element directly
1554 // under the mouse cursor after a click. This makes Chrome associate
1555 // subsequent mousemove events with the scroll-blocker. Since the
1556 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1557 // events do not cause the scrollport to scroll.
1558 //
1559 // It's a hack, but it's the cleanest way I could find.
1560 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001561 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
Raymes Khoury6dce2f82018-04-12 15:38:58 +10001562 this.scrollBlockerNode_.setAttribute('aria-hidden', 'true');
rgindad5613292012-06-19 15:40:37 -07001563 this.scrollBlockerNode_.style.cssText =
1564 ('position: absolute;' +
1565 'top: -99px;' +
1566 'display: block;' +
1567 'width: 10px;' +
1568 'height: 10px;');
1569 this.document_.body.appendChild(this.scrollBlockerNode_);
1570
rgindad5613292012-06-19 15:40:37 -07001571 this.scrollPort_.onScrollWheel = onMouse;
1572 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1573 ].forEach(function(event) {
1574 this.scrollBlockerNode_.addEventListener(event, onMouse);
1575 this.cursorNode_.addEventListener(event, onMouse);
1576 this.document_.addEventListener(event, onMouse);
1577 }.bind(this));
1578
1579 this.cursorNode_.addEventListener('mousedown', function() {
1580 setTimeout(this.focus.bind(this));
1581 }.bind(this));
1582
rginda8ba33642011-12-14 12:31:31 -08001583 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001584
rginda87b86462011-12-14 13:48:03 -08001585 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001586 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001587};
1588
rginda0918b652012-04-04 11:26:24 -07001589/**
1590 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001591 *
1592 * @return {HTMLDocument}
rginda0918b652012-04-04 11:26:24 -07001593 */
rginda87b86462011-12-14 13:48:03 -08001594hterm.Terminal.prototype.getDocument = function() {
1595 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001596};
1597
1598/**
rginda0918b652012-04-04 11:26:24 -07001599 * Focus the terminal.
1600 */
1601hterm.Terminal.prototype.focus = function() {
1602 this.scrollPort_.focus();
1603};
1604
1605/**
rginda8ba33642011-12-14 12:31:31 -08001606 * Return the HTML Element for a given row index.
1607 *
1608 * This is a method from the RowProvider interface. The ScrollPort uses
1609 * it to fetch rows on demand as they are scrolled into view.
1610 *
1611 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1612 * pairs to conserve memory.
1613 *
1614 * @param {integer} index The zero-based row index, measured relative to the
1615 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001616 * largest indices.
rginda8ba33642011-12-14 12:31:31 -08001617 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1618 */
1619hterm.Terminal.prototype.getRowNode = function(index) {
1620 if (index < this.scrollbackRows_.length)
1621 return this.scrollbackRows_[index];
1622
1623 var screenIndex = index - this.scrollbackRows_.length;
1624 return this.screen_.rowsArray[screenIndex];
1625};
1626
1627/**
1628 * Return the text content for a given range of rows.
1629 *
1630 * This is a method from the RowProvider interface. The ScrollPort uses
1631 * it to fetch text content on demand when the user attempts to copy their
1632 * selection to the clipboard.
1633 *
1634 * @param {integer} start The zero-based row index to start from, measured
1635 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001636 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001637 * @param {integer} end The zero-based row index to end on, measured
1638 * relative to the start of the scrollback buffer.
1639 * @return {string} A single string containing the text value of the range of
1640 * rows. Lines will be newline delimited, with no trailing newline.
1641 */
1642hterm.Terminal.prototype.getRowsText = function(start, end) {
1643 var ary = [];
1644 for (var i = start; i < end; i++) {
1645 var node = this.getRowNode(i);
1646 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001647 if (i < end - 1 && !node.getAttribute('line-overflow'))
1648 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001649 }
1650
rgindaa09e7332012-08-17 12:49:51 -07001651 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001652};
1653
1654/**
1655 * Return the text content for a given row.
1656 *
1657 * This is a method from the RowProvider interface. The ScrollPort uses
1658 * it to fetch text content on demand when the user attempts to copy their
1659 * selection to the clipboard.
1660 *
1661 * @param {integer} index The zero-based row index to return, measured
1662 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001663 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001664 * @return {string} A string containing the text value of the selected row.
1665 */
1666hterm.Terminal.prototype.getRowText = function(index) {
1667 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001668 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001669};
1670
1671/**
1672 * Return the total number of rows in the addressable screen and in the
1673 * scrollback buffer of this terminal.
1674 *
1675 * This is a method from the RowProvider interface. The ScrollPort uses
1676 * it to compute the size of the scrollbar.
1677 *
1678 * @return {integer} The number of rows in this terminal.
1679 */
1680hterm.Terminal.prototype.getRowCount = function() {
1681 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1682};
1683
1684/**
1685 * Create DOM nodes for new rows and append them to the end of the terminal.
1686 *
1687 * This is the only correct way to add a new DOM node for a row. Notice that
1688 * the new row is appended to the bottom of the list of rows, and does not
1689 * require renumbering (of the rowIndex property) of previous rows.
1690 *
1691 * If you think you want a new blank row somewhere in the middle of the
1692 * terminal, look into moveRows_().
1693 *
1694 * This method does not pay attention to vtScrollTop/Bottom, since you should
1695 * be using moveRows() in cases where they would matter.
1696 *
1697 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001698 *
1699 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001700 */
1701hterm.Terminal.prototype.appendRows_ = function(count) {
1702 var cursorRow = this.screen_.rowsArray.length;
1703 var offset = this.scrollbackRows_.length + cursorRow;
1704 for (var i = 0; i < count; i++) {
1705 var row = this.document_.createElement('x-row');
1706 row.appendChild(this.document_.createTextNode(''));
1707 row.rowIndex = offset + i;
1708 this.screen_.pushRow(row);
1709 }
1710
1711 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1712 if (extraRows > 0) {
1713 var ary = this.screen_.shiftRows(extraRows);
1714 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001715 if (this.scrollPort_.isScrolledEnd)
1716 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001717 }
1718
1719 if (cursorRow >= this.screen_.rowsArray.length)
1720 cursorRow = this.screen_.rowsArray.length - 1;
1721
rginda87b86462011-12-14 13:48:03 -08001722 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001723};
1724
1725/**
1726 * Relocate rows from one part of the addressable screen to another.
1727 *
1728 * This is used to recycle rows during VT scrolls (those which are driven
1729 * by VT commands, rather than by the user manipulating the scrollbar.)
1730 *
1731 * In this case, the blank lines scrolled into the scroll region are made of
1732 * the nodes we scrolled off. These have their rowIndex properties carefully
1733 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001734 *
1735 * @param {number} fromIndex The start index.
1736 * @param {number} count The number of rows to move.
1737 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001738 */
1739hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1740 var ary = this.screen_.removeRows(fromIndex, count);
1741 this.screen_.insertRows(toIndex, ary);
1742
1743 var start, end;
1744 if (fromIndex < toIndex) {
1745 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001746 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001747 } else {
1748 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001749 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001750 }
1751
1752 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001753 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001754};
1755
1756/**
1757 * Renumber the rowIndex property of the given range of rows.
1758 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001759 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001760 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001761 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001762 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001763 *
1764 * @param {number} start The start index.
1765 * @param {number} end The end index.
1766 * @param {hterm.Screen} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001767 */
Robert Ginda40932892012-12-10 17:26:40 -08001768hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1769 var screen = opt_screen || this.screen_;
1770
rginda8ba33642011-12-14 12:31:31 -08001771 var offset = this.scrollbackRows_.length;
1772 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001773 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001774 }
1775};
1776
1777/**
1778 * Print a string to the terminal.
1779 *
1780 * This respects the current insert and wraparound modes. It will add new lines
1781 * to the end of the terminal, scrolling off the top into the scrollback buffer
1782 * if necessary.
1783 *
1784 * The string is *not* parsed for escape codes. Use the interpret() method if
1785 * that's what you're after.
1786 *
1787 * @param{string} str The string to print.
1788 */
1789hterm.Terminal.prototype.print = function(str) {
Raymes Khouryb199d4d2018-07-12 15:08:12 +10001790 this.scheduleSyncCursorPosition_();
1791
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001792 // Basic accessibility output for the screen reader.
Raymes Khoury177aec72018-06-26 10:58:53 +10001793 this.accessibilityReader_.announce(str);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001794
rgindaa9abdd82012-08-06 18:05:09 -07001795 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001796
Ricky Liang48f05cb2013-12-31 23:35:29 +08001797 var strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04001798 // Fun edge case: If the string only contains zero width codepoints (like
1799 // combining characters), we make sure to iterate at least once below.
1800 if (strWidth == 0 && str)
1801 strWidth = 1;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001802
1803 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001804 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1805 this.screen_.commitLineOverflow();
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001806 this.newLine(true);
rgindaa09e7332012-08-17 12:49:51 -07001807 }
rgindaa19afe22012-01-25 15:40:22 -08001808
Ricky Liang48f05cb2013-12-31 23:35:29 +08001809 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001810 var didOverflow = false;
1811 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001812
rgindaa9abdd82012-08-06 18:05:09 -07001813 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1814 didOverflow = true;
1815 count = this.screenSize.width - this.screen_.cursorPosition.column;
1816 }
rgindaa19afe22012-01-25 15:40:22 -08001817
rgindaa9abdd82012-08-06 18:05:09 -07001818 if (didOverflow && !this.options_.wraparound) {
1819 // If the string overflowed the line but wraparound is off, then the
1820 // last printed character should be the last of the string.
1821 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001822 substr = lib.wc.substr(str, startOffset, count - 1) +
1823 lib.wc.substr(str, strWidth - 1);
1824 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001825 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001826 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001827 }
rgindaa19afe22012-01-25 15:40:22 -08001828
Ricky Liang48f05cb2013-12-31 23:35:29 +08001829 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1830 for (var i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001831 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
1832 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001833
1834 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001835 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001836 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001837 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001838 }
1839 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001840 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07001841 }
1842
1843 this.screen_.maybeClipCurrentRow();
1844 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001845 }
rginda8ba33642011-12-14 12:31:31 -08001846
rginda9f5222b2012-03-05 11:53:28 -08001847 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001848 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001849};
1850
1851/**
rginda87b86462011-12-14 13:48:03 -08001852 * Set the VT scroll region.
1853 *
rginda87b86462011-12-14 13:48:03 -08001854 * This also resets the cursor position to the absolute (0, 0) position, since
1855 * that's what xterm appears to do.
1856 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001857 * Setting the scroll region to the full height of the terminal will clear
1858 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1859 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1860 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1861 * continue to work as most users would expect.
1862 *
rginda87b86462011-12-14 13:48:03 -08001863 * @param {integer} scrollTop The zero-based top of the scroll region.
1864 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1865 * inclusive.
1866 */
1867hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001868 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001869 this.vtScrollTop_ = null;
1870 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001871 } else {
1872 this.vtScrollTop_ = scrollTop;
1873 this.vtScrollBottom_ = scrollBottom;
1874 }
rginda87b86462011-12-14 13:48:03 -08001875};
1876
1877/**
rginda8ba33642011-12-14 12:31:31 -08001878 * Return the top row index according to the VT.
1879 *
1880 * This will return 0 unless the terminal has been told to restrict scrolling
1881 * to some lower row. It is used for some VT cursor positioning and scrolling
1882 * commands.
1883 *
1884 * @return {integer} The topmost row in the terminal's scroll region.
1885 */
1886hterm.Terminal.prototype.getVTScrollTop = function() {
1887 if (this.vtScrollTop_ != null)
1888 return this.vtScrollTop_;
1889
1890 return 0;
rginda87b86462011-12-14 13:48:03 -08001891};
rginda8ba33642011-12-14 12:31:31 -08001892
1893/**
1894 * Return the bottom row index according to the VT.
1895 *
1896 * This will return the height of the terminal unless the it has been told to
1897 * restrict scrolling to some higher row. It is used for some VT cursor
1898 * positioning and scrolling commands.
1899 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001900 * @return {integer} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001901 */
1902hterm.Terminal.prototype.getVTScrollBottom = function() {
1903 if (this.vtScrollBottom_ != null)
1904 return this.vtScrollBottom_;
1905
rginda87b86462011-12-14 13:48:03 -08001906 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001907};
rginda8ba33642011-12-14 12:31:31 -08001908
1909/**
1910 * Process a '\n' character.
1911 *
1912 * If the cursor is on the final row of the terminal this will append a new
1913 * blank row to the screen and scroll the topmost row into the scrollback
1914 * buffer.
1915 *
1916 * Otherwise, this moves the cursor to column zero of the next row.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001917 *
1918 * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of
1919 * the terminal.
rginda8ba33642011-12-14 12:31:31 -08001920 */
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001921hterm.Terminal.prototype.newLine = function(dueToOverflow = false) {
1922 if (!dueToOverflow)
1923 this.accessibilityReader_.newLine();
1924
Robert Ginda9937abc2013-07-25 16:09:23 -07001925 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1926 this.screen_.rowsArray.length - 1);
1927
1928 if (this.vtScrollBottom_ != null) {
1929 // A VT Scroll region is active, we never append new rows.
1930 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1931 // We're at the end of the VT Scroll Region, perform a VT scroll.
1932 this.vtScrollUp(1);
1933 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1934 } else if (cursorAtEndOfScreen) {
1935 // We're at the end of the screen, the only thing to do is put the
1936 // cursor to column 0.
1937 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1938 } else {
1939 // Anywhere else, advance the cursor row, and reset the column.
1940 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1941 }
1942 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001943 // We're at the end of the screen. Append a new row to the terminal,
1944 // shifting the top row into the scrollback.
1945 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001946 } else {
rginda87b86462011-12-14 13:48:03 -08001947 // Anywhere else in the screen just moves the cursor.
1948 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001949 }
1950};
1951
1952/**
1953 * Like newLine(), except maintain the cursor column.
1954 */
1955hterm.Terminal.prototype.lineFeed = function() {
1956 var column = this.screen_.cursorPosition.column;
1957 this.newLine();
1958 this.setCursorColumn(column);
1959};
1960
1961/**
rginda87b86462011-12-14 13:48:03 -08001962 * If autoCarriageReturn is set then newLine(), else lineFeed().
1963 */
1964hterm.Terminal.prototype.formFeed = function() {
1965 if (this.options_.autoCarriageReturn) {
1966 this.newLine();
1967 } else {
1968 this.lineFeed();
1969 }
1970};
1971
1972/**
1973 * Move the cursor up one row, possibly inserting a blank line.
1974 *
1975 * The cursor column is not changed.
1976 */
1977hterm.Terminal.prototype.reverseLineFeed = function() {
1978 var scrollTop = this.getVTScrollTop();
1979 var currentRow = this.screen_.cursorPosition.row;
1980
1981 if (currentRow == scrollTop) {
1982 this.insertLines(1);
1983 } else {
1984 this.setAbsoluteCursorRow(currentRow - 1);
1985 }
1986};
1987
1988/**
rginda8ba33642011-12-14 12:31:31 -08001989 * Replace all characters to the left of the current cursor with the space
1990 * character.
1991 *
1992 * TODO(rginda): This should probably *remove* the characters (not just replace
1993 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001994 * position.
rginda8ba33642011-12-14 12:31:31 -08001995 */
1996hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001997 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001998 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04001999 const count = cursor.column + 1;
2000 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08002001 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002002};
2003
2004/**
David Benjamin684a9b72012-05-01 17:19:58 -04002005 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08002006 *
2007 * The cursor position is unchanged.
2008 *
Robert Gindaf2547f12012-10-25 20:36:21 -07002009 * If the current background color is not the default background color this
2010 * will insert spaces rather than delete. This is unfortunate because the
2011 * trailing space will affect text selection, but it's difficult to come up
2012 * with a way to style empty space that wouldn't trip up the hterm.Screen
2013 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07002014 *
2015 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
2016 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2017 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002018 *
2019 * @param {number} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002020 */
2021hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002022 if (this.screen_.cursorPosition.overflow)
2023 return;
2024
Robert Ginda7fd57082012-09-25 14:41:47 -07002025 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
2026 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002027
2028 if (this.screen_.textAttributes.background ===
2029 this.screen_.textAttributes.DEFAULT_COLOR) {
2030 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002031 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002032 this.screen_.cursorPosition.column + count) {
2033 this.screen_.deleteChars(count);
2034 this.clearCursorOverflow();
2035 return;
2036 }
2037 }
2038
rginda87b86462011-12-14 13:48:03 -08002039 var cursor = this.saveCursor();
Mike Frysinger6380bed2017-08-24 18:46:39 -04002040 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08002041 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002042 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002043};
2044
2045/**
2046 * Erase the current line.
2047 *
2048 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002049 */
2050hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08002051 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002052 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002053 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002054 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002055};
2056
2057/**
David Benjamina08d78f2012-05-05 00:28:49 -04002058 * Erase all characters from the start of the screen to the current cursor
2059 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002060 *
2061 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002062 */
2063hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08002064 var cursor = this.saveCursor();
2065
2066 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002067
David Benjamina08d78f2012-05-05 00:28:49 -04002068 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002069 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002070 this.screen_.clearCursorRow();
2071 }
2072
rginda87b86462011-12-14 13:48:03 -08002073 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002074 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002075};
2076
2077/**
2078 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002079 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002080 *
2081 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002082 */
2083hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08002084 var cursor = this.saveCursor();
2085
2086 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002087
David Benjamina08d78f2012-05-05 00:28:49 -04002088 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08002089 for (var i = cursor.row + 1; i <= bottom; i++) {
2090 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002091 this.screen_.clearCursorRow();
2092 }
2093
rginda87b86462011-12-14 13:48:03 -08002094 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002095 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002096};
2097
2098/**
2099 * Fill the terminal with a given character.
2100 *
2101 * This methods does not respect the VT scroll region.
2102 *
2103 * @param {string} ch The character to use for the fill.
2104 */
2105hterm.Terminal.prototype.fill = function(ch) {
2106 var cursor = this.saveCursor();
2107
2108 this.setAbsoluteCursorPosition(0, 0);
2109 for (var row = 0; row < this.screenSize.height; row++) {
2110 for (var col = 0; col < this.screenSize.width; col++) {
2111 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002112 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002113 }
2114 }
2115
2116 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002117};
2118
2119/**
rginda9ea433c2012-03-16 11:57:00 -07002120 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002121 *
rginda9ea433c2012-03-16 11:57:00 -07002122 * This does not respect the scroll region.
2123 *
2124 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2125 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002126 */
rginda9ea433c2012-03-16 11:57:00 -07002127hterm.Terminal.prototype.clearHome = function(opt_screen) {
2128 var screen = opt_screen || this.screen_;
2129 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002130
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002131 this.accessibilityReader_.clear();
2132
rginda11057d52012-04-25 12:29:56 -07002133 if (bottom == 0) {
2134 // Empty screen, nothing to do.
2135 return;
2136 }
2137
rgindae4d29232012-01-19 10:47:13 -08002138 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002139 screen.setCursorPosition(i, 0);
2140 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002141 }
2142
rginda9ea433c2012-03-16 11:57:00 -07002143 screen.setCursorPosition(0, 0);
2144};
2145
2146/**
2147 * Erase the entire display without changing the cursor position.
2148 *
2149 * The cursor position is unchanged. This does not respect the scroll
2150 * region.
2151 *
2152 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2153 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002154 */
2155hterm.Terminal.prototype.clear = function(opt_screen) {
2156 var screen = opt_screen || this.screen_;
2157 var cursor = screen.cursorPosition.clone();
2158 this.clearHome(screen);
2159 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002160};
2161
2162/**
2163 * VT command to insert lines at the current cursor row.
2164 *
2165 * This respects the current scroll region. Rows pushed off the bottom are
2166 * lost (they won't show up in the scrollback buffer).
2167 *
rginda8ba33642011-12-14 12:31:31 -08002168 * @param {integer} count The number of lines to insert.
2169 */
2170hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002171 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002172
2173 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002174 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002175
Robert Ginda579186b2012-09-26 11:40:04 -07002176 // The moveCount is the number of rows we need to relocate to make room for
2177 // the new row(s). The count is the distance to move them.
2178 var moveCount = bottom - cursorRow - count + 1;
2179 if (moveCount)
2180 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002181
Robert Ginda579186b2012-09-26 11:40:04 -07002182 for (var i = count - 1; i >= 0; i--) {
2183 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002184 this.screen_.clearCursorRow();
2185 }
rginda8ba33642011-12-14 12:31:31 -08002186};
2187
2188/**
2189 * VT command to delete lines at the current cursor row.
2190 *
2191 * New rows are added to the bottom of scroll region to take their place. New
2192 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002193 *
2194 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002195 */
2196hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002197 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002198
rginda87b86462011-12-14 13:48:03 -08002199 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002200 var bottom = this.getVTScrollBottom();
2201
rginda87b86462011-12-14 13:48:03 -08002202 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002203 count = Math.min(count, maxCount);
2204
rginda87b86462011-12-14 13:48:03 -08002205 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002206 if (count != maxCount)
2207 this.moveRows_(top, count, moveStart);
2208
2209 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002210 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002211 this.screen_.clearCursorRow();
2212 }
2213
rginda87b86462011-12-14 13:48:03 -08002214 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002215 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002216};
2217
2218/**
2219 * Inserts the given number of spaces at the current cursor position.
2220 *
rginda87b86462011-12-14 13:48:03 -08002221 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002222 *
2223 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002224 */
2225hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002226 var cursor = this.saveCursor();
2227
rgindacbbd7482012-06-13 15:06:16 -07002228 var ws = lib.f.getWhitespace(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002229 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002230 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002231
2232 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002233 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002234};
2235
2236/**
2237 * Forward-delete the specified number of characters starting at the cursor
2238 * position.
2239 *
2240 * @param {integer} count The number of characters to delete.
2241 */
2242hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002243 var deleted = this.screen_.deleteChars(count);
2244 if (deleted && !this.screen_.textAttributes.isDefault()) {
2245 var cursor = this.saveCursor();
2246 this.setCursorColumn(this.screenSize.width - deleted);
2247 this.screen_.insertString(lib.f.getWhitespace(deleted));
2248 this.restoreCursor(cursor);
2249 }
2250
David Benjamin54e8bf62012-06-01 22:31:40 -04002251 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002252};
2253
2254/**
2255 * Shift rows in the scroll region upwards by a given number of lines.
2256 *
2257 * New rows are inserted at the bottom of the scroll region to fill the
2258 * vacated rows. The new rows not filled out with the current text attributes.
2259 *
2260 * This function does not affect the scrollback rows at all. Rows shifted
2261 * off the top are lost.
2262 *
rginda87b86462011-12-14 13:48:03 -08002263 * The cursor position is not altered.
2264 *
rginda8ba33642011-12-14 12:31:31 -08002265 * @param {integer} count The number of rows to scroll.
2266 */
2267hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002268 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002269
rginda87b86462011-12-14 13:48:03 -08002270 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002271 this.deleteLines(count);
2272
rginda87b86462011-12-14 13:48:03 -08002273 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002274};
2275
2276/**
2277 * Shift rows below the cursor down by a given number of lines.
2278 *
2279 * This function respects the current scroll region.
2280 *
2281 * New rows are inserted at the top of the scroll region to fill the
2282 * vacated rows. The new rows not filled out with the current text attributes.
2283 *
2284 * This function does not affect the scrollback rows at all. Rows shifted
2285 * off the bottom are lost.
2286 *
2287 * @param {integer} count The number of rows to scroll.
2288 */
2289hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08002290 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002291
rginda87b86462011-12-14 13:48:03 -08002292 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08002293 this.insertLines(opt_count);
2294
rginda87b86462011-12-14 13:48:03 -08002295 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002296};
2297
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002298/**
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002299 * Enable accessibility-friendly features that have a performance impact.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002300 *
2301 * This will generate additional DOM nodes in an aria-live region that will
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002302 * cause Assitive Technology to announce the output of the terminal. It also
2303 * enables other features that aid assistive technology. All the features gated
2304 * behind this flag have a performance impact on the terminal which is why they
2305 * are made optional.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002306 *
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002307 * @param {boolean} enabled Whether to enable accessibility-friendly features.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002308 */
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002309hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) {
Raymes Khoury177aec72018-06-26 10:58:53 +10002310 this.accessibilityReader_.setAccessibilityEnabled(enabled);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002311};
rginda87b86462011-12-14 13:48:03 -08002312
rginda8ba33642011-12-14 12:31:31 -08002313/**
2314 * Set the cursor position.
2315 *
2316 * The cursor row is relative to the scroll region if the terminal has
2317 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2318 *
2319 * @param {integer} row The new zero-based cursor row.
2320 * @param {integer} row The new zero-based cursor column.
2321 */
2322hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2323 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002324 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002325 } else {
rginda87b86462011-12-14 13:48:03 -08002326 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002327 }
rginda87b86462011-12-14 13:48:03 -08002328};
rginda8ba33642011-12-14 12:31:31 -08002329
Evan Jones2600d4f2016-12-06 09:29:36 -05002330/**
2331 * Move the cursor relative to its current position.
2332 *
2333 * @param {number} row
2334 * @param {number} column
2335 */
rginda87b86462011-12-14 13:48:03 -08002336hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2337 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002338 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2339 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002340 this.screen_.setCursorPosition(row, column);
2341};
2342
Evan Jones2600d4f2016-12-06 09:29:36 -05002343/**
2344 * Move the cursor to the specified position.
2345 *
2346 * @param {number} row
2347 * @param {number} column
2348 */
rginda87b86462011-12-14 13:48:03 -08002349hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002350 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2351 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002352 this.screen_.setCursorPosition(row, column);
2353};
2354
2355/**
2356 * Set the cursor column.
2357 *
2358 * @param {integer} column The new zero-based cursor column.
2359 */
2360hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002361 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002362};
2363
2364/**
2365 * Return the cursor column.
2366 *
2367 * @return {integer} The zero-based cursor column.
2368 */
2369hterm.Terminal.prototype.getCursorColumn = function() {
2370 return this.screen_.cursorPosition.column;
2371};
2372
2373/**
2374 * Set the cursor row.
2375 *
2376 * The cursor row is relative to the scroll region if the terminal has
2377 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2378 *
2379 * @param {integer} row The new cursor row.
2380 */
rginda87b86462011-12-14 13:48:03 -08002381hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2382 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002383};
2384
2385/**
2386 * Return the cursor row.
2387 *
2388 * @return {integer} The zero-based cursor row.
2389 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002390hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002391 return this.screen_.cursorPosition.row;
2392};
2393
2394/**
2395 * Request that the ScrollPort redraw itself soon.
2396 *
2397 * The redraw will happen asynchronously, soon after the call stack winds down.
2398 * Multiple calls will be coalesced into a single redraw.
2399 */
2400hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002401 if (this.timeouts_.redraw)
2402 return;
rginda8ba33642011-12-14 12:31:31 -08002403
2404 var self = this;
rginda87b86462011-12-14 13:48:03 -08002405 this.timeouts_.redraw = setTimeout(function() {
2406 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002407 self.scrollPort_.redraw_();
2408 }, 0);
2409};
2410
2411/**
2412 * Request that the ScrollPort be scrolled to the bottom.
2413 *
2414 * The scroll will happen asynchronously, soon after the call stack winds down.
2415 * Multiple calls will be coalesced into a single scroll.
2416 *
2417 * This affects the scrollbar position of the ScrollPort, and has nothing to
2418 * do with the VT scroll commands.
2419 */
2420hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2421 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002422 return;
rginda8ba33642011-12-14 12:31:31 -08002423
2424 var self = this;
2425 this.timeouts_.scrollDown = setTimeout(function() {
2426 delete self.timeouts_.scrollDown;
2427 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2428 }, 10);
2429};
2430
2431/**
2432 * Move the cursor up a specified number of rows.
2433 *
2434 * @param {integer} count The number of rows to move the cursor.
2435 */
2436hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002437 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002438};
2439
2440/**
2441 * Move the cursor down a specified number of rows.
2442 *
2443 * @param {integer} count The number of rows to move the cursor.
2444 */
2445hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002446 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002447 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2448 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2449 this.screenSize.height - 1);
2450
rgindacbbd7482012-06-13 15:06:16 -07002451 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002452 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002453 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002454};
2455
2456/**
2457 * Move the cursor left a specified number of columns.
2458 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002459 * If reverse wraparound mode is enabled and the previous row wrapped into
2460 * the current row then we back up through the wraparound as well.
2461 *
rginda8ba33642011-12-14 12:31:31 -08002462 * @param {integer} count The number of columns to move the cursor.
2463 */
2464hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002465 count = count || 1;
2466
2467 if (count < 1)
2468 return;
2469
2470 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002471 if (this.options_.reverseWraparound) {
2472 if (this.screen_.cursorPosition.overflow) {
2473 // If this cursor is in the right margin, consume one count to get it
2474 // back to the last column. This only applies when we're in reverse
2475 // wraparound mode.
2476 count--;
2477 this.clearCursorOverflow();
2478
2479 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002480 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002481 }
2482
Robert Gindabfb32622014-07-17 13:20:27 -07002483 var newRow = this.screen_.cursorPosition.row;
2484 var newColumn = currentColumn - count;
2485 if (newColumn < 0) {
2486 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2487 if (newRow < 0) {
2488 // xterm also wraps from row 0 to the last row.
2489 newRow = this.screenSize.height + newRow % this.screenSize.height;
2490 }
2491 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2492 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002493
Robert Gindabfb32622014-07-17 13:20:27 -07002494 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2495
2496 } else {
2497 var newColumn = Math.max(currentColumn - count, 0);
2498 this.setCursorColumn(newColumn);
2499 }
rginda8ba33642011-12-14 12:31:31 -08002500};
2501
2502/**
2503 * Move the cursor right a specified number of columns.
2504 *
2505 * @param {integer} count The number of columns to move the cursor.
2506 */
2507hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002508 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002509
2510 if (count < 1)
2511 return;
2512
rgindacbbd7482012-06-13 15:06:16 -07002513 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002514 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002515 this.setCursorColumn(column);
2516};
2517
2518/**
2519 * Reverse the foreground and background colors of the terminal.
2520 *
2521 * This only affects text that was drawn with no attributes.
2522 *
2523 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2524 * been drawn with attributes that happen to coincide with the default
2525 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002526 *
2527 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002528 */
2529hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002530 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002531 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002532 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2533 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002534 } else {
rginda9f5222b2012-03-05 11:53:28 -08002535 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2536 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002537 }
2538};
2539
2540/**
rginda87b86462011-12-14 13:48:03 -08002541 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002542 *
2543 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002544 */
2545hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002546 this.cursorNode_.style.backgroundColor =
2547 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002548
2549 var self = this;
2550 setTimeout(function() {
Matheus Fernandes2d733082017-09-11 06:43:01 -04002551 self.restyleCursor_();
rginda6d397402012-01-17 10:58:29 -08002552 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002553
Michael Kelly485ecd12014-06-09 11:41:56 -04002554 // bellSquelchTimeout_ affects both audio and notification bells.
2555 if (this.bellSquelchTimeout_)
2556 return;
2557
Robert Ginda92e18102013-03-14 13:56:37 -07002558 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002559 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002560 this.bellSequelchTimeout_ = setTimeout(function() {
2561 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002562 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002563 } else {
2564 delete this.bellSquelchTimeout_;
2565 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002566
2567 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002568 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002569 this.bellNotificationList_.push(n);
2570 // TODO: Should we try to raise the window here?
2571 n.onclick = function() { self.closeBellNotifications_(); };
2572 }
rginda87b86462011-12-14 13:48:03 -08002573};
2574
2575/**
rginda8ba33642011-12-14 12:31:31 -08002576 * Set the origin mode bit.
2577 *
2578 * If origin mode is on, certain VT cursor and scrolling commands measure their
2579 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2580 * to the top of the addressable screen.
2581 *
2582 * Defaults to off.
2583 *
2584 * @param {boolean} state True to set origin mode, false to unset.
2585 */
2586hterm.Terminal.prototype.setOriginMode = function(state) {
2587 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002588 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002589};
2590
2591/**
2592 * Set the insert mode bit.
2593 *
2594 * If insert mode is on, existing text beyond the cursor position will be
2595 * shifted right to make room for new text. Otherwise, new text overwrites
2596 * any existing text.
2597 *
2598 * Defaults to off.
2599 *
2600 * @param {boolean} state True to set insert mode, false to unset.
2601 */
2602hterm.Terminal.prototype.setInsertMode = function(state) {
2603 this.options_.insertMode = state;
2604};
2605
2606/**
rginda87b86462011-12-14 13:48:03 -08002607 * Set the auto carriage return bit.
2608 *
2609 * If auto carriage return is on then a formfeed character is interpreted
2610 * as a newline, otherwise it's the same as a linefeed. The difference boils
2611 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002612 *
2613 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002614 */
2615hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2616 this.options_.autoCarriageReturn = state;
2617};
2618
2619/**
rginda8ba33642011-12-14 12:31:31 -08002620 * Set the wraparound mode bit.
2621 *
2622 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2623 * to the start of the following row. Otherwise, the cursor is clamped to the
2624 * end of the screen and attempts to write past it are ignored.
2625 *
2626 * Defaults to on.
2627 *
2628 * @param {boolean} state True to set wraparound mode, false to unset.
2629 */
2630hterm.Terminal.prototype.setWraparound = function(state) {
2631 this.options_.wraparound = state;
2632};
2633
2634/**
2635 * Set the reverse-wraparound mode bit.
2636 *
2637 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2638 * to the end of the previous row. Otherwise, the cursor is clamped to column
2639 * 0.
2640 *
2641 * Defaults to off.
2642 *
2643 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2644 */
2645hterm.Terminal.prototype.setReverseWraparound = function(state) {
2646 this.options_.reverseWraparound = state;
2647};
2648
2649/**
2650 * Selects between the primary and alternate screens.
2651 *
2652 * If alternate mode is on, the alternate screen is active. Otherwise the
2653 * primary screen is active.
2654 *
2655 * Swapping screens has no effect on the scrollback buffer.
2656 *
2657 * Each screen maintains its own cursor position.
2658 *
2659 * Defaults to off.
2660 *
2661 * @param {boolean} state True to set alternate mode, false to unset.
2662 */
2663hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002664 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002665 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2666
rginda35c456b2012-02-09 17:29:05 -08002667 if (this.screen_.rowsArray.length &&
2668 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2669 // If the screen changed sizes while we were away, our rowIndexes may
2670 // be incorrect.
2671 var offset = this.scrollbackRows_.length;
2672 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002673 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002674 ary[i].rowIndex = offset + i;
2675 }
2676 }
rginda8ba33642011-12-14 12:31:31 -08002677
rginda35c456b2012-02-09 17:29:05 -08002678 this.realizeWidth_(this.screenSize.width);
2679 this.realizeHeight_(this.screenSize.height);
2680 this.scrollPort_.syncScrollHeight();
2681 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002682
rginda6d397402012-01-17 10:58:29 -08002683 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002684 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002685};
2686
2687/**
2688 * Set the cursor-blink mode bit.
2689 *
2690 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2691 * a visible cursor does not blink.
2692 *
2693 * You should make sure to turn blinking off if you're going to dispose of a
2694 * terminal, otherwise you'll leak a timeout.
2695 *
2696 * Defaults to on.
2697 *
2698 * @param {boolean} state True to set cursor-blink mode, false to unset.
2699 */
2700hterm.Terminal.prototype.setCursorBlink = function(state) {
2701 this.options_.cursorBlink = state;
2702
2703 if (!state && this.timeouts_.cursorBlink) {
2704 clearTimeout(this.timeouts_.cursorBlink);
2705 delete this.timeouts_.cursorBlink;
2706 }
2707
2708 if (this.options_.cursorVisible)
2709 this.setCursorVisible(true);
2710};
2711
2712/**
2713 * Set the cursor-visible mode bit.
2714 *
2715 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2716 *
2717 * Defaults to on.
2718 *
2719 * @param {boolean} state True to set cursor-visible mode, false to unset.
2720 */
2721hterm.Terminal.prototype.setCursorVisible = function(state) {
2722 this.options_.cursorVisible = state;
2723
2724 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002725 if (this.timeouts_.cursorBlink) {
2726 clearTimeout(this.timeouts_.cursorBlink);
2727 delete this.timeouts_.cursorBlink;
2728 }
rginda87b86462011-12-14 13:48:03 -08002729 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002730 return;
2731 }
2732
rginda87b86462011-12-14 13:48:03 -08002733 this.syncCursorPosition_();
2734
2735 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002736
2737 if (this.options_.cursorBlink) {
2738 if (this.timeouts_.cursorBlink)
2739 return;
2740
Robert Gindaea2183e2014-07-17 09:51:51 -07002741 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002742 } else {
2743 if (this.timeouts_.cursorBlink) {
2744 clearTimeout(this.timeouts_.cursorBlink);
2745 delete this.timeouts_.cursorBlink;
2746 }
2747 }
2748};
2749
2750/**
rginda87b86462011-12-14 13:48:03 -08002751 * Synchronizes the visible cursor and document selection with the current
2752 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002753 */
2754hterm.Terminal.prototype.syncCursorPosition_ = function() {
2755 var topRowIndex = this.scrollPort_.getTopRowIndex();
2756 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2757 var cursorRowIndex = this.scrollbackRows_.length +
2758 this.screen_.cursorPosition.row;
2759
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002760 if (this.accessibilityReader_.accessibilityEnabled) {
2761 // Report the new position of the cursor for accessibility purposes.
2762 const cursorColumnIndex = this.screen_.cursorPosition.column;
2763 const cursorLineText =
2764 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
2765 this.accessibilityReader_.afterCursorChange(
2766 cursorLineText, cursorRowIndex, cursorColumnIndex);
2767 }
2768
rginda8ba33642011-12-14 12:31:31 -08002769 if (cursorRowIndex > bottomRowIndex) {
2770 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04002771 this.setCssVar('cursor-offset-row', '-1');
rginda8ba33642011-12-14 12:31:31 -08002772 return;
2773 }
2774
Robert Gindab837c052014-08-11 11:17:51 -07002775 if (this.options_.cursorVisible &&
2776 this.cursorNode_.style.display == 'none') {
2777 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2778 this.cursorNode_.style.display = '';
2779 }
2780
Mike Frysinger44c32202017-08-05 01:13:09 -04002781 // Position the cursor using CSS variable math. If we do the math in JS,
2782 // the float math will end up being more precise than the CSS which will
2783 // cause the cursor tracking to be off.
2784 this.setCssVar(
2785 'cursor-offset-row',
2786 `${cursorRowIndex - topRowIndex} + ` +
2787 `${this.scrollPort_.visibleRowTopMargin}px`);
2788 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08002789
2790 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04002791 '(' + this.screen_.cursorPosition.column +
2792 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08002793 ')');
2794
2795 // Update the caret for a11y purposes.
2796 var selection = this.document_.getSelection();
2797 if (selection && selection.isCollapsed)
2798 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002799};
2800
Robert Gindafb1be6a2013-12-11 11:56:22 -08002801/**
2802 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2803 * and character cell dimensions.
2804 */
Robert Ginda830583c2013-08-07 13:20:46 -07002805hterm.Terminal.prototype.restyleCursor_ = function() {
2806 var shape = this.cursorShape_;
2807
2808 if (this.cursorNode_.getAttribute('focus') == 'false') {
2809 // Always show a block cursor when unfocused.
2810 shape = hterm.Terminal.cursorShape.BLOCK;
2811 }
2812
2813 var style = this.cursorNode_.style;
2814
2815 switch (shape) {
2816 case hterm.Terminal.cursorShape.BEAM:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002817 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002818 style.backgroundColor = 'transparent';
2819 style.borderBottomStyle = null;
2820 style.borderLeftStyle = 'solid';
2821 break;
2822
2823 case hterm.Terminal.cursorShape.UNDERLINE:
2824 style.height = this.scrollPort_.characterSize.baseline + 'px';
2825 style.backgroundColor = 'transparent';
2826 style.borderBottomStyle = 'solid';
2827 // correct the size to put it exactly at the baseline
2828 style.borderLeftStyle = null;
2829 break;
2830
2831 default:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002832 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002833 style.backgroundColor = this.cursorColor_;
2834 style.borderBottomStyle = null;
2835 style.borderLeftStyle = null;
2836 break;
2837 }
2838};
2839
rginda8ba33642011-12-14 12:31:31 -08002840/**
2841 * Synchronizes the visible cursor with the current cursor coordinates.
2842 *
2843 * The sync will happen asynchronously, soon after the call stack winds down.
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002844 * Multiple calls will be coalesced into a single sync. This should be called
2845 * prior to the cursor actually changing position.
rginda8ba33642011-12-14 12:31:31 -08002846 */
2847hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2848 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002849 return;
rginda8ba33642011-12-14 12:31:31 -08002850
Raymes Khouryb199d4d2018-07-12 15:08:12 +10002851 if (this.accessibilityReader_.accessibilityEnabled) {
2852 // Report the previous position of the cursor for accessibility purposes.
2853 const cursorRowIndex = this.scrollbackRows_.length +
2854 this.screen_.cursorPosition.row;
2855 const cursorColumnIndex = this.screen_.cursorPosition.column;
2856 const cursorLineText =
2857 this.screen_.rowsArray[this.screen_.cursorPosition.row].innerText;
2858 this.accessibilityReader_.beforeCursorChange(
2859 cursorLineText, cursorRowIndex, cursorColumnIndex);
2860 }
2861
rginda8ba33642011-12-14 12:31:31 -08002862 var self = this;
2863 this.timeouts_.syncCursor = setTimeout(function() {
2864 self.syncCursorPosition_();
2865 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002866 }, 0);
2867};
2868
rgindacc2996c2012-02-24 14:59:31 -08002869/**
rgindaf522ce02012-04-17 17:49:17 -07002870 * Show or hide the zoom warning.
2871 *
2872 * The zoom warning is a message warning the user that their browser zoom must
2873 * be set to 100% in order for hterm to function properly.
2874 *
2875 * @param {boolean} state True to show the message, false to hide it.
2876 */
2877hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2878 if (!this.zoomWarningNode_) {
2879 if (!state)
2880 return;
2881
2882 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002883 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07002884 this.zoomWarningNode_.style.cssText = (
2885 'color: black;' +
2886 'background-color: #ff2222;' +
2887 'font-size: large;' +
2888 'border-radius: 8px;' +
2889 'opacity: 0.75;' +
2890 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2891 'top: 0.5em;' +
2892 'right: 1.2em;' +
2893 'position: absolute;' +
2894 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002895 '-webkit-user-select: none;' +
2896 '-moz-text-size-adjust: none;' +
2897 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002898
2899 this.zoomWarningNode_.addEventListener('click', function(e) {
2900 this.parentNode.removeChild(this);
2901 });
rgindaf522ce02012-04-17 17:49:17 -07002902 }
2903
Robert Gindab4839c22013-02-28 16:52:10 -08002904 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2905 hterm.zoomWarningMessage,
2906 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2907
rgindaf522ce02012-04-17 17:49:17 -07002908 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2909
2910 if (state) {
2911 if (!this.zoomWarningNode_.parentNode)
2912 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2913 } else if (this.zoomWarningNode_.parentNode) {
2914 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2915 }
2916};
2917
2918/**
rgindacc2996c2012-02-24 14:59:31 -08002919 * Show the terminal overlay for a given amount of time.
2920 *
2921 * The terminal overlay appears in inverse video in a large font, centered
2922 * over the terminal. You should probably keep the overlay message brief,
2923 * since it's in a large font and you probably aren't going to check the size
2924 * of the terminal first.
2925 *
2926 * @param {string} msg The text (not HTML) message to display in the overlay.
2927 * @param {number} opt_timeout The amount of time to wait before fading out
2928 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2929 * stay up forever (or until the next overlay).
2930 */
2931hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002932 if (!this.overlayNode_) {
2933 if (!this.div_)
2934 return;
2935
2936 this.overlayNode_ = this.document_.createElement('div');
2937 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002938 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002939 'font-size: xx-large;' +
2940 'opacity: 0.75;' +
2941 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2942 'position: absolute;' +
2943 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002944 '-webkit-transition: opacity 180ms ease-in;' +
2945 '-moz-user-select: none;' +
2946 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002947
2948 this.overlayNode_.addEventListener('mousedown', function(e) {
2949 e.preventDefault();
2950 e.stopPropagation();
2951 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002952 }
2953
rginda9f5222b2012-03-05 11:53:28 -08002954 this.overlayNode_.style.color = this.prefs_.get('background-color');
2955 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2956 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2957
rgindaf0090c92012-02-10 14:58:52 -08002958 this.overlayNode_.textContent = msg;
2959 this.overlayNode_.style.opacity = '0.75';
2960
2961 if (!this.overlayNode_.parentNode)
2962 this.div_.appendChild(this.overlayNode_);
2963
Robert Ginda97769282013-02-01 15:30:30 -08002964 var divSize = hterm.getClientSize(this.div_);
2965 var overlaySize = hterm.getClientSize(this.overlayNode_);
2966
Robert Ginda8a59f762014-07-23 11:29:55 -07002967 this.overlayNode_.style.top =
2968 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08002969 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07002970 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08002971
rgindaf0090c92012-02-10 14:58:52 -08002972 if (this.overlayTimeout_)
2973 clearTimeout(this.overlayTimeout_);
2974
Raymes Khouryc7a06382018-07-04 10:25:45 +10002975 this.accessibilityReader_.assertiveAnnounce(msg);
2976
rgindacc2996c2012-02-24 14:59:31 -08002977 if (opt_timeout === null)
2978 return;
2979
Mike Frysingerb6cfded2017-09-18 00:39:31 -04002980 this.overlayTimeout_ = setTimeout(() => {
2981 this.overlayNode_.style.opacity = '0';
2982 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
2983 }, opt_timeout || 1500);
2984};
2985
2986/**
2987 * Hide the terminal overlay immediately.
2988 *
2989 * Useful when we show an overlay for an event with an unknown end time.
2990 */
2991hterm.Terminal.prototype.hideOverlay = function() {
2992 if (this.overlayTimeout_)
2993 clearTimeout(this.overlayTimeout_);
2994 this.overlayTimeout_ = null;
2995
2996 if (this.overlayNode_.parentNode)
2997 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
2998 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08002999};
3000
rginda4bba5e12012-06-20 16:15:30 -07003001/**
3002 * Paste from the system clipboard to the terminal.
3003 */
3004hterm.Terminal.prototype.paste = function() {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003005 return hterm.pasteFromClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07003006};
3007
3008/**
3009 * Copy a string to the system clipboard.
3010 *
3011 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05003012 *
3013 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07003014 */
3015hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08003016 if (this.prefs_.get('enable-clipboard-notice'))
3017 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
3018
rgindaa09e7332012-08-17 12:49:51 -07003019 var copySource = this.document_.createElement('pre');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04003020 copySource.id = 'hterm:copy-to-clipboard-source';
rginda4bba5e12012-06-20 16:15:30 -07003021 copySource.textContent = str;
3022 copySource.style.cssText = (
3023 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07003024 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07003025 'position: absolute;' +
3026 'top: -99px');
3027
3028 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07003029
rginda4bba5e12012-06-20 16:15:30 -07003030 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07003031 var anchorNode = selection.anchorNode;
3032 var anchorOffset = selection.anchorOffset;
3033 var focusNode = selection.focusNode;
3034 var focusOffset = selection.focusOffset;
3035
rginda4bba5e12012-06-20 16:15:30 -07003036 selection.selectAllChildren(copySource);
3037
rgindaa09e7332012-08-17 12:49:51 -07003038 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07003039
Rob Spies56953412014-04-28 14:09:47 -07003040 // IE doesn't support selection.extend. This means that the selection
3041 // won't return on IE.
Robert Ginda6f4f0aa2014-07-28 10:21:39 -07003042 if (selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07003043 selection.collapse(anchorNode, anchorOffset);
3044 selection.extend(focusNode, focusOffset);
3045 }
rgindafaa74742012-08-21 13:34:03 -07003046
rginda4bba5e12012-06-20 16:15:30 -07003047 copySource.parentNode.removeChild(copySource);
3048};
3049
Evan Jones2600d4f2016-12-06 09:29:36 -05003050/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003051 * Display an image.
3052 *
3053 * @param {Object} options The image to display.
3054 * @param {string=} options.name A human readable string for the image.
3055 * @param {string|number=} options.size The size (in bytes).
3056 * @param {boolean=} options.preserveAspectRatio Whether to preserve aspect.
3057 * @param {boolean=} options.inline Whether to display the image inline.
3058 * @param {string|number=} options.width The width of the image.
3059 * @param {string|number=} options.height The height of the image.
3060 * @param {string=} options.align Direction to align the image.
3061 * @param {string} options.uri The source URI for the image.
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003062 * @param {function=} onLoad Callback when loading finishes.
3063 * @param {function(Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003064 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003065hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003066 // Make sure we're actually given a resource to display.
3067 if (options.uri === undefined)
3068 return;
3069
3070 // Set up the defaults to simplify code below.
3071 if (!options.name)
3072 options.name = '';
3073
3074 // Has the user approved image display yet?
3075 if (this.allowImagesInline !== true) {
3076 this.newLine();
3077 const row = this.getRowNode(this.scrollbackRows_.length +
3078 this.getCursorRow() - 1);
3079
3080 if (this.allowImagesInline === false) {
3081 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3082 'Inline Images Disabled');
3083 return;
3084 }
3085
3086 // Show a prompt.
3087 let button;
3088 const span = this.document_.createElement('span');
3089 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3090 span.style.fontWeight = 'bold';
3091 span.style.borderWidth = '1px';
3092 span.style.borderStyle = 'dashed';
3093 button = this.document_.createElement('span');
3094 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3095 button.style.marginLeft = '1em';
3096 button.style.borderWidth = '1px';
3097 button.style.borderStyle = 'solid';
3098 button.addEventListener('click', () => {
3099 this.prefs_.set('allow-images-inline', false);
3100 });
3101 span.appendChild(button);
3102 button = this.document_.createElement('span');
3103 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3104 'allow this session');
3105 button.style.marginLeft = '1em';
3106 button.style.borderWidth = '1px';
3107 button.style.borderStyle = 'solid';
3108 button.addEventListener('click', () => {
3109 this.allowImagesInline = true;
3110 });
3111 span.appendChild(button);
3112 button = this.document_.createElement('span');
3113 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3114 button.style.marginLeft = '1em';
3115 button.style.borderWidth = '1px';
3116 button.style.borderStyle = 'solid';
3117 button.addEventListener('click', () => {
3118 this.prefs_.set('allow-images-inline', true);
3119 });
3120 span.appendChild(button);
3121
3122 row.appendChild(span);
3123 return;
3124 }
3125
3126 // See if we should show this object directly, or download it.
3127 if (options.inline) {
3128 const io = this.io.push();
3129 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
3130 'Loading $1 ...'), null);
3131
3132 // While we're loading the image, eat all the user's input.
3133 io.onVTKeystroke = io.sendString = () => {};
3134
3135 // Initialize this new image.
3136 const img = this.document_.createElement('img');
3137 img.src = options.uri;
3138 img.title = img.alt = options.name;
3139
3140 // Attach the image to the page to let it load/render. It won't stay here.
3141 // This is needed so it's visible and the DOM can calculate the height. If
3142 // the image is hidden or not in the DOM, the height is always 0.
3143 this.document_.body.appendChild(img);
3144
3145 // Wait for the image to finish loading before we try moving it to the
3146 // right place in the terminal.
3147 img.onload = () => {
3148 // Now that we have the image dimensions, figure out how to show it.
3149 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
3150 img.style.maxWidth = `${this.document_.body.clientWidth}px`;
3151 img.style.maxHeight = `${this.document_.body.clientHeight}px`;
3152
3153 // Parse a width/height specification.
3154 const parseDim = (dim, maxDim, cssVar) => {
3155 if (!dim || dim == 'auto')
3156 return '';
3157
3158 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3159 if (ary) {
3160 if (ary[2] == '%')
3161 return maxDim * parseInt(ary[1]) / 100 + 'px';
3162 else if (ary[2] == 'px')
3163 return dim;
3164 else
3165 return `calc(${dim} * var(${cssVar}))`;
3166 }
3167
3168 return '';
3169 };
3170 img.style.width =
3171 parseDim(options.width, this.document_.body.clientWidth,
3172 '--hterm-charsize-width');
3173 img.style.height =
3174 parseDim(options.height, this.document_.body.clientHeight,
3175 '--hterm-charsize-height');
3176
3177 // Figure out how many rows the image occupies, then add that many.
3178 // XXX: This count will be inaccurate if the font size changes on us.
3179 const padRows = Math.ceil(img.clientHeight /
3180 this.scrollPort_.characterSize.height);
3181 for (let i = 0; i < padRows; ++i)
3182 this.newLine();
3183
3184 // Update the max height in case the user shrinks the character size.
3185 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3186
3187 // Move the image to the last row. This way when we scroll up, it doesn't
3188 // disappear when the first row gets clipped. It will disappear when we
3189 // scroll down and the last row is clipped ...
3190 this.document_.body.removeChild(img);
3191 // Create a wrapper node so we can do an absolute in a relative position.
3192 // This helps with rounding errors between JS & CSS counts.
3193 const div = this.document_.createElement('div');
3194 div.style.position = 'relative';
3195 div.style.textAlign = options.align;
3196 img.style.position = 'absolute';
3197 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3198 div.appendChild(img);
3199 const row = this.getRowNode(this.scrollbackRows_.length +
3200 this.getCursorRow() - 1);
3201 row.appendChild(div);
3202
3203 io.hideOverlay();
3204 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003205
3206 if (onLoad)
3207 onLoad();
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003208 };
3209
3210 // If we got a malformed image, give up.
3211 img.onerror = (e) => {
3212 this.document_.body.removeChild(img);
3213 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
Mike Frysingere14a8c42018-03-10 00:17:30 -08003214 'Loading $1 failed'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003215 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003216
3217 if (onError)
3218 onError(e);
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003219 };
3220 } else {
3221 // We can't use chrome.downloads.download as that requires "downloads"
3222 // permissions, and that works only in extensions, not apps.
3223 const a = this.document_.createElement('a');
3224 a.href = options.uri;
3225 a.download = options.name;
3226 this.document_.body.appendChild(a);
3227 a.click();
3228 a.remove();
3229 }
3230};
3231
3232/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003233 * Returns the selected text, or null if no text is selected.
3234 *
3235 * @return {string|null}
3236 */
rgindaa09e7332012-08-17 12:49:51 -07003237hterm.Terminal.prototype.getSelectionText = function() {
3238 var selection = this.scrollPort_.selection;
3239 selection.sync();
3240
3241 if (selection.isCollapsed)
3242 return null;
3243
rgindaa09e7332012-08-17 12:49:51 -07003244 // Start offset measures from the beginning of the line.
3245 var startOffset = selection.startOffset;
3246 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003247
Raymes Khoury334625a2018-06-25 10:29:40 +10003248 // If an x-row isn't selected, |node| will be null.
3249 if (!node)
3250 return null;
3251
Robert Gindafdbb3f22012-09-06 20:23:06 -07003252 if (node.nodeName != 'X-ROW') {
3253 // If the selection doesn't start on an x-row node, then it must be
3254 // somewhere inside the x-row. Add any characters from previous siblings
3255 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003256
3257 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3258 // If node is the text node in a styled span, move up to the span node.
3259 node = node.parentNode;
3260 }
3261
Robert Gindafdbb3f22012-09-06 20:23:06 -07003262 while (node.previousSibling) {
3263 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003264 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003265 }
rgindaa09e7332012-08-17 12:49:51 -07003266 }
3267
3268 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08003269 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
3270 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003271 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003272
Robert Gindafdbb3f22012-09-06 20:23:06 -07003273 if (node.nodeName != 'X-ROW') {
3274 // If the selection doesn't end on an x-row node, then it must be
3275 // somewhere inside the x-row. Add any characters from following siblings
3276 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003277
3278 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3279 // If node is the text node in a styled span, move up to the span node.
3280 node = node.parentNode;
3281 }
3282
Robert Gindafdbb3f22012-09-06 20:23:06 -07003283 while (node.nextSibling) {
3284 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003285 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003286 }
rgindaa09e7332012-08-17 12:49:51 -07003287 }
3288
3289 var rv = this.getRowsText(selection.startRow.rowIndex,
3290 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003291 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003292};
3293
rginda4bba5e12012-06-20 16:15:30 -07003294/**
3295 * Copy the current selection to the system clipboard, then clear it after a
3296 * short delay.
3297 */
3298hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07003299 var text = this.getSelectionText();
3300 if (text != null)
3301 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07003302};
3303
rgindaf0090c92012-02-10 14:58:52 -08003304hterm.Terminal.prototype.overlaySize = function() {
3305 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
3306};
3307
rginda87b86462011-12-14 13:48:03 -08003308/**
3309 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3310 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003311 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003312 */
3313hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08003314 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08003315 this.scrollPort_.scrollRowToBottom(this.getRowCount());
3316
Robert Ginda8cb7d902013-06-20 14:37:18 -07003317 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08003318};
3319
3320/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003321 * Open the selected url.
3322 */
3323hterm.Terminal.prototype.openSelectedUrl_ = function() {
3324 var str = this.getSelectionText();
3325
3326 // If there is no selection, try and expand wherever they clicked.
3327 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003328 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003329 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003330
3331 // If clicking in empty space, return.
3332 if (str == null)
3333 return;
Mike Frysinger70b94692017-01-26 18:57:50 -10003334 }
3335
3336 // Make sure URL is valid before opening.
3337 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
3338 return;
Mike Frysinger43472622017-06-26 18:11:07 -04003339
3340 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003341 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003342 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3343 // We have to whitelist a few protocols that lack authorities and thus
3344 // never use the //. Like mailto.
3345 switch (str.split(':', 1)[0]) {
3346 case 'mailto':
3347 break;
3348 default:
3349 str = 'http://' + str;
3350 break;
3351 }
3352 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003353
Mike Frysinger720fa832017-10-23 01:15:52 -04003354 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003355};
Mike Frysinger70b94692017-01-26 18:57:50 -10003356
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003357/**
3358 * Manage the automatic mouse hiding behavior while typing.
3359 *
3360 * @param {boolean=} v Whether to enable automatic hiding.
3361 */
3362hterm.Terminal.prototype.setAutomaticMouseHiding = function(v=null) {
3363 // Since Chrome OS & macOS do this by default everywhere, we don't need to.
3364 // Linux & Windows seem to leave this to specific applications to manage.
3365 if (v === null)
3366 v = (hterm.os != 'cros' && hterm.os != 'mac');
3367
3368 this.mouseHideWhileTyping_ = !!v;
3369};
3370
3371/**
3372 * Handler for monitoring user keyboard activity.
3373 *
3374 * This isn't for processing the keystrokes directly, but for updating any
3375 * state that might toggle based on the user using the keyboard at all.
3376 *
3377 * @param {KeyboardEvent} e The keyboard event that triggered us.
3378 */
3379hterm.Terminal.prototype.onKeyboardActivity_ = function(e) {
3380 // When the user starts typing, hide the mouse cursor.
3381 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_)
3382 this.setCssVar('mouse-cursor-style', 'none');
3383};
Mike Frysinger70b94692017-01-26 18:57:50 -10003384
3385/**
rgindad5613292012-06-19 15:40:37 -07003386 * Add the terminalRow and terminalColumn properties to mouse events and
3387 * then forward on to onMouse().
3388 *
3389 * The terminalRow and terminalColumn properties contain the (row, column)
3390 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003391 *
3392 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003393 */
3394hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003395 if (e.processedByTerminalHandler_) {
3396 // We register our event handlers on the document, as well as the cursor
3397 // and the scroll blocker. Mouse events that occur on the cursor or
3398 // scroll blocker will also appear on the document, but we don't want to
3399 // process them twice.
3400 //
3401 // We can't just prevent bubbling because that has other side effects, so
3402 // we decorate the event object with this property instead.
3403 return;
3404 }
3405
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003406 var reportMouseEvents = (!this.defeatMouseReports_ &&
3407 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3408
rgindafaa74742012-08-21 13:34:03 -07003409 e.processedByTerminalHandler_ = true;
3410
Mike Frysinger02ded6d2018-06-21 14:25:20 -04003411 // Handle auto hiding of mouse cursor while typing.
3412 if (this.mouseHideWhileTyping_ && !this.mouseHideDelay_) {
3413 // Make sure the mouse cursor is visible.
3414 this.syncMouseStyle();
3415 // This debounce isn't perfect, but should work well enough for such a
3416 // simple implementation. If the user moved the mouse, we enabled this
3417 // debounce, and then moved the mouse just before the timeout, we wouldn't
3418 // debounce that later movement.
3419 this.mouseHideDelay_ = setTimeout(() => this.mouseHideDelay_ = null, 1000);
3420 }
3421
Robert Gindaeda48db2014-07-17 09:25:30 -07003422 // One based row/column stored on the mouse event.
3423 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
3424 this.scrollPort_.characterSize.height) + 1;
3425 e.terminalColumn = parseInt(e.clientX /
3426 this.scrollPort_.characterSize.width) + 1;
3427
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003428 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3429 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003430 return;
3431 }
3432
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003433 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003434 // If the cursor is visible and we're not sending mouse events to the
3435 // host app, then we want to hide the terminal cursor when the mouse
3436 // cursor is over top. This keeps the terminal cursor from interfering
3437 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003438 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3439 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3440 this.cursorNode_.style.display = 'none';
3441 } else if (this.cursorNode_.style.display == 'none') {
3442 this.cursorNode_.style.display = '';
3443 }
3444 }
rgindad5613292012-06-19 15:40:37 -07003445
Robert Ginda928cf632014-03-05 15:07:41 -08003446 if (e.type == 'mousedown') {
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003447 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003448 // If VT mouse reporting is disabled, or has been defeated with
3449 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003450 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003451 this.setSelectionEnabled(true);
3452 } else {
3453 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003454 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003455 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003456 this.setSelectionEnabled(false);
3457 e.preventDefault();
3458 }
3459 }
3460
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003461 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003462 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003463 this.screen_.expandSelection(this.document_.getSelection());
John Lin2aad22e2018-03-16 13:58:11 +08003464 if (this.copyOnSelect)
3465 this.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07003466 }
3467
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003468 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003469 // Debounce this event with the dblclick event. If you try to doubleclick
3470 // a URL to open it, Chrome will fire click then dblclick, but we won't
3471 // have expanded the selection text at the first click event.
3472 clearTimeout(this.timeouts_.openUrl);
3473 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3474 500);
3475 return;
3476 }
3477
Mike Frysinger847577f2017-05-23 23:25:57 -04003478 if (e.type == 'mousedown') {
3479 if ((this.mouseRightClickPaste && e.button == 2 /* right button */) ||
Mike Frysinger2edd3612017-05-24 00:54:39 -04003480 e.button == this.mousePasteButton) {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003481 if (!this.paste())
Mike Frysinger05a57f02017-08-27 17:48:55 -04003482 console.warn('Could not paste manually due to web restrictions');
Mike Frysinger847577f2017-05-23 23:25:57 -04003483 }
3484 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003485
Mike Frysinger2edd3612017-05-24 00:54:39 -04003486 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003487 !this.document_.getSelection().isCollapsed) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003488 this.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003489 }
3490
3491 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3492 this.scrollBlockerNode_.engaged) {
3493 // Disengage the scroll-blocker after one of these events.
3494 this.scrollBlockerNode_.engaged = false;
3495 this.scrollBlockerNode_.style.top = '-99px';
3496 }
3497
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003498 // Emulate arrow key presses via scroll wheel events.
3499 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3500 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003501 if (e.type == 'wheel') {
3502 var delta = this.scrollPort_.scrollWheelDelta(e);
3503 var lines = lib.f.smartFloorDivide(
3504 Math.abs(delta), this.scrollPort_.characterSize.height);
3505
3506 var data = '\x1bO' + (delta < 0 ? 'B' : 'A');
3507 this.io.sendString(data.repeat(lines));
3508
3509 e.preventDefault();
3510 }
3511 }
Robert Ginda928cf632014-03-05 15:07:41 -08003512 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003513 if (!this.scrollBlockerNode_.engaged) {
3514 if (e.type == 'mousedown') {
3515 // Move the scroll-blocker into place if we want to keep the scrollport
3516 // from scrolling.
3517 this.scrollBlockerNode_.engaged = true;
3518 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3519 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3520 } else if (e.type == 'mousemove') {
3521 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3522 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003523 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003524 e.preventDefault();
3525 }
3526 }
Robert Ginda928cf632014-03-05 15:07:41 -08003527
3528 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003529 }
3530
Robert Ginda928cf632014-03-05 15:07:41 -08003531 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3532 // Restore this on mouseup in case it was temporarily defeated with a
3533 // alt-mousedown. Only do this when the selection is empty so that
3534 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003535 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003536 }
rgindad5613292012-06-19 15:40:37 -07003537};
3538
3539/**
3540 * Clients should override this if they care to know about mouse events.
3541 *
3542 * The event parameter will be a normal DOM mouse click event with additional
3543 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003544 *
3545 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003546 */
3547hterm.Terminal.prototype.onMouse = function(e) { };
3548
3549/**
rginda8e92a692012-05-20 19:37:20 -07003550 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003551 *
3552 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003553 */
Rob Spies06533ba2014-04-24 11:20:37 -07003554hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3555 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003556 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003557
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003558 if (this.reportFocus)
3559 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003560
Michael Kelly485ecd12014-06-09 11:41:56 -04003561 if (focused === true)
3562 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003563};
3564
3565/**
rginda8ba33642011-12-14 12:31:31 -08003566 * React when the ScrollPort is scrolled.
3567 */
3568hterm.Terminal.prototype.onScroll_ = function() {
3569 this.scheduleSyncCursorPosition_();
3570};
3571
3572/**
rginda9846e2f2012-01-27 13:53:33 -08003573 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003574 *
3575 * @param {Event} e The DOM paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003576 */
3577hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07003578 var data = e.text.replace(/\n/mg, '\r');
Connor Hegartyf525ceb2014-09-03 13:36:03 -07003579 data = this.keyboard.encode(data);
Mike Frysingere8c32c82018-03-11 14:57:28 -07003580 if (this.options_.bracketedPaste) {
3581 // We strip out most escape sequences as they can cause issues (like
3582 // inserting an \x1b[201~ midstream). We pass through whitespace
3583 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
3584 // This matches xterm behavior.
3585 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
3586 data = '\x1b[200~' + filter(data) + '\x1b[201~';
3587 }
Robert Gindaa063b202014-07-21 11:08:25 -07003588
3589 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003590};
3591
3592/**
rgindaa09e7332012-08-17 12:49:51 -07003593 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003594 *
3595 * @param {Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003596 */
3597hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003598 if (!this.useDefaultWindowCopy) {
3599 e.preventDefault();
3600 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3601 }
rgindaa09e7332012-08-17 12:49:51 -07003602};
3603
3604/**
rginda8ba33642011-12-14 12:31:31 -08003605 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003606 *
3607 * Note: This function should not directly contain code that alters the internal
3608 * state of the terminal. That kind of code belongs in realizeWidth or
3609 * realizeHeight, so that it can be executed synchronously in the case of a
3610 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003611 */
3612hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003613 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003614 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003615 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003616 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003617
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003618 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003619 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003620 // gets removed from the document or during the initial load, and we can't
3621 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003622 // This can also happen if called before the scrollPort calculates the
3623 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003624 return;
3625 }
3626
rgindaa8ba17d2012-08-15 14:41:10 -07003627 var isNewSize = (columnCount != this.screenSize.width ||
3628 rowCount != this.screenSize.height);
3629
3630 // We do this even if the size didn't change, just to be sure everything is
3631 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003632 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003633 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003634
3635 if (isNewSize)
3636 this.overlaySize();
3637
Robert Gindafb1be6a2013-12-11 11:56:22 -08003638 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003639 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08003640};
3641
3642/**
3643 * Service the cursor blink timeout.
3644 */
3645hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003646 if (!this.options_.cursorBlink) {
3647 delete this.timeouts_.cursorBlink;
3648 return;
3649 }
3650
Robert Ginda830583c2013-08-07 13:20:46 -07003651 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3652 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003653 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003654 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3655 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003656 } else {
rginda87b86462011-12-14 13:48:03 -08003657 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003658 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3659 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003660 }
3661};
David Reveman8f552492012-03-28 12:18:41 -04003662
3663/**
3664 * Set the scrollbar-visible mode bit.
3665 *
3666 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3667 * Otherwise it will not.
3668 *
3669 * Defaults to on.
3670 *
3671 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3672 */
3673hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3674 this.scrollPort_.setScrollbarVisible(state);
3675};
Michael Kelly485ecd12014-06-09 11:41:56 -04003676
3677/**
Rob Spies49039e52014-12-17 13:40:04 -08003678 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003679 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003680 *
3681 * Defaults to 1.
3682 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003683 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003684 */
3685hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3686 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3687};
3688
3689/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003690 * Close all web notifications created by terminal bells.
3691 */
3692hterm.Terminal.prototype.closeBellNotifications_ = function() {
3693 this.bellNotificationList_.forEach(function(n) {
3694 n.close();
3695 });
3696 this.bellNotificationList_.length = 0;
3697};