blob: 306f87bbd2fa2b467fcedd1bbabd37954c999667 [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
rgindaf0090c92012-02-10 14:58:52 -0800105 // Terminal bell sound.
106 this.bellAudio_ = this.document_.createElement('audio');
Mike Frysingerd826f1a2017-07-06 16:20:06 -0400107 this.bellAudio_.id = 'hterm:bell-audio';
rgindaf0090c92012-02-10 14:58:52 -0800108 this.bellAudio_.setAttribute('preload', 'auto');
109
Raymes Khoury3e44bc92018-05-17 10:54:23 +1000110 // The AccessibilityReader object for announcing command output.
111 this.accessibilityReader_ = null;
112
113 // Whether command output should be rendered for Assistive Technology.
114 // This isn't always enabled because it has an impact on performance.
115 this.accessibilityEnabled_ = false;
116
Michael Kelly485ecd12014-06-09 11:41:56 -0400117 // All terminal bell notifications that have been generated (not necessarily
118 // shown).
119 this.bellNotificationList_ = [];
120
121 // Whether we have permission to display notifications.
122 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400123
rginda6d397402012-01-17 10:58:29 -0800124 // Cursor position and attributes saved with DECSC.
125 this.savedOptions_ = {};
126
rginda8ba33642011-12-14 12:31:31 -0800127 // The current mode bits for the terminal.
128 this.options_ = new hterm.Options();
129
130 // Timeouts we might need to clear.
131 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800132
133 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800134 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800135
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800136 this.saveCursorAndState(true);
137
Zhu Qunying30d40712017-03-14 16:27:00 -0700138 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800139 this.keyboard = new hterm.Keyboard(this);
140
rginda87b86462011-12-14 13:48:03 -0800141 // General IO interface that can be given to third parties without exposing
142 // the entire terminal object.
143 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800144
rgindad5613292012-06-19 15:40:37 -0700145 // True if mouse-click-drag should scroll the terminal.
146 this.enableMouseDragScroll = true;
147
Robert Ginda57f03b42012-09-13 11:02:48 -0700148 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400149 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700150 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700151
Zhu Qunying30d40712017-03-14 16:27:00 -0700152 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700153 this.useDefaultWindowCopy = false;
154
155 this.clearSelectionAfterCopy = true;
156
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400157 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800158 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700159
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400160 // Whether we allow images to be shown.
161 this.allowImagesInline = null;
162
Gabriel Holodake8a09be2017-10-10 01:07:11 -0400163 this.reportFocus = false;
164
Robert Ginda57f03b42012-09-13 11:02:48 -0700165 this.setProfile(opt_profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500166 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800167};
168
169/**
Robert Ginda830583c2013-08-07 13:20:46 -0700170 * Possible cursor shapes.
171 */
172hterm.Terminal.cursorShape = {
173 BLOCK: 'BLOCK',
174 BEAM: 'BEAM',
175 UNDERLINE: 'UNDERLINE'
176};
177
178/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700179 * Clients should override this to be notified when the terminal is ready
180 * for use.
181 *
182 * The terminal initialization is asynchronous, and shouldn't be used before
183 * this method is called.
184 */
185hterm.Terminal.prototype.onTerminalReady = function() { };
186
187/**
rginda35c456b2012-02-09 17:29:05 -0800188 * Default tab with of 8 to match xterm.
189 */
190hterm.Terminal.prototype.tabWidth = 8;
191
192/**
rginda9f5222b2012-03-05 11:53:28 -0800193 * Select a preference profile.
194 *
195 * This will load the terminal preferences for the given profile name and
196 * associate subsequent preference changes with the new preference profile.
197 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500198 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800199 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700200 * @param {function} opt_callback Optional callback to invoke when the profile
201 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800202 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700203hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
204 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800205
Robert Ginda57f03b42012-09-13 11:02:48 -0700206 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800207
Robert Ginda57f03b42012-09-13 11:02:48 -0700208 if (this.prefs_)
209 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800210
Robert Ginda57f03b42012-09-13 11:02:48 -0700211 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
212 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800213 'alt-gr-mode': function(v) {
214 if (v == null) {
215 if (navigator.language.toLowerCase() == 'en-us') {
216 v = 'none';
217 } else {
218 v = 'right-alt';
219 }
220 } else if (typeof v == 'string') {
221 v = v.toLowerCase();
222 } else {
223 v = 'none';
224 }
225
226 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v))
227 v = 'none';
228
229 terminal.keyboard.altGrMode = v;
230 },
231
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700232 'alt-backspace-is-meta-backspace': function(v) {
233 terminal.keyboard.altBackspaceIsMetaBackspace = v;
234 },
235
Robert Ginda57f03b42012-09-13 11:02:48 -0700236 'alt-is-meta': function(v) {
237 terminal.keyboard.altIsMeta = v;
238 },
239
240 'alt-sends-what': function(v) {
241 if (!/^(escape|8-bit|browser-key)$/.test(v))
242 v = 'escape';
243
244 terminal.keyboard.altSendsWhat = v;
245 },
246
247 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800248 var ary = v.match(/^lib-resource:(\S+)/);
249 if (ary) {
250 terminal.bellAudio_.setAttribute('src',
251 lib.resource.getDataUrl(ary[1]));
252 } else {
253 terminal.bellAudio_.setAttribute('src', v);
254 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700255 },
256
Michael Kelly485ecd12014-06-09 11:41:56 -0400257 'desktop-notification-bell': function(v) {
258 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700259 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400260 Notification.permission === 'granted';
261 if (!terminal.desktopNotificationBell_) {
262 // Note: We don't call Notification.requestPermission here because
263 // Chrome requires the call be the result of a user action (such as an
264 // onclick handler), and pref listeners are run asynchronously.
265 //
266 // A way of working around this would be to display a dialog in the
267 // terminal with a "click-to-request-permission" button.
268 console.warn('desktop-notification-bell is true but we do not have ' +
269 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400270 }
271 } else {
272 terminal.desktopNotificationBell_ = false;
273 }
274 },
275
Robert Ginda57f03b42012-09-13 11:02:48 -0700276 'background-color': function(v) {
277 terminal.setBackgroundColor(v);
278 },
279
280 'background-image': function(v) {
281 terminal.scrollPort_.setBackgroundImage(v);
282 },
283
284 'background-size': function(v) {
285 terminal.scrollPort_.setBackgroundSize(v);
286 },
287
288 'background-position': function(v) {
289 terminal.scrollPort_.setBackgroundPosition(v);
290 },
291
292 'backspace-sends-backspace': function(v) {
293 terminal.keyboard.backspaceSendsBackspace = v;
294 },
295
Brad Town18654b62015-03-12 00:27:45 -0700296 'character-map-overrides': function(v) {
297 if (!(v == null || v instanceof Object)) {
298 console.warn('Preference character-map-modifications is not an ' +
299 'object: ' + v);
300 return;
301 }
302
Mike Frysinger095d4062017-06-14 00:29:48 -0700303 terminal.vt.characterMaps.reset();
304 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700305 },
306
Robert Ginda57f03b42012-09-13 11:02:48 -0700307 'cursor-blink': function(v) {
308 terminal.setCursorBlink(!!v);
309 },
310
Robert Gindaea2183e2014-07-17 09:51:51 -0700311 'cursor-blink-cycle': function(v) {
312 if (v instanceof Array &&
313 typeof v[0] == 'number' &&
314 typeof v[1] == 'number') {
315 terminal.cursorBlinkCycle_ = v;
316 } else if (typeof v == 'number') {
317 terminal.cursorBlinkCycle_ = [v, v];
318 } else {
319 // Fast blink indicates an error.
320 terminal.cursorBlinkCycle_ = [100, 100];
321 }
322 },
323
Robert Ginda57f03b42012-09-13 11:02:48 -0700324 'cursor-color': function(v) {
325 terminal.setCursorColor(v);
326 },
327
328 'color-palette-overrides': function(v) {
329 if (!(v == null || v instanceof Object || v instanceof Array)) {
330 console.warn('Preference color-palette-overrides is not an array or ' +
331 'object: ' + v);
332 return;
rginda9f5222b2012-03-05 11:53:28 -0800333 }
rginda9f5222b2012-03-05 11:53:28 -0800334
Robert Ginda57f03b42012-09-13 11:02:48 -0700335 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700336
Robert Ginda57f03b42012-09-13 11:02:48 -0700337 if (v) {
338 for (var key in v) {
339 var i = parseInt(key);
340 if (isNaN(i) || i < 0 || i > 255) {
341 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
342 continue;
343 }
344
345 if (v[i]) {
346 var rgb = lib.colors.normalizeCSS(v[i]);
347 if (rgb)
348 lib.colors.colorPalette[i] = rgb;
349 }
350 }
rginda30f20f62012-04-05 16:36:19 -0700351 }
rginda30f20f62012-04-05 16:36:19 -0700352
Evan Jones5f9df812016-12-06 09:38:58 -0500353 terminal.primaryScreen_.textAttributes.resetColorPalette();
Robert Ginda57f03b42012-09-13 11:02:48 -0700354 terminal.alternateScreen_.textAttributes.resetColorPalette();
355 },
rginda30f20f62012-04-05 16:36:19 -0700356
Robert Ginda57f03b42012-09-13 11:02:48 -0700357 'copy-on-select': function(v) {
358 terminal.copyOnSelect = !!v;
359 },
rginda9f5222b2012-03-05 11:53:28 -0800360
Rob Spies0bec09b2014-06-06 15:58:09 -0700361 'use-default-window-copy': function(v) {
362 terminal.useDefaultWindowCopy = !!v;
363 },
364
365 'clear-selection-after-copy': function(v) {
366 terminal.clearSelectionAfterCopy = !!v;
367 },
368
Robert Ginda7e5e9522014-03-14 12:23:58 -0700369 'ctrl-plus-minus-zero-zoom': function(v) {
370 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
371 },
372
Robert Gindafb5a3f92014-05-13 14:12:00 -0700373 'ctrl-c-copy': function(v) {
374 terminal.keyboard.ctrlCCopy = v;
375 },
376
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100377 'ctrl-v-paste': function(v) {
378 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700379 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100380 },
381
Masaya Suzuki273aa982014-05-31 07:25:55 +0900382 'east-asian-ambiguous-as-two-column': function(v) {
383 lib.wc.regardCjkAmbiguous = v;
384 },
385
Robert Ginda57f03b42012-09-13 11:02:48 -0700386 'enable-8-bit-control': function(v) {
387 terminal.vt.enable8BitControl = !!v;
388 },
rginda30f20f62012-04-05 16:36:19 -0700389
Robert Ginda57f03b42012-09-13 11:02:48 -0700390 'enable-bold': function(v) {
391 terminal.syncBoldSafeState();
392 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400393
Robert Ginda3e278d72014-03-25 13:18:51 -0700394 'enable-bold-as-bright': function(v) {
395 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
396 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
397 },
398
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400399 'enable-blink': function(v) {
Mike Frysinger261597c2017-12-28 01:14:21 -0500400 terminal.setTextBlink(!!v);
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400401 },
402
Robert Ginda57f03b42012-09-13 11:02:48 -0700403 'enable-clipboard-write': function(v) {
404 terminal.vt.enableClipboardWrite = !!v;
405 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400406
Robert Ginda3755e752013-05-31 13:34:09 -0700407 'enable-dec12': function(v) {
408 terminal.vt.enableDec12 = !!v;
409 },
410
Robert Ginda57f03b42012-09-13 11:02:48 -0700411 'font-family': function(v) {
412 terminal.syncFontFamily();
413 },
rginda30f20f62012-04-05 16:36:19 -0700414
Robert Ginda57f03b42012-09-13 11:02:48 -0700415 'font-size': function(v) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500416 v = parseInt(v);
417 if (v <= 0) {
418 console.error(`Invalid font size: ${v}`);
419 return;
420 }
421
Robert Ginda57f03b42012-09-13 11:02:48 -0700422 terminal.setFontSize(v);
423 },
rginda9875d902012-08-20 16:21:57 -0700424
Robert Ginda57f03b42012-09-13 11:02:48 -0700425 'font-smoothing': function(v) {
426 terminal.syncFontFamily();
427 },
rgindade84e382012-04-20 15:39:31 -0700428
Robert Ginda57f03b42012-09-13 11:02:48 -0700429 'foreground-color': function(v) {
430 terminal.setForegroundColor(v);
431 },
rginda30f20f62012-04-05 16:36:19 -0700432
Robert Ginda57f03b42012-09-13 11:02:48 -0700433 'home-keys-scroll': function(v) {
434 terminal.keyboard.homeKeysScroll = v;
435 },
rginda4bba5e12012-06-20 16:15:30 -0700436
Robert Gindaa8165692015-06-15 14:46:31 -0700437 'keybindings': function(v) {
438 terminal.keyboard.bindings.clear();
439
440 if (!v)
441 return;
442
443 if (!(v instanceof Object)) {
444 console.error('Error in keybindings preference: Expected object');
445 return;
446 }
447
448 try {
449 terminal.keyboard.bindings.addBindings(v);
450 } catch (ex) {
451 console.error('Error in keybindings preference: ' + ex);
452 }
453 },
454
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700455 'media-keys-are-fkeys': function(v) {
456 terminal.keyboard.mediaKeysAreFKeys = v;
457 },
458
Robert Ginda57f03b42012-09-13 11:02:48 -0700459 'meta-sends-escape': function(v) {
460 terminal.keyboard.metaSendsEscape = v;
461 },
rginda30f20f62012-04-05 16:36:19 -0700462
Mike Frysinger847577f2017-05-23 23:25:57 -0400463 'mouse-right-click-paste': function(v) {
464 terminal.mouseRightClickPaste = v;
465 },
466
Robert Ginda57f03b42012-09-13 11:02:48 -0700467 'mouse-paste-button': function(v) {
468 terminal.syncMousePasteButton();
469 },
rgindaa8ba17d2012-08-15 14:41:10 -0700470
Robert Gindae76aa9f2014-03-14 12:29:12 -0700471 'page-keys-scroll': function(v) {
472 terminal.keyboard.pageKeysScroll = v;
473 },
474
Robert Ginda40932892012-12-10 17:26:40 -0800475 'pass-alt-number': function(v) {
476 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800477 // Let Alt-1..9 pass to the browser (to control tab switching) on
478 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500479 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800480 }
481
482 terminal.passAltNumber = v;
483 },
484
485 'pass-ctrl-number': function(v) {
486 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800487 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
488 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500489 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800490 }
491
492 terminal.passCtrlNumber = v;
493 },
494
495 'pass-meta-number': function(v) {
496 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800497 // Let Meta-1..9 pass to the browser (to control tab switching) on
498 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500499 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800500 }
501
502 terminal.passMetaNumber = v;
503 },
504
Marius Schilder77857b32014-05-14 16:21:26 -0700505 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700506 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700507 },
508
Robert Ginda8cb7d902013-06-20 14:37:18 -0700509 'receive-encoding': function(v) {
510 if (!(/^(utf-8|raw)$/).test(v)) {
511 console.warn('Invalid value for "receive-encoding": ' + v);
512 v = 'utf-8';
513 }
514
515 terminal.vt.characterEncoding = v;
516 },
517
Robert Ginda57f03b42012-09-13 11:02:48 -0700518 'scroll-on-keystroke': function(v) {
519 terminal.scrollOnKeystroke_ = v;
520 },
rginda9f5222b2012-03-05 11:53:28 -0800521
Robert Ginda57f03b42012-09-13 11:02:48 -0700522 'scroll-on-output': function(v) {
523 terminal.scrollOnOutput_ = v;
524 },
rginda30f20f62012-04-05 16:36:19 -0700525
Robert Ginda57f03b42012-09-13 11:02:48 -0700526 'scrollbar-visible': function(v) {
527 terminal.setScrollbarVisible(v);
528 },
rginda9f5222b2012-03-05 11:53:28 -0800529
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400530 'scroll-wheel-may-send-arrow-keys': function(v) {
531 terminal.scrollWheelArrowKeys_ = v;
532 },
533
Rob Spies49039e52014-12-17 13:40:04 -0800534 'scroll-wheel-move-multiplier': function(v) {
535 terminal.setScrollWheelMoveMultipler(v);
536 },
537
Robert Ginda8cb7d902013-06-20 14:37:18 -0700538 'send-encoding': function(v) {
539 if (!(/^(utf-8|raw)$/).test(v)) {
540 console.warn('Invalid value for "send-encoding": ' + v);
541 v = 'utf-8';
542 }
543
544 terminal.keyboard.characterEncoding = v;
545 },
546
Robert Ginda57f03b42012-09-13 11:02:48 -0700547 'shift-insert-paste': function(v) {
548 terminal.keyboard.shiftInsertPaste = v;
549 },
rginda9f5222b2012-03-05 11:53:28 -0800550
Mike Frysingera7768922017-07-28 15:00:12 -0400551 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400552 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400553 },
554
Robert Gindae76aa9f2014-03-14 12:29:12 -0700555 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400556 terminal.scrollPort_.setUserCssUrl(v);
557 },
558
559 'user-css-text': function(v) {
560 terminal.scrollPort_.setUserCssText(v);
561 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400562
563 'word-break-match-left': function(v) {
564 terminal.primaryScreen_.wordBreakMatchLeft = v;
565 terminal.alternateScreen_.wordBreakMatchLeft = v;
566 },
567
568 'word-break-match-right': function(v) {
569 terminal.primaryScreen_.wordBreakMatchRight = v;
570 terminal.alternateScreen_.wordBreakMatchRight = v;
571 },
572
573 'word-break-match-middle': function(v) {
574 terminal.primaryScreen_.wordBreakMatchMiddle = v;
575 terminal.alternateScreen_.wordBreakMatchMiddle = v;
576 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400577
578 'allow-images-inline': function(v) {
579 terminal.allowImagesInline = v;
580 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700581 });
rginda30f20f62012-04-05 16:36:19 -0700582
Robert Ginda57f03b42012-09-13 11:02:48 -0700583 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800584 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700585
586 if (opt_callback)
587 opt_callback();
588 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800589};
590
Rob Spies56953412014-04-28 14:09:47 -0700591
592/**
593 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500594 *
595 * @return {hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700596 */
597hterm.Terminal.prototype.getPrefs = function() {
598 return this.prefs_;
599};
600
Robert Gindaa063b202014-07-21 11:08:25 -0700601/**
602 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500603 *
604 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700605 */
606hterm.Terminal.prototype.setBracketedPaste = function(state) {
607 this.options_.bracketedPaste = state;
608};
Rob Spies56953412014-04-28 14:09:47 -0700609
rginda8e92a692012-05-20 19:37:20 -0700610/**
611 * Set the color for the cursor.
612 *
613 * If you want this setting to persist, set it through prefs_, rather than
614 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500615 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500616 * @param {string=} color The color to set. If not defined, we reset to the
617 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700618 */
619hterm.Terminal.prototype.setCursorColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500620 if (color === undefined)
621 color = this.prefs_.get('cursor-color');
622
Robert Ginda830583c2013-08-07 13:20:46 -0700623 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700624 this.cursorNode_.style.backgroundColor = color;
625 this.cursorNode_.style.borderColor = color;
626};
627
628/**
629 * Return the current cursor color as a string.
Evan Jones2600d4f2016-12-06 09:29:36 -0500630 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700631 */
632hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700633 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700634};
635
636/**
rgindad5613292012-06-19 15:40:37 -0700637 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500638 *
639 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700640 */
641hterm.Terminal.prototype.setSelectionEnabled = function(state) {
642 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700643};
644
645/**
rginda8e92a692012-05-20 19:37:20 -0700646 * Set the background color.
647 *
648 * If you want this setting to persist, set it through prefs_, rather than
649 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500650 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500651 * @param {string=} color The color to set. If not defined, we reset to the
652 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700653 */
654hterm.Terminal.prototype.setBackgroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500655 if (color === undefined)
656 color = this.prefs_.get('background-color');
657
rgindacbbd7482012-06-13 15:06:16 -0700658 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700659 this.primaryScreen_.textAttributes.setDefaults(
660 this.foregroundColor_, this.backgroundColor_);
661 this.alternateScreen_.textAttributes.setDefaults(
662 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700663 this.scrollPort_.setBackgroundColor(color);
664};
665
rginda9f5222b2012-03-05 11:53:28 -0800666/**
667 * Return the current terminal background color.
668 *
669 * Intended for use by other classes, so we don't have to expose the entire
670 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500671 *
672 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800673 */
674hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700675 return this.backgroundColor_;
676};
677
678/**
679 * Set the foreground color.
680 *
681 * If you want this setting to persist, set it through prefs_, rather than
682 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500683 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500684 * @param {string=} color The color to set. If not defined, we reset to the
685 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700686 */
687hterm.Terminal.prototype.setForegroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500688 if (color === undefined)
689 color = this.prefs_.get('foreground-color');
690
rgindacbbd7482012-06-13 15:06:16 -0700691 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700692 this.primaryScreen_.textAttributes.setDefaults(
693 this.foregroundColor_, this.backgroundColor_);
694 this.alternateScreen_.textAttributes.setDefaults(
695 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700696 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800697};
698
699/**
700 * Return the current terminal foreground color.
701 *
702 * Intended for use by other classes, so we don't have to expose the entire
703 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500704 *
705 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800706 */
707hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700708 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800709};
710
711/**
rginda87b86462011-12-14 13:48:03 -0800712 * Create a new instance of a terminal command and run it with a given
713 * argument string.
714 *
715 * @param {function} commandClass The constructor for a terminal command.
716 * @param {string} argString The argument string to pass to the command.
717 */
718hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700719 var environment = this.prefs_.get('environment');
720 if (typeof environment != 'object' || environment == null)
721 environment = {};
722
rginda87b86462011-12-14 13:48:03 -0800723 var self = this;
724 this.command = new commandClass(
725 { argString: argString || '',
726 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700727 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800728 onExit: function(code) {
729 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800730 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700731 if (self.prefs_.get('close-on-exit'))
732 window.close();
rginda87b86462011-12-14 13:48:03 -0800733 }
734 });
735
rgindafeaf3142012-01-31 15:14:20 -0800736 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800737 this.command.run();
738};
739
740/**
rgindafeaf3142012-01-31 15:14:20 -0800741 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500742 *
743 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800744 */
745hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700746 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800747};
748
749/**
750 * Install the keyboard handler for this terminal.
751 *
752 * This will prevent the browser from seeing any keystrokes sent to the
753 * terminal.
754 */
755hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700756 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400757};
rgindafeaf3142012-01-31 15:14:20 -0800758
759/**
760 * Uninstall the keyboard handler for this terminal.
761 */
762hterm.Terminal.prototype.uninstallKeyboard = function() {
763 this.keyboard.installKeyboard(null);
Mike Frysinger8416e0a2017-05-17 09:09:46 -0400764};
rgindafeaf3142012-01-31 15:14:20 -0800765
766/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400767 * Set a CSS variable.
768 *
769 * Normally this is used to set variables in the hterm namespace.
770 *
771 * @param {string} name The variable to set.
772 * @param {string} value The value to assign to the variable.
773 * @param {string?} opt_prefix The variable namespace/prefix to use.
774 */
775hterm.Terminal.prototype.setCssVar = function(name, value,
776 opt_prefix='--hterm-') {
777 this.document_.documentElement.style.setProperty(
778 `${opt_prefix}${name}`, value);
779};
780
781/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500782 * Get a CSS variable.
783 *
784 * Normally this is used to get variables in the hterm namespace.
785 *
786 * @param {string} name The variable to read.
787 * @param {string?} opt_prefix The variable namespace/prefix to use.
788 * @return {string} The current setting for this variable.
789 */
790hterm.Terminal.prototype.getCssVar = function(name, opt_prefix='--hterm-') {
791 return this.document_.documentElement.style.getPropertyValue(
792 `${opt_prefix}${name}`);
793};
794
795/**
rginda35c456b2012-02-09 17:29:05 -0800796 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800797 *
798 * Call setFontSize(0) to reset to the default font size.
799 *
800 * This function does not modify the font-size preference.
801 *
802 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800803 */
804hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500805 if (px <= 0)
rginda9f5222b2012-03-05 11:53:28 -0800806 px = this.prefs_.get('font-size');
807
rginda35c456b2012-02-09 17:29:05 -0800808 this.scrollPort_.setFontSize(px);
Mike Frysingercce97c42017-08-05 01:11:22 -0400809 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
810 this.setCssVar('charsize-height',
811 this.scrollPort_.characterSize.height + 'px');
rginda35c456b2012-02-09 17:29:05 -0800812};
813
814/**
815 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500816 *
817 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800818 */
819hterm.Terminal.prototype.getFontSize = function() {
820 return this.scrollPort_.getFontSize();
821};
822
823/**
rginda8e92a692012-05-20 19:37:20 -0700824 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500825 *
826 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700827 */
828hterm.Terminal.prototype.getFontFamily = function() {
829 return this.scrollPort_.getFontFamily();
830};
831
832/**
rginda35c456b2012-02-09 17:29:05 -0800833 * Set the CSS "font-family" for this terminal.
834 */
rginda9f5222b2012-03-05 11:53:28 -0800835hterm.Terminal.prototype.syncFontFamily = function() {
836 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
837 this.prefs_.get('font-smoothing'));
838 this.syncBoldSafeState();
839};
840
rginda4bba5e12012-06-20 16:15:30 -0700841/**
842 * Set this.mousePasteButton based on the mouse-paste-button pref,
843 * autodetecting if necessary.
844 */
845hterm.Terminal.prototype.syncMousePasteButton = function() {
846 var button = this.prefs_.get('mouse-paste-button');
847 if (typeof button == 'number') {
848 this.mousePasteButton = button;
849 return;
850 }
851
Mike Frysingeree81a002017-12-12 16:14:53 -0500852 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400853 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700854 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400855 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700856 }
857};
858
859/**
860 * Enable or disable bold based on the enable-bold pref, autodetecting if
861 * necessary.
862 */
rginda9f5222b2012-03-05 11:53:28 -0800863hterm.Terminal.prototype.syncBoldSafeState = function() {
864 var enableBold = this.prefs_.get('enable-bold');
865 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700866 this.primaryScreen_.textAttributes.enableBold = enableBold;
867 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800868 return;
869 }
870
rgindaf7521392012-02-28 17:20:34 -0800871 var normalSize = this.scrollPort_.measureCharacterSize();
872 var boldSize = this.scrollPort_.measureCharacterSize('bold');
873
874 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800875 if (!isBoldSafe) {
876 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700877 'from normal. Font family is: ' +
878 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800879 }
rginda9f5222b2012-03-05 11:53:28 -0800880
Robert Gindaed016262012-10-26 16:27:09 -0700881 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
882 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800883};
884
885/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500886 * Control text blinking behavior.
887 *
888 * @param {boolean=} state Whether to enable support for blinking text.
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400889 */
Mike Frysinger261597c2017-12-28 01:14:21 -0500890hterm.Terminal.prototype.setTextBlink = function(state) {
891 if (state === undefined)
892 state = this.prefs_.get('enable-blink');
893 this.setCssVar('blink-node-duration', state ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400894};
895
896/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400897 * Set the mouse cursor style based on the current terminal mode.
898 */
899hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400900 this.setCssVar('mouse-cursor-style',
901 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
902 'var(--hterm-mouse-cursor-text)' :
903 'var(--hterm-mouse-cursor-pointer)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400904};
905
906/**
rginda87b86462011-12-14 13:48:03 -0800907 * Return a copy of the current cursor position.
908 *
909 * @return {hterm.RowCol} The RowCol object representing the current position.
910 */
911hterm.Terminal.prototype.saveCursor = function() {
912 return this.screen_.cursorPosition.clone();
913};
914
Evan Jones2600d4f2016-12-06 09:29:36 -0500915/**
916 * Return the current text attributes.
917 *
918 * @return {string}
919 */
rgindaa19afe22012-01-25 15:40:22 -0800920hterm.Terminal.prototype.getTextAttributes = function() {
921 return this.screen_.textAttributes;
922};
923
Evan Jones2600d4f2016-12-06 09:29:36 -0500924/**
925 * Set the text attributes.
926 *
927 * @param {string} textAttributes The attributes to set.
928 */
rginda1a09aa02012-06-18 21:11:25 -0700929hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
930 this.screen_.textAttributes = textAttributes;
931};
932
rginda87b86462011-12-14 13:48:03 -0800933/**
rgindaf522ce02012-04-17 17:49:17 -0700934 * Return the current browser zoom factor applied to the terminal.
935 *
936 * @return {number} The current browser zoom factor.
937 */
938hterm.Terminal.prototype.getZoomFactor = function() {
939 return this.scrollPort_.characterSize.zoomFactor;
940};
941
942/**
rginda9846e2f2012-01-27 13:53:33 -0800943 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500944 *
945 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800946 */
947hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800948 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800949};
950
951/**
rginda87b86462011-12-14 13:48:03 -0800952 * Restore a previously saved cursor position.
953 *
954 * @param {hterm.RowCol} cursor The position to restore.
955 */
956hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700957 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
958 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800959 this.screen_.setCursorPosition(row, column);
960 if (cursor.column > column ||
961 cursor.column == column && cursor.overflow) {
962 this.screen_.cursorPosition.overflow = true;
963 }
rginda87b86462011-12-14 13:48:03 -0800964};
965
966/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400967 * Clear the cursor's overflow flag.
968 */
969hterm.Terminal.prototype.clearCursorOverflow = function() {
970 this.screen_.cursorPosition.overflow = false;
971};
972
973/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800974 * Save the current cursor state to the corresponding screens.
975 *
976 * See the hterm.Screen.CursorState class for more details.
977 *
978 * @param {boolean=} both If true, update both screens, else only update the
979 * current screen.
980 */
981hterm.Terminal.prototype.saveCursorAndState = function(both) {
982 if (both) {
983 this.primaryScreen_.saveCursorAndState(this.vt);
984 this.alternateScreen_.saveCursorAndState(this.vt);
985 } else
986 this.screen_.saveCursorAndState(this.vt);
987};
988
989/**
990 * Restore the saved cursor state in the corresponding screens.
991 *
992 * See the hterm.Screen.CursorState class for more details.
993 *
994 * @param {boolean=} both If true, update both screens, else only update the
995 * current screen.
996 */
997hterm.Terminal.prototype.restoreCursorAndState = function(both) {
998 if (both) {
999 this.primaryScreen_.restoreCursorAndState(this.vt);
1000 this.alternateScreen_.restoreCursorAndState(this.vt);
1001 } else
1002 this.screen_.restoreCursorAndState(this.vt);
1003};
1004
1005/**
Robert Ginda830583c2013-08-07 13:20:46 -07001006 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001007 *
1008 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -07001009 */
1010hterm.Terminal.prototype.setCursorShape = function(shape) {
1011 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001012 this.restyleCursor_();
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001013};
Robert Ginda830583c2013-08-07 13:20:46 -07001014
1015/**
1016 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001017 *
1018 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -07001019 */
1020hterm.Terminal.prototype.getCursorShape = function() {
1021 return this.cursorShape_;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001022};
Robert Ginda830583c2013-08-07 13:20:46 -07001023
1024/**
rginda87b86462011-12-14 13:48:03 -08001025 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001026 *
1027 * @param {number} columnCount
rginda87b86462011-12-14 13:48:03 -08001028 */
1029hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001030 if (columnCount == null) {
1031 this.div_.style.width = '100%';
1032 return;
1033 }
1034
Robert Ginda26806d12014-07-24 13:44:07 -07001035 this.div_.style.width = Math.ceil(
1036 this.scrollPort_.characterSize.width *
1037 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001038 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001039 this.scheduleSyncCursorPosition_();
1040};
rginda87b86462011-12-14 13:48:03 -08001041
rgindac9bc5502012-01-18 11:48:44 -08001042/**
rginda35c456b2012-02-09 17:29:05 -08001043 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001044 *
1045 * @param {number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001046 */
1047hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001048 if (rowCount == null) {
1049 this.div_.style.height = '100%';
1050 return;
1051 }
1052
rginda35c456b2012-02-09 17:29:05 -08001053 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -07001054 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -08001055 this.realizeSize_(this.screenSize.width, rowCount);
1056 this.scheduleSyncCursorPosition_();
1057};
1058
1059/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001060 * Deal with terminal size changes.
1061 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001062 * @param {number} columnCount The number of columns.
1063 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001064 */
1065hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
1066 if (columnCount != this.screenSize.width)
1067 this.realizeWidth_(columnCount);
1068
1069 if (rowCount != this.screenSize.height)
1070 this.realizeHeight_(rowCount);
1071
1072 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -07001073 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001074};
1075
1076/**
rgindac9bc5502012-01-18 11:48:44 -08001077 * Deal with terminal width changes.
1078 *
1079 * This function does what needs to be done when the terminal width changes
1080 * out from under us. It happens here rather than in onResize_() because this
1081 * code may need to run synchronously to handle programmatic changes of
1082 * terminal width.
1083 *
1084 * Relying on the browser to send us an async resize event means we may not be
1085 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001086 *
1087 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001088 */
1089hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001090 if (columnCount <= 0)
1091 throw new Error('Attempt to realize bad width: ' + columnCount);
1092
rgindac9bc5502012-01-18 11:48:44 -08001093 var deltaColumns = columnCount - this.screen_.getWidth();
1094
rginda87b86462011-12-14 13:48:03 -08001095 this.screenSize.width = columnCount;
1096 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001097
1098 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -04001099 if (this.defaultTabStops)
1100 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -08001101 } else {
1102 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -04001103 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -08001104 break;
1105
1106 this.tabStops_.pop();
1107 }
1108 }
1109
1110 this.screen_.setColumnCount(this.screenSize.width);
1111};
1112
1113/**
1114 * Deal with terminal height changes.
1115 *
1116 * This function does what needs to be done when the terminal height changes
1117 * out from under us. It happens here rather than in onResize_() because this
1118 * code may need to run synchronously to handle programmatic changes of
1119 * terminal height.
1120 *
1121 * Relying on the browser to send us an async resize event means we may not be
1122 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001123 *
1124 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001125 */
1126hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001127 if (rowCount <= 0)
1128 throw new Error('Attempt to realize bad height: ' + rowCount);
1129
rgindac9bc5502012-01-18 11:48:44 -08001130 var deltaRows = rowCount - this.screen_.getHeight();
1131
1132 this.screenSize.height = rowCount;
1133
1134 var cursor = this.saveCursor();
1135
1136 if (deltaRows < 0) {
1137 // Screen got smaller.
1138 deltaRows *= -1;
1139 while (deltaRows) {
1140 var lastRow = this.getRowCount() - 1;
1141 if (lastRow - this.scrollbackRows_.length == cursor.row)
1142 break;
1143
1144 if (this.getRowText(lastRow))
1145 break;
1146
1147 this.screen_.popRow();
1148 deltaRows--;
1149 }
1150
1151 var ary = this.screen_.shiftRows(deltaRows);
1152 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1153
1154 // We just removed rows from the top of the screen, we need to update
1155 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001156 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001157 } else if (deltaRows > 0) {
1158 // Screen got larger.
1159
1160 if (deltaRows <= this.scrollbackRows_.length) {
1161 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1162 var rows = this.scrollbackRows_.splice(
1163 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1164 this.screen_.unshiftRows(rows);
1165 deltaRows -= scrollbackCount;
1166 cursor.row += scrollbackCount;
1167 }
1168
1169 if (deltaRows)
1170 this.appendRows_(deltaRows);
1171 }
1172
rginda35c456b2012-02-09 17:29:05 -08001173 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001174 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001175};
1176
1177/**
1178 * Scroll the terminal to the top of the scrollback buffer.
1179 */
1180hterm.Terminal.prototype.scrollHome = function() {
1181 this.scrollPort_.scrollRowToTop(0);
1182};
1183
1184/**
1185 * Scroll the terminal to the end.
1186 */
1187hterm.Terminal.prototype.scrollEnd = function() {
1188 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1189};
1190
1191/**
1192 * Scroll the terminal one page up (minus one line) relative to the current
1193 * position.
1194 */
1195hterm.Terminal.prototype.scrollPageUp = function() {
1196 var i = this.scrollPort_.getTopRowIndex();
1197 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
1198};
1199
1200/**
1201 * Scroll the terminal one page down (minus one line) relative to the current
1202 * position.
1203 */
1204hterm.Terminal.prototype.scrollPageDown = function() {
1205 var i = this.scrollPort_.getTopRowIndex();
1206 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -08001207};
1208
rgindac9bc5502012-01-18 11:48:44 -08001209/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001210 * Scroll the terminal one line up relative to the current position.
1211 */
1212hterm.Terminal.prototype.scrollLineUp = function() {
1213 var i = this.scrollPort_.getTopRowIndex();
1214 this.scrollPort_.scrollRowToTop(i - 1);
1215};
1216
1217/**
1218 * Scroll the terminal one line down relative to the current position.
1219 */
1220hterm.Terminal.prototype.scrollLineDown = function() {
1221 var i = this.scrollPort_.getTopRowIndex();
1222 this.scrollPort_.scrollRowToTop(i + 1);
1223};
1224
1225/**
Robert Ginda40932892012-12-10 17:26:40 -08001226 * Clear primary screen, secondary screen, and the scrollback buffer.
1227 */
1228hterm.Terminal.prototype.wipeContents = function() {
1229 this.scrollbackRows_.length = 0;
1230 this.scrollPort_.resetCache();
1231
1232 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
1233 var bottom = screen.getHeight();
1234 if (bottom > 0) {
1235 this.renumberRows_(0, bottom);
1236 this.clearHome(screen);
1237 }
1238 }.bind(this));
1239
1240 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001241 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001242};
1243
1244/**
rgindac9bc5502012-01-18 11:48:44 -08001245 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001246 *
1247 * Perform a full reset to the default values listed in
1248 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001249 */
rginda87b86462011-12-14 13:48:03 -08001250hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001251 this.vt.reset();
1252
rgindac9bc5502012-01-18 11:48:44 -08001253 this.clearAllTabStops();
1254 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001255
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001256 const resetScreen = (screen) => {
1257 // We want to make sure to reset the attributes before we clear the screen.
1258 // The attributes might be used to initialize default/empty rows.
1259 screen.textAttributes.reset();
1260 screen.textAttributes.resetColorPalette();
1261 this.clearHome(screen);
1262 screen.saveCursorAndState(this.vt);
1263 };
1264 resetScreen(this.primaryScreen_);
1265 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001266
Mike Frysinger84301d02017-11-29 13:28:46 -08001267 // Reset terminal options to their default values.
1268 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001269 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1270
Mike Frysinger84301d02017-11-29 13:28:46 -08001271 this.setVTScrollRegion(null, null);
1272
1273 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001274};
1275
rgindac9bc5502012-01-18 11:48:44 -08001276/**
1277 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001278 *
1279 * Perform a soft reset to the default values listed in
1280 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001281 */
rginda0f5c0292012-01-13 11:00:13 -08001282hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001283 this.vt.reset();
1284
rgindab8bc8932012-04-27 12:45:03 -07001285 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001286 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001287
Brad Townb62dfdc2015-03-16 19:07:15 -07001288 // We show the cursor on soft reset but do not alter the blink state.
1289 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1290
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001291 const resetScreen = (screen) => {
1292 // Xterm also resets the color palette on soft reset, even though it doesn't
1293 // seem to be documented anywhere.
1294 screen.textAttributes.reset();
1295 screen.textAttributes.resetColorPalette();
1296 screen.saveCursorAndState(this.vt);
1297 };
1298 resetScreen(this.primaryScreen_);
1299 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001300
rgindab8bc8932012-04-27 12:45:03 -07001301 // The xterm man page explicitly says this will happen on soft reset.
1302 this.setVTScrollRegion(null, null);
1303
1304 // Xterm also shows the cursor on soft reset, but does not alter the blink
1305 // state.
rgindaa19afe22012-01-25 15:40:22 -08001306 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001307};
1308
rgindac9bc5502012-01-18 11:48:44 -08001309/**
1310 * Move the cursor forward to the next tab stop, or to the last column
1311 * if no more tab stops are set.
1312 */
1313hterm.Terminal.prototype.forwardTabStop = function() {
1314 var column = this.screen_.cursorPosition.column;
1315
1316 for (var i = 0; i < this.tabStops_.length; i++) {
1317 if (this.tabStops_[i] > column) {
1318 this.setCursorColumn(this.tabStops_[i]);
1319 return;
1320 }
1321 }
1322
David Benjamin66e954d2012-05-05 21:08:12 -04001323 // xterm does not clear the overflow flag on HT or CHT.
1324 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001325 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001326 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001327};
1328
rgindac9bc5502012-01-18 11:48:44 -08001329/**
1330 * Move the cursor backward to the previous tab stop, or to the first column
1331 * if no previous tab stops are set.
1332 */
1333hterm.Terminal.prototype.backwardTabStop = function() {
1334 var column = this.screen_.cursorPosition.column;
1335
1336 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1337 if (this.tabStops_[i] < column) {
1338 this.setCursorColumn(this.tabStops_[i]);
1339 return;
1340 }
1341 }
1342
1343 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001344};
1345
rgindac9bc5502012-01-18 11:48:44 -08001346/**
1347 * Set a tab stop at the given column.
1348 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001349 * @param {integer} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001350 */
1351hterm.Terminal.prototype.setTabStop = function(column) {
1352 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1353 if (this.tabStops_[i] == column)
1354 return;
1355
1356 if (this.tabStops_[i] < column) {
1357 this.tabStops_.splice(i + 1, 0, column);
1358 return;
1359 }
1360 }
1361
1362 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001363};
1364
rgindac9bc5502012-01-18 11:48:44 -08001365/**
1366 * Clear the tab stop at the current cursor position.
1367 *
1368 * No effect if there is no tab stop at the current cursor position.
1369 */
1370hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1371 var column = this.screen_.cursorPosition.column;
1372
1373 var i = this.tabStops_.indexOf(column);
1374 if (i == -1)
1375 return;
1376
1377 this.tabStops_.splice(i, 1);
1378};
1379
1380/**
1381 * Clear all tab stops.
1382 */
1383hterm.Terminal.prototype.clearAllTabStops = function() {
1384 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001385 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001386};
1387
1388/**
1389 * Set up the default tab stops, starting from a given column.
1390 *
1391 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001392 * from the specified column, or 0 if no column is provided. It also flags
1393 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001394 *
1395 * This does not clear the existing tab stops first, use clearAllTabStops
1396 * for that.
1397 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001398 * @param {integer} opt_start Optional starting zero based starting column, useful
rgindac9bc5502012-01-18 11:48:44 -08001399 * for filling out missing tab stops when the terminal is resized.
1400 */
1401hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1402 var start = opt_start || 0;
1403 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001404 // Round start up to a default tab stop.
1405 start = start - 1 - ((start - 1) % w) + w;
1406 for (var i = start; i < this.screenSize.width; i += w) {
1407 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001408 }
David Benjamin66e954d2012-05-05 21:08:12 -04001409
1410 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001411};
1412
rginda6d397402012-01-17 10:58:29 -08001413/**
rginda8ba33642011-12-14 12:31:31 -08001414 * Interpret a sequence of characters.
1415 *
1416 * Incomplete escape sequences are buffered until the next call.
1417 *
1418 * @param {string} str Sequence of characters to interpret or pass through.
1419 */
1420hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001421 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001422 this.scheduleSyncCursorPosition_();
1423};
1424
1425/**
1426 * Take over the given DIV for use as the terminal display.
1427 *
1428 * @param {HTMLDivElement} div The div to use as the terminal display.
1429 */
1430hterm.Terminal.prototype.decorate = function(div) {
Mike Frysinger5768a9d2017-12-26 12:57:44 -05001431 const charset = div.ownerDocument.characterSet.toLowerCase();
1432 if (charset != 'utf-8') {
1433 console.warn(`Document encoding should be set to utf-8, not "${charset}";` +
1434 ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`);
1435 }
1436
rginda87b86462011-12-14 13:48:03 -08001437 this.div_ = div;
1438
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001439 this.accessibilityReader_ = new hterm.AccessibilityReader(div);
1440
rginda8ba33642011-12-14 12:31:31 -08001441 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001442 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001443 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1444 this.scrollPort_.setBackgroundPosition(
1445 this.prefs_.get('background-position'));
Mike Frysinger08bad432017-04-24 00:50:54 -04001446 this.scrollPort_.setUserCssUrl(this.prefs_.get('user-css'));
1447 this.scrollPort_.setUserCssText(this.prefs_.get('user-css-text'));
rginda30f20f62012-04-05 16:36:19 -07001448
rginda0918b652012-04-04 11:26:24 -07001449 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001450
rginda9f5222b2012-03-05 11:53:28 -08001451 this.setFontSize(this.prefs_.get('font-size'));
1452 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001453
David Reveman8f552492012-03-28 12:18:41 -04001454 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001455 this.setScrollWheelMoveMultipler(
1456 this.prefs_.get('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001457
rginda8ba33642011-12-14 12:31:31 -08001458 this.document_ = this.scrollPort_.getDocument();
1459
Evan Jones5f9df812016-12-06 09:38:58 -05001460 this.document_.body.oncontextmenu = function() { return false; };
rginda4bba5e12012-06-20 16:15:30 -07001461
1462 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001463 var screenNode = this.scrollPort_.getScreenNode();
1464 screenNode.addEventListener('mousedown', onMouse);
1465 screenNode.addEventListener('mouseup', onMouse);
1466 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001467 this.scrollPort_.onScrollWheel = onMouse;
1468
Toni Barzic0bfa8922013-11-22 11:18:35 -08001469 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001470 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001471 // Listen for mousedown events on the screenNode as in FF the focus
1472 // events don't bubble.
1473 screenNode.addEventListener('mousedown', function() {
1474 setTimeout(this.onFocusChange_.bind(this, true));
1475 }.bind(this));
1476
Toni Barzic0bfa8922013-11-22 11:18:35 -08001477 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001478 'blur', this.onFocusChange_.bind(this, false));
1479
1480 var style = this.document_.createElement('style');
1481 style.textContent =
1482 ('.cursor-node[focus="false"] {' +
1483 ' box-sizing: border-box;' +
1484 ' background-color: transparent !important;' +
1485 ' border-width: 2px;' +
1486 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001487 '}' +
1488 '.wc-node {' +
1489 ' display: inline-block;' +
1490 ' text-align: center;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001491 ' width: calc(var(--hterm-charsize-width) * 2);' +
Mike Frysinger4036f6e2017-05-31 14:02:55 -04001492 ' line-height: var(--hterm-charsize-height);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001493 '}' +
1494 ':root {' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001495 ' --hterm-charsize-width: ' + this.scrollPort_.characterSize.width + 'px;' +
1496 ' --hterm-charsize-height: ' + this.scrollPort_.characterSize.height + 'px;' +
Mike Frysingera27c0502017-08-23 22:37:10 -04001497 // Default position hides the cursor for when the window is initializing.
1498 ' --hterm-cursor-offset-col: -1;' +
1499 ' --hterm-cursor-offset-row: -1;' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001500 ' --hterm-blink-node-duration: 0.7s;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001501 ' --hterm-mouse-cursor-text: text;' +
1502 ' --hterm-mouse-cursor-pointer: default;' +
1503 ' --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001504 '}' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001505 '.uri-node:hover {' +
1506 ' text-decoration: underline;' +
1507 ' cursor: pointer;' +
1508 '}' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001509 '@keyframes blink {' +
1510 ' from { opacity: 1.0; }' +
1511 ' to { opacity: 0.0; }' +
1512 '}' +
1513 '.blink-node {' +
1514 ' animation-name: blink;' +
1515 ' animation-duration: var(--hterm-blink-node-duration);' +
1516 ' animation-iteration-count: infinite;' +
1517 ' animation-timing-function: ease-in-out;' +
1518 ' animation-direction: alternate;' +
rginda8e92a692012-05-20 19:37:20 -07001519 '}');
1520 this.document_.head.appendChild(style);
1521
rginda8ba33642011-12-14 12:31:31 -08001522 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001523 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001524 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001525 this.cursorNode_.style.cssText =
1526 ('position: absolute;' +
Mike Frysinger44c32202017-08-05 01:13:09 -04001527 'left: calc(var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));' +
1528 'top: calc(var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));' +
Mike Frysingerd60f4c22018-03-15 21:25:30 -07001529 'display: ' + (this.options_.cursorVisible ? '' : 'none') + ';' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001530 'width: var(--hterm-charsize-width);' +
1531 'height: var(--hterm-charsize-height);' +
Rob Spies06533ba2014-04-24 11:20:37 -07001532 '-webkit-transition: opacity, background-color 100ms linear;' +
1533 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001534
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001535 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001536 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1537 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001538
rginda8ba33642011-12-14 12:31:31 -08001539 this.document_.body.appendChild(this.cursorNode_);
1540
rgindad5613292012-06-19 15:40:37 -07001541 // When 'enableMouseDragScroll' is off we reposition this element directly
1542 // under the mouse cursor after a click. This makes Chrome associate
1543 // subsequent mousemove events with the scroll-blocker. Since the
1544 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1545 // events do not cause the scrollport to scroll.
1546 //
1547 // It's a hack, but it's the cleanest way I could find.
1548 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001549 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
Raymes Khoury6dce2f82018-04-12 15:38:58 +10001550 this.scrollBlockerNode_.setAttribute('aria-hidden', 'true');
rgindad5613292012-06-19 15:40:37 -07001551 this.scrollBlockerNode_.style.cssText =
1552 ('position: absolute;' +
1553 'top: -99px;' +
1554 'display: block;' +
1555 'width: 10px;' +
1556 'height: 10px;');
1557 this.document_.body.appendChild(this.scrollBlockerNode_);
1558
rgindad5613292012-06-19 15:40:37 -07001559 this.scrollPort_.onScrollWheel = onMouse;
1560 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1561 ].forEach(function(event) {
1562 this.scrollBlockerNode_.addEventListener(event, onMouse);
1563 this.cursorNode_.addEventListener(event, onMouse);
1564 this.document_.addEventListener(event, onMouse);
1565 }.bind(this));
1566
1567 this.cursorNode_.addEventListener('mousedown', function() {
1568 setTimeout(this.focus.bind(this));
1569 }.bind(this));
1570
rginda8ba33642011-12-14 12:31:31 -08001571 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001572
rginda87b86462011-12-14 13:48:03 -08001573 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001574 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001575};
1576
rginda0918b652012-04-04 11:26:24 -07001577/**
1578 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001579 *
1580 * @return {HTMLDocument}
rginda0918b652012-04-04 11:26:24 -07001581 */
rginda87b86462011-12-14 13:48:03 -08001582hterm.Terminal.prototype.getDocument = function() {
1583 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001584};
1585
1586/**
rginda0918b652012-04-04 11:26:24 -07001587 * Focus the terminal.
1588 */
1589hterm.Terminal.prototype.focus = function() {
1590 this.scrollPort_.focus();
1591};
1592
1593/**
rginda8ba33642011-12-14 12:31:31 -08001594 * Return the HTML Element for a given row index.
1595 *
1596 * This is a method from the RowProvider interface. The ScrollPort uses
1597 * it to fetch rows on demand as they are scrolled into view.
1598 *
1599 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1600 * pairs to conserve memory.
1601 *
1602 * @param {integer} index The zero-based row index, measured relative to the
1603 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001604 * largest indices.
rginda8ba33642011-12-14 12:31:31 -08001605 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1606 */
1607hterm.Terminal.prototype.getRowNode = function(index) {
1608 if (index < this.scrollbackRows_.length)
1609 return this.scrollbackRows_[index];
1610
1611 var screenIndex = index - this.scrollbackRows_.length;
1612 return this.screen_.rowsArray[screenIndex];
1613};
1614
1615/**
1616 * Return the text content for a given range of rows.
1617 *
1618 * This is a method from the RowProvider interface. The ScrollPort uses
1619 * it to fetch text content on demand when the user attempts to copy their
1620 * selection to the clipboard.
1621 *
1622 * @param {integer} start The zero-based row index to start from, measured
1623 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001624 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001625 * @param {integer} end The zero-based row index to end on, measured
1626 * relative to the start of the scrollback buffer.
1627 * @return {string} A single string containing the text value of the range of
1628 * rows. Lines will be newline delimited, with no trailing newline.
1629 */
1630hterm.Terminal.prototype.getRowsText = function(start, end) {
1631 var ary = [];
1632 for (var i = start; i < end; i++) {
1633 var node = this.getRowNode(i);
1634 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001635 if (i < end - 1 && !node.getAttribute('line-overflow'))
1636 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001637 }
1638
rgindaa09e7332012-08-17 12:49:51 -07001639 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001640};
1641
1642/**
1643 * Return the text content for a given row.
1644 *
1645 * This is a method from the RowProvider interface. The ScrollPort uses
1646 * it to fetch text content on demand when the user attempts to copy their
1647 * selection to the clipboard.
1648 *
1649 * @param {integer} index The zero-based row index to return, measured
1650 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001651 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001652 * @return {string} A string containing the text value of the selected row.
1653 */
1654hterm.Terminal.prototype.getRowText = function(index) {
1655 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001656 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001657};
1658
1659/**
1660 * Return the total number of rows in the addressable screen and in the
1661 * scrollback buffer of this terminal.
1662 *
1663 * This is a method from the RowProvider interface. The ScrollPort uses
1664 * it to compute the size of the scrollbar.
1665 *
1666 * @return {integer} The number of rows in this terminal.
1667 */
1668hterm.Terminal.prototype.getRowCount = function() {
1669 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1670};
1671
1672/**
1673 * Create DOM nodes for new rows and append them to the end of the terminal.
1674 *
1675 * This is the only correct way to add a new DOM node for a row. Notice that
1676 * the new row is appended to the bottom of the list of rows, and does not
1677 * require renumbering (of the rowIndex property) of previous rows.
1678 *
1679 * If you think you want a new blank row somewhere in the middle of the
1680 * terminal, look into moveRows_().
1681 *
1682 * This method does not pay attention to vtScrollTop/Bottom, since you should
1683 * be using moveRows() in cases where they would matter.
1684 *
1685 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001686 *
1687 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001688 */
1689hterm.Terminal.prototype.appendRows_ = function(count) {
1690 var cursorRow = this.screen_.rowsArray.length;
1691 var offset = this.scrollbackRows_.length + cursorRow;
1692 for (var i = 0; i < count; i++) {
1693 var row = this.document_.createElement('x-row');
1694 row.appendChild(this.document_.createTextNode(''));
1695 row.rowIndex = offset + i;
1696 this.screen_.pushRow(row);
1697 }
1698
1699 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1700 if (extraRows > 0) {
1701 var ary = this.screen_.shiftRows(extraRows);
1702 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001703 if (this.scrollPort_.isScrolledEnd)
1704 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001705 }
1706
1707 if (cursorRow >= this.screen_.rowsArray.length)
1708 cursorRow = this.screen_.rowsArray.length - 1;
1709
rginda87b86462011-12-14 13:48:03 -08001710 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001711};
1712
1713/**
1714 * Relocate rows from one part of the addressable screen to another.
1715 *
1716 * This is used to recycle rows during VT scrolls (those which are driven
1717 * by VT commands, rather than by the user manipulating the scrollbar.)
1718 *
1719 * In this case, the blank lines scrolled into the scroll region are made of
1720 * the nodes we scrolled off. These have their rowIndex properties carefully
1721 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001722 *
1723 * @param {number} fromIndex The start index.
1724 * @param {number} count The number of rows to move.
1725 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001726 */
1727hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1728 var ary = this.screen_.removeRows(fromIndex, count);
1729 this.screen_.insertRows(toIndex, ary);
1730
1731 var start, end;
1732 if (fromIndex < toIndex) {
1733 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001734 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001735 } else {
1736 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001737 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001738 }
1739
1740 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001741 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001742};
1743
1744/**
1745 * Renumber the rowIndex property of the given range of rows.
1746 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001747 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001748 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001749 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001750 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001751 *
1752 * @param {number} start The start index.
1753 * @param {number} end The end index.
1754 * @param {hterm.Screen} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001755 */
Robert Ginda40932892012-12-10 17:26:40 -08001756hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1757 var screen = opt_screen || this.screen_;
1758
rginda8ba33642011-12-14 12:31:31 -08001759 var offset = this.scrollbackRows_.length;
1760 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001761 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001762 }
1763};
1764
1765/**
1766 * Print a string to the terminal.
1767 *
1768 * This respects the current insert and wraparound modes. It will add new lines
1769 * to the end of the terminal, scrolling off the top into the scrollback buffer
1770 * if necessary.
1771 *
1772 * The string is *not* parsed for escape codes. Use the interpret() method if
1773 * that's what you're after.
1774 *
1775 * @param{string} str The string to print.
1776 */
1777hterm.Terminal.prototype.print = function(str) {
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001778 // Basic accessibility output for the screen reader.
1779 if (this.accessibilityEnabled_)
1780 this.accessibilityReader_.announce(str);
1781
rgindaa9abdd82012-08-06 18:05:09 -07001782 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001783
Ricky Liang48f05cb2013-12-31 23:35:29 +08001784 var strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04001785 // Fun edge case: If the string only contains zero width codepoints (like
1786 // combining characters), we make sure to iterate at least once below.
1787 if (strWidth == 0 && str)
1788 strWidth = 1;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001789
1790 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001791 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1792 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001793 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001794 }
rgindaa19afe22012-01-25 15:40:22 -08001795
Ricky Liang48f05cb2013-12-31 23:35:29 +08001796 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001797 var didOverflow = false;
1798 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001799
rgindaa9abdd82012-08-06 18:05:09 -07001800 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1801 didOverflow = true;
1802 count = this.screenSize.width - this.screen_.cursorPosition.column;
1803 }
rgindaa19afe22012-01-25 15:40:22 -08001804
rgindaa9abdd82012-08-06 18:05:09 -07001805 if (didOverflow && !this.options_.wraparound) {
1806 // If the string overflowed the line but wraparound is off, then the
1807 // last printed character should be the last of the string.
1808 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001809 substr = lib.wc.substr(str, startOffset, count - 1) +
1810 lib.wc.substr(str, strWidth - 1);
1811 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001812 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001813 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001814 }
rgindaa19afe22012-01-25 15:40:22 -08001815
Ricky Liang48f05cb2013-12-31 23:35:29 +08001816 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1817 for (var i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001818 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
1819 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001820
1821 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001822 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001823 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001824 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001825 }
1826 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001827 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07001828 }
1829
1830 this.screen_.maybeClipCurrentRow();
1831 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001832 }
rginda8ba33642011-12-14 12:31:31 -08001833
1834 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001835
rginda9f5222b2012-03-05 11:53:28 -08001836 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001837 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001838};
1839
1840/**
rginda87b86462011-12-14 13:48:03 -08001841 * Set the VT scroll region.
1842 *
rginda87b86462011-12-14 13:48:03 -08001843 * This also resets the cursor position to the absolute (0, 0) position, since
1844 * that's what xterm appears to do.
1845 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001846 * Setting the scroll region to the full height of the terminal will clear
1847 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1848 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1849 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1850 * continue to work as most users would expect.
1851 *
rginda87b86462011-12-14 13:48:03 -08001852 * @param {integer} scrollTop The zero-based top of the scroll region.
1853 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1854 * inclusive.
1855 */
1856hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001857 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001858 this.vtScrollTop_ = null;
1859 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001860 } else {
1861 this.vtScrollTop_ = scrollTop;
1862 this.vtScrollBottom_ = scrollBottom;
1863 }
rginda87b86462011-12-14 13:48:03 -08001864};
1865
1866/**
rginda8ba33642011-12-14 12:31:31 -08001867 * Return the top row index according to the VT.
1868 *
1869 * This will return 0 unless the terminal has been told to restrict scrolling
1870 * to some lower row. It is used for some VT cursor positioning and scrolling
1871 * commands.
1872 *
1873 * @return {integer} The topmost row in the terminal's scroll region.
1874 */
1875hterm.Terminal.prototype.getVTScrollTop = function() {
1876 if (this.vtScrollTop_ != null)
1877 return this.vtScrollTop_;
1878
1879 return 0;
rginda87b86462011-12-14 13:48:03 -08001880};
rginda8ba33642011-12-14 12:31:31 -08001881
1882/**
1883 * Return the bottom row index according to the VT.
1884 *
1885 * This will return the height of the terminal unless the it has been told to
1886 * restrict scrolling to some higher row. It is used for some VT cursor
1887 * positioning and scrolling commands.
1888 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001889 * @return {integer} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001890 */
1891hterm.Terminal.prototype.getVTScrollBottom = function() {
1892 if (this.vtScrollBottom_ != null)
1893 return this.vtScrollBottom_;
1894
rginda87b86462011-12-14 13:48:03 -08001895 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001896};
rginda8ba33642011-12-14 12:31:31 -08001897
1898/**
1899 * Process a '\n' character.
1900 *
1901 * If the cursor is on the final row of the terminal this will append a new
1902 * blank row to the screen and scroll the topmost row into the scrollback
1903 * buffer.
1904 *
1905 * Otherwise, this moves the cursor to column zero of the next row.
1906 */
1907hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001908 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1909 this.screen_.rowsArray.length - 1);
1910
1911 if (this.vtScrollBottom_ != null) {
1912 // A VT Scroll region is active, we never append new rows.
1913 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1914 // We're at the end of the VT Scroll Region, perform a VT scroll.
1915 this.vtScrollUp(1);
1916 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1917 } else if (cursorAtEndOfScreen) {
1918 // We're at the end of the screen, the only thing to do is put the
1919 // cursor to column 0.
1920 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1921 } else {
1922 // Anywhere else, advance the cursor row, and reset the column.
1923 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1924 }
1925 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001926 // We're at the end of the screen. Append a new row to the terminal,
1927 // shifting the top row into the scrollback.
1928 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001929 } else {
rginda87b86462011-12-14 13:48:03 -08001930 // Anywhere else in the screen just moves the cursor.
1931 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001932 }
1933};
1934
1935/**
1936 * Like newLine(), except maintain the cursor column.
1937 */
1938hterm.Terminal.prototype.lineFeed = function() {
1939 var column = this.screen_.cursorPosition.column;
1940 this.newLine();
1941 this.setCursorColumn(column);
1942};
1943
1944/**
rginda87b86462011-12-14 13:48:03 -08001945 * If autoCarriageReturn is set then newLine(), else lineFeed().
1946 */
1947hterm.Terminal.prototype.formFeed = function() {
1948 if (this.options_.autoCarriageReturn) {
1949 this.newLine();
1950 } else {
1951 this.lineFeed();
1952 }
1953};
1954
1955/**
1956 * Move the cursor up one row, possibly inserting a blank line.
1957 *
1958 * The cursor column is not changed.
1959 */
1960hterm.Terminal.prototype.reverseLineFeed = function() {
1961 var scrollTop = this.getVTScrollTop();
1962 var currentRow = this.screen_.cursorPosition.row;
1963
1964 if (currentRow == scrollTop) {
1965 this.insertLines(1);
1966 } else {
1967 this.setAbsoluteCursorRow(currentRow - 1);
1968 }
1969};
1970
1971/**
rginda8ba33642011-12-14 12:31:31 -08001972 * Replace all characters to the left of the current cursor with the space
1973 * character.
1974 *
1975 * TODO(rginda): This should probably *remove* the characters (not just replace
1976 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001977 * position.
rginda8ba33642011-12-14 12:31:31 -08001978 */
1979hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001980 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001981 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04001982 const count = cursor.column + 1;
1983 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08001984 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001985};
1986
1987/**
David Benjamin684a9b72012-05-01 17:19:58 -04001988 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001989 *
1990 * The cursor position is unchanged.
1991 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001992 * If the current background color is not the default background color this
1993 * will insert spaces rather than delete. This is unfortunate because the
1994 * trailing space will affect text selection, but it's difficult to come up
1995 * with a way to style empty space that wouldn't trip up the hterm.Screen
1996 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001997 *
1998 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1999 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2000 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002001 *
2002 * @param {number} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002003 */
2004hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002005 if (this.screen_.cursorPosition.overflow)
2006 return;
2007
Robert Ginda7fd57082012-09-25 14:41:47 -07002008 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
2009 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002010
2011 if (this.screen_.textAttributes.background ===
2012 this.screen_.textAttributes.DEFAULT_COLOR) {
2013 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002014 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002015 this.screen_.cursorPosition.column + count) {
2016 this.screen_.deleteChars(count);
2017 this.clearCursorOverflow();
2018 return;
2019 }
2020 }
2021
rginda87b86462011-12-14 13:48:03 -08002022 var cursor = this.saveCursor();
Mike Frysinger6380bed2017-08-24 18:46:39 -04002023 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08002024 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002025 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002026};
2027
2028/**
2029 * Erase the current line.
2030 *
2031 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002032 */
2033hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08002034 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002035 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002036 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002037 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002038};
2039
2040/**
David Benjamina08d78f2012-05-05 00:28:49 -04002041 * Erase all characters from the start of the screen to the current cursor
2042 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002043 *
2044 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002045 */
2046hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08002047 var cursor = this.saveCursor();
2048
2049 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002050
David Benjamina08d78f2012-05-05 00:28:49 -04002051 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002052 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002053 this.screen_.clearCursorRow();
2054 }
2055
rginda87b86462011-12-14 13:48:03 -08002056 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002057 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002058};
2059
2060/**
2061 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002062 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002063 *
2064 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002065 */
2066hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08002067 var cursor = this.saveCursor();
2068
2069 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002070
David Benjamina08d78f2012-05-05 00:28:49 -04002071 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08002072 for (var i = cursor.row + 1; i <= bottom; i++) {
2073 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002074 this.screen_.clearCursorRow();
2075 }
2076
rginda87b86462011-12-14 13:48:03 -08002077 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002078 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002079};
2080
2081/**
2082 * Fill the terminal with a given character.
2083 *
2084 * This methods does not respect the VT scroll region.
2085 *
2086 * @param {string} ch The character to use for the fill.
2087 */
2088hterm.Terminal.prototype.fill = function(ch) {
2089 var cursor = this.saveCursor();
2090
2091 this.setAbsoluteCursorPosition(0, 0);
2092 for (var row = 0; row < this.screenSize.height; row++) {
2093 for (var col = 0; col < this.screenSize.width; col++) {
2094 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002095 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002096 }
2097 }
2098
2099 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002100};
2101
2102/**
rginda9ea433c2012-03-16 11:57:00 -07002103 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002104 *
rginda9ea433c2012-03-16 11:57:00 -07002105 * This does not respect the scroll region.
2106 *
2107 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2108 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002109 */
rginda9ea433c2012-03-16 11:57:00 -07002110hterm.Terminal.prototype.clearHome = function(opt_screen) {
2111 var screen = opt_screen || this.screen_;
2112 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002113
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002114 this.accessibilityReader_.clear();
2115
rginda11057d52012-04-25 12:29:56 -07002116 if (bottom == 0) {
2117 // Empty screen, nothing to do.
2118 return;
2119 }
2120
rgindae4d29232012-01-19 10:47:13 -08002121 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002122 screen.setCursorPosition(i, 0);
2123 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002124 }
2125
rginda9ea433c2012-03-16 11:57:00 -07002126 screen.setCursorPosition(0, 0);
2127};
2128
2129/**
2130 * Erase the entire display without changing the cursor position.
2131 *
2132 * The cursor position is unchanged. This does not respect the scroll
2133 * region.
2134 *
2135 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2136 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002137 */
2138hterm.Terminal.prototype.clear = function(opt_screen) {
2139 var screen = opt_screen || this.screen_;
2140 var cursor = screen.cursorPosition.clone();
2141 this.clearHome(screen);
2142 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002143};
2144
2145/**
2146 * VT command to insert lines at the current cursor row.
2147 *
2148 * This respects the current scroll region. Rows pushed off the bottom are
2149 * lost (they won't show up in the scrollback buffer).
2150 *
rginda8ba33642011-12-14 12:31:31 -08002151 * @param {integer} count The number of lines to insert.
2152 */
2153hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002154 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002155
2156 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002157 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002158
Robert Ginda579186b2012-09-26 11:40:04 -07002159 // The moveCount is the number of rows we need to relocate to make room for
2160 // the new row(s). The count is the distance to move them.
2161 var moveCount = bottom - cursorRow - count + 1;
2162 if (moveCount)
2163 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002164
Robert Ginda579186b2012-09-26 11:40:04 -07002165 for (var i = count - 1; i >= 0; i--) {
2166 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002167 this.screen_.clearCursorRow();
2168 }
rginda8ba33642011-12-14 12:31:31 -08002169};
2170
2171/**
2172 * VT command to delete lines at the current cursor row.
2173 *
2174 * New rows are added to the bottom of scroll region to take their place. New
2175 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002176 *
2177 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002178 */
2179hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002180 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002181
rginda87b86462011-12-14 13:48:03 -08002182 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002183 var bottom = this.getVTScrollBottom();
2184
rginda87b86462011-12-14 13:48:03 -08002185 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002186 count = Math.min(count, maxCount);
2187
rginda87b86462011-12-14 13:48:03 -08002188 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002189 if (count != maxCount)
2190 this.moveRows_(top, count, moveStart);
2191
2192 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002193 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002194 this.screen_.clearCursorRow();
2195 }
2196
rginda87b86462011-12-14 13:48:03 -08002197 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002198 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002199};
2200
2201/**
2202 * Inserts the given number of spaces at the current cursor position.
2203 *
rginda87b86462011-12-14 13:48:03 -08002204 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002205 *
2206 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002207 */
2208hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002209 var cursor = this.saveCursor();
2210
rgindacbbd7482012-06-13 15:06:16 -07002211 var ws = lib.f.getWhitespace(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002212 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002213 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002214
2215 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002216 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002217};
2218
2219/**
2220 * Forward-delete the specified number of characters starting at the cursor
2221 * position.
2222 *
2223 * @param {integer} count The number of characters to delete.
2224 */
2225hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002226 var deleted = this.screen_.deleteChars(count);
2227 if (deleted && !this.screen_.textAttributes.isDefault()) {
2228 var cursor = this.saveCursor();
2229 this.setCursorColumn(this.screenSize.width - deleted);
2230 this.screen_.insertString(lib.f.getWhitespace(deleted));
2231 this.restoreCursor(cursor);
2232 }
2233
David Benjamin54e8bf62012-06-01 22:31:40 -04002234 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002235};
2236
2237/**
2238 * Shift rows in the scroll region upwards by a given number of lines.
2239 *
2240 * New rows are inserted at the bottom of the scroll region to fill the
2241 * vacated rows. The new rows not filled out with the current text attributes.
2242 *
2243 * This function does not affect the scrollback rows at all. Rows shifted
2244 * off the top are lost.
2245 *
rginda87b86462011-12-14 13:48:03 -08002246 * The cursor position is not altered.
2247 *
rginda8ba33642011-12-14 12:31:31 -08002248 * @param {integer} count The number of rows to scroll.
2249 */
2250hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002251 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002252
rginda87b86462011-12-14 13:48:03 -08002253 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002254 this.deleteLines(count);
2255
rginda87b86462011-12-14 13:48:03 -08002256 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002257};
2258
2259/**
2260 * Shift rows below the cursor down by a given number of lines.
2261 *
2262 * This function respects the current scroll region.
2263 *
2264 * New rows are inserted at the top of the scroll region to fill the
2265 * vacated rows. The new rows not filled out with the current text attributes.
2266 *
2267 * This function does not affect the scrollback rows at all. Rows shifted
2268 * off the bottom are lost.
2269 *
2270 * @param {integer} count The number of rows to scroll.
2271 */
2272hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08002273 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002274
rginda87b86462011-12-14 13:48:03 -08002275 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08002276 this.insertLines(opt_count);
2277
rginda87b86462011-12-14 13:48:03 -08002278 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002279};
2280
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002281/**
2282 * Set live output for accessibility.
2283 *
2284 * This will generate additional DOM nodes in an aria-live region that will
2285 * cause Assitive Technology to announce the output of the terminal. This isn't
2286 * enabled by default as it can have a performance impact.
2287 *
2288 * @param {boolean} enabled Whether to enable live output.
2289 */
2290hterm.Terminal.prototype.setLiveOutputForAccessibility = function(enabled) {
2291 this.accessibilityEnabled_ = enabled;
2292};
rginda87b86462011-12-14 13:48:03 -08002293
rginda8ba33642011-12-14 12:31:31 -08002294/**
2295 * Set the cursor position.
2296 *
2297 * The cursor row is relative to the scroll region if the terminal has
2298 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2299 *
2300 * @param {integer} row The new zero-based cursor row.
2301 * @param {integer} row The new zero-based cursor column.
2302 */
2303hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2304 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002305 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002306 } else {
rginda87b86462011-12-14 13:48:03 -08002307 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002308 }
rginda87b86462011-12-14 13:48:03 -08002309};
rginda8ba33642011-12-14 12:31:31 -08002310
Evan Jones2600d4f2016-12-06 09:29:36 -05002311/**
2312 * Move the cursor relative to its current position.
2313 *
2314 * @param {number} row
2315 * @param {number} column
2316 */
rginda87b86462011-12-14 13:48:03 -08002317hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2318 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002319 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2320 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002321 this.screen_.setCursorPosition(row, column);
2322};
2323
Evan Jones2600d4f2016-12-06 09:29:36 -05002324/**
2325 * Move the cursor to the specified position.
2326 *
2327 * @param {number} row
2328 * @param {number} column
2329 */
rginda87b86462011-12-14 13:48:03 -08002330hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002331 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2332 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002333 this.screen_.setCursorPosition(row, column);
2334};
2335
2336/**
2337 * Set the cursor column.
2338 *
2339 * @param {integer} column The new zero-based cursor column.
2340 */
2341hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002342 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002343};
2344
2345/**
2346 * Return the cursor column.
2347 *
2348 * @return {integer} The zero-based cursor column.
2349 */
2350hterm.Terminal.prototype.getCursorColumn = function() {
2351 return this.screen_.cursorPosition.column;
2352};
2353
2354/**
2355 * Set the cursor row.
2356 *
2357 * The cursor row is relative to the scroll region if the terminal has
2358 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2359 *
2360 * @param {integer} row The new cursor row.
2361 */
rginda87b86462011-12-14 13:48:03 -08002362hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2363 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002364};
2365
2366/**
2367 * Return the cursor row.
2368 *
2369 * @return {integer} The zero-based cursor row.
2370 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002371hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002372 return this.screen_.cursorPosition.row;
2373};
2374
2375/**
2376 * Request that the ScrollPort redraw itself soon.
2377 *
2378 * The redraw will happen asynchronously, soon after the call stack winds down.
2379 * Multiple calls will be coalesced into a single redraw.
2380 */
2381hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002382 if (this.timeouts_.redraw)
2383 return;
rginda8ba33642011-12-14 12:31:31 -08002384
2385 var self = this;
rginda87b86462011-12-14 13:48:03 -08002386 this.timeouts_.redraw = setTimeout(function() {
2387 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002388 self.scrollPort_.redraw_();
2389 }, 0);
2390};
2391
2392/**
2393 * Request that the ScrollPort be scrolled to the bottom.
2394 *
2395 * The scroll will happen asynchronously, soon after the call stack winds down.
2396 * Multiple calls will be coalesced into a single scroll.
2397 *
2398 * This affects the scrollbar position of the ScrollPort, and has nothing to
2399 * do with the VT scroll commands.
2400 */
2401hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2402 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002403 return;
rginda8ba33642011-12-14 12:31:31 -08002404
2405 var self = this;
2406 this.timeouts_.scrollDown = setTimeout(function() {
2407 delete self.timeouts_.scrollDown;
2408 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2409 }, 10);
2410};
2411
2412/**
2413 * Move the cursor up a specified number of rows.
2414 *
2415 * @param {integer} count The number of rows to move the cursor.
2416 */
2417hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002418 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002419};
2420
2421/**
2422 * Move the cursor down a specified number of rows.
2423 *
2424 * @param {integer} count The number of rows to move the cursor.
2425 */
2426hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002427 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002428 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2429 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2430 this.screenSize.height - 1);
2431
rgindacbbd7482012-06-13 15:06:16 -07002432 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002433 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002434 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002435};
2436
2437/**
2438 * Move the cursor left a specified number of columns.
2439 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002440 * If reverse wraparound mode is enabled and the previous row wrapped into
2441 * the current row then we back up through the wraparound as well.
2442 *
rginda8ba33642011-12-14 12:31:31 -08002443 * @param {integer} count The number of columns to move the cursor.
2444 */
2445hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002446 count = count || 1;
2447
2448 if (count < 1)
2449 return;
2450
2451 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002452 if (this.options_.reverseWraparound) {
2453 if (this.screen_.cursorPosition.overflow) {
2454 // If this cursor is in the right margin, consume one count to get it
2455 // back to the last column. This only applies when we're in reverse
2456 // wraparound mode.
2457 count--;
2458 this.clearCursorOverflow();
2459
2460 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002461 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002462 }
2463
Robert Gindabfb32622014-07-17 13:20:27 -07002464 var newRow = this.screen_.cursorPosition.row;
2465 var newColumn = currentColumn - count;
2466 if (newColumn < 0) {
2467 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2468 if (newRow < 0) {
2469 // xterm also wraps from row 0 to the last row.
2470 newRow = this.screenSize.height + newRow % this.screenSize.height;
2471 }
2472 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2473 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002474
Robert Gindabfb32622014-07-17 13:20:27 -07002475 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2476
2477 } else {
2478 var newColumn = Math.max(currentColumn - count, 0);
2479 this.setCursorColumn(newColumn);
2480 }
rginda8ba33642011-12-14 12:31:31 -08002481};
2482
2483/**
2484 * Move the cursor right a specified number of columns.
2485 *
2486 * @param {integer} count The number of columns to move the cursor.
2487 */
2488hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002489 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002490
2491 if (count < 1)
2492 return;
2493
rgindacbbd7482012-06-13 15:06:16 -07002494 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002495 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002496 this.setCursorColumn(column);
2497};
2498
2499/**
2500 * Reverse the foreground and background colors of the terminal.
2501 *
2502 * This only affects text that was drawn with no attributes.
2503 *
2504 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2505 * been drawn with attributes that happen to coincide with the default
2506 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002507 *
2508 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002509 */
2510hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002511 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002512 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002513 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2514 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002515 } else {
rginda9f5222b2012-03-05 11:53:28 -08002516 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2517 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002518 }
2519};
2520
2521/**
rginda87b86462011-12-14 13:48:03 -08002522 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002523 *
2524 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002525 */
2526hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002527 this.cursorNode_.style.backgroundColor =
2528 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002529
2530 var self = this;
2531 setTimeout(function() {
Matheus Fernandes2d733082017-09-11 06:43:01 -04002532 self.restyleCursor_();
rginda6d397402012-01-17 10:58:29 -08002533 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002534
Michael Kelly485ecd12014-06-09 11:41:56 -04002535 // bellSquelchTimeout_ affects both audio and notification bells.
2536 if (this.bellSquelchTimeout_)
2537 return;
2538
Robert Ginda92e18102013-03-14 13:56:37 -07002539 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002540 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002541 this.bellSequelchTimeout_ = setTimeout(function() {
2542 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002543 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002544 } else {
2545 delete this.bellSquelchTimeout_;
2546 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002547
2548 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002549 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002550 this.bellNotificationList_.push(n);
2551 // TODO: Should we try to raise the window here?
2552 n.onclick = function() { self.closeBellNotifications_(); };
2553 }
rginda87b86462011-12-14 13:48:03 -08002554};
2555
2556/**
rginda8ba33642011-12-14 12:31:31 -08002557 * Set the origin mode bit.
2558 *
2559 * If origin mode is on, certain VT cursor and scrolling commands measure their
2560 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2561 * to the top of the addressable screen.
2562 *
2563 * Defaults to off.
2564 *
2565 * @param {boolean} state True to set origin mode, false to unset.
2566 */
2567hterm.Terminal.prototype.setOriginMode = function(state) {
2568 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002569 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002570};
2571
2572/**
2573 * Set the insert mode bit.
2574 *
2575 * If insert mode is on, existing text beyond the cursor position will be
2576 * shifted right to make room for new text. Otherwise, new text overwrites
2577 * any existing text.
2578 *
2579 * Defaults to off.
2580 *
2581 * @param {boolean} state True to set insert mode, false to unset.
2582 */
2583hterm.Terminal.prototype.setInsertMode = function(state) {
2584 this.options_.insertMode = state;
2585};
2586
2587/**
rginda87b86462011-12-14 13:48:03 -08002588 * Set the auto carriage return bit.
2589 *
2590 * If auto carriage return is on then a formfeed character is interpreted
2591 * as a newline, otherwise it's the same as a linefeed. The difference boils
2592 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002593 *
2594 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002595 */
2596hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2597 this.options_.autoCarriageReturn = state;
2598};
2599
2600/**
rginda8ba33642011-12-14 12:31:31 -08002601 * Set the wraparound mode bit.
2602 *
2603 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2604 * to the start of the following row. Otherwise, the cursor is clamped to the
2605 * end of the screen and attempts to write past it are ignored.
2606 *
2607 * Defaults to on.
2608 *
2609 * @param {boolean} state True to set wraparound mode, false to unset.
2610 */
2611hterm.Terminal.prototype.setWraparound = function(state) {
2612 this.options_.wraparound = state;
2613};
2614
2615/**
2616 * Set the reverse-wraparound mode bit.
2617 *
2618 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2619 * to the end of the previous row. Otherwise, the cursor is clamped to column
2620 * 0.
2621 *
2622 * Defaults to off.
2623 *
2624 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2625 */
2626hterm.Terminal.prototype.setReverseWraparound = function(state) {
2627 this.options_.reverseWraparound = state;
2628};
2629
2630/**
2631 * Selects between the primary and alternate screens.
2632 *
2633 * If alternate mode is on, the alternate screen is active. Otherwise the
2634 * primary screen is active.
2635 *
2636 * Swapping screens has no effect on the scrollback buffer.
2637 *
2638 * Each screen maintains its own cursor position.
2639 *
2640 * Defaults to off.
2641 *
2642 * @param {boolean} state True to set alternate mode, false to unset.
2643 */
2644hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002645 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002646 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2647
rginda35c456b2012-02-09 17:29:05 -08002648 if (this.screen_.rowsArray.length &&
2649 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2650 // If the screen changed sizes while we were away, our rowIndexes may
2651 // be incorrect.
2652 var offset = this.scrollbackRows_.length;
2653 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002654 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002655 ary[i].rowIndex = offset + i;
2656 }
2657 }
rginda8ba33642011-12-14 12:31:31 -08002658
rginda35c456b2012-02-09 17:29:05 -08002659 this.realizeWidth_(this.screenSize.width);
2660 this.realizeHeight_(this.screenSize.height);
2661 this.scrollPort_.syncScrollHeight();
2662 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002663
rginda6d397402012-01-17 10:58:29 -08002664 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002665 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002666};
2667
2668/**
2669 * Set the cursor-blink mode bit.
2670 *
2671 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2672 * a visible cursor does not blink.
2673 *
2674 * You should make sure to turn blinking off if you're going to dispose of a
2675 * terminal, otherwise you'll leak a timeout.
2676 *
2677 * Defaults to on.
2678 *
2679 * @param {boolean} state True to set cursor-blink mode, false to unset.
2680 */
2681hterm.Terminal.prototype.setCursorBlink = function(state) {
2682 this.options_.cursorBlink = state;
2683
2684 if (!state && this.timeouts_.cursorBlink) {
2685 clearTimeout(this.timeouts_.cursorBlink);
2686 delete this.timeouts_.cursorBlink;
2687 }
2688
2689 if (this.options_.cursorVisible)
2690 this.setCursorVisible(true);
2691};
2692
2693/**
2694 * Set the cursor-visible mode bit.
2695 *
2696 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2697 *
2698 * Defaults to on.
2699 *
2700 * @param {boolean} state True to set cursor-visible mode, false to unset.
2701 */
2702hterm.Terminal.prototype.setCursorVisible = function(state) {
2703 this.options_.cursorVisible = state;
2704
2705 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002706 if (this.timeouts_.cursorBlink) {
2707 clearTimeout(this.timeouts_.cursorBlink);
2708 delete this.timeouts_.cursorBlink;
2709 }
rginda87b86462011-12-14 13:48:03 -08002710 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002711 return;
2712 }
2713
rginda87b86462011-12-14 13:48:03 -08002714 this.syncCursorPosition_();
2715
2716 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002717
2718 if (this.options_.cursorBlink) {
2719 if (this.timeouts_.cursorBlink)
2720 return;
2721
Robert Gindaea2183e2014-07-17 09:51:51 -07002722 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002723 } else {
2724 if (this.timeouts_.cursorBlink) {
2725 clearTimeout(this.timeouts_.cursorBlink);
2726 delete this.timeouts_.cursorBlink;
2727 }
2728 }
2729};
2730
2731/**
rginda87b86462011-12-14 13:48:03 -08002732 * Synchronizes the visible cursor and document selection with the current
2733 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002734 */
2735hterm.Terminal.prototype.syncCursorPosition_ = function() {
2736 var topRowIndex = this.scrollPort_.getTopRowIndex();
2737 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2738 var cursorRowIndex = this.scrollbackRows_.length +
2739 this.screen_.cursorPosition.row;
2740
2741 if (cursorRowIndex > bottomRowIndex) {
2742 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04002743 this.setCssVar('cursor-offset-row', '-1');
rginda8ba33642011-12-14 12:31:31 -08002744 return;
2745 }
2746
Robert Gindab837c052014-08-11 11:17:51 -07002747 if (this.options_.cursorVisible &&
2748 this.cursorNode_.style.display == 'none') {
2749 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2750 this.cursorNode_.style.display = '';
2751 }
2752
Mike Frysinger44c32202017-08-05 01:13:09 -04002753 // Position the cursor using CSS variable math. If we do the math in JS,
2754 // the float math will end up being more precise than the CSS which will
2755 // cause the cursor tracking to be off.
2756 this.setCssVar(
2757 'cursor-offset-row',
2758 `${cursorRowIndex - topRowIndex} + ` +
2759 `${this.scrollPort_.visibleRowTopMargin}px`);
2760 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08002761
2762 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04002763 '(' + this.screen_.cursorPosition.column +
2764 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08002765 ')');
2766
2767 // Update the caret for a11y purposes.
2768 var selection = this.document_.getSelection();
2769 if (selection && selection.isCollapsed)
2770 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002771};
2772
Robert Gindafb1be6a2013-12-11 11:56:22 -08002773/**
2774 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2775 * and character cell dimensions.
2776 */
Robert Ginda830583c2013-08-07 13:20:46 -07002777hterm.Terminal.prototype.restyleCursor_ = function() {
2778 var shape = this.cursorShape_;
2779
2780 if (this.cursorNode_.getAttribute('focus') == 'false') {
2781 // Always show a block cursor when unfocused.
2782 shape = hterm.Terminal.cursorShape.BLOCK;
2783 }
2784
2785 var style = this.cursorNode_.style;
2786
2787 switch (shape) {
2788 case hterm.Terminal.cursorShape.BEAM:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002789 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002790 style.backgroundColor = 'transparent';
2791 style.borderBottomStyle = null;
2792 style.borderLeftStyle = 'solid';
2793 break;
2794
2795 case hterm.Terminal.cursorShape.UNDERLINE:
2796 style.height = this.scrollPort_.characterSize.baseline + 'px';
2797 style.backgroundColor = 'transparent';
2798 style.borderBottomStyle = 'solid';
2799 // correct the size to put it exactly at the baseline
2800 style.borderLeftStyle = null;
2801 break;
2802
2803 default:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002804 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002805 style.backgroundColor = this.cursorColor_;
2806 style.borderBottomStyle = null;
2807 style.borderLeftStyle = null;
2808 break;
2809 }
2810};
2811
rginda8ba33642011-12-14 12:31:31 -08002812/**
2813 * Synchronizes the visible cursor with the current cursor coordinates.
2814 *
2815 * The sync will happen asynchronously, soon after the call stack winds down.
2816 * Multiple calls will be coalesced into a single sync.
2817 */
2818hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2819 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002820 return;
rginda8ba33642011-12-14 12:31:31 -08002821
2822 var self = this;
2823 this.timeouts_.syncCursor = setTimeout(function() {
2824 self.syncCursorPosition_();
2825 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002826 }, 0);
2827};
2828
rgindacc2996c2012-02-24 14:59:31 -08002829/**
rgindaf522ce02012-04-17 17:49:17 -07002830 * Show or hide the zoom warning.
2831 *
2832 * The zoom warning is a message warning the user that their browser zoom must
2833 * be set to 100% in order for hterm to function properly.
2834 *
2835 * @param {boolean} state True to show the message, false to hide it.
2836 */
2837hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2838 if (!this.zoomWarningNode_) {
2839 if (!state)
2840 return;
2841
2842 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002843 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07002844 this.zoomWarningNode_.style.cssText = (
2845 'color: black;' +
2846 'background-color: #ff2222;' +
2847 'font-size: large;' +
2848 'border-radius: 8px;' +
2849 'opacity: 0.75;' +
2850 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2851 'top: 0.5em;' +
2852 'right: 1.2em;' +
2853 'position: absolute;' +
2854 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002855 '-webkit-user-select: none;' +
2856 '-moz-text-size-adjust: none;' +
2857 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002858
2859 this.zoomWarningNode_.addEventListener('click', function(e) {
2860 this.parentNode.removeChild(this);
2861 });
rgindaf522ce02012-04-17 17:49:17 -07002862 }
2863
Robert Gindab4839c22013-02-28 16:52:10 -08002864 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2865 hterm.zoomWarningMessage,
2866 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2867
rgindaf522ce02012-04-17 17:49:17 -07002868 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2869
2870 if (state) {
2871 if (!this.zoomWarningNode_.parentNode)
2872 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2873 } else if (this.zoomWarningNode_.parentNode) {
2874 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2875 }
2876};
2877
2878/**
rgindacc2996c2012-02-24 14:59:31 -08002879 * Show the terminal overlay for a given amount of time.
2880 *
2881 * The terminal overlay appears in inverse video in a large font, centered
2882 * over the terminal. You should probably keep the overlay message brief,
2883 * since it's in a large font and you probably aren't going to check the size
2884 * of the terminal first.
2885 *
2886 * @param {string} msg The text (not HTML) message to display in the overlay.
2887 * @param {number} opt_timeout The amount of time to wait before fading out
2888 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2889 * stay up forever (or until the next overlay).
2890 */
2891hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002892 if (!this.overlayNode_) {
2893 if (!this.div_)
2894 return;
2895
2896 this.overlayNode_ = this.document_.createElement('div');
2897 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002898 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002899 'font-size: xx-large;' +
2900 'opacity: 0.75;' +
2901 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2902 'position: absolute;' +
2903 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002904 '-webkit-transition: opacity 180ms ease-in;' +
2905 '-moz-user-select: none;' +
2906 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002907
2908 this.overlayNode_.addEventListener('mousedown', function(e) {
2909 e.preventDefault();
2910 e.stopPropagation();
2911 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002912 }
2913
rginda9f5222b2012-03-05 11:53:28 -08002914 this.overlayNode_.style.color = this.prefs_.get('background-color');
2915 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2916 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2917
rgindaf0090c92012-02-10 14:58:52 -08002918 this.overlayNode_.textContent = msg;
2919 this.overlayNode_.style.opacity = '0.75';
2920
2921 if (!this.overlayNode_.parentNode)
2922 this.div_.appendChild(this.overlayNode_);
2923
Robert Ginda97769282013-02-01 15:30:30 -08002924 var divSize = hterm.getClientSize(this.div_);
2925 var overlaySize = hterm.getClientSize(this.overlayNode_);
2926
Robert Ginda8a59f762014-07-23 11:29:55 -07002927 this.overlayNode_.style.top =
2928 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08002929 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07002930 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08002931
rgindaf0090c92012-02-10 14:58:52 -08002932 if (this.overlayTimeout_)
2933 clearTimeout(this.overlayTimeout_);
2934
rgindacc2996c2012-02-24 14:59:31 -08002935 if (opt_timeout === null)
2936 return;
2937
Mike Frysingerb6cfded2017-09-18 00:39:31 -04002938 this.overlayTimeout_ = setTimeout(() => {
2939 this.overlayNode_.style.opacity = '0';
2940 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
2941 }, opt_timeout || 1500);
2942};
2943
2944/**
2945 * Hide the terminal overlay immediately.
2946 *
2947 * Useful when we show an overlay for an event with an unknown end time.
2948 */
2949hterm.Terminal.prototype.hideOverlay = function() {
2950 if (this.overlayTimeout_)
2951 clearTimeout(this.overlayTimeout_);
2952 this.overlayTimeout_ = null;
2953
2954 if (this.overlayNode_.parentNode)
2955 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
2956 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08002957};
2958
rginda4bba5e12012-06-20 16:15:30 -07002959/**
2960 * Paste from the system clipboard to the terminal.
2961 */
2962hterm.Terminal.prototype.paste = function() {
Mike Frysinger4628ad22017-07-20 02:44:20 -04002963 return hterm.pasteFromClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002964};
2965
2966/**
2967 * Copy a string to the system clipboard.
2968 *
2969 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05002970 *
2971 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07002972 */
2973hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002974 if (this.prefs_.get('enable-clipboard-notice'))
2975 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2976
rgindaa09e7332012-08-17 12:49:51 -07002977 var copySource = this.document_.createElement('pre');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002978 copySource.id = 'hterm:copy-to-clipboard-source';
rginda4bba5e12012-06-20 16:15:30 -07002979 copySource.textContent = str;
2980 copySource.style.cssText = (
2981 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002982 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07002983 'position: absolute;' +
2984 'top: -99px');
2985
2986 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002987
rginda4bba5e12012-06-20 16:15:30 -07002988 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002989 var anchorNode = selection.anchorNode;
2990 var anchorOffset = selection.anchorOffset;
2991 var focusNode = selection.focusNode;
2992 var focusOffset = selection.focusOffset;
2993
rginda4bba5e12012-06-20 16:15:30 -07002994 selection.selectAllChildren(copySource);
2995
rgindaa09e7332012-08-17 12:49:51 -07002996 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002997
Rob Spies56953412014-04-28 14:09:47 -07002998 // IE doesn't support selection.extend. This means that the selection
2999 // won't return on IE.
Robert Ginda6f4f0aa2014-07-28 10:21:39 -07003000 if (selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07003001 selection.collapse(anchorNode, anchorOffset);
3002 selection.extend(focusNode, focusOffset);
3003 }
rgindafaa74742012-08-21 13:34:03 -07003004
rginda4bba5e12012-06-20 16:15:30 -07003005 copySource.parentNode.removeChild(copySource);
3006};
3007
Evan Jones2600d4f2016-12-06 09:29:36 -05003008/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003009 * Display an image.
3010 *
3011 * @param {Object} options The image to display.
3012 * @param {string=} options.name A human readable string for the image.
3013 * @param {string|number=} options.size The size (in bytes).
3014 * @param {boolean=} options.preserveAspectRatio Whether to preserve aspect.
3015 * @param {boolean=} options.inline Whether to display the image inline.
3016 * @param {string|number=} options.width The width of the image.
3017 * @param {string|number=} options.height The height of the image.
3018 * @param {string=} options.align Direction to align the image.
3019 * @param {string} options.uri The source URI for the image.
3020 */
3021hterm.Terminal.prototype.displayImage = function(options) {
3022 // Make sure we're actually given a resource to display.
3023 if (options.uri === undefined)
3024 return;
3025
3026 // Set up the defaults to simplify code below.
3027 if (!options.name)
3028 options.name = '';
3029
3030 // Has the user approved image display yet?
3031 if (this.allowImagesInline !== true) {
3032 this.newLine();
3033 const row = this.getRowNode(this.scrollbackRows_.length +
3034 this.getCursorRow() - 1);
3035
3036 if (this.allowImagesInline === false) {
3037 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3038 'Inline Images Disabled');
3039 return;
3040 }
3041
3042 // Show a prompt.
3043 let button;
3044 const span = this.document_.createElement('span');
3045 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3046 span.style.fontWeight = 'bold';
3047 span.style.borderWidth = '1px';
3048 span.style.borderStyle = 'dashed';
3049 button = this.document_.createElement('span');
3050 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3051 button.style.marginLeft = '1em';
3052 button.style.borderWidth = '1px';
3053 button.style.borderStyle = 'solid';
3054 button.addEventListener('click', () => {
3055 this.prefs_.set('allow-images-inline', false);
3056 });
3057 span.appendChild(button);
3058 button = this.document_.createElement('span');
3059 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3060 'allow this session');
3061 button.style.marginLeft = '1em';
3062 button.style.borderWidth = '1px';
3063 button.style.borderStyle = 'solid';
3064 button.addEventListener('click', () => {
3065 this.allowImagesInline = true;
3066 });
3067 span.appendChild(button);
3068 button = this.document_.createElement('span');
3069 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3070 button.style.marginLeft = '1em';
3071 button.style.borderWidth = '1px';
3072 button.style.borderStyle = 'solid';
3073 button.addEventListener('click', () => {
3074 this.prefs_.set('allow-images-inline', true);
3075 });
3076 span.appendChild(button);
3077
3078 row.appendChild(span);
3079 return;
3080 }
3081
3082 // See if we should show this object directly, or download it.
3083 if (options.inline) {
3084 const io = this.io.push();
3085 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
3086 'Loading $1 ...'), null);
3087
3088 // While we're loading the image, eat all the user's input.
3089 io.onVTKeystroke = io.sendString = () => {};
3090
3091 // Initialize this new image.
3092 const img = this.document_.createElement('img');
3093 img.src = options.uri;
3094 img.title = img.alt = options.name;
3095
3096 // Attach the image to the page to let it load/render. It won't stay here.
3097 // This is needed so it's visible and the DOM can calculate the height. If
3098 // the image is hidden or not in the DOM, the height is always 0.
3099 this.document_.body.appendChild(img);
3100
3101 // Wait for the image to finish loading before we try moving it to the
3102 // right place in the terminal.
3103 img.onload = () => {
3104 // Now that we have the image dimensions, figure out how to show it.
3105 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
3106 img.style.maxWidth = `${this.document_.body.clientWidth}px`;
3107 img.style.maxHeight = `${this.document_.body.clientHeight}px`;
3108
3109 // Parse a width/height specification.
3110 const parseDim = (dim, maxDim, cssVar) => {
3111 if (!dim || dim == 'auto')
3112 return '';
3113
3114 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3115 if (ary) {
3116 if (ary[2] == '%')
3117 return maxDim * parseInt(ary[1]) / 100 + 'px';
3118 else if (ary[2] == 'px')
3119 return dim;
3120 else
3121 return `calc(${dim} * var(${cssVar}))`;
3122 }
3123
3124 return '';
3125 };
3126 img.style.width =
3127 parseDim(options.width, this.document_.body.clientWidth,
3128 '--hterm-charsize-width');
3129 img.style.height =
3130 parseDim(options.height, this.document_.body.clientHeight,
3131 '--hterm-charsize-height');
3132
3133 // Figure out how many rows the image occupies, then add that many.
3134 // XXX: This count will be inaccurate if the font size changes on us.
3135 const padRows = Math.ceil(img.clientHeight /
3136 this.scrollPort_.characterSize.height);
3137 for (let i = 0; i < padRows; ++i)
3138 this.newLine();
3139
3140 // Update the max height in case the user shrinks the character size.
3141 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3142
3143 // Move the image to the last row. This way when we scroll up, it doesn't
3144 // disappear when the first row gets clipped. It will disappear when we
3145 // scroll down and the last row is clipped ...
3146 this.document_.body.removeChild(img);
3147 // Create a wrapper node so we can do an absolute in a relative position.
3148 // This helps with rounding errors between JS & CSS counts.
3149 const div = this.document_.createElement('div');
3150 div.style.position = 'relative';
3151 div.style.textAlign = options.align;
3152 img.style.position = 'absolute';
3153 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3154 div.appendChild(img);
3155 const row = this.getRowNode(this.scrollbackRows_.length +
3156 this.getCursorRow() - 1);
3157 row.appendChild(div);
3158
3159 io.hideOverlay();
3160 io.pop();
3161 };
3162
3163 // If we got a malformed image, give up.
3164 img.onerror = (e) => {
3165 this.document_.body.removeChild(img);
3166 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
3167 'Loading $1 failed ...'));
3168 io.pop();
3169 };
3170 } else {
3171 // We can't use chrome.downloads.download as that requires "downloads"
3172 // permissions, and that works only in extensions, not apps.
3173 const a = this.document_.createElement('a');
3174 a.href = options.uri;
3175 a.download = options.name;
3176 this.document_.body.appendChild(a);
3177 a.click();
3178 a.remove();
3179 }
3180};
3181
3182/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003183 * Returns the selected text, or null if no text is selected.
3184 *
3185 * @return {string|null}
3186 */
rgindaa09e7332012-08-17 12:49:51 -07003187hterm.Terminal.prototype.getSelectionText = function() {
3188 var selection = this.scrollPort_.selection;
3189 selection.sync();
3190
3191 if (selection.isCollapsed)
3192 return null;
3193
3194
3195 // Start offset measures from the beginning of the line.
3196 var startOffset = selection.startOffset;
3197 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003198
Robert Gindafdbb3f22012-09-06 20:23:06 -07003199 if (node.nodeName != 'X-ROW') {
3200 // If the selection doesn't start on an x-row node, then it must be
3201 // somewhere inside the x-row. Add any characters from previous siblings
3202 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003203
3204 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3205 // If node is the text node in a styled span, move up to the span node.
3206 node = node.parentNode;
3207 }
3208
Robert Gindafdbb3f22012-09-06 20:23:06 -07003209 while (node.previousSibling) {
3210 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003211 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003212 }
rgindaa09e7332012-08-17 12:49:51 -07003213 }
3214
3215 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08003216 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
3217 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003218 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003219
Robert Gindafdbb3f22012-09-06 20:23:06 -07003220 if (node.nodeName != 'X-ROW') {
3221 // If the selection doesn't end on an x-row node, then it must be
3222 // somewhere inside the x-row. Add any characters from following siblings
3223 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003224
3225 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3226 // If node is the text node in a styled span, move up to the span node.
3227 node = node.parentNode;
3228 }
3229
Robert Gindafdbb3f22012-09-06 20:23:06 -07003230 while (node.nextSibling) {
3231 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003232 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003233 }
rgindaa09e7332012-08-17 12:49:51 -07003234 }
3235
3236 var rv = this.getRowsText(selection.startRow.rowIndex,
3237 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003238 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003239};
3240
rginda4bba5e12012-06-20 16:15:30 -07003241/**
3242 * Copy the current selection to the system clipboard, then clear it after a
3243 * short delay.
3244 */
3245hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07003246 var text = this.getSelectionText();
3247 if (text != null)
3248 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07003249};
3250
rgindaf0090c92012-02-10 14:58:52 -08003251hterm.Terminal.prototype.overlaySize = function() {
3252 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
3253};
3254
rginda87b86462011-12-14 13:48:03 -08003255/**
3256 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3257 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003258 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003259 */
3260hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08003261 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08003262 this.scrollPort_.scrollRowToBottom(this.getRowCount());
3263
Robert Ginda8cb7d902013-06-20 14:37:18 -07003264 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08003265};
3266
3267/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003268 * Open the selected url.
3269 */
3270hterm.Terminal.prototype.openSelectedUrl_ = function() {
3271 var str = this.getSelectionText();
3272
3273 // If there is no selection, try and expand wherever they clicked.
3274 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003275 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003276 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003277
3278 // If clicking in empty space, return.
3279 if (str == null)
3280 return;
Mike Frysinger70b94692017-01-26 18:57:50 -10003281 }
3282
3283 // Make sure URL is valid before opening.
3284 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
3285 return;
Mike Frysinger43472622017-06-26 18:11:07 -04003286
3287 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003288 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003289 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3290 // We have to whitelist a few protocols that lack authorities and thus
3291 // never use the //. Like mailto.
3292 switch (str.split(':', 1)[0]) {
3293 case 'mailto':
3294 break;
3295 default:
3296 str = 'http://' + str;
3297 break;
3298 }
3299 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003300
Mike Frysinger720fa832017-10-23 01:15:52 -04003301 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003302};
Mike Frysinger70b94692017-01-26 18:57:50 -10003303
3304
3305/**
rgindad5613292012-06-19 15:40:37 -07003306 * Add the terminalRow and terminalColumn properties to mouse events and
3307 * then forward on to onMouse().
3308 *
3309 * The terminalRow and terminalColumn properties contain the (row, column)
3310 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003311 *
3312 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003313 */
3314hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003315 if (e.processedByTerminalHandler_) {
3316 // We register our event handlers on the document, as well as the cursor
3317 // and the scroll blocker. Mouse events that occur on the cursor or
3318 // scroll blocker will also appear on the document, but we don't want to
3319 // process them twice.
3320 //
3321 // We can't just prevent bubbling because that has other side effects, so
3322 // we decorate the event object with this property instead.
3323 return;
3324 }
3325
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003326 var reportMouseEvents = (!this.defeatMouseReports_ &&
3327 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3328
rgindafaa74742012-08-21 13:34:03 -07003329 e.processedByTerminalHandler_ = true;
3330
Robert Gindaeda48db2014-07-17 09:25:30 -07003331 // One based row/column stored on the mouse event.
3332 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
3333 this.scrollPort_.characterSize.height) + 1;
3334 e.terminalColumn = parseInt(e.clientX /
3335 this.scrollPort_.characterSize.width) + 1;
3336
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003337 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3338 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003339 return;
3340 }
3341
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003342 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003343 // If the cursor is visible and we're not sending mouse events to the
3344 // host app, then we want to hide the terminal cursor when the mouse
3345 // cursor is over top. This keeps the terminal cursor from interfering
3346 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003347 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3348 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3349 this.cursorNode_.style.display = 'none';
3350 } else if (this.cursorNode_.style.display == 'none') {
3351 this.cursorNode_.style.display = '';
3352 }
3353 }
rgindad5613292012-06-19 15:40:37 -07003354
Robert Ginda928cf632014-03-05 15:07:41 -08003355 if (e.type == 'mousedown') {
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003356 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003357 // If VT mouse reporting is disabled, or has been defeated with
3358 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003359 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003360 this.setSelectionEnabled(true);
3361 } else {
3362 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003363 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003364 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003365 this.setSelectionEnabled(false);
3366 e.preventDefault();
3367 }
3368 }
3369
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003370 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003371 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003372 this.screen_.expandSelection(this.document_.getSelection());
John Lin2aad22e2018-03-16 13:58:11 +08003373 if (this.copyOnSelect)
3374 this.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07003375 }
3376
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003377 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003378 // Debounce this event with the dblclick event. If you try to doubleclick
3379 // a URL to open it, Chrome will fire click then dblclick, but we won't
3380 // have expanded the selection text at the first click event.
3381 clearTimeout(this.timeouts_.openUrl);
3382 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3383 500);
3384 return;
3385 }
3386
Mike Frysinger847577f2017-05-23 23:25:57 -04003387 if (e.type == 'mousedown') {
3388 if ((this.mouseRightClickPaste && e.button == 2 /* right button */) ||
Mike Frysinger2edd3612017-05-24 00:54:39 -04003389 e.button == this.mousePasteButton) {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003390 if (!this.paste())
Mike Frysinger05a57f02017-08-27 17:48:55 -04003391 console.warn('Could not paste manually due to web restrictions');
Mike Frysinger847577f2017-05-23 23:25:57 -04003392 }
3393 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003394
Mike Frysinger2edd3612017-05-24 00:54:39 -04003395 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003396 !this.document_.getSelection().isCollapsed) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003397 this.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003398 }
3399
3400 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3401 this.scrollBlockerNode_.engaged) {
3402 // Disengage the scroll-blocker after one of these events.
3403 this.scrollBlockerNode_.engaged = false;
3404 this.scrollBlockerNode_.style.top = '-99px';
3405 }
3406
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003407 // Emulate arrow key presses via scroll wheel events.
3408 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3409 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003410 if (e.type == 'wheel') {
3411 var delta = this.scrollPort_.scrollWheelDelta(e);
3412 var lines = lib.f.smartFloorDivide(
3413 Math.abs(delta), this.scrollPort_.characterSize.height);
3414
3415 var data = '\x1bO' + (delta < 0 ? 'B' : 'A');
3416 this.io.sendString(data.repeat(lines));
3417
3418 e.preventDefault();
3419 }
3420 }
Robert Ginda928cf632014-03-05 15:07:41 -08003421 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003422 if (!this.scrollBlockerNode_.engaged) {
3423 if (e.type == 'mousedown') {
3424 // Move the scroll-blocker into place if we want to keep the scrollport
3425 // from scrolling.
3426 this.scrollBlockerNode_.engaged = true;
3427 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3428 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3429 } else if (e.type == 'mousemove') {
3430 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3431 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003432 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003433 e.preventDefault();
3434 }
3435 }
Robert Ginda928cf632014-03-05 15:07:41 -08003436
3437 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003438 }
3439
Robert Ginda928cf632014-03-05 15:07:41 -08003440 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3441 // Restore this on mouseup in case it was temporarily defeated with a
3442 // alt-mousedown. Only do this when the selection is empty so that
3443 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003444 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003445 }
rgindad5613292012-06-19 15:40:37 -07003446};
3447
3448/**
3449 * Clients should override this if they care to know about mouse events.
3450 *
3451 * The event parameter will be a normal DOM mouse click event with additional
3452 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003453 *
3454 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003455 */
3456hterm.Terminal.prototype.onMouse = function(e) { };
3457
3458/**
rginda8e92a692012-05-20 19:37:20 -07003459 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003460 *
3461 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003462 */
Rob Spies06533ba2014-04-24 11:20:37 -07003463hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3464 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003465 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003466
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003467 if (this.reportFocus)
3468 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003469
Michael Kelly485ecd12014-06-09 11:41:56 -04003470 if (focused === true)
3471 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003472};
3473
3474/**
rginda8ba33642011-12-14 12:31:31 -08003475 * React when the ScrollPort is scrolled.
3476 */
3477hterm.Terminal.prototype.onScroll_ = function() {
3478 this.scheduleSyncCursorPosition_();
3479};
3480
3481/**
rginda9846e2f2012-01-27 13:53:33 -08003482 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003483 *
3484 * @param {Event} e The DOM paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003485 */
3486hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07003487 var data = e.text.replace(/\n/mg, '\r');
Connor Hegartyf525ceb2014-09-03 13:36:03 -07003488 data = this.keyboard.encode(data);
Mike Frysingere8c32c82018-03-11 14:57:28 -07003489 if (this.options_.bracketedPaste) {
3490 // We strip out most escape sequences as they can cause issues (like
3491 // inserting an \x1b[201~ midstream). We pass through whitespace
3492 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
3493 // This matches xterm behavior.
3494 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
3495 data = '\x1b[200~' + filter(data) + '\x1b[201~';
3496 }
Robert Gindaa063b202014-07-21 11:08:25 -07003497
3498 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003499};
3500
3501/**
rgindaa09e7332012-08-17 12:49:51 -07003502 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003503 *
3504 * @param {Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003505 */
3506hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003507 if (!this.useDefaultWindowCopy) {
3508 e.preventDefault();
3509 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3510 }
rgindaa09e7332012-08-17 12:49:51 -07003511};
3512
3513/**
rginda8ba33642011-12-14 12:31:31 -08003514 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003515 *
3516 * Note: This function should not directly contain code that alters the internal
3517 * state of the terminal. That kind of code belongs in realizeWidth or
3518 * realizeHeight, so that it can be executed synchronously in the case of a
3519 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003520 */
3521hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003522 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003523 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003524 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003525 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003526
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003527 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003528 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003529 // gets removed from the document or during the initial load, and we can't
3530 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003531 // This can also happen if called before the scrollPort calculates the
3532 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003533 return;
3534 }
3535
rgindaa8ba17d2012-08-15 14:41:10 -07003536 var isNewSize = (columnCount != this.screenSize.width ||
3537 rowCount != this.screenSize.height);
3538
3539 // We do this even if the size didn't change, just to be sure everything is
3540 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003541 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003542 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003543
3544 if (isNewSize)
3545 this.overlaySize();
3546
Robert Gindafb1be6a2013-12-11 11:56:22 -08003547 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003548 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08003549};
3550
3551/**
3552 * Service the cursor blink timeout.
3553 */
3554hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003555 if (!this.options_.cursorBlink) {
3556 delete this.timeouts_.cursorBlink;
3557 return;
3558 }
3559
Robert Ginda830583c2013-08-07 13:20:46 -07003560 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3561 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003562 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003563 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3564 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003565 } else {
rginda87b86462011-12-14 13:48:03 -08003566 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003567 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3568 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003569 }
3570};
David Reveman8f552492012-03-28 12:18:41 -04003571
3572/**
3573 * Set the scrollbar-visible mode bit.
3574 *
3575 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3576 * Otherwise it will not.
3577 *
3578 * Defaults to on.
3579 *
3580 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3581 */
3582hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3583 this.scrollPort_.setScrollbarVisible(state);
3584};
Michael Kelly485ecd12014-06-09 11:41:56 -04003585
3586/**
Rob Spies49039e52014-12-17 13:40:04 -08003587 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003588 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003589 *
3590 * Defaults to 1.
3591 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003592 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003593 */
3594hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3595 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3596};
3597
3598/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003599 * Close all web notifications created by terminal bells.
3600 */
3601hterm.Terminal.prototype.closeBellNotifications_ = function() {
3602 this.bellNotificationList_.forEach(function(n) {
3603 n.close();
3604 });
3605 this.bellNotificationList_.length = 0;
3606};