blob: 0650e6a1c533eebade4383f52b6b8a040a447cee [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.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001779 if (this.accessibilityEnabled_) {
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001780 this.accessibilityReader_.announce(str);
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001781 }
Raymes Khoury3e44bc92018-05-17 10:54:23 +10001782
rgindaa9abdd82012-08-06 18:05:09 -07001783 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001784
Ricky Liang48f05cb2013-12-31 23:35:29 +08001785 var strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04001786 // Fun edge case: If the string only contains zero width codepoints (like
1787 // combining characters), we make sure to iterate at least once below.
1788 if (strWidth == 0 && str)
1789 strWidth = 1;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001790
1791 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001792 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1793 this.screen_.commitLineOverflow();
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001794 this.newLine(true);
rgindaa09e7332012-08-17 12:49:51 -07001795 }
rgindaa19afe22012-01-25 15:40:22 -08001796
Ricky Liang48f05cb2013-12-31 23:35:29 +08001797 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001798 var didOverflow = false;
1799 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001800
rgindaa9abdd82012-08-06 18:05:09 -07001801 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1802 didOverflow = true;
1803 count = this.screenSize.width - this.screen_.cursorPosition.column;
1804 }
rgindaa19afe22012-01-25 15:40:22 -08001805
rgindaa9abdd82012-08-06 18:05:09 -07001806 if (didOverflow && !this.options_.wraparound) {
1807 // If the string overflowed the line but wraparound is off, then the
1808 // last printed character should be the last of the string.
1809 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001810 substr = lib.wc.substr(str, startOffset, count - 1) +
1811 lib.wc.substr(str, strWidth - 1);
1812 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001813 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001814 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001815 }
rgindaa19afe22012-01-25 15:40:22 -08001816
Ricky Liang48f05cb2013-12-31 23:35:29 +08001817 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1818 for (var i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001819 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
1820 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001821
1822 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001823 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001824 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001825 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001826 }
1827 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001828 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07001829 }
1830
1831 this.screen_.maybeClipCurrentRow();
1832 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001833 }
rginda8ba33642011-12-14 12:31:31 -08001834
1835 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001836
rginda9f5222b2012-03-05 11:53:28 -08001837 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001838 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001839};
1840
1841/**
rginda87b86462011-12-14 13:48:03 -08001842 * Set the VT scroll region.
1843 *
rginda87b86462011-12-14 13:48:03 -08001844 * This also resets the cursor position to the absolute (0, 0) position, since
1845 * that's what xterm appears to do.
1846 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001847 * Setting the scroll region to the full height of the terminal will clear
1848 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1849 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1850 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1851 * continue to work as most users would expect.
1852 *
rginda87b86462011-12-14 13:48:03 -08001853 * @param {integer} scrollTop The zero-based top of the scroll region.
1854 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1855 * inclusive.
1856 */
1857hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001858 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001859 this.vtScrollTop_ = null;
1860 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001861 } else {
1862 this.vtScrollTop_ = scrollTop;
1863 this.vtScrollBottom_ = scrollBottom;
1864 }
rginda87b86462011-12-14 13:48:03 -08001865};
1866
1867/**
rginda8ba33642011-12-14 12:31:31 -08001868 * Return the top row index according to the VT.
1869 *
1870 * This will return 0 unless the terminal has been told to restrict scrolling
1871 * to some lower row. It is used for some VT cursor positioning and scrolling
1872 * commands.
1873 *
1874 * @return {integer} The topmost row in the terminal's scroll region.
1875 */
1876hterm.Terminal.prototype.getVTScrollTop = function() {
1877 if (this.vtScrollTop_ != null)
1878 return this.vtScrollTop_;
1879
1880 return 0;
rginda87b86462011-12-14 13:48:03 -08001881};
rginda8ba33642011-12-14 12:31:31 -08001882
1883/**
1884 * Return the bottom row index according to the VT.
1885 *
1886 * This will return the height of the terminal unless the it has been told to
1887 * restrict scrolling to some higher row. It is used for some VT cursor
1888 * positioning and scrolling commands.
1889 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001890 * @return {integer} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001891 */
1892hterm.Terminal.prototype.getVTScrollBottom = function() {
1893 if (this.vtScrollBottom_ != null)
1894 return this.vtScrollBottom_;
1895
rginda87b86462011-12-14 13:48:03 -08001896 return this.screenSize.height - 1;
Mike Frysinger8416e0a2017-05-17 09:09:46 -04001897};
rginda8ba33642011-12-14 12:31:31 -08001898
1899/**
1900 * Process a '\n' character.
1901 *
1902 * If the cursor is on the final row of the terminal this will append a new
1903 * blank row to the screen and scroll the topmost row into the scrollback
1904 * buffer.
1905 *
1906 * Otherwise, this moves the cursor to column zero of the next row.
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001907 *
1908 * @param {boolean=} dueToOverflow Whether the newline is due to wraparound of
1909 * the terminal.
rginda8ba33642011-12-14 12:31:31 -08001910 */
Raymes Khouryf1c61ba2018-05-28 14:05:38 +10001911hterm.Terminal.prototype.newLine = function(dueToOverflow = false) {
1912 if (!dueToOverflow)
1913 this.accessibilityReader_.newLine();
1914
Robert Ginda9937abc2013-07-25 16:09:23 -07001915 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1916 this.screen_.rowsArray.length - 1);
1917
1918 if (this.vtScrollBottom_ != null) {
1919 // A VT Scroll region is active, we never append new rows.
1920 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1921 // We're at the end of the VT Scroll Region, perform a VT scroll.
1922 this.vtScrollUp(1);
1923 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1924 } else if (cursorAtEndOfScreen) {
1925 // We're at the end of the screen, the only thing to do is put the
1926 // cursor to column 0.
1927 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1928 } else {
1929 // Anywhere else, advance the cursor row, and reset the column.
1930 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1931 }
1932 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001933 // We're at the end of the screen. Append a new row to the terminal,
1934 // shifting the top row into the scrollback.
1935 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001936 } else {
rginda87b86462011-12-14 13:48:03 -08001937 // Anywhere else in the screen just moves the cursor.
1938 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001939 }
1940};
1941
1942/**
1943 * Like newLine(), except maintain the cursor column.
1944 */
1945hterm.Terminal.prototype.lineFeed = function() {
1946 var column = this.screen_.cursorPosition.column;
1947 this.newLine();
1948 this.setCursorColumn(column);
1949};
1950
1951/**
rginda87b86462011-12-14 13:48:03 -08001952 * If autoCarriageReturn is set then newLine(), else lineFeed().
1953 */
1954hterm.Terminal.prototype.formFeed = function() {
1955 if (this.options_.autoCarriageReturn) {
1956 this.newLine();
1957 } else {
1958 this.lineFeed();
1959 }
1960};
1961
1962/**
1963 * Move the cursor up one row, possibly inserting a blank line.
1964 *
1965 * The cursor column is not changed.
1966 */
1967hterm.Terminal.prototype.reverseLineFeed = function() {
1968 var scrollTop = this.getVTScrollTop();
1969 var currentRow = this.screen_.cursorPosition.row;
1970
1971 if (currentRow == scrollTop) {
1972 this.insertLines(1);
1973 } else {
1974 this.setAbsoluteCursorRow(currentRow - 1);
1975 }
1976};
1977
1978/**
rginda8ba33642011-12-14 12:31:31 -08001979 * Replace all characters to the left of the current cursor with the space
1980 * character.
1981 *
1982 * TODO(rginda): This should probably *remove* the characters (not just replace
1983 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001984 * position.
rginda8ba33642011-12-14 12:31:31 -08001985 */
1986hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001987 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001988 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04001989 const count = cursor.column + 1;
1990 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08001991 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001992};
1993
1994/**
David Benjamin684a9b72012-05-01 17:19:58 -04001995 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001996 *
1997 * The cursor position is unchanged.
1998 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001999 * If the current background color is not the default background color this
2000 * will insert spaces rather than delete. This is unfortunate because the
2001 * trailing space will affect text selection, but it's difficult to come up
2002 * with a way to style empty space that wouldn't trip up the hterm.Screen
2003 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07002004 *
2005 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
2006 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
2007 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05002008 *
2009 * @param {number} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08002010 */
2011hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07002012 if (this.screen_.cursorPosition.overflow)
2013 return;
2014
Robert Ginda7fd57082012-09-25 14:41:47 -07002015 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
2016 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07002017
2018 if (this.screen_.textAttributes.background ===
2019 this.screen_.textAttributes.DEFAULT_COLOR) {
2020 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002021 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002022 this.screen_.cursorPosition.column + count) {
2023 this.screen_.deleteChars(count);
2024 this.clearCursorOverflow();
2025 return;
2026 }
2027 }
2028
rginda87b86462011-12-14 13:48:03 -08002029 var cursor = this.saveCursor();
Mike Frysinger6380bed2017-08-24 18:46:39 -04002030 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08002031 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002032 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002033};
2034
2035/**
2036 * Erase the current line.
2037 *
2038 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002039 */
2040hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08002041 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002042 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002043 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002044 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002045};
2046
2047/**
David Benjamina08d78f2012-05-05 00:28:49 -04002048 * Erase all characters from the start of the screen to the current cursor
2049 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002050 *
2051 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002052 */
2053hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08002054 var cursor = this.saveCursor();
2055
2056 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002057
David Benjamina08d78f2012-05-05 00:28:49 -04002058 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002059 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002060 this.screen_.clearCursorRow();
2061 }
2062
rginda87b86462011-12-14 13:48:03 -08002063 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002064 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002065};
2066
2067/**
2068 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002069 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002070 *
2071 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002072 */
2073hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08002074 var cursor = this.saveCursor();
2075
2076 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002077
David Benjamina08d78f2012-05-05 00:28:49 -04002078 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08002079 for (var i = cursor.row + 1; i <= bottom; i++) {
2080 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002081 this.screen_.clearCursorRow();
2082 }
2083
rginda87b86462011-12-14 13:48:03 -08002084 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002085 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002086};
2087
2088/**
2089 * Fill the terminal with a given character.
2090 *
2091 * This methods does not respect the VT scroll region.
2092 *
2093 * @param {string} ch The character to use for the fill.
2094 */
2095hterm.Terminal.prototype.fill = function(ch) {
2096 var cursor = this.saveCursor();
2097
2098 this.setAbsoluteCursorPosition(0, 0);
2099 for (var row = 0; row < this.screenSize.height; row++) {
2100 for (var col = 0; col < this.screenSize.width; col++) {
2101 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002102 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002103 }
2104 }
2105
2106 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002107};
2108
2109/**
rginda9ea433c2012-03-16 11:57:00 -07002110 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002111 *
rginda9ea433c2012-03-16 11:57:00 -07002112 * This does not respect the scroll region.
2113 *
2114 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2115 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002116 */
rginda9ea433c2012-03-16 11:57:00 -07002117hterm.Terminal.prototype.clearHome = function(opt_screen) {
2118 var screen = opt_screen || this.screen_;
2119 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002120
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002121 this.accessibilityReader_.clear();
2122
rginda11057d52012-04-25 12:29:56 -07002123 if (bottom == 0) {
2124 // Empty screen, nothing to do.
2125 return;
2126 }
2127
rgindae4d29232012-01-19 10:47:13 -08002128 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002129 screen.setCursorPosition(i, 0);
2130 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002131 }
2132
rginda9ea433c2012-03-16 11:57:00 -07002133 screen.setCursorPosition(0, 0);
2134};
2135
2136/**
2137 * Erase the entire display without changing the cursor position.
2138 *
2139 * The cursor position is unchanged. This does not respect the scroll
2140 * region.
2141 *
2142 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2143 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002144 */
2145hterm.Terminal.prototype.clear = function(opt_screen) {
2146 var screen = opt_screen || this.screen_;
2147 var cursor = screen.cursorPosition.clone();
2148 this.clearHome(screen);
2149 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002150};
2151
2152/**
2153 * VT command to insert lines at the current cursor row.
2154 *
2155 * This respects the current scroll region. Rows pushed off the bottom are
2156 * lost (they won't show up in the scrollback buffer).
2157 *
rginda8ba33642011-12-14 12:31:31 -08002158 * @param {integer} count The number of lines to insert.
2159 */
2160hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002161 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002162
2163 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002164 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002165
Robert Ginda579186b2012-09-26 11:40:04 -07002166 // The moveCount is the number of rows we need to relocate to make room for
2167 // the new row(s). The count is the distance to move them.
2168 var moveCount = bottom - cursorRow - count + 1;
2169 if (moveCount)
2170 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002171
Robert Ginda579186b2012-09-26 11:40:04 -07002172 for (var i = count - 1; i >= 0; i--) {
2173 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002174 this.screen_.clearCursorRow();
2175 }
rginda8ba33642011-12-14 12:31:31 -08002176};
2177
2178/**
2179 * VT command to delete lines at the current cursor row.
2180 *
2181 * New rows are added to the bottom of scroll region to take their place. New
2182 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002183 *
2184 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002185 */
2186hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002187 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002188
rginda87b86462011-12-14 13:48:03 -08002189 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002190 var bottom = this.getVTScrollBottom();
2191
rginda87b86462011-12-14 13:48:03 -08002192 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002193 count = Math.min(count, maxCount);
2194
rginda87b86462011-12-14 13:48:03 -08002195 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002196 if (count != maxCount)
2197 this.moveRows_(top, count, moveStart);
2198
2199 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002200 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002201 this.screen_.clearCursorRow();
2202 }
2203
rginda87b86462011-12-14 13:48:03 -08002204 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002205 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002206};
2207
2208/**
2209 * Inserts the given number of spaces at the current cursor position.
2210 *
rginda87b86462011-12-14 13:48:03 -08002211 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002212 *
2213 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002214 */
2215hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002216 var cursor = this.saveCursor();
2217
rgindacbbd7482012-06-13 15:06:16 -07002218 var ws = lib.f.getWhitespace(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002219 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002220 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002221
2222 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002223 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002224};
2225
2226/**
2227 * Forward-delete the specified number of characters starting at the cursor
2228 * position.
2229 *
2230 * @param {integer} count The number of characters to delete.
2231 */
2232hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002233 var deleted = this.screen_.deleteChars(count);
2234 if (deleted && !this.screen_.textAttributes.isDefault()) {
2235 var cursor = this.saveCursor();
2236 this.setCursorColumn(this.screenSize.width - deleted);
2237 this.screen_.insertString(lib.f.getWhitespace(deleted));
2238 this.restoreCursor(cursor);
2239 }
2240
David Benjamin54e8bf62012-06-01 22:31:40 -04002241 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002242};
2243
2244/**
2245 * Shift rows in the scroll region upwards by a given number of lines.
2246 *
2247 * New rows are inserted at the bottom of the scroll region to fill the
2248 * vacated rows. The new rows not filled out with the current text attributes.
2249 *
2250 * This function does not affect the scrollback rows at all. Rows shifted
2251 * off the top are lost.
2252 *
rginda87b86462011-12-14 13:48:03 -08002253 * The cursor position is not altered.
2254 *
rginda8ba33642011-12-14 12:31:31 -08002255 * @param {integer} count The number of rows to scroll.
2256 */
2257hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002258 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002259
rginda87b86462011-12-14 13:48:03 -08002260 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002261 this.deleteLines(count);
2262
rginda87b86462011-12-14 13:48:03 -08002263 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002264};
2265
2266/**
2267 * Shift rows below the cursor down by a given number of lines.
2268 *
2269 * This function respects the current scroll region.
2270 *
2271 * New rows are inserted at the top of the scroll region to fill the
2272 * vacated rows. The new rows not filled out with the current text attributes.
2273 *
2274 * This function does not affect the scrollback rows at all. Rows shifted
2275 * off the bottom are lost.
2276 *
2277 * @param {integer} count The number of rows to scroll.
2278 */
2279hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08002280 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002281
rginda87b86462011-12-14 13:48:03 -08002282 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08002283 this.insertLines(opt_count);
2284
rginda87b86462011-12-14 13:48:03 -08002285 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002286};
2287
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002288/**
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002289 * Enable accessibility-friendly features that have a performance impact.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002290 *
2291 * This will generate additional DOM nodes in an aria-live region that will
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002292 * cause Assitive Technology to announce the output of the terminal. It also
2293 * enables other features that aid assistive technology. All the features gated
2294 * behind this flag have a performance impact on the terminal which is why they
2295 * are made optional.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002296 *
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002297 * @param {boolean} enabled Whether to enable accessibility-friendly features.
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002298 */
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002299hterm.Terminal.prototype.setAccessibilityEnabled = function(enabled) {
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002300 this.accessibilityEnabled_ = enabled;
Raymes Khouryfa06b1d2018-06-06 16:43:39 +10002301 this.scrollPort_.setAccessibilityEnabled(enabled);
Raymes Khoury3e44bc92018-05-17 10:54:23 +10002302};
rginda87b86462011-12-14 13:48:03 -08002303
rginda8ba33642011-12-14 12:31:31 -08002304/**
2305 * Set the cursor position.
2306 *
2307 * The cursor row is relative to the scroll region if the terminal has
2308 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2309 *
2310 * @param {integer} row The new zero-based cursor row.
2311 * @param {integer} row The new zero-based cursor column.
2312 */
2313hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2314 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002315 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002316 } else {
rginda87b86462011-12-14 13:48:03 -08002317 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002318 }
rginda87b86462011-12-14 13:48:03 -08002319};
rginda8ba33642011-12-14 12:31:31 -08002320
Evan Jones2600d4f2016-12-06 09:29:36 -05002321/**
2322 * Move the cursor relative to its current position.
2323 *
2324 * @param {number} row
2325 * @param {number} column
2326 */
rginda87b86462011-12-14 13:48:03 -08002327hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2328 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002329 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2330 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002331 this.screen_.setCursorPosition(row, column);
2332};
2333
Evan Jones2600d4f2016-12-06 09:29:36 -05002334/**
2335 * Move the cursor to the specified position.
2336 *
2337 * @param {number} row
2338 * @param {number} column
2339 */
rginda87b86462011-12-14 13:48:03 -08002340hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002341 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2342 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002343 this.screen_.setCursorPosition(row, column);
2344};
2345
2346/**
2347 * Set the cursor column.
2348 *
2349 * @param {integer} column The new zero-based cursor column.
2350 */
2351hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002352 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002353};
2354
2355/**
2356 * Return the cursor column.
2357 *
2358 * @return {integer} The zero-based cursor column.
2359 */
2360hterm.Terminal.prototype.getCursorColumn = function() {
2361 return this.screen_.cursorPosition.column;
2362};
2363
2364/**
2365 * Set the cursor row.
2366 *
2367 * The cursor row is relative to the scroll region if the terminal has
2368 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2369 *
2370 * @param {integer} row The new cursor row.
2371 */
rginda87b86462011-12-14 13:48:03 -08002372hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2373 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002374};
2375
2376/**
2377 * Return the cursor row.
2378 *
2379 * @return {integer} The zero-based cursor row.
2380 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002381hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002382 return this.screen_.cursorPosition.row;
2383};
2384
2385/**
2386 * Request that the ScrollPort redraw itself soon.
2387 *
2388 * The redraw will happen asynchronously, soon after the call stack winds down.
2389 * Multiple calls will be coalesced into a single redraw.
2390 */
2391hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002392 if (this.timeouts_.redraw)
2393 return;
rginda8ba33642011-12-14 12:31:31 -08002394
2395 var self = this;
rginda87b86462011-12-14 13:48:03 -08002396 this.timeouts_.redraw = setTimeout(function() {
2397 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002398 self.scrollPort_.redraw_();
2399 }, 0);
2400};
2401
2402/**
2403 * Request that the ScrollPort be scrolled to the bottom.
2404 *
2405 * The scroll will happen asynchronously, soon after the call stack winds down.
2406 * Multiple calls will be coalesced into a single scroll.
2407 *
2408 * This affects the scrollbar position of the ScrollPort, and has nothing to
2409 * do with the VT scroll commands.
2410 */
2411hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2412 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002413 return;
rginda8ba33642011-12-14 12:31:31 -08002414
2415 var self = this;
2416 this.timeouts_.scrollDown = setTimeout(function() {
2417 delete self.timeouts_.scrollDown;
2418 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2419 }, 10);
2420};
2421
2422/**
2423 * Move the cursor up a specified number of rows.
2424 *
2425 * @param {integer} count The number of rows to move the cursor.
2426 */
2427hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002428 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002429};
2430
2431/**
2432 * Move the cursor down a specified number of rows.
2433 *
2434 * @param {integer} count The number of rows to move the cursor.
2435 */
2436hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002437 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002438 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2439 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2440 this.screenSize.height - 1);
2441
rgindacbbd7482012-06-13 15:06:16 -07002442 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002443 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002444 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002445};
2446
2447/**
2448 * Move the cursor left a specified number of columns.
2449 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002450 * If reverse wraparound mode is enabled and the previous row wrapped into
2451 * the current row then we back up through the wraparound as well.
2452 *
rginda8ba33642011-12-14 12:31:31 -08002453 * @param {integer} count The number of columns to move the cursor.
2454 */
2455hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002456 count = count || 1;
2457
2458 if (count < 1)
2459 return;
2460
2461 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002462 if (this.options_.reverseWraparound) {
2463 if (this.screen_.cursorPosition.overflow) {
2464 // If this cursor is in the right margin, consume one count to get it
2465 // back to the last column. This only applies when we're in reverse
2466 // wraparound mode.
2467 count--;
2468 this.clearCursorOverflow();
2469
2470 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002471 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002472 }
2473
Robert Gindabfb32622014-07-17 13:20:27 -07002474 var newRow = this.screen_.cursorPosition.row;
2475 var newColumn = currentColumn - count;
2476 if (newColumn < 0) {
2477 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2478 if (newRow < 0) {
2479 // xterm also wraps from row 0 to the last row.
2480 newRow = this.screenSize.height + newRow % this.screenSize.height;
2481 }
2482 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2483 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002484
Robert Gindabfb32622014-07-17 13:20:27 -07002485 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2486
2487 } else {
2488 var newColumn = Math.max(currentColumn - count, 0);
2489 this.setCursorColumn(newColumn);
2490 }
rginda8ba33642011-12-14 12:31:31 -08002491};
2492
2493/**
2494 * Move the cursor right a specified number of columns.
2495 *
2496 * @param {integer} count The number of columns to move the cursor.
2497 */
2498hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002499 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002500
2501 if (count < 1)
2502 return;
2503
rgindacbbd7482012-06-13 15:06:16 -07002504 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002505 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002506 this.setCursorColumn(column);
2507};
2508
2509/**
2510 * Reverse the foreground and background colors of the terminal.
2511 *
2512 * This only affects text that was drawn with no attributes.
2513 *
2514 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2515 * been drawn with attributes that happen to coincide with the default
2516 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002517 *
2518 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002519 */
2520hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002521 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002522 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002523 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2524 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002525 } else {
rginda9f5222b2012-03-05 11:53:28 -08002526 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2527 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002528 }
2529};
2530
2531/**
rginda87b86462011-12-14 13:48:03 -08002532 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002533 *
2534 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002535 */
2536hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002537 this.cursorNode_.style.backgroundColor =
2538 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002539
2540 var self = this;
2541 setTimeout(function() {
Matheus Fernandes2d733082017-09-11 06:43:01 -04002542 self.restyleCursor_();
rginda6d397402012-01-17 10:58:29 -08002543 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002544
Michael Kelly485ecd12014-06-09 11:41:56 -04002545 // bellSquelchTimeout_ affects both audio and notification bells.
2546 if (this.bellSquelchTimeout_)
2547 return;
2548
Robert Ginda92e18102013-03-14 13:56:37 -07002549 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002550 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002551 this.bellSequelchTimeout_ = setTimeout(function() {
2552 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002553 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002554 } else {
2555 delete this.bellSquelchTimeout_;
2556 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002557
2558 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002559 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002560 this.bellNotificationList_.push(n);
2561 // TODO: Should we try to raise the window here?
2562 n.onclick = function() { self.closeBellNotifications_(); };
2563 }
rginda87b86462011-12-14 13:48:03 -08002564};
2565
2566/**
rginda8ba33642011-12-14 12:31:31 -08002567 * Set the origin mode bit.
2568 *
2569 * If origin mode is on, certain VT cursor and scrolling commands measure their
2570 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2571 * to the top of the addressable screen.
2572 *
2573 * Defaults to off.
2574 *
2575 * @param {boolean} state True to set origin mode, false to unset.
2576 */
2577hterm.Terminal.prototype.setOriginMode = function(state) {
2578 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002579 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002580};
2581
2582/**
2583 * Set the insert mode bit.
2584 *
2585 * If insert mode is on, existing text beyond the cursor position will be
2586 * shifted right to make room for new text. Otherwise, new text overwrites
2587 * any existing text.
2588 *
2589 * Defaults to off.
2590 *
2591 * @param {boolean} state True to set insert mode, false to unset.
2592 */
2593hterm.Terminal.prototype.setInsertMode = function(state) {
2594 this.options_.insertMode = state;
2595};
2596
2597/**
rginda87b86462011-12-14 13:48:03 -08002598 * Set the auto carriage return bit.
2599 *
2600 * If auto carriage return is on then a formfeed character is interpreted
2601 * as a newline, otherwise it's the same as a linefeed. The difference boils
2602 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002603 *
2604 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002605 */
2606hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2607 this.options_.autoCarriageReturn = state;
2608};
2609
2610/**
rginda8ba33642011-12-14 12:31:31 -08002611 * Set the wraparound mode bit.
2612 *
2613 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2614 * to the start of the following row. Otherwise, the cursor is clamped to the
2615 * end of the screen and attempts to write past it are ignored.
2616 *
2617 * Defaults to on.
2618 *
2619 * @param {boolean} state True to set wraparound mode, false to unset.
2620 */
2621hterm.Terminal.prototype.setWraparound = function(state) {
2622 this.options_.wraparound = state;
2623};
2624
2625/**
2626 * Set the reverse-wraparound mode bit.
2627 *
2628 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2629 * to the end of the previous row. Otherwise, the cursor is clamped to column
2630 * 0.
2631 *
2632 * Defaults to off.
2633 *
2634 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2635 */
2636hterm.Terminal.prototype.setReverseWraparound = function(state) {
2637 this.options_.reverseWraparound = state;
2638};
2639
2640/**
2641 * Selects between the primary and alternate screens.
2642 *
2643 * If alternate mode is on, the alternate screen is active. Otherwise the
2644 * primary screen is active.
2645 *
2646 * Swapping screens has no effect on the scrollback buffer.
2647 *
2648 * Each screen maintains its own cursor position.
2649 *
2650 * Defaults to off.
2651 *
2652 * @param {boolean} state True to set alternate mode, false to unset.
2653 */
2654hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002655 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002656 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2657
rginda35c456b2012-02-09 17:29:05 -08002658 if (this.screen_.rowsArray.length &&
2659 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2660 // If the screen changed sizes while we were away, our rowIndexes may
2661 // be incorrect.
2662 var offset = this.scrollbackRows_.length;
2663 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002664 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002665 ary[i].rowIndex = offset + i;
2666 }
2667 }
rginda8ba33642011-12-14 12:31:31 -08002668
rginda35c456b2012-02-09 17:29:05 -08002669 this.realizeWidth_(this.screenSize.width);
2670 this.realizeHeight_(this.screenSize.height);
2671 this.scrollPort_.syncScrollHeight();
2672 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002673
rginda6d397402012-01-17 10:58:29 -08002674 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002675 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002676};
2677
2678/**
2679 * Set the cursor-blink mode bit.
2680 *
2681 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2682 * a visible cursor does not blink.
2683 *
2684 * You should make sure to turn blinking off if you're going to dispose of a
2685 * terminal, otherwise you'll leak a timeout.
2686 *
2687 * Defaults to on.
2688 *
2689 * @param {boolean} state True to set cursor-blink mode, false to unset.
2690 */
2691hterm.Terminal.prototype.setCursorBlink = function(state) {
2692 this.options_.cursorBlink = state;
2693
2694 if (!state && this.timeouts_.cursorBlink) {
2695 clearTimeout(this.timeouts_.cursorBlink);
2696 delete this.timeouts_.cursorBlink;
2697 }
2698
2699 if (this.options_.cursorVisible)
2700 this.setCursorVisible(true);
2701};
2702
2703/**
2704 * Set the cursor-visible mode bit.
2705 *
2706 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2707 *
2708 * Defaults to on.
2709 *
2710 * @param {boolean} state True to set cursor-visible mode, false to unset.
2711 */
2712hterm.Terminal.prototype.setCursorVisible = function(state) {
2713 this.options_.cursorVisible = state;
2714
2715 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002716 if (this.timeouts_.cursorBlink) {
2717 clearTimeout(this.timeouts_.cursorBlink);
2718 delete this.timeouts_.cursorBlink;
2719 }
rginda87b86462011-12-14 13:48:03 -08002720 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002721 return;
2722 }
2723
rginda87b86462011-12-14 13:48:03 -08002724 this.syncCursorPosition_();
2725
2726 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002727
2728 if (this.options_.cursorBlink) {
2729 if (this.timeouts_.cursorBlink)
2730 return;
2731
Robert Gindaea2183e2014-07-17 09:51:51 -07002732 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002733 } else {
2734 if (this.timeouts_.cursorBlink) {
2735 clearTimeout(this.timeouts_.cursorBlink);
2736 delete this.timeouts_.cursorBlink;
2737 }
2738 }
2739};
2740
2741/**
rginda87b86462011-12-14 13:48:03 -08002742 * Synchronizes the visible cursor and document selection with the current
2743 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002744 */
2745hterm.Terminal.prototype.syncCursorPosition_ = function() {
2746 var topRowIndex = this.scrollPort_.getTopRowIndex();
2747 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2748 var cursorRowIndex = this.scrollbackRows_.length +
2749 this.screen_.cursorPosition.row;
2750
2751 if (cursorRowIndex > bottomRowIndex) {
2752 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04002753 this.setCssVar('cursor-offset-row', '-1');
rginda8ba33642011-12-14 12:31:31 -08002754 return;
2755 }
2756
Robert Gindab837c052014-08-11 11:17:51 -07002757 if (this.options_.cursorVisible &&
2758 this.cursorNode_.style.display == 'none') {
2759 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2760 this.cursorNode_.style.display = '';
2761 }
2762
Mike Frysinger44c32202017-08-05 01:13:09 -04002763 // Position the cursor using CSS variable math. If we do the math in JS,
2764 // the float math will end up being more precise than the CSS which will
2765 // cause the cursor tracking to be off.
2766 this.setCssVar(
2767 'cursor-offset-row',
2768 `${cursorRowIndex - topRowIndex} + ` +
2769 `${this.scrollPort_.visibleRowTopMargin}px`);
2770 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08002771
2772 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04002773 '(' + this.screen_.cursorPosition.column +
2774 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08002775 ')');
2776
2777 // Update the caret for a11y purposes.
2778 var selection = this.document_.getSelection();
2779 if (selection && selection.isCollapsed)
2780 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002781};
2782
Robert Gindafb1be6a2013-12-11 11:56:22 -08002783/**
2784 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2785 * and character cell dimensions.
2786 */
Robert Ginda830583c2013-08-07 13:20:46 -07002787hterm.Terminal.prototype.restyleCursor_ = function() {
2788 var shape = this.cursorShape_;
2789
2790 if (this.cursorNode_.getAttribute('focus') == 'false') {
2791 // Always show a block cursor when unfocused.
2792 shape = hterm.Terminal.cursorShape.BLOCK;
2793 }
2794
2795 var style = this.cursorNode_.style;
2796
2797 switch (shape) {
2798 case hterm.Terminal.cursorShape.BEAM:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002799 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002800 style.backgroundColor = 'transparent';
2801 style.borderBottomStyle = null;
2802 style.borderLeftStyle = 'solid';
2803 break;
2804
2805 case hterm.Terminal.cursorShape.UNDERLINE:
2806 style.height = this.scrollPort_.characterSize.baseline + 'px';
2807 style.backgroundColor = 'transparent';
2808 style.borderBottomStyle = 'solid';
2809 // correct the size to put it exactly at the baseline
2810 style.borderLeftStyle = null;
2811 break;
2812
2813 default:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002814 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002815 style.backgroundColor = this.cursorColor_;
2816 style.borderBottomStyle = null;
2817 style.borderLeftStyle = null;
2818 break;
2819 }
2820};
2821
rginda8ba33642011-12-14 12:31:31 -08002822/**
2823 * Synchronizes the visible cursor with the current cursor coordinates.
2824 *
2825 * The sync will happen asynchronously, soon after the call stack winds down.
2826 * Multiple calls will be coalesced into a single sync.
2827 */
2828hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2829 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002830 return;
rginda8ba33642011-12-14 12:31:31 -08002831
2832 var self = this;
2833 this.timeouts_.syncCursor = setTimeout(function() {
2834 self.syncCursorPosition_();
2835 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002836 }, 0);
2837};
2838
rgindacc2996c2012-02-24 14:59:31 -08002839/**
rgindaf522ce02012-04-17 17:49:17 -07002840 * Show or hide the zoom warning.
2841 *
2842 * The zoom warning is a message warning the user that their browser zoom must
2843 * be set to 100% in order for hterm to function properly.
2844 *
2845 * @param {boolean} state True to show the message, false to hide it.
2846 */
2847hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2848 if (!this.zoomWarningNode_) {
2849 if (!state)
2850 return;
2851
2852 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002853 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07002854 this.zoomWarningNode_.style.cssText = (
2855 'color: black;' +
2856 'background-color: #ff2222;' +
2857 'font-size: large;' +
2858 'border-radius: 8px;' +
2859 'opacity: 0.75;' +
2860 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2861 'top: 0.5em;' +
2862 'right: 1.2em;' +
2863 'position: absolute;' +
2864 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002865 '-webkit-user-select: none;' +
2866 '-moz-text-size-adjust: none;' +
2867 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002868
2869 this.zoomWarningNode_.addEventListener('click', function(e) {
2870 this.parentNode.removeChild(this);
2871 });
rgindaf522ce02012-04-17 17:49:17 -07002872 }
2873
Robert Gindab4839c22013-02-28 16:52:10 -08002874 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2875 hterm.zoomWarningMessage,
2876 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2877
rgindaf522ce02012-04-17 17:49:17 -07002878 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2879
2880 if (state) {
2881 if (!this.zoomWarningNode_.parentNode)
2882 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2883 } else if (this.zoomWarningNode_.parentNode) {
2884 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2885 }
2886};
2887
2888/**
rgindacc2996c2012-02-24 14:59:31 -08002889 * Show the terminal overlay for a given amount of time.
2890 *
2891 * The terminal overlay appears in inverse video in a large font, centered
2892 * over the terminal. You should probably keep the overlay message brief,
2893 * since it's in a large font and you probably aren't going to check the size
2894 * of the terminal first.
2895 *
2896 * @param {string} msg The text (not HTML) message to display in the overlay.
2897 * @param {number} opt_timeout The amount of time to wait before fading out
2898 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2899 * stay up forever (or until the next overlay).
2900 */
2901hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002902 if (!this.overlayNode_) {
2903 if (!this.div_)
2904 return;
2905
2906 this.overlayNode_ = this.document_.createElement('div');
2907 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002908 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002909 'font-size: xx-large;' +
2910 'opacity: 0.75;' +
2911 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2912 'position: absolute;' +
2913 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002914 '-webkit-transition: opacity 180ms ease-in;' +
2915 '-moz-user-select: none;' +
2916 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002917
2918 this.overlayNode_.addEventListener('mousedown', function(e) {
2919 e.preventDefault();
2920 e.stopPropagation();
2921 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002922 }
2923
rginda9f5222b2012-03-05 11:53:28 -08002924 this.overlayNode_.style.color = this.prefs_.get('background-color');
2925 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2926 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2927
rgindaf0090c92012-02-10 14:58:52 -08002928 this.overlayNode_.textContent = msg;
2929 this.overlayNode_.style.opacity = '0.75';
2930
2931 if (!this.overlayNode_.parentNode)
2932 this.div_.appendChild(this.overlayNode_);
2933
Robert Ginda97769282013-02-01 15:30:30 -08002934 var divSize = hterm.getClientSize(this.div_);
2935 var overlaySize = hterm.getClientSize(this.overlayNode_);
2936
Robert Ginda8a59f762014-07-23 11:29:55 -07002937 this.overlayNode_.style.top =
2938 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08002939 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07002940 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08002941
rgindaf0090c92012-02-10 14:58:52 -08002942 if (this.overlayTimeout_)
2943 clearTimeout(this.overlayTimeout_);
2944
rgindacc2996c2012-02-24 14:59:31 -08002945 if (opt_timeout === null)
2946 return;
2947
Mike Frysingerb6cfded2017-09-18 00:39:31 -04002948 this.overlayTimeout_ = setTimeout(() => {
2949 this.overlayNode_.style.opacity = '0';
2950 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
2951 }, opt_timeout || 1500);
2952};
2953
2954/**
2955 * Hide the terminal overlay immediately.
2956 *
2957 * Useful when we show an overlay for an event with an unknown end time.
2958 */
2959hterm.Terminal.prototype.hideOverlay = function() {
2960 if (this.overlayTimeout_)
2961 clearTimeout(this.overlayTimeout_);
2962 this.overlayTimeout_ = null;
2963
2964 if (this.overlayNode_.parentNode)
2965 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
2966 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08002967};
2968
rginda4bba5e12012-06-20 16:15:30 -07002969/**
2970 * Paste from the system clipboard to the terminal.
2971 */
2972hterm.Terminal.prototype.paste = function() {
Mike Frysinger4628ad22017-07-20 02:44:20 -04002973 return hterm.pasteFromClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002974};
2975
2976/**
2977 * Copy a string to the system clipboard.
2978 *
2979 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05002980 *
2981 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07002982 */
2983hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002984 if (this.prefs_.get('enable-clipboard-notice'))
2985 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2986
rgindaa09e7332012-08-17 12:49:51 -07002987 var copySource = this.document_.createElement('pre');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002988 copySource.id = 'hterm:copy-to-clipboard-source';
rginda4bba5e12012-06-20 16:15:30 -07002989 copySource.textContent = str;
2990 copySource.style.cssText = (
2991 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002992 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07002993 'position: absolute;' +
2994 'top: -99px');
2995
2996 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002997
rginda4bba5e12012-06-20 16:15:30 -07002998 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002999 var anchorNode = selection.anchorNode;
3000 var anchorOffset = selection.anchorOffset;
3001 var focusNode = selection.focusNode;
3002 var focusOffset = selection.focusOffset;
3003
rginda4bba5e12012-06-20 16:15:30 -07003004 selection.selectAllChildren(copySource);
3005
rgindaa09e7332012-08-17 12:49:51 -07003006 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07003007
Rob Spies56953412014-04-28 14:09:47 -07003008 // IE doesn't support selection.extend. This means that the selection
3009 // won't return on IE.
Robert Ginda6f4f0aa2014-07-28 10:21:39 -07003010 if (selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07003011 selection.collapse(anchorNode, anchorOffset);
3012 selection.extend(focusNode, focusOffset);
3013 }
rgindafaa74742012-08-21 13:34:03 -07003014
rginda4bba5e12012-06-20 16:15:30 -07003015 copySource.parentNode.removeChild(copySource);
3016};
3017
Evan Jones2600d4f2016-12-06 09:29:36 -05003018/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003019 * Display an image.
3020 *
3021 * @param {Object} options The image to display.
3022 * @param {string=} options.name A human readable string for the image.
3023 * @param {string|number=} options.size The size (in bytes).
3024 * @param {boolean=} options.preserveAspectRatio Whether to preserve aspect.
3025 * @param {boolean=} options.inline Whether to display the image inline.
3026 * @param {string|number=} options.width The width of the image.
3027 * @param {string|number=} options.height The height of the image.
3028 * @param {string=} options.align Direction to align the image.
3029 * @param {string} options.uri The source URI for the image.
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003030 * @param {function=} onLoad Callback when loading finishes.
3031 * @param {function(Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003032 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003033hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003034 // Make sure we're actually given a resource to display.
3035 if (options.uri === undefined)
3036 return;
3037
3038 // Set up the defaults to simplify code below.
3039 if (!options.name)
3040 options.name = '';
3041
3042 // Has the user approved image display yet?
3043 if (this.allowImagesInline !== true) {
3044 this.newLine();
3045 const row = this.getRowNode(this.scrollbackRows_.length +
3046 this.getCursorRow() - 1);
3047
3048 if (this.allowImagesInline === false) {
3049 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3050 'Inline Images Disabled');
3051 return;
3052 }
3053
3054 // Show a prompt.
3055 let button;
3056 const span = this.document_.createElement('span');
3057 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3058 span.style.fontWeight = 'bold';
3059 span.style.borderWidth = '1px';
3060 span.style.borderStyle = 'dashed';
3061 button = this.document_.createElement('span');
3062 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3063 button.style.marginLeft = '1em';
3064 button.style.borderWidth = '1px';
3065 button.style.borderStyle = 'solid';
3066 button.addEventListener('click', () => {
3067 this.prefs_.set('allow-images-inline', false);
3068 });
3069 span.appendChild(button);
3070 button = this.document_.createElement('span');
3071 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3072 'allow this session');
3073 button.style.marginLeft = '1em';
3074 button.style.borderWidth = '1px';
3075 button.style.borderStyle = 'solid';
3076 button.addEventListener('click', () => {
3077 this.allowImagesInline = true;
3078 });
3079 span.appendChild(button);
3080 button = this.document_.createElement('span');
3081 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3082 button.style.marginLeft = '1em';
3083 button.style.borderWidth = '1px';
3084 button.style.borderStyle = 'solid';
3085 button.addEventListener('click', () => {
3086 this.prefs_.set('allow-images-inline', true);
3087 });
3088 span.appendChild(button);
3089
3090 row.appendChild(span);
3091 return;
3092 }
3093
3094 // See if we should show this object directly, or download it.
3095 if (options.inline) {
3096 const io = this.io.push();
3097 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
3098 'Loading $1 ...'), null);
3099
3100 // While we're loading the image, eat all the user's input.
3101 io.onVTKeystroke = io.sendString = () => {};
3102
3103 // Initialize this new image.
3104 const img = this.document_.createElement('img');
3105 img.src = options.uri;
3106 img.title = img.alt = options.name;
3107
3108 // Attach the image to the page to let it load/render. It won't stay here.
3109 // This is needed so it's visible and the DOM can calculate the height. If
3110 // the image is hidden or not in the DOM, the height is always 0.
3111 this.document_.body.appendChild(img);
3112
3113 // Wait for the image to finish loading before we try moving it to the
3114 // right place in the terminal.
3115 img.onload = () => {
3116 // Now that we have the image dimensions, figure out how to show it.
3117 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
3118 img.style.maxWidth = `${this.document_.body.clientWidth}px`;
3119 img.style.maxHeight = `${this.document_.body.clientHeight}px`;
3120
3121 // Parse a width/height specification.
3122 const parseDim = (dim, maxDim, cssVar) => {
3123 if (!dim || dim == 'auto')
3124 return '';
3125
3126 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3127 if (ary) {
3128 if (ary[2] == '%')
3129 return maxDim * parseInt(ary[1]) / 100 + 'px';
3130 else if (ary[2] == 'px')
3131 return dim;
3132 else
3133 return `calc(${dim} * var(${cssVar}))`;
3134 }
3135
3136 return '';
3137 };
3138 img.style.width =
3139 parseDim(options.width, this.document_.body.clientWidth,
3140 '--hterm-charsize-width');
3141 img.style.height =
3142 parseDim(options.height, this.document_.body.clientHeight,
3143 '--hterm-charsize-height');
3144
3145 // Figure out how many rows the image occupies, then add that many.
3146 // XXX: This count will be inaccurate if the font size changes on us.
3147 const padRows = Math.ceil(img.clientHeight /
3148 this.scrollPort_.characterSize.height);
3149 for (let i = 0; i < padRows; ++i)
3150 this.newLine();
3151
3152 // Update the max height in case the user shrinks the character size.
3153 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3154
3155 // Move the image to the last row. This way when we scroll up, it doesn't
3156 // disappear when the first row gets clipped. It will disappear when we
3157 // scroll down and the last row is clipped ...
3158 this.document_.body.removeChild(img);
3159 // Create a wrapper node so we can do an absolute in a relative position.
3160 // This helps with rounding errors between JS & CSS counts.
3161 const div = this.document_.createElement('div');
3162 div.style.position = 'relative';
3163 div.style.textAlign = options.align;
3164 img.style.position = 'absolute';
3165 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3166 div.appendChild(img);
3167 const row = this.getRowNode(this.scrollbackRows_.length +
3168 this.getCursorRow() - 1);
3169 row.appendChild(div);
3170
3171 io.hideOverlay();
3172 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003173
3174 if (onLoad)
3175 onLoad();
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003176 };
3177
3178 // If we got a malformed image, give up.
3179 img.onerror = (e) => {
3180 this.document_.body.removeChild(img);
3181 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
Mike Frysingere14a8c42018-03-10 00:17:30 -08003182 'Loading $1 failed'));
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003183 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003184
3185 if (onError)
3186 onError(e);
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003187 };
3188 } else {
3189 // We can't use chrome.downloads.download as that requires "downloads"
3190 // permissions, and that works only in extensions, not apps.
3191 const a = this.document_.createElement('a');
3192 a.href = options.uri;
3193 a.download = options.name;
3194 this.document_.body.appendChild(a);
3195 a.click();
3196 a.remove();
3197 }
3198};
3199
3200/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003201 * Returns the selected text, or null if no text is selected.
3202 *
3203 * @return {string|null}
3204 */
rgindaa09e7332012-08-17 12:49:51 -07003205hterm.Terminal.prototype.getSelectionText = function() {
3206 var selection = this.scrollPort_.selection;
3207 selection.sync();
3208
3209 if (selection.isCollapsed)
3210 return null;
3211
3212
3213 // Start offset measures from the beginning of the line.
3214 var startOffset = selection.startOffset;
3215 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003216
Robert Gindafdbb3f22012-09-06 20:23:06 -07003217 if (node.nodeName != 'X-ROW') {
3218 // If the selection doesn't start on an x-row node, then it must be
3219 // somewhere inside the x-row. Add any characters from previous siblings
3220 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003221
3222 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3223 // If node is the text node in a styled span, move up to the span node.
3224 node = node.parentNode;
3225 }
3226
Robert Gindafdbb3f22012-09-06 20:23:06 -07003227 while (node.previousSibling) {
3228 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003229 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003230 }
rgindaa09e7332012-08-17 12:49:51 -07003231 }
3232
3233 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08003234 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
3235 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003236 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003237
Robert Gindafdbb3f22012-09-06 20:23:06 -07003238 if (node.nodeName != 'X-ROW') {
3239 // If the selection doesn't end on an x-row node, then it must be
3240 // somewhere inside the x-row. Add any characters from following siblings
3241 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003242
3243 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3244 // If node is the text node in a styled span, move up to the span node.
3245 node = node.parentNode;
3246 }
3247
Robert Gindafdbb3f22012-09-06 20:23:06 -07003248 while (node.nextSibling) {
3249 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003250 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003251 }
rgindaa09e7332012-08-17 12:49:51 -07003252 }
3253
3254 var rv = this.getRowsText(selection.startRow.rowIndex,
3255 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003256 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003257};
3258
rginda4bba5e12012-06-20 16:15:30 -07003259/**
3260 * Copy the current selection to the system clipboard, then clear it after a
3261 * short delay.
3262 */
3263hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07003264 var text = this.getSelectionText();
3265 if (text != null)
3266 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07003267};
3268
rgindaf0090c92012-02-10 14:58:52 -08003269hterm.Terminal.prototype.overlaySize = function() {
3270 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
3271};
3272
rginda87b86462011-12-14 13:48:03 -08003273/**
3274 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3275 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003276 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003277 */
3278hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08003279 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08003280 this.scrollPort_.scrollRowToBottom(this.getRowCount());
3281
Robert Ginda8cb7d902013-06-20 14:37:18 -07003282 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08003283};
3284
3285/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003286 * Open the selected url.
3287 */
3288hterm.Terminal.prototype.openSelectedUrl_ = function() {
3289 var str = this.getSelectionText();
3290
3291 // If there is no selection, try and expand wherever they clicked.
3292 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003293 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003294 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003295
3296 // If clicking in empty space, return.
3297 if (str == null)
3298 return;
Mike Frysinger70b94692017-01-26 18:57:50 -10003299 }
3300
3301 // Make sure URL is valid before opening.
3302 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
3303 return;
Mike Frysinger43472622017-06-26 18:11:07 -04003304
3305 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003306 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003307 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3308 // We have to whitelist a few protocols that lack authorities and thus
3309 // never use the //. Like mailto.
3310 switch (str.split(':', 1)[0]) {
3311 case 'mailto':
3312 break;
3313 default:
3314 str = 'http://' + str;
3315 break;
3316 }
3317 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003318
Mike Frysinger720fa832017-10-23 01:15:52 -04003319 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003320};
Mike Frysinger70b94692017-01-26 18:57:50 -10003321
3322
3323/**
rgindad5613292012-06-19 15:40:37 -07003324 * Add the terminalRow and terminalColumn properties to mouse events and
3325 * then forward on to onMouse().
3326 *
3327 * The terminalRow and terminalColumn properties contain the (row, column)
3328 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003329 *
3330 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003331 */
3332hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003333 if (e.processedByTerminalHandler_) {
3334 // We register our event handlers on the document, as well as the cursor
3335 // and the scroll blocker. Mouse events that occur on the cursor or
3336 // scroll blocker will also appear on the document, but we don't want to
3337 // process them twice.
3338 //
3339 // We can't just prevent bubbling because that has other side effects, so
3340 // we decorate the event object with this property instead.
3341 return;
3342 }
3343
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003344 var reportMouseEvents = (!this.defeatMouseReports_ &&
3345 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3346
rgindafaa74742012-08-21 13:34:03 -07003347 e.processedByTerminalHandler_ = true;
3348
Robert Gindaeda48db2014-07-17 09:25:30 -07003349 // One based row/column stored on the mouse event.
3350 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
3351 this.scrollPort_.characterSize.height) + 1;
3352 e.terminalColumn = parseInt(e.clientX /
3353 this.scrollPort_.characterSize.width) + 1;
3354
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003355 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3356 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003357 return;
3358 }
3359
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003360 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003361 // If the cursor is visible and we're not sending mouse events to the
3362 // host app, then we want to hide the terminal cursor when the mouse
3363 // cursor is over top. This keeps the terminal cursor from interfering
3364 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003365 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3366 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3367 this.cursorNode_.style.display = 'none';
3368 } else if (this.cursorNode_.style.display == 'none') {
3369 this.cursorNode_.style.display = '';
3370 }
3371 }
rgindad5613292012-06-19 15:40:37 -07003372
Robert Ginda928cf632014-03-05 15:07:41 -08003373 if (e.type == 'mousedown') {
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003374 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003375 // If VT mouse reporting is disabled, or has been defeated with
3376 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003377 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003378 this.setSelectionEnabled(true);
3379 } else {
3380 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003381 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003382 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003383 this.setSelectionEnabled(false);
3384 e.preventDefault();
3385 }
3386 }
3387
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003388 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003389 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003390 this.screen_.expandSelection(this.document_.getSelection());
John Lin2aad22e2018-03-16 13:58:11 +08003391 if (this.copyOnSelect)
3392 this.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07003393 }
3394
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003395 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003396 // Debounce this event with the dblclick event. If you try to doubleclick
3397 // a URL to open it, Chrome will fire click then dblclick, but we won't
3398 // have expanded the selection text at the first click event.
3399 clearTimeout(this.timeouts_.openUrl);
3400 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3401 500);
3402 return;
3403 }
3404
Mike Frysinger847577f2017-05-23 23:25:57 -04003405 if (e.type == 'mousedown') {
3406 if ((this.mouseRightClickPaste && e.button == 2 /* right button */) ||
Mike Frysinger2edd3612017-05-24 00:54:39 -04003407 e.button == this.mousePasteButton) {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003408 if (!this.paste())
Mike Frysinger05a57f02017-08-27 17:48:55 -04003409 console.warn('Could not paste manually due to web restrictions');
Mike Frysinger847577f2017-05-23 23:25:57 -04003410 }
3411 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003412
Mike Frysinger2edd3612017-05-24 00:54:39 -04003413 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003414 !this.document_.getSelection().isCollapsed) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003415 this.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003416 }
3417
3418 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3419 this.scrollBlockerNode_.engaged) {
3420 // Disengage the scroll-blocker after one of these events.
3421 this.scrollBlockerNode_.engaged = false;
3422 this.scrollBlockerNode_.style.top = '-99px';
3423 }
3424
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003425 // Emulate arrow key presses via scroll wheel events.
3426 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3427 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003428 if (e.type == 'wheel') {
3429 var delta = this.scrollPort_.scrollWheelDelta(e);
3430 var lines = lib.f.smartFloorDivide(
3431 Math.abs(delta), this.scrollPort_.characterSize.height);
3432
3433 var data = '\x1bO' + (delta < 0 ? 'B' : 'A');
3434 this.io.sendString(data.repeat(lines));
3435
3436 e.preventDefault();
3437 }
3438 }
Robert Ginda928cf632014-03-05 15:07:41 -08003439 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003440 if (!this.scrollBlockerNode_.engaged) {
3441 if (e.type == 'mousedown') {
3442 // Move the scroll-blocker into place if we want to keep the scrollport
3443 // from scrolling.
3444 this.scrollBlockerNode_.engaged = true;
3445 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3446 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3447 } else if (e.type == 'mousemove') {
3448 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3449 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003450 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003451 e.preventDefault();
3452 }
3453 }
Robert Ginda928cf632014-03-05 15:07:41 -08003454
3455 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003456 }
3457
Robert Ginda928cf632014-03-05 15:07:41 -08003458 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3459 // Restore this on mouseup in case it was temporarily defeated with a
3460 // alt-mousedown. Only do this when the selection is empty so that
3461 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003462 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003463 }
rgindad5613292012-06-19 15:40:37 -07003464};
3465
3466/**
3467 * Clients should override this if they care to know about mouse events.
3468 *
3469 * The event parameter will be a normal DOM mouse click event with additional
3470 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003471 *
3472 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003473 */
3474hterm.Terminal.prototype.onMouse = function(e) { };
3475
3476/**
rginda8e92a692012-05-20 19:37:20 -07003477 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003478 *
3479 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003480 */
Rob Spies06533ba2014-04-24 11:20:37 -07003481hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3482 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003483 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003484
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003485 if (this.reportFocus)
3486 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003487
Michael Kelly485ecd12014-06-09 11:41:56 -04003488 if (focused === true)
3489 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003490};
3491
3492/**
rginda8ba33642011-12-14 12:31:31 -08003493 * React when the ScrollPort is scrolled.
3494 */
3495hterm.Terminal.prototype.onScroll_ = function() {
3496 this.scheduleSyncCursorPosition_();
3497};
3498
3499/**
rginda9846e2f2012-01-27 13:53:33 -08003500 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003501 *
3502 * @param {Event} e The DOM paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003503 */
3504hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07003505 var data = e.text.replace(/\n/mg, '\r');
Connor Hegartyf525ceb2014-09-03 13:36:03 -07003506 data = this.keyboard.encode(data);
Mike Frysingere8c32c82018-03-11 14:57:28 -07003507 if (this.options_.bracketedPaste) {
3508 // We strip out most escape sequences as they can cause issues (like
3509 // inserting an \x1b[201~ midstream). We pass through whitespace
3510 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
3511 // This matches xterm behavior.
3512 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
3513 data = '\x1b[200~' + filter(data) + '\x1b[201~';
3514 }
Robert Gindaa063b202014-07-21 11:08:25 -07003515
3516 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003517};
3518
3519/**
rgindaa09e7332012-08-17 12:49:51 -07003520 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003521 *
3522 * @param {Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003523 */
3524hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003525 if (!this.useDefaultWindowCopy) {
3526 e.preventDefault();
3527 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3528 }
rgindaa09e7332012-08-17 12:49:51 -07003529};
3530
3531/**
rginda8ba33642011-12-14 12:31:31 -08003532 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003533 *
3534 * Note: This function should not directly contain code that alters the internal
3535 * state of the terminal. That kind of code belongs in realizeWidth or
3536 * realizeHeight, so that it can be executed synchronously in the case of a
3537 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003538 */
3539hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003540 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003541 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003542 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003543 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003544
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003545 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003546 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003547 // gets removed from the document or during the initial load, and we can't
3548 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003549 // This can also happen if called before the scrollPort calculates the
3550 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003551 return;
3552 }
3553
rgindaa8ba17d2012-08-15 14:41:10 -07003554 var isNewSize = (columnCount != this.screenSize.width ||
3555 rowCount != this.screenSize.height);
3556
3557 // We do this even if the size didn't change, just to be sure everything is
3558 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003559 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003560 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003561
3562 if (isNewSize)
3563 this.overlaySize();
3564
Robert Gindafb1be6a2013-12-11 11:56:22 -08003565 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003566 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08003567};
3568
3569/**
3570 * Service the cursor blink timeout.
3571 */
3572hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003573 if (!this.options_.cursorBlink) {
3574 delete this.timeouts_.cursorBlink;
3575 return;
3576 }
3577
Robert Ginda830583c2013-08-07 13:20:46 -07003578 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3579 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003580 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003581 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3582 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003583 } else {
rginda87b86462011-12-14 13:48:03 -08003584 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003585 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3586 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003587 }
3588};
David Reveman8f552492012-03-28 12:18:41 -04003589
3590/**
3591 * Set the scrollbar-visible mode bit.
3592 *
3593 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3594 * Otherwise it will not.
3595 *
3596 * Defaults to on.
3597 *
3598 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3599 */
3600hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3601 this.scrollPort_.setScrollbarVisible(state);
3602};
Michael Kelly485ecd12014-06-09 11:41:56 -04003603
3604/**
Rob Spies49039e52014-12-17 13:40:04 -08003605 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003606 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003607 *
3608 * Defaults to 1.
3609 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003610 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003611 */
3612hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3613 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3614};
3615
3616/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003617 * Close all web notifications created by terminal bells.
3618 */
3619hterm.Terminal.prototype.closeBellNotifications_ = function() {
3620 this.bellNotificationList_.forEach(function(n) {
3621 n.close();
3622 });
3623 this.bellNotificationList_.length = 0;
3624};