blob: ac890eca9e90399d64bb4764fa623f42bc394c14 [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/**
2289 * Set live output for accessibility.
2290 *
2291 * This will generate additional DOM nodes in an aria-live region that will
2292 * cause Assitive Technology to announce the output of the terminal. This isn't
2293 * enabled by default as it can have a performance impact.
2294 *
2295 * @param {boolean} enabled Whether to enable live output.
2296 */
2297hterm.Terminal.prototype.setLiveOutputForAccessibility = function(enabled) {
2298 this.accessibilityEnabled_ = enabled;
2299};
rginda87b86462011-12-14 13:48:03 -08002300
rginda8ba33642011-12-14 12:31:31 -08002301/**
2302 * Set the cursor position.
2303 *
2304 * The cursor row is relative to the scroll region if the terminal has
2305 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2306 *
2307 * @param {integer} row The new zero-based cursor row.
2308 * @param {integer} row The new zero-based cursor column.
2309 */
2310hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2311 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002312 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002313 } else {
rginda87b86462011-12-14 13:48:03 -08002314 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002315 }
rginda87b86462011-12-14 13:48:03 -08002316};
rginda8ba33642011-12-14 12:31:31 -08002317
Evan Jones2600d4f2016-12-06 09:29:36 -05002318/**
2319 * Move the cursor relative to its current position.
2320 *
2321 * @param {number} row
2322 * @param {number} column
2323 */
rginda87b86462011-12-14 13:48:03 -08002324hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2325 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002326 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2327 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002328 this.screen_.setCursorPosition(row, column);
2329};
2330
Evan Jones2600d4f2016-12-06 09:29:36 -05002331/**
2332 * Move the cursor to the specified position.
2333 *
2334 * @param {number} row
2335 * @param {number} column
2336 */
rginda87b86462011-12-14 13:48:03 -08002337hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002338 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2339 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002340 this.screen_.setCursorPosition(row, column);
2341};
2342
2343/**
2344 * Set the cursor column.
2345 *
2346 * @param {integer} column The new zero-based cursor column.
2347 */
2348hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002349 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002350};
2351
2352/**
2353 * Return the cursor column.
2354 *
2355 * @return {integer} The zero-based cursor column.
2356 */
2357hterm.Terminal.prototype.getCursorColumn = function() {
2358 return this.screen_.cursorPosition.column;
2359};
2360
2361/**
2362 * Set the cursor row.
2363 *
2364 * The cursor row is relative to the scroll region if the terminal has
2365 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2366 *
2367 * @param {integer} row The new cursor row.
2368 */
rginda87b86462011-12-14 13:48:03 -08002369hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2370 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002371};
2372
2373/**
2374 * Return the cursor row.
2375 *
2376 * @return {integer} The zero-based cursor row.
2377 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002378hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002379 return this.screen_.cursorPosition.row;
2380};
2381
2382/**
2383 * Request that the ScrollPort redraw itself soon.
2384 *
2385 * The redraw will happen asynchronously, soon after the call stack winds down.
2386 * Multiple calls will be coalesced into a single redraw.
2387 */
2388hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002389 if (this.timeouts_.redraw)
2390 return;
rginda8ba33642011-12-14 12:31:31 -08002391
2392 var self = this;
rginda87b86462011-12-14 13:48:03 -08002393 this.timeouts_.redraw = setTimeout(function() {
2394 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002395 self.scrollPort_.redraw_();
2396 }, 0);
2397};
2398
2399/**
2400 * Request that the ScrollPort be scrolled to the bottom.
2401 *
2402 * The scroll will happen asynchronously, soon after the call stack winds down.
2403 * Multiple calls will be coalesced into a single scroll.
2404 *
2405 * This affects the scrollbar position of the ScrollPort, and has nothing to
2406 * do with the VT scroll commands.
2407 */
2408hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2409 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002410 return;
rginda8ba33642011-12-14 12:31:31 -08002411
2412 var self = this;
2413 this.timeouts_.scrollDown = setTimeout(function() {
2414 delete self.timeouts_.scrollDown;
2415 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2416 }, 10);
2417};
2418
2419/**
2420 * Move the cursor up a specified number of rows.
2421 *
2422 * @param {integer} count The number of rows to move the cursor.
2423 */
2424hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002425 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002426};
2427
2428/**
2429 * Move the cursor down a specified number of rows.
2430 *
2431 * @param {integer} count The number of rows to move the cursor.
2432 */
2433hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002434 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002435 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2436 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2437 this.screenSize.height - 1);
2438
rgindacbbd7482012-06-13 15:06:16 -07002439 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002440 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002441 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002442};
2443
2444/**
2445 * Move the cursor left a specified number of columns.
2446 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002447 * If reverse wraparound mode is enabled and the previous row wrapped into
2448 * the current row then we back up through the wraparound as well.
2449 *
rginda8ba33642011-12-14 12:31:31 -08002450 * @param {integer} count The number of columns to move the cursor.
2451 */
2452hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002453 count = count || 1;
2454
2455 if (count < 1)
2456 return;
2457
2458 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002459 if (this.options_.reverseWraparound) {
2460 if (this.screen_.cursorPosition.overflow) {
2461 // If this cursor is in the right margin, consume one count to get it
2462 // back to the last column. This only applies when we're in reverse
2463 // wraparound mode.
2464 count--;
2465 this.clearCursorOverflow();
2466
2467 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002468 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002469 }
2470
Robert Gindabfb32622014-07-17 13:20:27 -07002471 var newRow = this.screen_.cursorPosition.row;
2472 var newColumn = currentColumn - count;
2473 if (newColumn < 0) {
2474 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2475 if (newRow < 0) {
2476 // xterm also wraps from row 0 to the last row.
2477 newRow = this.screenSize.height + newRow % this.screenSize.height;
2478 }
2479 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2480 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002481
Robert Gindabfb32622014-07-17 13:20:27 -07002482 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2483
2484 } else {
2485 var newColumn = Math.max(currentColumn - count, 0);
2486 this.setCursorColumn(newColumn);
2487 }
rginda8ba33642011-12-14 12:31:31 -08002488};
2489
2490/**
2491 * Move the cursor right a specified number of columns.
2492 *
2493 * @param {integer} count The number of columns to move the cursor.
2494 */
2495hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002496 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002497
2498 if (count < 1)
2499 return;
2500
rgindacbbd7482012-06-13 15:06:16 -07002501 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002502 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002503 this.setCursorColumn(column);
2504};
2505
2506/**
2507 * Reverse the foreground and background colors of the terminal.
2508 *
2509 * This only affects text that was drawn with no attributes.
2510 *
2511 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2512 * been drawn with attributes that happen to coincide with the default
2513 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002514 *
2515 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002516 */
2517hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002518 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002519 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002520 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2521 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002522 } else {
rginda9f5222b2012-03-05 11:53:28 -08002523 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2524 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002525 }
2526};
2527
2528/**
rginda87b86462011-12-14 13:48:03 -08002529 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002530 *
2531 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002532 */
2533hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002534 this.cursorNode_.style.backgroundColor =
2535 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002536
2537 var self = this;
2538 setTimeout(function() {
Matheus Fernandes2d733082017-09-11 06:43:01 -04002539 self.restyleCursor_();
rginda6d397402012-01-17 10:58:29 -08002540 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002541
Michael Kelly485ecd12014-06-09 11:41:56 -04002542 // bellSquelchTimeout_ affects both audio and notification bells.
2543 if (this.bellSquelchTimeout_)
2544 return;
2545
Robert Ginda92e18102013-03-14 13:56:37 -07002546 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002547 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002548 this.bellSequelchTimeout_ = setTimeout(function() {
2549 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002550 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002551 } else {
2552 delete this.bellSquelchTimeout_;
2553 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002554
2555 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002556 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002557 this.bellNotificationList_.push(n);
2558 // TODO: Should we try to raise the window here?
2559 n.onclick = function() { self.closeBellNotifications_(); };
2560 }
rginda87b86462011-12-14 13:48:03 -08002561};
2562
2563/**
rginda8ba33642011-12-14 12:31:31 -08002564 * Set the origin mode bit.
2565 *
2566 * If origin mode is on, certain VT cursor and scrolling commands measure their
2567 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2568 * to the top of the addressable screen.
2569 *
2570 * Defaults to off.
2571 *
2572 * @param {boolean} state True to set origin mode, false to unset.
2573 */
2574hterm.Terminal.prototype.setOriginMode = function(state) {
2575 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002576 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002577};
2578
2579/**
2580 * Set the insert mode bit.
2581 *
2582 * If insert mode is on, existing text beyond the cursor position will be
2583 * shifted right to make room for new text. Otherwise, new text overwrites
2584 * any existing text.
2585 *
2586 * Defaults to off.
2587 *
2588 * @param {boolean} state True to set insert mode, false to unset.
2589 */
2590hterm.Terminal.prototype.setInsertMode = function(state) {
2591 this.options_.insertMode = state;
2592};
2593
2594/**
rginda87b86462011-12-14 13:48:03 -08002595 * Set the auto carriage return bit.
2596 *
2597 * If auto carriage return is on then a formfeed character is interpreted
2598 * as a newline, otherwise it's the same as a linefeed. The difference boils
2599 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002600 *
2601 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002602 */
2603hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2604 this.options_.autoCarriageReturn = state;
2605};
2606
2607/**
rginda8ba33642011-12-14 12:31:31 -08002608 * Set the wraparound mode bit.
2609 *
2610 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2611 * to the start of the following row. Otherwise, the cursor is clamped to the
2612 * end of the screen and attempts to write past it are ignored.
2613 *
2614 * Defaults to on.
2615 *
2616 * @param {boolean} state True to set wraparound mode, false to unset.
2617 */
2618hterm.Terminal.prototype.setWraparound = function(state) {
2619 this.options_.wraparound = state;
2620};
2621
2622/**
2623 * Set the reverse-wraparound mode bit.
2624 *
2625 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2626 * to the end of the previous row. Otherwise, the cursor is clamped to column
2627 * 0.
2628 *
2629 * Defaults to off.
2630 *
2631 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2632 */
2633hterm.Terminal.prototype.setReverseWraparound = function(state) {
2634 this.options_.reverseWraparound = state;
2635};
2636
2637/**
2638 * Selects between the primary and alternate screens.
2639 *
2640 * If alternate mode is on, the alternate screen is active. Otherwise the
2641 * primary screen is active.
2642 *
2643 * Swapping screens has no effect on the scrollback buffer.
2644 *
2645 * Each screen maintains its own cursor position.
2646 *
2647 * Defaults to off.
2648 *
2649 * @param {boolean} state True to set alternate mode, false to unset.
2650 */
2651hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002652 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002653 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2654
rginda35c456b2012-02-09 17:29:05 -08002655 if (this.screen_.rowsArray.length &&
2656 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2657 // If the screen changed sizes while we were away, our rowIndexes may
2658 // be incorrect.
2659 var offset = this.scrollbackRows_.length;
2660 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002661 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002662 ary[i].rowIndex = offset + i;
2663 }
2664 }
rginda8ba33642011-12-14 12:31:31 -08002665
rginda35c456b2012-02-09 17:29:05 -08002666 this.realizeWidth_(this.screenSize.width);
2667 this.realizeHeight_(this.screenSize.height);
2668 this.scrollPort_.syncScrollHeight();
2669 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002670
rginda6d397402012-01-17 10:58:29 -08002671 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002672 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002673};
2674
2675/**
2676 * Set the cursor-blink mode bit.
2677 *
2678 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2679 * a visible cursor does not blink.
2680 *
2681 * You should make sure to turn blinking off if you're going to dispose of a
2682 * terminal, otherwise you'll leak a timeout.
2683 *
2684 * Defaults to on.
2685 *
2686 * @param {boolean} state True to set cursor-blink mode, false to unset.
2687 */
2688hterm.Terminal.prototype.setCursorBlink = function(state) {
2689 this.options_.cursorBlink = state;
2690
2691 if (!state && this.timeouts_.cursorBlink) {
2692 clearTimeout(this.timeouts_.cursorBlink);
2693 delete this.timeouts_.cursorBlink;
2694 }
2695
2696 if (this.options_.cursorVisible)
2697 this.setCursorVisible(true);
2698};
2699
2700/**
2701 * Set the cursor-visible mode bit.
2702 *
2703 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2704 *
2705 * Defaults to on.
2706 *
2707 * @param {boolean} state True to set cursor-visible mode, false to unset.
2708 */
2709hterm.Terminal.prototype.setCursorVisible = function(state) {
2710 this.options_.cursorVisible = state;
2711
2712 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002713 if (this.timeouts_.cursorBlink) {
2714 clearTimeout(this.timeouts_.cursorBlink);
2715 delete this.timeouts_.cursorBlink;
2716 }
rginda87b86462011-12-14 13:48:03 -08002717 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002718 return;
2719 }
2720
rginda87b86462011-12-14 13:48:03 -08002721 this.syncCursorPosition_();
2722
2723 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002724
2725 if (this.options_.cursorBlink) {
2726 if (this.timeouts_.cursorBlink)
2727 return;
2728
Robert Gindaea2183e2014-07-17 09:51:51 -07002729 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002730 } else {
2731 if (this.timeouts_.cursorBlink) {
2732 clearTimeout(this.timeouts_.cursorBlink);
2733 delete this.timeouts_.cursorBlink;
2734 }
2735 }
2736};
2737
2738/**
rginda87b86462011-12-14 13:48:03 -08002739 * Synchronizes the visible cursor and document selection with the current
2740 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002741 */
2742hterm.Terminal.prototype.syncCursorPosition_ = function() {
2743 var topRowIndex = this.scrollPort_.getTopRowIndex();
2744 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2745 var cursorRowIndex = this.scrollbackRows_.length +
2746 this.screen_.cursorPosition.row;
2747
2748 if (cursorRowIndex > bottomRowIndex) {
2749 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04002750 this.setCssVar('cursor-offset-row', '-1');
rginda8ba33642011-12-14 12:31:31 -08002751 return;
2752 }
2753
Robert Gindab837c052014-08-11 11:17:51 -07002754 if (this.options_.cursorVisible &&
2755 this.cursorNode_.style.display == 'none') {
2756 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2757 this.cursorNode_.style.display = '';
2758 }
2759
Mike Frysinger44c32202017-08-05 01:13:09 -04002760 // Position the cursor using CSS variable math. If we do the math in JS,
2761 // the float math will end up being more precise than the CSS which will
2762 // cause the cursor tracking to be off.
2763 this.setCssVar(
2764 'cursor-offset-row',
2765 `${cursorRowIndex - topRowIndex} + ` +
2766 `${this.scrollPort_.visibleRowTopMargin}px`);
2767 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08002768
2769 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04002770 '(' + this.screen_.cursorPosition.column +
2771 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08002772 ')');
2773
2774 // Update the caret for a11y purposes.
2775 var selection = this.document_.getSelection();
2776 if (selection && selection.isCollapsed)
2777 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002778};
2779
Robert Gindafb1be6a2013-12-11 11:56:22 -08002780/**
2781 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2782 * and character cell dimensions.
2783 */
Robert Ginda830583c2013-08-07 13:20:46 -07002784hterm.Terminal.prototype.restyleCursor_ = function() {
2785 var shape = this.cursorShape_;
2786
2787 if (this.cursorNode_.getAttribute('focus') == 'false') {
2788 // Always show a block cursor when unfocused.
2789 shape = hterm.Terminal.cursorShape.BLOCK;
2790 }
2791
2792 var style = this.cursorNode_.style;
2793
2794 switch (shape) {
2795 case hterm.Terminal.cursorShape.BEAM:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002796 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002797 style.backgroundColor = 'transparent';
2798 style.borderBottomStyle = null;
2799 style.borderLeftStyle = 'solid';
2800 break;
2801
2802 case hterm.Terminal.cursorShape.UNDERLINE:
2803 style.height = this.scrollPort_.characterSize.baseline + 'px';
2804 style.backgroundColor = 'transparent';
2805 style.borderBottomStyle = 'solid';
2806 // correct the size to put it exactly at the baseline
2807 style.borderLeftStyle = null;
2808 break;
2809
2810 default:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002811 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002812 style.backgroundColor = this.cursorColor_;
2813 style.borderBottomStyle = null;
2814 style.borderLeftStyle = null;
2815 break;
2816 }
2817};
2818
rginda8ba33642011-12-14 12:31:31 -08002819/**
2820 * Synchronizes the visible cursor with the current cursor coordinates.
2821 *
2822 * The sync will happen asynchronously, soon after the call stack winds down.
2823 * Multiple calls will be coalesced into a single sync.
2824 */
2825hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2826 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002827 return;
rginda8ba33642011-12-14 12:31:31 -08002828
2829 var self = this;
2830 this.timeouts_.syncCursor = setTimeout(function() {
2831 self.syncCursorPosition_();
2832 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002833 }, 0);
2834};
2835
rgindacc2996c2012-02-24 14:59:31 -08002836/**
rgindaf522ce02012-04-17 17:49:17 -07002837 * Show or hide the zoom warning.
2838 *
2839 * The zoom warning is a message warning the user that their browser zoom must
2840 * be set to 100% in order for hterm to function properly.
2841 *
2842 * @param {boolean} state True to show the message, false to hide it.
2843 */
2844hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2845 if (!this.zoomWarningNode_) {
2846 if (!state)
2847 return;
2848
2849 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002850 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07002851 this.zoomWarningNode_.style.cssText = (
2852 'color: black;' +
2853 'background-color: #ff2222;' +
2854 'font-size: large;' +
2855 'border-radius: 8px;' +
2856 'opacity: 0.75;' +
2857 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2858 'top: 0.5em;' +
2859 'right: 1.2em;' +
2860 'position: absolute;' +
2861 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002862 '-webkit-user-select: none;' +
2863 '-moz-text-size-adjust: none;' +
2864 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002865
2866 this.zoomWarningNode_.addEventListener('click', function(e) {
2867 this.parentNode.removeChild(this);
2868 });
rgindaf522ce02012-04-17 17:49:17 -07002869 }
2870
Robert Gindab4839c22013-02-28 16:52:10 -08002871 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2872 hterm.zoomWarningMessage,
2873 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2874
rgindaf522ce02012-04-17 17:49:17 -07002875 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2876
2877 if (state) {
2878 if (!this.zoomWarningNode_.parentNode)
2879 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2880 } else if (this.zoomWarningNode_.parentNode) {
2881 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2882 }
2883};
2884
2885/**
rgindacc2996c2012-02-24 14:59:31 -08002886 * Show the terminal overlay for a given amount of time.
2887 *
2888 * The terminal overlay appears in inverse video in a large font, centered
2889 * over the terminal. You should probably keep the overlay message brief,
2890 * since it's in a large font and you probably aren't going to check the size
2891 * of the terminal first.
2892 *
2893 * @param {string} msg The text (not HTML) message to display in the overlay.
2894 * @param {number} opt_timeout The amount of time to wait before fading out
2895 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2896 * stay up forever (or until the next overlay).
2897 */
2898hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002899 if (!this.overlayNode_) {
2900 if (!this.div_)
2901 return;
2902
2903 this.overlayNode_ = this.document_.createElement('div');
2904 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002905 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002906 'font-size: xx-large;' +
2907 'opacity: 0.75;' +
2908 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2909 'position: absolute;' +
2910 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002911 '-webkit-transition: opacity 180ms ease-in;' +
2912 '-moz-user-select: none;' +
2913 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002914
2915 this.overlayNode_.addEventListener('mousedown', function(e) {
2916 e.preventDefault();
2917 e.stopPropagation();
2918 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002919 }
2920
rginda9f5222b2012-03-05 11:53:28 -08002921 this.overlayNode_.style.color = this.prefs_.get('background-color');
2922 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2923 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2924
rgindaf0090c92012-02-10 14:58:52 -08002925 this.overlayNode_.textContent = msg;
2926 this.overlayNode_.style.opacity = '0.75';
2927
2928 if (!this.overlayNode_.parentNode)
2929 this.div_.appendChild(this.overlayNode_);
2930
Robert Ginda97769282013-02-01 15:30:30 -08002931 var divSize = hterm.getClientSize(this.div_);
2932 var overlaySize = hterm.getClientSize(this.overlayNode_);
2933
Robert Ginda8a59f762014-07-23 11:29:55 -07002934 this.overlayNode_.style.top =
2935 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08002936 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07002937 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08002938
rgindaf0090c92012-02-10 14:58:52 -08002939 if (this.overlayTimeout_)
2940 clearTimeout(this.overlayTimeout_);
2941
rgindacc2996c2012-02-24 14:59:31 -08002942 if (opt_timeout === null)
2943 return;
2944
Mike Frysingerb6cfded2017-09-18 00:39:31 -04002945 this.overlayTimeout_ = setTimeout(() => {
2946 this.overlayNode_.style.opacity = '0';
2947 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
2948 }, opt_timeout || 1500);
2949};
2950
2951/**
2952 * Hide the terminal overlay immediately.
2953 *
2954 * Useful when we show an overlay for an event with an unknown end time.
2955 */
2956hterm.Terminal.prototype.hideOverlay = function() {
2957 if (this.overlayTimeout_)
2958 clearTimeout(this.overlayTimeout_);
2959 this.overlayTimeout_ = null;
2960
2961 if (this.overlayNode_.parentNode)
2962 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
2963 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08002964};
2965
rginda4bba5e12012-06-20 16:15:30 -07002966/**
2967 * Paste from the system clipboard to the terminal.
2968 */
2969hterm.Terminal.prototype.paste = function() {
Mike Frysinger4628ad22017-07-20 02:44:20 -04002970 return hterm.pasteFromClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002971};
2972
2973/**
2974 * Copy a string to the system clipboard.
2975 *
2976 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05002977 *
2978 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07002979 */
2980hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002981 if (this.prefs_.get('enable-clipboard-notice'))
2982 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2983
rgindaa09e7332012-08-17 12:49:51 -07002984 var copySource = this.document_.createElement('pre');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002985 copySource.id = 'hterm:copy-to-clipboard-source';
rginda4bba5e12012-06-20 16:15:30 -07002986 copySource.textContent = str;
2987 copySource.style.cssText = (
2988 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002989 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07002990 'position: absolute;' +
2991 'top: -99px');
2992
2993 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002994
rginda4bba5e12012-06-20 16:15:30 -07002995 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002996 var anchorNode = selection.anchorNode;
2997 var anchorOffset = selection.anchorOffset;
2998 var focusNode = selection.focusNode;
2999 var focusOffset = selection.focusOffset;
3000
rginda4bba5e12012-06-20 16:15:30 -07003001 selection.selectAllChildren(copySource);
3002
rgindaa09e7332012-08-17 12:49:51 -07003003 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07003004
Rob Spies56953412014-04-28 14:09:47 -07003005 // IE doesn't support selection.extend. This means that the selection
3006 // won't return on IE.
Robert Ginda6f4f0aa2014-07-28 10:21:39 -07003007 if (selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07003008 selection.collapse(anchorNode, anchorOffset);
3009 selection.extend(focusNode, focusOffset);
3010 }
rgindafaa74742012-08-21 13:34:03 -07003011
rginda4bba5e12012-06-20 16:15:30 -07003012 copySource.parentNode.removeChild(copySource);
3013};
3014
Evan Jones2600d4f2016-12-06 09:29:36 -05003015/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003016 * Display an image.
3017 *
3018 * @param {Object} options The image to display.
3019 * @param {string=} options.name A human readable string for the image.
3020 * @param {string|number=} options.size The size (in bytes).
3021 * @param {boolean=} options.preserveAspectRatio Whether to preserve aspect.
3022 * @param {boolean=} options.inline Whether to display the image inline.
3023 * @param {string|number=} options.width The width of the image.
3024 * @param {string|number=} options.height The height of the image.
3025 * @param {string=} options.align Direction to align the image.
3026 * @param {string} options.uri The source URI for the image.
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003027 * @param {function=} onLoad Callback when loading finishes.
3028 * @param {function(Event)=} onError Callback when loading fails.
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003029 */
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003030hterm.Terminal.prototype.displayImage = function(options, onLoad, onError) {
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003031 // Make sure we're actually given a resource to display.
3032 if (options.uri === undefined)
3033 return;
3034
3035 // Set up the defaults to simplify code below.
3036 if (!options.name)
3037 options.name = '';
3038
3039 // Has the user approved image display yet?
3040 if (this.allowImagesInline !== true) {
3041 this.newLine();
3042 const row = this.getRowNode(this.scrollbackRows_.length +
3043 this.getCursorRow() - 1);
3044
3045 if (this.allowImagesInline === false) {
3046 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3047 'Inline Images Disabled');
3048 return;
3049 }
3050
3051 // Show a prompt.
3052 let button;
3053 const span = this.document_.createElement('span');
3054 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3055 span.style.fontWeight = 'bold';
3056 span.style.borderWidth = '1px';
3057 span.style.borderStyle = 'dashed';
3058 button = this.document_.createElement('span');
3059 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3060 button.style.marginLeft = '1em';
3061 button.style.borderWidth = '1px';
3062 button.style.borderStyle = 'solid';
3063 button.addEventListener('click', () => {
3064 this.prefs_.set('allow-images-inline', false);
3065 });
3066 span.appendChild(button);
3067 button = this.document_.createElement('span');
3068 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3069 'allow this session');
3070 button.style.marginLeft = '1em';
3071 button.style.borderWidth = '1px';
3072 button.style.borderStyle = 'solid';
3073 button.addEventListener('click', () => {
3074 this.allowImagesInline = true;
3075 });
3076 span.appendChild(button);
3077 button = this.document_.createElement('span');
3078 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3079 button.style.marginLeft = '1em';
3080 button.style.borderWidth = '1px';
3081 button.style.borderStyle = 'solid';
3082 button.addEventListener('click', () => {
3083 this.prefs_.set('allow-images-inline', true);
3084 });
3085 span.appendChild(button);
3086
3087 row.appendChild(span);
3088 return;
3089 }
3090
3091 // See if we should show this object directly, or download it.
3092 if (options.inline) {
3093 const io = this.io.push();
3094 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
3095 'Loading $1 ...'), null);
3096
3097 // While we're loading the image, eat all the user's input.
3098 io.onVTKeystroke = io.sendString = () => {};
3099
3100 // Initialize this new image.
3101 const img = this.document_.createElement('img');
3102 img.src = options.uri;
3103 img.title = img.alt = options.name;
3104
3105 // Attach the image to the page to let it load/render. It won't stay here.
3106 // This is needed so it's visible and the DOM can calculate the height. If
3107 // the image is hidden or not in the DOM, the height is always 0.
3108 this.document_.body.appendChild(img);
3109
3110 // Wait for the image to finish loading before we try moving it to the
3111 // right place in the terminal.
3112 img.onload = () => {
3113 // Now that we have the image dimensions, figure out how to show it.
3114 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
3115 img.style.maxWidth = `${this.document_.body.clientWidth}px`;
3116 img.style.maxHeight = `${this.document_.body.clientHeight}px`;
3117
3118 // Parse a width/height specification.
3119 const parseDim = (dim, maxDim, cssVar) => {
3120 if (!dim || dim == 'auto')
3121 return '';
3122
3123 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3124 if (ary) {
3125 if (ary[2] == '%')
3126 return maxDim * parseInt(ary[1]) / 100 + 'px';
3127 else if (ary[2] == 'px')
3128 return dim;
3129 else
3130 return `calc(${dim} * var(${cssVar}))`;
3131 }
3132
3133 return '';
3134 };
3135 img.style.width =
3136 parseDim(options.width, this.document_.body.clientWidth,
3137 '--hterm-charsize-width');
3138 img.style.height =
3139 parseDim(options.height, this.document_.body.clientHeight,
3140 '--hterm-charsize-height');
3141
3142 // Figure out how many rows the image occupies, then add that many.
3143 // XXX: This count will be inaccurate if the font size changes on us.
3144 const padRows = Math.ceil(img.clientHeight /
3145 this.scrollPort_.characterSize.height);
3146 for (let i = 0; i < padRows; ++i)
3147 this.newLine();
3148
3149 // Update the max height in case the user shrinks the character size.
3150 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3151
3152 // Move the image to the last row. This way when we scroll up, it doesn't
3153 // disappear when the first row gets clipped. It will disappear when we
3154 // scroll down and the last row is clipped ...
3155 this.document_.body.removeChild(img);
3156 // Create a wrapper node so we can do an absolute in a relative position.
3157 // This helps with rounding errors between JS & CSS counts.
3158 const div = this.document_.createElement('div');
3159 div.style.position = 'relative';
3160 div.style.textAlign = options.align;
3161 img.style.position = 'absolute';
3162 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3163 div.appendChild(img);
3164 const row = this.getRowNode(this.scrollbackRows_.length +
3165 this.getCursorRow() - 1);
3166 row.appendChild(div);
3167
3168 io.hideOverlay();
3169 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003170
3171 if (onLoad)
3172 onLoad();
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003173 };
3174
3175 // If we got a malformed image, give up.
3176 img.onerror = (e) => {
3177 this.document_.body.removeChild(img);
3178 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
3179 'Loading $1 failed ...'));
3180 io.pop();
Mike Frysinger3a62a2f2018-03-14 21:11:45 -07003181
3182 if (onError)
3183 onError(e);
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04003184 };
3185 } else {
3186 // We can't use chrome.downloads.download as that requires "downloads"
3187 // permissions, and that works only in extensions, not apps.
3188 const a = this.document_.createElement('a');
3189 a.href = options.uri;
3190 a.download = options.name;
3191 this.document_.body.appendChild(a);
3192 a.click();
3193 a.remove();
3194 }
3195};
3196
3197/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003198 * Returns the selected text, or null if no text is selected.
3199 *
3200 * @return {string|null}
3201 */
rgindaa09e7332012-08-17 12:49:51 -07003202hterm.Terminal.prototype.getSelectionText = function() {
3203 var selection = this.scrollPort_.selection;
3204 selection.sync();
3205
3206 if (selection.isCollapsed)
3207 return null;
3208
3209
3210 // Start offset measures from the beginning of the line.
3211 var startOffset = selection.startOffset;
3212 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003213
Robert Gindafdbb3f22012-09-06 20:23:06 -07003214 if (node.nodeName != 'X-ROW') {
3215 // If the selection doesn't start on an x-row node, then it must be
3216 // somewhere inside the x-row. Add any characters from previous siblings
3217 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003218
3219 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3220 // If node is the text node in a styled span, move up to the span node.
3221 node = node.parentNode;
3222 }
3223
Robert Gindafdbb3f22012-09-06 20:23:06 -07003224 while (node.previousSibling) {
3225 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003226 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003227 }
rgindaa09e7332012-08-17 12:49:51 -07003228 }
3229
3230 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08003231 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
3232 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003233 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003234
Robert Gindafdbb3f22012-09-06 20:23:06 -07003235 if (node.nodeName != 'X-ROW') {
3236 // If the selection doesn't end on an x-row node, then it must be
3237 // somewhere inside the x-row. Add any characters from following siblings
3238 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003239
3240 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3241 // If node is the text node in a styled span, move up to the span node.
3242 node = node.parentNode;
3243 }
3244
Robert Gindafdbb3f22012-09-06 20:23:06 -07003245 while (node.nextSibling) {
3246 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003247 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003248 }
rgindaa09e7332012-08-17 12:49:51 -07003249 }
3250
3251 var rv = this.getRowsText(selection.startRow.rowIndex,
3252 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003253 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003254};
3255
rginda4bba5e12012-06-20 16:15:30 -07003256/**
3257 * Copy the current selection to the system clipboard, then clear it after a
3258 * short delay.
3259 */
3260hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07003261 var text = this.getSelectionText();
3262 if (text != null)
3263 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07003264};
3265
rgindaf0090c92012-02-10 14:58:52 -08003266hterm.Terminal.prototype.overlaySize = function() {
3267 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
3268};
3269
rginda87b86462011-12-14 13:48:03 -08003270/**
3271 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3272 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003273 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003274 */
3275hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08003276 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08003277 this.scrollPort_.scrollRowToBottom(this.getRowCount());
3278
Robert Ginda8cb7d902013-06-20 14:37:18 -07003279 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08003280};
3281
3282/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003283 * Open the selected url.
3284 */
3285hterm.Terminal.prototype.openSelectedUrl_ = function() {
3286 var str = this.getSelectionText();
3287
3288 // If there is no selection, try and expand wherever they clicked.
3289 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003290 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003291 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003292
3293 // If clicking in empty space, return.
3294 if (str == null)
3295 return;
Mike Frysinger70b94692017-01-26 18:57:50 -10003296 }
3297
3298 // Make sure URL is valid before opening.
3299 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
3300 return;
Mike Frysinger43472622017-06-26 18:11:07 -04003301
3302 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003303 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003304 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3305 // We have to whitelist a few protocols that lack authorities and thus
3306 // never use the //. Like mailto.
3307 switch (str.split(':', 1)[0]) {
3308 case 'mailto':
3309 break;
3310 default:
3311 str = 'http://' + str;
3312 break;
3313 }
3314 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003315
Mike Frysinger720fa832017-10-23 01:15:52 -04003316 hterm.openUrl(str);
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003317};
Mike Frysinger70b94692017-01-26 18:57:50 -10003318
3319
3320/**
rgindad5613292012-06-19 15:40:37 -07003321 * Add the terminalRow and terminalColumn properties to mouse events and
3322 * then forward on to onMouse().
3323 *
3324 * The terminalRow and terminalColumn properties contain the (row, column)
3325 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003326 *
3327 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003328 */
3329hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003330 if (e.processedByTerminalHandler_) {
3331 // We register our event handlers on the document, as well as the cursor
3332 // and the scroll blocker. Mouse events that occur on the cursor or
3333 // scroll blocker will also appear on the document, but we don't want to
3334 // process them twice.
3335 //
3336 // We can't just prevent bubbling because that has other side effects, so
3337 // we decorate the event object with this property instead.
3338 return;
3339 }
3340
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003341 var reportMouseEvents = (!this.defeatMouseReports_ &&
3342 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3343
rgindafaa74742012-08-21 13:34:03 -07003344 e.processedByTerminalHandler_ = true;
3345
Robert Gindaeda48db2014-07-17 09:25:30 -07003346 // One based row/column stored on the mouse event.
3347 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
3348 this.scrollPort_.characterSize.height) + 1;
3349 e.terminalColumn = parseInt(e.clientX /
3350 this.scrollPort_.characterSize.width) + 1;
3351
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003352 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3353 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003354 return;
3355 }
3356
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003357 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003358 // If the cursor is visible and we're not sending mouse events to the
3359 // host app, then we want to hide the terminal cursor when the mouse
3360 // cursor is over top. This keeps the terminal cursor from interfering
3361 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003362 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3363 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3364 this.cursorNode_.style.display = 'none';
3365 } else if (this.cursorNode_.style.display == 'none') {
3366 this.cursorNode_.style.display = '';
3367 }
3368 }
rgindad5613292012-06-19 15:40:37 -07003369
Robert Ginda928cf632014-03-05 15:07:41 -08003370 if (e.type == 'mousedown') {
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003371 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003372 // If VT mouse reporting is disabled, or has been defeated with
3373 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003374 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003375 this.setSelectionEnabled(true);
3376 } else {
3377 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003378 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003379 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003380 this.setSelectionEnabled(false);
3381 e.preventDefault();
3382 }
3383 }
3384
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003385 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003386 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003387 this.screen_.expandSelection(this.document_.getSelection());
John Lin2aad22e2018-03-16 13:58:11 +08003388 if (this.copyOnSelect)
3389 this.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07003390 }
3391
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003392 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003393 // Debounce this event with the dblclick event. If you try to doubleclick
3394 // a URL to open it, Chrome will fire click then dblclick, but we won't
3395 // have expanded the selection text at the first click event.
3396 clearTimeout(this.timeouts_.openUrl);
3397 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3398 500);
3399 return;
3400 }
3401
Mike Frysinger847577f2017-05-23 23:25:57 -04003402 if (e.type == 'mousedown') {
3403 if ((this.mouseRightClickPaste && e.button == 2 /* right button */) ||
Mike Frysinger2edd3612017-05-24 00:54:39 -04003404 e.button == this.mousePasteButton) {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003405 if (!this.paste())
Mike Frysinger05a57f02017-08-27 17:48:55 -04003406 console.warn('Could not paste manually due to web restrictions');
Mike Frysinger847577f2017-05-23 23:25:57 -04003407 }
3408 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003409
Mike Frysinger2edd3612017-05-24 00:54:39 -04003410 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003411 !this.document_.getSelection().isCollapsed) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003412 this.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003413 }
3414
3415 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3416 this.scrollBlockerNode_.engaged) {
3417 // Disengage the scroll-blocker after one of these events.
3418 this.scrollBlockerNode_.engaged = false;
3419 this.scrollBlockerNode_.style.top = '-99px';
3420 }
3421
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003422 // Emulate arrow key presses via scroll wheel events.
3423 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3424 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003425 if (e.type == 'wheel') {
3426 var delta = this.scrollPort_.scrollWheelDelta(e);
3427 var lines = lib.f.smartFloorDivide(
3428 Math.abs(delta), this.scrollPort_.characterSize.height);
3429
3430 var data = '\x1bO' + (delta < 0 ? 'B' : 'A');
3431 this.io.sendString(data.repeat(lines));
3432
3433 e.preventDefault();
3434 }
3435 }
Robert Ginda928cf632014-03-05 15:07:41 -08003436 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003437 if (!this.scrollBlockerNode_.engaged) {
3438 if (e.type == 'mousedown') {
3439 // Move the scroll-blocker into place if we want to keep the scrollport
3440 // from scrolling.
3441 this.scrollBlockerNode_.engaged = true;
3442 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3443 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3444 } else if (e.type == 'mousemove') {
3445 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3446 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003447 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003448 e.preventDefault();
3449 }
3450 }
Robert Ginda928cf632014-03-05 15:07:41 -08003451
3452 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003453 }
3454
Robert Ginda928cf632014-03-05 15:07:41 -08003455 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3456 // Restore this on mouseup in case it was temporarily defeated with a
3457 // alt-mousedown. Only do this when the selection is empty so that
3458 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003459 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003460 }
rgindad5613292012-06-19 15:40:37 -07003461};
3462
3463/**
3464 * Clients should override this if they care to know about mouse events.
3465 *
3466 * The event parameter will be a normal DOM mouse click event with additional
3467 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003468 *
3469 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003470 */
3471hterm.Terminal.prototype.onMouse = function(e) { };
3472
3473/**
rginda8e92a692012-05-20 19:37:20 -07003474 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003475 *
3476 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003477 */
Rob Spies06533ba2014-04-24 11:20:37 -07003478hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3479 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003480 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003481
Mike Frysinger8416e0a2017-05-17 09:09:46 -04003482 if (this.reportFocus)
3483 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O');
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003484
Michael Kelly485ecd12014-06-09 11:41:56 -04003485 if (focused === true)
3486 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003487};
3488
3489/**
rginda8ba33642011-12-14 12:31:31 -08003490 * React when the ScrollPort is scrolled.
3491 */
3492hterm.Terminal.prototype.onScroll_ = function() {
3493 this.scheduleSyncCursorPosition_();
3494};
3495
3496/**
rginda9846e2f2012-01-27 13:53:33 -08003497 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003498 *
3499 * @param {Event} e The DOM paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003500 */
3501hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07003502 var data = e.text.replace(/\n/mg, '\r');
Connor Hegartyf525ceb2014-09-03 13:36:03 -07003503 data = this.keyboard.encode(data);
Mike Frysingere8c32c82018-03-11 14:57:28 -07003504 if (this.options_.bracketedPaste) {
3505 // We strip out most escape sequences as they can cause issues (like
3506 // inserting an \x1b[201~ midstream). We pass through whitespace
3507 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
3508 // This matches xterm behavior.
3509 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
3510 data = '\x1b[200~' + filter(data) + '\x1b[201~';
3511 }
Robert Gindaa063b202014-07-21 11:08:25 -07003512
3513 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003514};
3515
3516/**
rgindaa09e7332012-08-17 12:49:51 -07003517 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003518 *
3519 * @param {Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003520 */
3521hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003522 if (!this.useDefaultWindowCopy) {
3523 e.preventDefault();
3524 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3525 }
rgindaa09e7332012-08-17 12:49:51 -07003526};
3527
3528/**
rginda8ba33642011-12-14 12:31:31 -08003529 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003530 *
3531 * Note: This function should not directly contain code that alters the internal
3532 * state of the terminal. That kind of code belongs in realizeWidth or
3533 * realizeHeight, so that it can be executed synchronously in the case of a
3534 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003535 */
3536hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003537 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003538 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003539 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003540 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003541
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003542 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003543 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003544 // gets removed from the document or during the initial load, and we can't
3545 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003546 // This can also happen if called before the scrollPort calculates the
3547 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003548 return;
3549 }
3550
rgindaa8ba17d2012-08-15 14:41:10 -07003551 var isNewSize = (columnCount != this.screenSize.width ||
3552 rowCount != this.screenSize.height);
3553
3554 // We do this even if the size didn't change, just to be sure everything is
3555 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003556 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003557 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003558
3559 if (isNewSize)
3560 this.overlaySize();
3561
Robert Gindafb1be6a2013-12-11 11:56:22 -08003562 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003563 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08003564};
3565
3566/**
3567 * Service the cursor blink timeout.
3568 */
3569hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003570 if (!this.options_.cursorBlink) {
3571 delete this.timeouts_.cursorBlink;
3572 return;
3573 }
3574
Robert Ginda830583c2013-08-07 13:20:46 -07003575 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3576 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003577 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003578 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3579 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003580 } else {
rginda87b86462011-12-14 13:48:03 -08003581 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003582 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3583 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003584 }
3585};
David Reveman8f552492012-03-28 12:18:41 -04003586
3587/**
3588 * Set the scrollbar-visible mode bit.
3589 *
3590 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3591 * Otherwise it will not.
3592 *
3593 * Defaults to on.
3594 *
3595 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3596 */
3597hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3598 this.scrollPort_.setScrollbarVisible(state);
3599};
Michael Kelly485ecd12014-06-09 11:41:56 -04003600
3601/**
Rob Spies49039e52014-12-17 13:40:04 -08003602 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003603 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003604 *
3605 * Defaults to 1.
3606 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003607 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003608 */
3609hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3610 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3611};
3612
3613/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003614 * Close all web notifications created by terminal bells.
3615 */
3616hterm.Terminal.prototype.closeBellNotifications_ = function() {
3617 this.bellNotificationList_.forEach(function(n) {
3618 n.close();
3619 });
3620 this.bellNotificationList_.length = 0;
3621};