blob: 37bad5b76c6108dc78b3a2f40135c84a1f0f3af4 [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',
Rob Spiesf4e90e82015-01-28 12:10:13 -08008 'lib.f', 'hterm.Keyboard', 'hterm.Options', 'hterm.PreferenceManager',
Ricky Liang48f05cb2013-12-31 23:35:29 +08009 'hterm.Screen', 'hterm.ScrollPort', 'hterm.Size',
10 '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
Michael Kelly485ecd12014-06-09 11:41:56 -0400110 // All terminal bell notifications that have been generated (not necessarily
111 // shown).
112 this.bellNotificationList_ = [];
113
114 // Whether we have permission to display notifications.
115 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400116
rginda6d397402012-01-17 10:58:29 -0800117 // Cursor position and attributes saved with DECSC.
118 this.savedOptions_ = {};
119
rginda8ba33642011-12-14 12:31:31 -0800120 // The current mode bits for the terminal.
121 this.options_ = new hterm.Options();
122
123 // Timeouts we might need to clear.
124 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800125
126 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800127 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800128
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800129 this.saveCursorAndState(true);
130
Zhu Qunying30d40712017-03-14 16:27:00 -0700131 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800132 this.keyboard = new hterm.Keyboard(this);
133
rginda87b86462011-12-14 13:48:03 -0800134 // General IO interface that can be given to third parties without exposing
135 // the entire terminal object.
136 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800137
rgindad5613292012-06-19 15:40:37 -0700138 // True if mouse-click-drag should scroll the terminal.
139 this.enableMouseDragScroll = true;
140
Robert Ginda57f03b42012-09-13 11:02:48 -0700141 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400142 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700143 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700144
Zhu Qunying30d40712017-03-14 16:27:00 -0700145 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700146 this.useDefaultWindowCopy = false;
147
148 this.clearSelectionAfterCopy = true;
149
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400150 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800151 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700152
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400153 // Whether we allow images to be shown.
154 this.allowImagesInline = null;
155
Gabriel Holodake8a09be2017-10-10 01:07:11 -0400156 this.reportFocus = false;
157
Robert Ginda57f03b42012-09-13 11:02:48 -0700158 this.setProfile(opt_profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500159 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800160};
161
162/**
Robert Ginda830583c2013-08-07 13:20:46 -0700163 * Possible cursor shapes.
164 */
165hterm.Terminal.cursorShape = {
166 BLOCK: 'BLOCK',
167 BEAM: 'BEAM',
168 UNDERLINE: 'UNDERLINE'
169};
170
171/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700172 * Clients should override this to be notified when the terminal is ready
173 * for use.
174 *
175 * The terminal initialization is asynchronous, and shouldn't be used before
176 * this method is called.
177 */
178hterm.Terminal.prototype.onTerminalReady = function() { };
179
180/**
rginda35c456b2012-02-09 17:29:05 -0800181 * Default tab with of 8 to match xterm.
182 */
183hterm.Terminal.prototype.tabWidth = 8;
184
185/**
rginda9f5222b2012-03-05 11:53:28 -0800186 * Select a preference profile.
187 *
188 * This will load the terminal preferences for the given profile name and
189 * associate subsequent preference changes with the new preference profile.
190 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500191 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800192 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700193 * @param {function} opt_callback Optional callback to invoke when the profile
194 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800195 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700196hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
197 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800198
Robert Ginda57f03b42012-09-13 11:02:48 -0700199 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800200
Robert Ginda57f03b42012-09-13 11:02:48 -0700201 if (this.prefs_)
202 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800203
Robert Ginda57f03b42012-09-13 11:02:48 -0700204 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
205 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800206 'alt-gr-mode': function(v) {
207 if (v == null) {
208 if (navigator.language.toLowerCase() == 'en-us') {
209 v = 'none';
210 } else {
211 v = 'right-alt';
212 }
213 } else if (typeof v == 'string') {
214 v = v.toLowerCase();
215 } else {
216 v = 'none';
217 }
218
219 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v))
220 v = 'none';
221
222 terminal.keyboard.altGrMode = v;
223 },
224
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700225 'alt-backspace-is-meta-backspace': function(v) {
226 terminal.keyboard.altBackspaceIsMetaBackspace = v;
227 },
228
Robert Ginda57f03b42012-09-13 11:02:48 -0700229 'alt-is-meta': function(v) {
230 terminal.keyboard.altIsMeta = v;
231 },
232
233 'alt-sends-what': function(v) {
234 if (!/^(escape|8-bit|browser-key)$/.test(v))
235 v = 'escape';
236
237 terminal.keyboard.altSendsWhat = v;
238 },
239
240 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800241 var ary = v.match(/^lib-resource:(\S+)/);
242 if (ary) {
243 terminal.bellAudio_.setAttribute('src',
244 lib.resource.getDataUrl(ary[1]));
245 } else {
246 terminal.bellAudio_.setAttribute('src', v);
247 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700248 },
249
Michael Kelly485ecd12014-06-09 11:41:56 -0400250 'desktop-notification-bell': function(v) {
251 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700252 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400253 Notification.permission === 'granted';
254 if (!terminal.desktopNotificationBell_) {
255 // Note: We don't call Notification.requestPermission here because
256 // Chrome requires the call be the result of a user action (such as an
257 // onclick handler), and pref listeners are run asynchronously.
258 //
259 // A way of working around this would be to display a dialog in the
260 // terminal with a "click-to-request-permission" button.
261 console.warn('desktop-notification-bell is true but we do not have ' +
262 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400263 }
264 } else {
265 terminal.desktopNotificationBell_ = false;
266 }
267 },
268
Robert Ginda57f03b42012-09-13 11:02:48 -0700269 'background-color': function(v) {
270 terminal.setBackgroundColor(v);
271 },
272
273 'background-image': function(v) {
274 terminal.scrollPort_.setBackgroundImage(v);
275 },
276
277 'background-size': function(v) {
278 terminal.scrollPort_.setBackgroundSize(v);
279 },
280
281 'background-position': function(v) {
282 terminal.scrollPort_.setBackgroundPosition(v);
283 },
284
285 'backspace-sends-backspace': function(v) {
286 terminal.keyboard.backspaceSendsBackspace = v;
287 },
288
Brad Town18654b62015-03-12 00:27:45 -0700289 'character-map-overrides': function(v) {
290 if (!(v == null || v instanceof Object)) {
291 console.warn('Preference character-map-modifications is not an ' +
292 'object: ' + v);
293 return;
294 }
295
Mike Frysinger095d4062017-06-14 00:29:48 -0700296 terminal.vt.characterMaps.reset();
297 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700298 },
299
Robert Ginda57f03b42012-09-13 11:02:48 -0700300 'cursor-blink': function(v) {
301 terminal.setCursorBlink(!!v);
302 },
303
Robert Gindaea2183e2014-07-17 09:51:51 -0700304 'cursor-blink-cycle': function(v) {
305 if (v instanceof Array &&
306 typeof v[0] == 'number' &&
307 typeof v[1] == 'number') {
308 terminal.cursorBlinkCycle_ = v;
309 } else if (typeof v == 'number') {
310 terminal.cursorBlinkCycle_ = [v, v];
311 } else {
312 // Fast blink indicates an error.
313 terminal.cursorBlinkCycle_ = [100, 100];
314 }
315 },
316
Robert Ginda57f03b42012-09-13 11:02:48 -0700317 'cursor-color': function(v) {
318 terminal.setCursorColor(v);
319 },
320
321 'color-palette-overrides': function(v) {
322 if (!(v == null || v instanceof Object || v instanceof Array)) {
323 console.warn('Preference color-palette-overrides is not an array or ' +
324 'object: ' + v);
325 return;
rginda9f5222b2012-03-05 11:53:28 -0800326 }
rginda9f5222b2012-03-05 11:53:28 -0800327
Robert Ginda57f03b42012-09-13 11:02:48 -0700328 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700329
Robert Ginda57f03b42012-09-13 11:02:48 -0700330 if (v) {
331 for (var key in v) {
332 var i = parseInt(key);
333 if (isNaN(i) || i < 0 || i > 255) {
334 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
335 continue;
336 }
337
338 if (v[i]) {
339 var rgb = lib.colors.normalizeCSS(v[i]);
340 if (rgb)
341 lib.colors.colorPalette[i] = rgb;
342 }
343 }
rginda30f20f62012-04-05 16:36:19 -0700344 }
rginda30f20f62012-04-05 16:36:19 -0700345
Evan Jones5f9df812016-12-06 09:38:58 -0500346 terminal.primaryScreen_.textAttributes.resetColorPalette();
Robert Ginda57f03b42012-09-13 11:02:48 -0700347 terminal.alternateScreen_.textAttributes.resetColorPalette();
348 },
rginda30f20f62012-04-05 16:36:19 -0700349
Robert Ginda57f03b42012-09-13 11:02:48 -0700350 'copy-on-select': function(v) {
351 terminal.copyOnSelect = !!v;
352 },
rginda9f5222b2012-03-05 11:53:28 -0800353
Rob Spies0bec09b2014-06-06 15:58:09 -0700354 'use-default-window-copy': function(v) {
355 terminal.useDefaultWindowCopy = !!v;
356 },
357
358 'clear-selection-after-copy': function(v) {
359 terminal.clearSelectionAfterCopy = !!v;
360 },
361
Robert Ginda7e5e9522014-03-14 12:23:58 -0700362 'ctrl-plus-minus-zero-zoom': function(v) {
363 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
364 },
365
Robert Gindafb5a3f92014-05-13 14:12:00 -0700366 'ctrl-c-copy': function(v) {
367 terminal.keyboard.ctrlCCopy = v;
368 },
369
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100370 'ctrl-v-paste': function(v) {
371 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700372 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100373 },
374
Masaya Suzuki273aa982014-05-31 07:25:55 +0900375 'east-asian-ambiguous-as-two-column': function(v) {
376 lib.wc.regardCjkAmbiguous = v;
377 },
378
Robert Ginda57f03b42012-09-13 11:02:48 -0700379 'enable-8-bit-control': function(v) {
380 terminal.vt.enable8BitControl = !!v;
381 },
rginda30f20f62012-04-05 16:36:19 -0700382
Robert Ginda57f03b42012-09-13 11:02:48 -0700383 'enable-bold': function(v) {
384 terminal.syncBoldSafeState();
385 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400386
Robert Ginda3e278d72014-03-25 13:18:51 -0700387 'enable-bold-as-bright': function(v) {
388 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
389 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
390 },
391
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400392 'enable-blink': function(v) {
Mike Frysinger261597c2017-12-28 01:14:21 -0500393 terminal.setTextBlink(!!v);
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400394 },
395
Robert Ginda57f03b42012-09-13 11:02:48 -0700396 'enable-clipboard-write': function(v) {
397 terminal.vt.enableClipboardWrite = !!v;
398 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400399
Robert Ginda3755e752013-05-31 13:34:09 -0700400 'enable-dec12': function(v) {
401 terminal.vt.enableDec12 = !!v;
402 },
403
Robert Ginda57f03b42012-09-13 11:02:48 -0700404 'font-family': function(v) {
405 terminal.syncFontFamily();
406 },
rginda30f20f62012-04-05 16:36:19 -0700407
Robert Ginda57f03b42012-09-13 11:02:48 -0700408 'font-size': function(v) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500409 v = parseInt(v);
410 if (v <= 0) {
411 console.error(`Invalid font size: ${v}`);
412 return;
413 }
414
Robert Ginda57f03b42012-09-13 11:02:48 -0700415 terminal.setFontSize(v);
416 },
rginda9875d902012-08-20 16:21:57 -0700417
Robert Ginda57f03b42012-09-13 11:02:48 -0700418 'font-smoothing': function(v) {
419 terminal.syncFontFamily();
420 },
rgindade84e382012-04-20 15:39:31 -0700421
Robert Ginda57f03b42012-09-13 11:02:48 -0700422 'foreground-color': function(v) {
423 terminal.setForegroundColor(v);
424 },
rginda30f20f62012-04-05 16:36:19 -0700425
Robert Ginda57f03b42012-09-13 11:02:48 -0700426 'home-keys-scroll': function(v) {
427 terminal.keyboard.homeKeysScroll = v;
428 },
rginda4bba5e12012-06-20 16:15:30 -0700429
Robert Gindaa8165692015-06-15 14:46:31 -0700430 'keybindings': function(v) {
431 terminal.keyboard.bindings.clear();
432
433 if (!v)
434 return;
435
436 if (!(v instanceof Object)) {
437 console.error('Error in keybindings preference: Expected object');
438 return;
439 }
440
441 try {
442 terminal.keyboard.bindings.addBindings(v);
443 } catch (ex) {
444 console.error('Error in keybindings preference: ' + ex);
445 }
446 },
447
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700448 'media-keys-are-fkeys': function(v) {
449 terminal.keyboard.mediaKeysAreFKeys = v;
450 },
451
Robert Ginda57f03b42012-09-13 11:02:48 -0700452 'meta-sends-escape': function(v) {
453 terminal.keyboard.metaSendsEscape = v;
454 },
rginda30f20f62012-04-05 16:36:19 -0700455
Mike Frysinger847577f2017-05-23 23:25:57 -0400456 'mouse-right-click-paste': function(v) {
457 terminal.mouseRightClickPaste = v;
458 },
459
Robert Ginda57f03b42012-09-13 11:02:48 -0700460 'mouse-paste-button': function(v) {
461 terminal.syncMousePasteButton();
462 },
rgindaa8ba17d2012-08-15 14:41:10 -0700463
Robert Gindae76aa9f2014-03-14 12:29:12 -0700464 'page-keys-scroll': function(v) {
465 terminal.keyboard.pageKeysScroll = v;
466 },
467
Robert Ginda40932892012-12-10 17:26:40 -0800468 'pass-alt-number': function(v) {
469 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800470 // Let Alt-1..9 pass to the browser (to control tab switching) on
471 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500472 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800473 }
474
475 terminal.passAltNumber = v;
476 },
477
478 'pass-ctrl-number': function(v) {
479 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800480 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
481 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500482 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800483 }
484
485 terminal.passCtrlNumber = v;
486 },
487
488 'pass-meta-number': function(v) {
489 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800490 // Let Meta-1..9 pass to the browser (to control tab switching) on
491 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500492 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800493 }
494
495 terminal.passMetaNumber = v;
496 },
497
Marius Schilder77857b32014-05-14 16:21:26 -0700498 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700499 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700500 },
501
Robert Ginda8cb7d902013-06-20 14:37:18 -0700502 'receive-encoding': function(v) {
503 if (!(/^(utf-8|raw)$/).test(v)) {
504 console.warn('Invalid value for "receive-encoding": ' + v);
505 v = 'utf-8';
506 }
507
508 terminal.vt.characterEncoding = v;
509 },
510
Robert Ginda57f03b42012-09-13 11:02:48 -0700511 'scroll-on-keystroke': function(v) {
512 terminal.scrollOnKeystroke_ = v;
513 },
rginda9f5222b2012-03-05 11:53:28 -0800514
Robert Ginda57f03b42012-09-13 11:02:48 -0700515 'scroll-on-output': function(v) {
516 terminal.scrollOnOutput_ = v;
517 },
rginda30f20f62012-04-05 16:36:19 -0700518
Robert Ginda57f03b42012-09-13 11:02:48 -0700519 'scrollbar-visible': function(v) {
520 terminal.setScrollbarVisible(v);
521 },
rginda9f5222b2012-03-05 11:53:28 -0800522
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400523 'scroll-wheel-may-send-arrow-keys': function(v) {
524 terminal.scrollWheelArrowKeys_ = v;
525 },
526
Rob Spies49039e52014-12-17 13:40:04 -0800527 'scroll-wheel-move-multiplier': function(v) {
528 terminal.setScrollWheelMoveMultipler(v);
529 },
530
Robert Ginda8cb7d902013-06-20 14:37:18 -0700531 'send-encoding': function(v) {
532 if (!(/^(utf-8|raw)$/).test(v)) {
533 console.warn('Invalid value for "send-encoding": ' + v);
534 v = 'utf-8';
535 }
536
537 terminal.keyboard.characterEncoding = v;
538 },
539
Robert Ginda57f03b42012-09-13 11:02:48 -0700540 'shift-insert-paste': function(v) {
541 terminal.keyboard.shiftInsertPaste = v;
542 },
rginda9f5222b2012-03-05 11:53:28 -0800543
Mike Frysingera7768922017-07-28 15:00:12 -0400544 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400545 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400546 },
547
Robert Gindae76aa9f2014-03-14 12:29:12 -0700548 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400549 terminal.scrollPort_.setUserCssUrl(v);
550 },
551
552 'user-css-text': function(v) {
553 terminal.scrollPort_.setUserCssText(v);
554 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400555
556 'word-break-match-left': function(v) {
557 terminal.primaryScreen_.wordBreakMatchLeft = v;
558 terminal.alternateScreen_.wordBreakMatchLeft = v;
559 },
560
561 'word-break-match-right': function(v) {
562 terminal.primaryScreen_.wordBreakMatchRight = v;
563 terminal.alternateScreen_.wordBreakMatchRight = v;
564 },
565
566 'word-break-match-middle': function(v) {
567 terminal.primaryScreen_.wordBreakMatchMiddle = v;
568 terminal.alternateScreen_.wordBreakMatchMiddle = v;
569 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400570
571 'allow-images-inline': function(v) {
572 terminal.allowImagesInline = v;
573 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700574 });
rginda30f20f62012-04-05 16:36:19 -0700575
Robert Ginda57f03b42012-09-13 11:02:48 -0700576 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800577 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700578
579 if (opt_callback)
580 opt_callback();
581 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800582};
583
Rob Spies56953412014-04-28 14:09:47 -0700584
585/**
586 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500587 *
588 * @return {hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700589 */
590hterm.Terminal.prototype.getPrefs = function() {
591 return this.prefs_;
592};
593
Robert Gindaa063b202014-07-21 11:08:25 -0700594/**
595 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500596 *
597 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700598 */
599hterm.Terminal.prototype.setBracketedPaste = function(state) {
600 this.options_.bracketedPaste = state;
601};
Rob Spies56953412014-04-28 14:09:47 -0700602
rginda8e92a692012-05-20 19:37:20 -0700603/**
604 * Set the color for the cursor.
605 *
606 * If you want this setting to persist, set it through prefs_, rather than
607 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500608 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500609 * @param {string=} color The color to set. If not defined, we reset to the
610 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700611 */
612hterm.Terminal.prototype.setCursorColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500613 if (color === undefined)
614 color = this.prefs_.get('cursor-color');
615
Robert Ginda830583c2013-08-07 13:20:46 -0700616 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700617 this.cursorNode_.style.backgroundColor = color;
618 this.cursorNode_.style.borderColor = color;
619};
620
621/**
622 * Return the current cursor color as a string.
Evan Jones2600d4f2016-12-06 09:29:36 -0500623 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700624 */
625hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700626 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700627};
628
629/**
rgindad5613292012-06-19 15:40:37 -0700630 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500631 *
632 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700633 */
634hterm.Terminal.prototype.setSelectionEnabled = function(state) {
635 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700636};
637
638/**
rginda8e92a692012-05-20 19:37:20 -0700639 * Set the background color.
640 *
641 * If you want this setting to persist, set it through prefs_, rather than
642 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500643 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500644 * @param {string=} color The color to set. If not defined, we reset to the
645 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700646 */
647hterm.Terminal.prototype.setBackgroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500648 if (color === undefined)
649 color = this.prefs_.get('background-color');
650
rgindacbbd7482012-06-13 15:06:16 -0700651 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700652 this.primaryScreen_.textAttributes.setDefaults(
653 this.foregroundColor_, this.backgroundColor_);
654 this.alternateScreen_.textAttributes.setDefaults(
655 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700656 this.scrollPort_.setBackgroundColor(color);
657};
658
rginda9f5222b2012-03-05 11:53:28 -0800659/**
660 * Return the current terminal background color.
661 *
662 * Intended for use by other classes, so we don't have to expose the entire
663 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500664 *
665 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800666 */
667hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700668 return this.backgroundColor_;
669};
670
671/**
672 * Set the foreground color.
673 *
674 * If you want this setting to persist, set it through prefs_, rather than
675 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500676 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500677 * @param {string=} color The color to set. If not defined, we reset to the
678 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700679 */
680hterm.Terminal.prototype.setForegroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500681 if (color === undefined)
682 color = this.prefs_.get('foreground-color');
683
rgindacbbd7482012-06-13 15:06:16 -0700684 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700685 this.primaryScreen_.textAttributes.setDefaults(
686 this.foregroundColor_, this.backgroundColor_);
687 this.alternateScreen_.textAttributes.setDefaults(
688 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700689 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800690};
691
692/**
693 * Return the current terminal foreground color.
694 *
695 * Intended for use by other classes, so we don't have to expose the entire
696 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500697 *
698 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800699 */
700hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700701 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800702};
703
704/**
rginda87b86462011-12-14 13:48:03 -0800705 * Create a new instance of a terminal command and run it with a given
706 * argument string.
707 *
708 * @param {function} commandClass The constructor for a terminal command.
709 * @param {string} argString The argument string to pass to the command.
710 */
711hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700712 var environment = this.prefs_.get('environment');
713 if (typeof environment != 'object' || environment == null)
714 environment = {};
715
rginda87b86462011-12-14 13:48:03 -0800716 var self = this;
717 this.command = new commandClass(
718 { argString: argString || '',
719 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700720 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800721 onExit: function(code) {
722 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800723 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700724 if (self.prefs_.get('close-on-exit'))
725 window.close();
rginda87b86462011-12-14 13:48:03 -0800726 }
727 });
728
rgindafeaf3142012-01-31 15:14:20 -0800729 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800730 this.command.run();
731};
732
733/**
rgindafeaf3142012-01-31 15:14:20 -0800734 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500735 *
736 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800737 */
738hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700739 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800740};
741
742/**
743 * Install the keyboard handler for this terminal.
744 *
745 * This will prevent the browser from seeing any keystrokes sent to the
746 * terminal.
747 */
748hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700749 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
rgindafeaf3142012-01-31 15:14:20 -0800750}
751
752/**
753 * Uninstall the keyboard handler for this terminal.
754 */
755hterm.Terminal.prototype.uninstallKeyboard = function() {
756 this.keyboard.installKeyboard(null);
757}
758
759/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400760 * Set a CSS variable.
761 *
762 * Normally this is used to set variables in the hterm namespace.
763 *
764 * @param {string} name The variable to set.
765 * @param {string} value The value to assign to the variable.
766 * @param {string?} opt_prefix The variable namespace/prefix to use.
767 */
768hterm.Terminal.prototype.setCssVar = function(name, value,
769 opt_prefix='--hterm-') {
770 this.document_.documentElement.style.setProperty(
771 `${opt_prefix}${name}`, value);
772};
773
774/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500775 * Get a CSS variable.
776 *
777 * Normally this is used to get variables in the hterm namespace.
778 *
779 * @param {string} name The variable to read.
780 * @param {string?} opt_prefix The variable namespace/prefix to use.
781 * @return {string} The current setting for this variable.
782 */
783hterm.Terminal.prototype.getCssVar = function(name, opt_prefix='--hterm-') {
784 return this.document_.documentElement.style.getPropertyValue(
785 `${opt_prefix}${name}`);
786};
787
788/**
rginda35c456b2012-02-09 17:29:05 -0800789 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800790 *
791 * Call setFontSize(0) to reset to the default font size.
792 *
793 * This function does not modify the font-size preference.
794 *
795 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800796 */
797hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500798 if (px <= 0)
rginda9f5222b2012-03-05 11:53:28 -0800799 px = this.prefs_.get('font-size');
800
rginda35c456b2012-02-09 17:29:05 -0800801 this.scrollPort_.setFontSize(px);
Mike Frysingercce97c42017-08-05 01:11:22 -0400802 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
803 this.setCssVar('charsize-height',
804 this.scrollPort_.characterSize.height + 'px');
rginda35c456b2012-02-09 17:29:05 -0800805};
806
807/**
808 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500809 *
810 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800811 */
812hterm.Terminal.prototype.getFontSize = function() {
813 return this.scrollPort_.getFontSize();
814};
815
816/**
rginda8e92a692012-05-20 19:37:20 -0700817 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500818 *
819 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700820 */
821hterm.Terminal.prototype.getFontFamily = function() {
822 return this.scrollPort_.getFontFamily();
823};
824
825/**
rginda35c456b2012-02-09 17:29:05 -0800826 * Set the CSS "font-family" for this terminal.
827 */
rginda9f5222b2012-03-05 11:53:28 -0800828hterm.Terminal.prototype.syncFontFamily = function() {
829 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
830 this.prefs_.get('font-smoothing'));
831 this.syncBoldSafeState();
832};
833
rginda4bba5e12012-06-20 16:15:30 -0700834/**
835 * Set this.mousePasteButton based on the mouse-paste-button pref,
836 * autodetecting if necessary.
837 */
838hterm.Terminal.prototype.syncMousePasteButton = function() {
839 var button = this.prefs_.get('mouse-paste-button');
840 if (typeof button == 'number') {
841 this.mousePasteButton = button;
842 return;
843 }
844
Mike Frysingeree81a002017-12-12 16:14:53 -0500845 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400846 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700847 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400848 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700849 }
850};
851
852/**
853 * Enable or disable bold based on the enable-bold pref, autodetecting if
854 * necessary.
855 */
rginda9f5222b2012-03-05 11:53:28 -0800856hterm.Terminal.prototype.syncBoldSafeState = function() {
857 var enableBold = this.prefs_.get('enable-bold');
858 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700859 this.primaryScreen_.textAttributes.enableBold = enableBold;
860 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800861 return;
862 }
863
rgindaf7521392012-02-28 17:20:34 -0800864 var normalSize = this.scrollPort_.measureCharacterSize();
865 var boldSize = this.scrollPort_.measureCharacterSize('bold');
866
867 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800868 if (!isBoldSafe) {
869 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700870 'from normal. Font family is: ' +
871 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800872 }
rginda9f5222b2012-03-05 11:53:28 -0800873
Robert Gindaed016262012-10-26 16:27:09 -0700874 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
875 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800876};
877
878/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500879 * Control text blinking behavior.
880 *
881 * @param {boolean=} state Whether to enable support for blinking text.
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400882 */
Mike Frysinger261597c2017-12-28 01:14:21 -0500883hterm.Terminal.prototype.setTextBlink = function(state) {
884 if (state === undefined)
885 state = this.prefs_.get('enable-blink');
886 this.setCssVar('blink-node-duration', state ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400887};
888
889/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400890 * Set the mouse cursor style based on the current terminal mode.
891 */
892hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400893 this.setCssVar('mouse-cursor-style',
894 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
895 'var(--hterm-mouse-cursor-text)' :
896 'var(--hterm-mouse-cursor-pointer)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400897};
898
899/**
rginda87b86462011-12-14 13:48:03 -0800900 * Return a copy of the current cursor position.
901 *
902 * @return {hterm.RowCol} The RowCol object representing the current position.
903 */
904hterm.Terminal.prototype.saveCursor = function() {
905 return this.screen_.cursorPosition.clone();
906};
907
Evan Jones2600d4f2016-12-06 09:29:36 -0500908/**
909 * Return the current text attributes.
910 *
911 * @return {string}
912 */
rgindaa19afe22012-01-25 15:40:22 -0800913hterm.Terminal.prototype.getTextAttributes = function() {
914 return this.screen_.textAttributes;
915};
916
Evan Jones2600d4f2016-12-06 09:29:36 -0500917/**
918 * Set the text attributes.
919 *
920 * @param {string} textAttributes The attributes to set.
921 */
rginda1a09aa02012-06-18 21:11:25 -0700922hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
923 this.screen_.textAttributes = textAttributes;
924};
925
rginda87b86462011-12-14 13:48:03 -0800926/**
rgindaf522ce02012-04-17 17:49:17 -0700927 * Return the current browser zoom factor applied to the terminal.
928 *
929 * @return {number} The current browser zoom factor.
930 */
931hterm.Terminal.prototype.getZoomFactor = function() {
932 return this.scrollPort_.characterSize.zoomFactor;
933};
934
935/**
rginda9846e2f2012-01-27 13:53:33 -0800936 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500937 *
938 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800939 */
940hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800941 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800942};
943
944/**
rginda87b86462011-12-14 13:48:03 -0800945 * Restore a previously saved cursor position.
946 *
947 * @param {hterm.RowCol} cursor The position to restore.
948 */
949hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700950 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
951 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800952 this.screen_.setCursorPosition(row, column);
953 if (cursor.column > column ||
954 cursor.column == column && cursor.overflow) {
955 this.screen_.cursorPosition.overflow = true;
956 }
rginda87b86462011-12-14 13:48:03 -0800957};
958
959/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400960 * Clear the cursor's overflow flag.
961 */
962hterm.Terminal.prototype.clearCursorOverflow = function() {
963 this.screen_.cursorPosition.overflow = false;
964};
965
966/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800967 * Save the current cursor state to the corresponding screens.
968 *
969 * See the hterm.Screen.CursorState class for more details.
970 *
971 * @param {boolean=} both If true, update both screens, else only update the
972 * current screen.
973 */
974hterm.Terminal.prototype.saveCursorAndState = function(both) {
975 if (both) {
976 this.primaryScreen_.saveCursorAndState(this.vt);
977 this.alternateScreen_.saveCursorAndState(this.vt);
978 } else
979 this.screen_.saveCursorAndState(this.vt);
980};
981
982/**
983 * Restore the saved cursor state in the corresponding screens.
984 *
985 * See the hterm.Screen.CursorState class for more details.
986 *
987 * @param {boolean=} both If true, update both screens, else only update the
988 * current screen.
989 */
990hterm.Terminal.prototype.restoreCursorAndState = function(both) {
991 if (both) {
992 this.primaryScreen_.restoreCursorAndState(this.vt);
993 this.alternateScreen_.restoreCursorAndState(this.vt);
994 } else
995 this.screen_.restoreCursorAndState(this.vt);
996};
997
998/**
Robert Ginda830583c2013-08-07 13:20:46 -0700999 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001000 *
1001 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -07001002 */
1003hterm.Terminal.prototype.setCursorShape = function(shape) {
1004 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001005 this.restyleCursor_();
Robert Ginda830583c2013-08-07 13:20:46 -07001006}
1007
1008/**
1009 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001010 *
1011 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -07001012 */
1013hterm.Terminal.prototype.getCursorShape = function() {
1014 return this.cursorShape_;
1015}
1016
1017/**
rginda87b86462011-12-14 13:48:03 -08001018 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001019 *
1020 * @param {number} columnCount
rginda87b86462011-12-14 13:48:03 -08001021 */
1022hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001023 if (columnCount == null) {
1024 this.div_.style.width = '100%';
1025 return;
1026 }
1027
Robert Ginda26806d12014-07-24 13:44:07 -07001028 this.div_.style.width = Math.ceil(
1029 this.scrollPort_.characterSize.width *
1030 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001031 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001032 this.scheduleSyncCursorPosition_();
1033};
rginda87b86462011-12-14 13:48:03 -08001034
rgindac9bc5502012-01-18 11:48:44 -08001035/**
rginda35c456b2012-02-09 17:29:05 -08001036 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001037 *
1038 * @param {number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001039 */
1040hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001041 if (rowCount == null) {
1042 this.div_.style.height = '100%';
1043 return;
1044 }
1045
rginda35c456b2012-02-09 17:29:05 -08001046 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -07001047 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -08001048 this.realizeSize_(this.screenSize.width, rowCount);
1049 this.scheduleSyncCursorPosition_();
1050};
1051
1052/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001053 * Deal with terminal size changes.
1054 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001055 * @param {number} columnCount The number of columns.
1056 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001057 */
1058hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
1059 if (columnCount != this.screenSize.width)
1060 this.realizeWidth_(columnCount);
1061
1062 if (rowCount != this.screenSize.height)
1063 this.realizeHeight_(rowCount);
1064
1065 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -07001066 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001067};
1068
1069/**
rgindac9bc5502012-01-18 11:48:44 -08001070 * Deal with terminal width changes.
1071 *
1072 * This function does what needs to be done when the terminal width changes
1073 * out from under us. It happens here rather than in onResize_() because this
1074 * code may need to run synchronously to handle programmatic changes of
1075 * terminal width.
1076 *
1077 * Relying on the browser to send us an async resize event means we may not be
1078 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001079 *
1080 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001081 */
1082hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001083 if (columnCount <= 0)
1084 throw new Error('Attempt to realize bad width: ' + columnCount);
1085
rgindac9bc5502012-01-18 11:48:44 -08001086 var deltaColumns = columnCount - this.screen_.getWidth();
1087
rginda87b86462011-12-14 13:48:03 -08001088 this.screenSize.width = columnCount;
1089 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001090
1091 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -04001092 if (this.defaultTabStops)
1093 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -08001094 } else {
1095 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -04001096 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -08001097 break;
1098
1099 this.tabStops_.pop();
1100 }
1101 }
1102
1103 this.screen_.setColumnCount(this.screenSize.width);
1104};
1105
1106/**
1107 * Deal with terminal height changes.
1108 *
1109 * This function does what needs to be done when the terminal height changes
1110 * out from under us. It happens here rather than in onResize_() because this
1111 * code may need to run synchronously to handle programmatic changes of
1112 * terminal height.
1113 *
1114 * Relying on the browser to send us an async resize event means we may not be
1115 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001116 *
1117 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001118 */
1119hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001120 if (rowCount <= 0)
1121 throw new Error('Attempt to realize bad height: ' + rowCount);
1122
rgindac9bc5502012-01-18 11:48:44 -08001123 var deltaRows = rowCount - this.screen_.getHeight();
1124
1125 this.screenSize.height = rowCount;
1126
1127 var cursor = this.saveCursor();
1128
1129 if (deltaRows < 0) {
1130 // Screen got smaller.
1131 deltaRows *= -1;
1132 while (deltaRows) {
1133 var lastRow = this.getRowCount() - 1;
1134 if (lastRow - this.scrollbackRows_.length == cursor.row)
1135 break;
1136
1137 if (this.getRowText(lastRow))
1138 break;
1139
1140 this.screen_.popRow();
1141 deltaRows--;
1142 }
1143
1144 var ary = this.screen_.shiftRows(deltaRows);
1145 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1146
1147 // We just removed rows from the top of the screen, we need to update
1148 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001149 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001150 } else if (deltaRows > 0) {
1151 // Screen got larger.
1152
1153 if (deltaRows <= this.scrollbackRows_.length) {
1154 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1155 var rows = this.scrollbackRows_.splice(
1156 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1157 this.screen_.unshiftRows(rows);
1158 deltaRows -= scrollbackCount;
1159 cursor.row += scrollbackCount;
1160 }
1161
1162 if (deltaRows)
1163 this.appendRows_(deltaRows);
1164 }
1165
rginda35c456b2012-02-09 17:29:05 -08001166 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001167 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001168};
1169
1170/**
1171 * Scroll the terminal to the top of the scrollback buffer.
1172 */
1173hterm.Terminal.prototype.scrollHome = function() {
1174 this.scrollPort_.scrollRowToTop(0);
1175};
1176
1177/**
1178 * Scroll the terminal to the end.
1179 */
1180hterm.Terminal.prototype.scrollEnd = function() {
1181 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1182};
1183
1184/**
1185 * Scroll the terminal one page up (minus one line) relative to the current
1186 * position.
1187 */
1188hterm.Terminal.prototype.scrollPageUp = function() {
1189 var i = this.scrollPort_.getTopRowIndex();
1190 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
1191};
1192
1193/**
1194 * Scroll the terminal one page down (minus one line) relative to the current
1195 * position.
1196 */
1197hterm.Terminal.prototype.scrollPageDown = function() {
1198 var i = this.scrollPort_.getTopRowIndex();
1199 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -08001200};
1201
rgindac9bc5502012-01-18 11:48:44 -08001202/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001203 * Scroll the terminal one line up relative to the current position.
1204 */
1205hterm.Terminal.prototype.scrollLineUp = function() {
1206 var i = this.scrollPort_.getTopRowIndex();
1207 this.scrollPort_.scrollRowToTop(i - 1);
1208};
1209
1210/**
1211 * Scroll the terminal one line down relative to the current position.
1212 */
1213hterm.Terminal.prototype.scrollLineDown = function() {
1214 var i = this.scrollPort_.getTopRowIndex();
1215 this.scrollPort_.scrollRowToTop(i + 1);
1216};
1217
1218/**
Robert Ginda40932892012-12-10 17:26:40 -08001219 * Clear primary screen, secondary screen, and the scrollback buffer.
1220 */
1221hterm.Terminal.prototype.wipeContents = function() {
1222 this.scrollbackRows_.length = 0;
1223 this.scrollPort_.resetCache();
1224
1225 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
1226 var bottom = screen.getHeight();
1227 if (bottom > 0) {
1228 this.renumberRows_(0, bottom);
1229 this.clearHome(screen);
1230 }
1231 }.bind(this));
1232
1233 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001234 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001235};
1236
1237/**
rgindac9bc5502012-01-18 11:48:44 -08001238 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001239 *
1240 * Perform a full reset to the default values listed in
1241 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001242 */
rginda87b86462011-12-14 13:48:03 -08001243hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001244 this.vt.reset();
1245
rgindac9bc5502012-01-18 11:48:44 -08001246 this.clearAllTabStops();
1247 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001248
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001249 const resetScreen = (screen) => {
1250 // We want to make sure to reset the attributes before we clear the screen.
1251 // The attributes might be used to initialize default/empty rows.
1252 screen.textAttributes.reset();
1253 screen.textAttributes.resetColorPalette();
1254 this.clearHome(screen);
1255 screen.saveCursorAndState(this.vt);
1256 };
1257 resetScreen(this.primaryScreen_);
1258 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001259
Mike Frysinger84301d02017-11-29 13:28:46 -08001260 // Reset terminal options to their default values.
1261 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001262 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1263
Mike Frysinger84301d02017-11-29 13:28:46 -08001264 this.setVTScrollRegion(null, null);
1265
1266 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001267};
1268
rgindac9bc5502012-01-18 11:48:44 -08001269/**
1270 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001271 *
1272 * Perform a soft reset to the default values listed in
1273 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001274 */
rginda0f5c0292012-01-13 11:00:13 -08001275hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001276 this.vt.reset();
1277
rgindab8bc8932012-04-27 12:45:03 -07001278 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001279 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001280
Brad Townb62dfdc2015-03-16 19:07:15 -07001281 // We show the cursor on soft reset but do not alter the blink state.
1282 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1283
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001284 const resetScreen = (screen) => {
1285 // Xterm also resets the color palette on soft reset, even though it doesn't
1286 // seem to be documented anywhere.
1287 screen.textAttributes.reset();
1288 screen.textAttributes.resetColorPalette();
1289 screen.saveCursorAndState(this.vt);
1290 };
1291 resetScreen(this.primaryScreen_);
1292 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001293
rgindab8bc8932012-04-27 12:45:03 -07001294 // The xterm man page explicitly says this will happen on soft reset.
1295 this.setVTScrollRegion(null, null);
1296
1297 // Xterm also shows the cursor on soft reset, but does not alter the blink
1298 // state.
rgindaa19afe22012-01-25 15:40:22 -08001299 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001300};
1301
rgindac9bc5502012-01-18 11:48:44 -08001302/**
1303 * Move the cursor forward to the next tab stop, or to the last column
1304 * if no more tab stops are set.
1305 */
1306hterm.Terminal.prototype.forwardTabStop = function() {
1307 var column = this.screen_.cursorPosition.column;
1308
1309 for (var i = 0; i < this.tabStops_.length; i++) {
1310 if (this.tabStops_[i] > column) {
1311 this.setCursorColumn(this.tabStops_[i]);
1312 return;
1313 }
1314 }
1315
David Benjamin66e954d2012-05-05 21:08:12 -04001316 // xterm does not clear the overflow flag on HT or CHT.
1317 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001318 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001319 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001320};
1321
rgindac9bc5502012-01-18 11:48:44 -08001322/**
1323 * Move the cursor backward to the previous tab stop, or to the first column
1324 * if no previous tab stops are set.
1325 */
1326hterm.Terminal.prototype.backwardTabStop = function() {
1327 var column = this.screen_.cursorPosition.column;
1328
1329 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1330 if (this.tabStops_[i] < column) {
1331 this.setCursorColumn(this.tabStops_[i]);
1332 return;
1333 }
1334 }
1335
1336 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001337};
1338
rgindac9bc5502012-01-18 11:48:44 -08001339/**
1340 * Set a tab stop at the given column.
1341 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001342 * @param {integer} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001343 */
1344hterm.Terminal.prototype.setTabStop = function(column) {
1345 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1346 if (this.tabStops_[i] == column)
1347 return;
1348
1349 if (this.tabStops_[i] < column) {
1350 this.tabStops_.splice(i + 1, 0, column);
1351 return;
1352 }
1353 }
1354
1355 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001356};
1357
rgindac9bc5502012-01-18 11:48:44 -08001358/**
1359 * Clear the tab stop at the current cursor position.
1360 *
1361 * No effect if there is no tab stop at the current cursor position.
1362 */
1363hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1364 var column = this.screen_.cursorPosition.column;
1365
1366 var i = this.tabStops_.indexOf(column);
1367 if (i == -1)
1368 return;
1369
1370 this.tabStops_.splice(i, 1);
1371};
1372
1373/**
1374 * Clear all tab stops.
1375 */
1376hterm.Terminal.prototype.clearAllTabStops = function() {
1377 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001378 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001379};
1380
1381/**
1382 * Set up the default tab stops, starting from a given column.
1383 *
1384 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001385 * from the specified column, or 0 if no column is provided. It also flags
1386 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001387 *
1388 * This does not clear the existing tab stops first, use clearAllTabStops
1389 * for that.
1390 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001391 * @param {integer} opt_start Optional starting zero based starting column, useful
rgindac9bc5502012-01-18 11:48:44 -08001392 * for filling out missing tab stops when the terminal is resized.
1393 */
1394hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1395 var start = opt_start || 0;
1396 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001397 // Round start up to a default tab stop.
1398 start = start - 1 - ((start - 1) % w) + w;
1399 for (var i = start; i < this.screenSize.width; i += w) {
1400 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001401 }
David Benjamin66e954d2012-05-05 21:08:12 -04001402
1403 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001404};
1405
rginda6d397402012-01-17 10:58:29 -08001406/**
rginda8ba33642011-12-14 12:31:31 -08001407 * Interpret a sequence of characters.
1408 *
1409 * Incomplete escape sequences are buffered until the next call.
1410 *
1411 * @param {string} str Sequence of characters to interpret or pass through.
1412 */
1413hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001414 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001415 this.scheduleSyncCursorPosition_();
1416};
1417
1418/**
1419 * Take over the given DIV for use as the terminal display.
1420 *
1421 * @param {HTMLDivElement} div The div to use as the terminal display.
1422 */
1423hterm.Terminal.prototype.decorate = function(div) {
Mike Frysinger5768a9d2017-12-26 12:57:44 -05001424 const charset = div.ownerDocument.characterSet.toLowerCase();
1425 if (charset != 'utf-8') {
1426 console.warn(`Document encoding should be set to utf-8, not "${charset}";` +
1427 ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`);
1428 }
1429
rginda87b86462011-12-14 13:48:03 -08001430 this.div_ = div;
1431
rginda8ba33642011-12-14 12:31:31 -08001432 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001433 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001434 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1435 this.scrollPort_.setBackgroundPosition(
1436 this.prefs_.get('background-position'));
Mike Frysinger08bad432017-04-24 00:50:54 -04001437 this.scrollPort_.setUserCssUrl(this.prefs_.get('user-css'));
1438 this.scrollPort_.setUserCssText(this.prefs_.get('user-css-text'));
rginda30f20f62012-04-05 16:36:19 -07001439
rginda0918b652012-04-04 11:26:24 -07001440 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001441
rginda9f5222b2012-03-05 11:53:28 -08001442 this.setFontSize(this.prefs_.get('font-size'));
1443 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001444
David Reveman8f552492012-03-28 12:18:41 -04001445 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001446 this.setScrollWheelMoveMultipler(
1447 this.prefs_.get('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001448
rginda8ba33642011-12-14 12:31:31 -08001449 this.document_ = this.scrollPort_.getDocument();
1450
Evan Jones5f9df812016-12-06 09:38:58 -05001451 this.document_.body.oncontextmenu = function() { return false; };
rginda4bba5e12012-06-20 16:15:30 -07001452
1453 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001454 var screenNode = this.scrollPort_.getScreenNode();
1455 screenNode.addEventListener('mousedown', onMouse);
1456 screenNode.addEventListener('mouseup', onMouse);
1457 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001458 this.scrollPort_.onScrollWheel = onMouse;
1459
Toni Barzic0bfa8922013-11-22 11:18:35 -08001460 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001461 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001462 // Listen for mousedown events on the screenNode as in FF the focus
1463 // events don't bubble.
1464 screenNode.addEventListener('mousedown', function() {
1465 setTimeout(this.onFocusChange_.bind(this, true));
1466 }.bind(this));
1467
Toni Barzic0bfa8922013-11-22 11:18:35 -08001468 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001469 'blur', this.onFocusChange_.bind(this, false));
1470
1471 var style = this.document_.createElement('style');
1472 style.textContent =
1473 ('.cursor-node[focus="false"] {' +
1474 ' box-sizing: border-box;' +
1475 ' background-color: transparent !important;' +
1476 ' border-width: 2px;' +
1477 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001478 '}' +
1479 '.wc-node {' +
1480 ' display: inline-block;' +
1481 ' text-align: center;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001482 ' width: calc(var(--hterm-charsize-width) * 2);' +
Mike Frysinger4036f6e2017-05-31 14:02:55 -04001483 ' line-height: var(--hterm-charsize-height);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001484 '}' +
1485 ':root {' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001486 ' --hterm-charsize-width: ' + this.scrollPort_.characterSize.width + 'px;' +
1487 ' --hterm-charsize-height: ' + this.scrollPort_.characterSize.height + 'px;' +
Mike Frysingera27c0502017-08-23 22:37:10 -04001488 // Default position hides the cursor for when the window is initializing.
1489 ' --hterm-cursor-offset-col: -1;' +
1490 ' --hterm-cursor-offset-row: -1;' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001491 ' --hterm-blink-node-duration: 0.7s;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001492 ' --hterm-mouse-cursor-text: text;' +
1493 ' --hterm-mouse-cursor-pointer: default;' +
1494 ' --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001495 '}' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001496 '.uri-node:hover {' +
1497 ' text-decoration: underline;' +
1498 ' cursor: pointer;' +
1499 '}' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001500 '@keyframes blink {' +
1501 ' from { opacity: 1.0; }' +
1502 ' to { opacity: 0.0; }' +
1503 '}' +
1504 '.blink-node {' +
1505 ' animation-name: blink;' +
1506 ' animation-duration: var(--hterm-blink-node-duration);' +
1507 ' animation-iteration-count: infinite;' +
1508 ' animation-timing-function: ease-in-out;' +
1509 ' animation-direction: alternate;' +
rginda8e92a692012-05-20 19:37:20 -07001510 '}');
1511 this.document_.head.appendChild(style);
1512
rginda8ba33642011-12-14 12:31:31 -08001513 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001514 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001515 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001516 this.cursorNode_.style.cssText =
1517 ('position: absolute;' +
Mike Frysinger44c32202017-08-05 01:13:09 -04001518 'left: calc(var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));' +
1519 'top: calc(var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));' +
rginda87b86462011-12-14 13:48:03 -08001520 'display: block;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001521 'width: var(--hterm-charsize-width);' +
1522 'height: var(--hterm-charsize-height);' +
Rob Spies06533ba2014-04-24 11:20:37 -07001523 '-webkit-transition: opacity, background-color 100ms linear;' +
1524 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001525
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001526 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001527 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1528 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001529
rginda8ba33642011-12-14 12:31:31 -08001530 this.document_.body.appendChild(this.cursorNode_);
1531
rgindad5613292012-06-19 15:40:37 -07001532 // When 'enableMouseDragScroll' is off we reposition this element directly
1533 // under the mouse cursor after a click. This makes Chrome associate
1534 // subsequent mousemove events with the scroll-blocker. Since the
1535 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1536 // events do not cause the scrollport to scroll.
1537 //
1538 // It's a hack, but it's the cleanest way I could find.
1539 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001540 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
Raymes Khoury6dce2f82018-04-12 15:38:58 +10001541 this.scrollBlockerNode_.setAttribute('aria-hidden', 'true');
rgindad5613292012-06-19 15:40:37 -07001542 this.scrollBlockerNode_.style.cssText =
1543 ('position: absolute;' +
1544 'top: -99px;' +
1545 'display: block;' +
1546 'width: 10px;' +
1547 'height: 10px;');
1548 this.document_.body.appendChild(this.scrollBlockerNode_);
1549
rgindad5613292012-06-19 15:40:37 -07001550 this.scrollPort_.onScrollWheel = onMouse;
1551 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1552 ].forEach(function(event) {
1553 this.scrollBlockerNode_.addEventListener(event, onMouse);
1554 this.cursorNode_.addEventListener(event, onMouse);
1555 this.document_.addEventListener(event, onMouse);
1556 }.bind(this));
1557
1558 this.cursorNode_.addEventListener('mousedown', function() {
1559 setTimeout(this.focus.bind(this));
1560 }.bind(this));
1561
rginda8ba33642011-12-14 12:31:31 -08001562 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001563
rginda87b86462011-12-14 13:48:03 -08001564 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001565 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001566};
1567
rginda0918b652012-04-04 11:26:24 -07001568/**
1569 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001570 *
1571 * @return {HTMLDocument}
rginda0918b652012-04-04 11:26:24 -07001572 */
rginda87b86462011-12-14 13:48:03 -08001573hterm.Terminal.prototype.getDocument = function() {
1574 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001575};
1576
1577/**
rginda0918b652012-04-04 11:26:24 -07001578 * Focus the terminal.
1579 */
1580hterm.Terminal.prototype.focus = function() {
1581 this.scrollPort_.focus();
1582};
1583
1584/**
rginda8ba33642011-12-14 12:31:31 -08001585 * Return the HTML Element for a given row index.
1586 *
1587 * This is a method from the RowProvider interface. The ScrollPort uses
1588 * it to fetch rows on demand as they are scrolled into view.
1589 *
1590 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1591 * pairs to conserve memory.
1592 *
1593 * @param {integer} index The zero-based row index, measured relative to the
1594 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001595 * largest indices.
rginda8ba33642011-12-14 12:31:31 -08001596 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1597 */
1598hterm.Terminal.prototype.getRowNode = function(index) {
1599 if (index < this.scrollbackRows_.length)
1600 return this.scrollbackRows_[index];
1601
1602 var screenIndex = index - this.scrollbackRows_.length;
1603 return this.screen_.rowsArray[screenIndex];
1604};
1605
1606/**
1607 * Return the text content for a given range of rows.
1608 *
1609 * This is a method from the RowProvider interface. The ScrollPort uses
1610 * it to fetch text content on demand when the user attempts to copy their
1611 * selection to the clipboard.
1612 *
1613 * @param {integer} start The zero-based row index to start from, measured
1614 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001615 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001616 * @param {integer} end The zero-based row index to end on, measured
1617 * relative to the start of the scrollback buffer.
1618 * @return {string} A single string containing the text value of the range of
1619 * rows. Lines will be newline delimited, with no trailing newline.
1620 */
1621hterm.Terminal.prototype.getRowsText = function(start, end) {
1622 var ary = [];
1623 for (var i = start; i < end; i++) {
1624 var node = this.getRowNode(i);
1625 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001626 if (i < end - 1 && !node.getAttribute('line-overflow'))
1627 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001628 }
1629
rgindaa09e7332012-08-17 12:49:51 -07001630 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001631};
1632
1633/**
1634 * Return the text content for a given row.
1635 *
1636 * This is a method from the RowProvider interface. The ScrollPort uses
1637 * it to fetch text content on demand when the user attempts to copy their
1638 * selection to the clipboard.
1639 *
1640 * @param {integer} index The zero-based row index to return, measured
1641 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001642 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001643 * @return {string} A string containing the text value of the selected row.
1644 */
1645hterm.Terminal.prototype.getRowText = function(index) {
1646 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001647 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001648};
1649
1650/**
1651 * Return the total number of rows in the addressable screen and in the
1652 * scrollback buffer of this terminal.
1653 *
1654 * This is a method from the RowProvider interface. The ScrollPort uses
1655 * it to compute the size of the scrollbar.
1656 *
1657 * @return {integer} The number of rows in this terminal.
1658 */
1659hterm.Terminal.prototype.getRowCount = function() {
1660 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1661};
1662
1663/**
1664 * Create DOM nodes for new rows and append them to the end of the terminal.
1665 *
1666 * This is the only correct way to add a new DOM node for a row. Notice that
1667 * the new row is appended to the bottom of the list of rows, and does not
1668 * require renumbering (of the rowIndex property) of previous rows.
1669 *
1670 * If you think you want a new blank row somewhere in the middle of the
1671 * terminal, look into moveRows_().
1672 *
1673 * This method does not pay attention to vtScrollTop/Bottom, since you should
1674 * be using moveRows() in cases where they would matter.
1675 *
1676 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001677 *
1678 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001679 */
1680hterm.Terminal.prototype.appendRows_ = function(count) {
1681 var cursorRow = this.screen_.rowsArray.length;
1682 var offset = this.scrollbackRows_.length + cursorRow;
1683 for (var i = 0; i < count; i++) {
1684 var row = this.document_.createElement('x-row');
1685 row.appendChild(this.document_.createTextNode(''));
1686 row.rowIndex = offset + i;
1687 this.screen_.pushRow(row);
1688 }
1689
1690 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1691 if (extraRows > 0) {
1692 var ary = this.screen_.shiftRows(extraRows);
1693 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001694 if (this.scrollPort_.isScrolledEnd)
1695 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001696 }
1697
1698 if (cursorRow >= this.screen_.rowsArray.length)
1699 cursorRow = this.screen_.rowsArray.length - 1;
1700
rginda87b86462011-12-14 13:48:03 -08001701 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001702};
1703
1704/**
1705 * Relocate rows from one part of the addressable screen to another.
1706 *
1707 * This is used to recycle rows during VT scrolls (those which are driven
1708 * by VT commands, rather than by the user manipulating the scrollbar.)
1709 *
1710 * In this case, the blank lines scrolled into the scroll region are made of
1711 * the nodes we scrolled off. These have their rowIndex properties carefully
1712 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001713 *
1714 * @param {number} fromIndex The start index.
1715 * @param {number} count The number of rows to move.
1716 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001717 */
1718hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1719 var ary = this.screen_.removeRows(fromIndex, count);
1720 this.screen_.insertRows(toIndex, ary);
1721
1722 var start, end;
1723 if (fromIndex < toIndex) {
1724 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001725 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001726 } else {
1727 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001728 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001729 }
1730
1731 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001732 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001733};
1734
1735/**
1736 * Renumber the rowIndex property of the given range of rows.
1737 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001738 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001739 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001740 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001741 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001742 *
1743 * @param {number} start The start index.
1744 * @param {number} end The end index.
1745 * @param {hterm.Screen} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001746 */
Robert Ginda40932892012-12-10 17:26:40 -08001747hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1748 var screen = opt_screen || this.screen_;
1749
rginda8ba33642011-12-14 12:31:31 -08001750 var offset = this.scrollbackRows_.length;
1751 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001752 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001753 }
1754};
1755
1756/**
1757 * Print a string to the terminal.
1758 *
1759 * This respects the current insert and wraparound modes. It will add new lines
1760 * to the end of the terminal, scrolling off the top into the scrollback buffer
1761 * if necessary.
1762 *
1763 * The string is *not* parsed for escape codes. Use the interpret() method if
1764 * that's what you're after.
1765 *
1766 * @param{string} str The string to print.
1767 */
1768hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001769 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001770
Ricky Liang48f05cb2013-12-31 23:35:29 +08001771 var strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04001772 // Fun edge case: If the string only contains zero width codepoints (like
1773 // combining characters), we make sure to iterate at least once below.
1774 if (strWidth == 0 && str)
1775 strWidth = 1;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001776
1777 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001778 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1779 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001780 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001781 }
rgindaa19afe22012-01-25 15:40:22 -08001782
Ricky Liang48f05cb2013-12-31 23:35:29 +08001783 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001784 var didOverflow = false;
1785 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001786
rgindaa9abdd82012-08-06 18:05:09 -07001787 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1788 didOverflow = true;
1789 count = this.screenSize.width - this.screen_.cursorPosition.column;
1790 }
rgindaa19afe22012-01-25 15:40:22 -08001791
rgindaa9abdd82012-08-06 18:05:09 -07001792 if (didOverflow && !this.options_.wraparound) {
1793 // If the string overflowed the line but wraparound is off, then the
1794 // last printed character should be the last of the string.
1795 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001796 substr = lib.wc.substr(str, startOffset, count - 1) +
1797 lib.wc.substr(str, strWidth - 1);
1798 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001799 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001800 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001801 }
rgindaa19afe22012-01-25 15:40:22 -08001802
Ricky Liang48f05cb2013-12-31 23:35:29 +08001803 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1804 for (var i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001805 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
1806 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001807
1808 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001809 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001810 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001811 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001812 }
1813 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001814 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07001815 }
1816
1817 this.screen_.maybeClipCurrentRow();
1818 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001819 }
rginda8ba33642011-12-14 12:31:31 -08001820
1821 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001822
rginda9f5222b2012-03-05 11:53:28 -08001823 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001824 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001825};
1826
1827/**
rginda87b86462011-12-14 13:48:03 -08001828 * Set the VT scroll region.
1829 *
rginda87b86462011-12-14 13:48:03 -08001830 * This also resets the cursor position to the absolute (0, 0) position, since
1831 * that's what xterm appears to do.
1832 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001833 * Setting the scroll region to the full height of the terminal will clear
1834 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1835 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1836 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1837 * continue to work as most users would expect.
1838 *
rginda87b86462011-12-14 13:48:03 -08001839 * @param {integer} scrollTop The zero-based top of the scroll region.
1840 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1841 * inclusive.
1842 */
1843hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001844 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001845 this.vtScrollTop_ = null;
1846 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001847 } else {
1848 this.vtScrollTop_ = scrollTop;
1849 this.vtScrollBottom_ = scrollBottom;
1850 }
rginda87b86462011-12-14 13:48:03 -08001851};
1852
1853/**
rginda8ba33642011-12-14 12:31:31 -08001854 * Return the top row index according to the VT.
1855 *
1856 * This will return 0 unless the terminal has been told to restrict scrolling
1857 * to some lower row. It is used for some VT cursor positioning and scrolling
1858 * commands.
1859 *
1860 * @return {integer} The topmost row in the terminal's scroll region.
1861 */
1862hterm.Terminal.prototype.getVTScrollTop = function() {
1863 if (this.vtScrollTop_ != null)
1864 return this.vtScrollTop_;
1865
1866 return 0;
rginda87b86462011-12-14 13:48:03 -08001867};
rginda8ba33642011-12-14 12:31:31 -08001868
1869/**
1870 * Return the bottom row index according to the VT.
1871 *
1872 * This will return the height of the terminal unless the it has been told to
1873 * restrict scrolling to some higher row. It is used for some VT cursor
1874 * positioning and scrolling commands.
1875 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001876 * @return {integer} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001877 */
1878hterm.Terminal.prototype.getVTScrollBottom = function() {
1879 if (this.vtScrollBottom_ != null)
1880 return this.vtScrollBottom_;
1881
rginda87b86462011-12-14 13:48:03 -08001882 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001883}
1884
1885/**
1886 * Process a '\n' character.
1887 *
1888 * If the cursor is on the final row of the terminal this will append a new
1889 * blank row to the screen and scroll the topmost row into the scrollback
1890 * buffer.
1891 *
1892 * Otherwise, this moves the cursor to column zero of the next row.
1893 */
1894hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001895 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1896 this.screen_.rowsArray.length - 1);
1897
1898 if (this.vtScrollBottom_ != null) {
1899 // A VT Scroll region is active, we never append new rows.
1900 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1901 // We're at the end of the VT Scroll Region, perform a VT scroll.
1902 this.vtScrollUp(1);
1903 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1904 } else if (cursorAtEndOfScreen) {
1905 // We're at the end of the screen, the only thing to do is put the
1906 // cursor to column 0.
1907 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1908 } else {
1909 // Anywhere else, advance the cursor row, and reset the column.
1910 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1911 }
1912 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001913 // We're at the end of the screen. Append a new row to the terminal,
1914 // shifting the top row into the scrollback.
1915 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001916 } else {
rginda87b86462011-12-14 13:48:03 -08001917 // Anywhere else in the screen just moves the cursor.
1918 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001919 }
1920};
1921
1922/**
1923 * Like newLine(), except maintain the cursor column.
1924 */
1925hterm.Terminal.prototype.lineFeed = function() {
1926 var column = this.screen_.cursorPosition.column;
1927 this.newLine();
1928 this.setCursorColumn(column);
1929};
1930
1931/**
rginda87b86462011-12-14 13:48:03 -08001932 * If autoCarriageReturn is set then newLine(), else lineFeed().
1933 */
1934hterm.Terminal.prototype.formFeed = function() {
1935 if (this.options_.autoCarriageReturn) {
1936 this.newLine();
1937 } else {
1938 this.lineFeed();
1939 }
1940};
1941
1942/**
1943 * Move the cursor up one row, possibly inserting a blank line.
1944 *
1945 * The cursor column is not changed.
1946 */
1947hterm.Terminal.prototype.reverseLineFeed = function() {
1948 var scrollTop = this.getVTScrollTop();
1949 var currentRow = this.screen_.cursorPosition.row;
1950
1951 if (currentRow == scrollTop) {
1952 this.insertLines(1);
1953 } else {
1954 this.setAbsoluteCursorRow(currentRow - 1);
1955 }
1956};
1957
1958/**
rginda8ba33642011-12-14 12:31:31 -08001959 * Replace all characters to the left of the current cursor with the space
1960 * character.
1961 *
1962 * TODO(rginda): This should probably *remove* the characters (not just replace
1963 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001964 * position.
rginda8ba33642011-12-14 12:31:31 -08001965 */
1966hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001967 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001968 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04001969 const count = cursor.column + 1;
1970 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08001971 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001972};
1973
1974/**
David Benjamin684a9b72012-05-01 17:19:58 -04001975 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001976 *
1977 * The cursor position is unchanged.
1978 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001979 * If the current background color is not the default background color this
1980 * will insert spaces rather than delete. This is unfortunate because the
1981 * trailing space will affect text selection, but it's difficult to come up
1982 * with a way to style empty space that wouldn't trip up the hterm.Screen
1983 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001984 *
1985 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1986 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1987 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05001988 *
1989 * @param {number} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08001990 */
1991hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001992 if (this.screen_.cursorPosition.overflow)
1993 return;
1994
Robert Ginda7fd57082012-09-25 14:41:47 -07001995 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1996 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001997
1998 if (this.screen_.textAttributes.background ===
1999 this.screen_.textAttributes.DEFAULT_COLOR) {
2000 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002001 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002002 this.screen_.cursorPosition.column + count) {
2003 this.screen_.deleteChars(count);
2004 this.clearCursorOverflow();
2005 return;
2006 }
2007 }
2008
rginda87b86462011-12-14 13:48:03 -08002009 var cursor = this.saveCursor();
Mike Frysinger6380bed2017-08-24 18:46:39 -04002010 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08002011 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002012 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002013};
2014
2015/**
2016 * Erase the current line.
2017 *
2018 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002019 */
2020hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08002021 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002022 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002023 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002024 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002025};
2026
2027/**
David Benjamina08d78f2012-05-05 00:28:49 -04002028 * Erase all characters from the start of the screen to the current cursor
2029 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002030 *
2031 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002032 */
2033hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08002034 var cursor = this.saveCursor();
2035
2036 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002037
David Benjamina08d78f2012-05-05 00:28:49 -04002038 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002039 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002040 this.screen_.clearCursorRow();
2041 }
2042
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/**
2048 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002049 * screen, 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.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08002054 var cursor = this.saveCursor();
2055
2056 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002057
David Benjamina08d78f2012-05-05 00:28:49 -04002058 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08002059 for (var i = cursor.row + 1; i <= bottom; i++) {
2060 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002061 this.screen_.clearCursorRow();
2062 }
2063
rginda87b86462011-12-14 13:48:03 -08002064 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002065 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002066};
2067
2068/**
2069 * Fill the terminal with a given character.
2070 *
2071 * This methods does not respect the VT scroll region.
2072 *
2073 * @param {string} ch The character to use for the fill.
2074 */
2075hterm.Terminal.prototype.fill = function(ch) {
2076 var cursor = this.saveCursor();
2077
2078 this.setAbsoluteCursorPosition(0, 0);
2079 for (var row = 0; row < this.screenSize.height; row++) {
2080 for (var col = 0; col < this.screenSize.width; col++) {
2081 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002082 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002083 }
2084 }
2085
2086 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002087};
2088
2089/**
rginda9ea433c2012-03-16 11:57:00 -07002090 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002091 *
rginda9ea433c2012-03-16 11:57:00 -07002092 * This does not respect the scroll region.
2093 *
2094 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2095 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002096 */
rginda9ea433c2012-03-16 11:57:00 -07002097hterm.Terminal.prototype.clearHome = function(opt_screen) {
2098 var screen = opt_screen || this.screen_;
2099 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002100
rginda11057d52012-04-25 12:29:56 -07002101 if (bottom == 0) {
2102 // Empty screen, nothing to do.
2103 return;
2104 }
2105
rgindae4d29232012-01-19 10:47:13 -08002106 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002107 screen.setCursorPosition(i, 0);
2108 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002109 }
2110
rginda9ea433c2012-03-16 11:57:00 -07002111 screen.setCursorPosition(0, 0);
2112};
2113
2114/**
2115 * Erase the entire display without changing the cursor position.
2116 *
2117 * The cursor position is unchanged. This does not respect the scroll
2118 * region.
2119 *
2120 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2121 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002122 */
2123hterm.Terminal.prototype.clear = function(opt_screen) {
2124 var screen = opt_screen || this.screen_;
2125 var cursor = screen.cursorPosition.clone();
2126 this.clearHome(screen);
2127 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002128};
2129
2130/**
2131 * VT command to insert lines at the current cursor row.
2132 *
2133 * This respects the current scroll region. Rows pushed off the bottom are
2134 * lost (they won't show up in the scrollback buffer).
2135 *
rginda8ba33642011-12-14 12:31:31 -08002136 * @param {integer} count The number of lines to insert.
2137 */
2138hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002139 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002140
2141 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002142 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002143
Robert Ginda579186b2012-09-26 11:40:04 -07002144 // The moveCount is the number of rows we need to relocate to make room for
2145 // the new row(s). The count is the distance to move them.
2146 var moveCount = bottom - cursorRow - count + 1;
2147 if (moveCount)
2148 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002149
Robert Ginda579186b2012-09-26 11:40:04 -07002150 for (var i = count - 1; i >= 0; i--) {
2151 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002152 this.screen_.clearCursorRow();
2153 }
rginda8ba33642011-12-14 12:31:31 -08002154};
2155
2156/**
2157 * VT command to delete lines at the current cursor row.
2158 *
2159 * New rows are added to the bottom of scroll region to take their place. New
2160 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002161 *
2162 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002163 */
2164hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002165 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002166
rginda87b86462011-12-14 13:48:03 -08002167 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002168 var bottom = this.getVTScrollBottom();
2169
rginda87b86462011-12-14 13:48:03 -08002170 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002171 count = Math.min(count, maxCount);
2172
rginda87b86462011-12-14 13:48:03 -08002173 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002174 if (count != maxCount)
2175 this.moveRows_(top, count, moveStart);
2176
2177 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002178 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002179 this.screen_.clearCursorRow();
2180 }
2181
rginda87b86462011-12-14 13:48:03 -08002182 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002183 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002184};
2185
2186/**
2187 * Inserts the given number of spaces at the current cursor position.
2188 *
rginda87b86462011-12-14 13:48:03 -08002189 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002190 *
2191 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002192 */
2193hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002194 var cursor = this.saveCursor();
2195
rgindacbbd7482012-06-13 15:06:16 -07002196 var ws = lib.f.getWhitespace(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002197 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002198 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002199
2200 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002201 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002202};
2203
2204/**
2205 * Forward-delete the specified number of characters starting at the cursor
2206 * position.
2207 *
2208 * @param {integer} count The number of characters to delete.
2209 */
2210hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002211 var deleted = this.screen_.deleteChars(count);
2212 if (deleted && !this.screen_.textAttributes.isDefault()) {
2213 var cursor = this.saveCursor();
2214 this.setCursorColumn(this.screenSize.width - deleted);
2215 this.screen_.insertString(lib.f.getWhitespace(deleted));
2216 this.restoreCursor(cursor);
2217 }
2218
David Benjamin54e8bf62012-06-01 22:31:40 -04002219 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002220};
2221
2222/**
2223 * Shift rows in the scroll region upwards by a given number of lines.
2224 *
2225 * New rows are inserted at the bottom of the scroll region to fill the
2226 * vacated rows. The new rows not filled out with the current text attributes.
2227 *
2228 * This function does not affect the scrollback rows at all. Rows shifted
2229 * off the top are lost.
2230 *
rginda87b86462011-12-14 13:48:03 -08002231 * The cursor position is not altered.
2232 *
rginda8ba33642011-12-14 12:31:31 -08002233 * @param {integer} count The number of rows to scroll.
2234 */
2235hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002236 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002237
rginda87b86462011-12-14 13:48:03 -08002238 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002239 this.deleteLines(count);
2240
rginda87b86462011-12-14 13:48:03 -08002241 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002242};
2243
2244/**
2245 * Shift rows below the cursor down by a given number of lines.
2246 *
2247 * This function respects the current scroll region.
2248 *
2249 * New rows are inserted at the top of the scroll region to fill the
2250 * vacated rows. The new rows not filled out with the current text attributes.
2251 *
2252 * This function does not affect the scrollback rows at all. Rows shifted
2253 * off the bottom are lost.
2254 *
2255 * @param {integer} count The number of rows to scroll.
2256 */
2257hterm.Terminal.prototype.vtScrollDown = function(opt_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.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08002261 this.insertLines(opt_count);
2262
rginda87b86462011-12-14 13:48:03 -08002263 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002264};
2265
rginda87b86462011-12-14 13:48:03 -08002266
rginda8ba33642011-12-14 12:31:31 -08002267/**
2268 * Set the cursor position.
2269 *
2270 * The cursor row is relative to the scroll region if the terminal has
2271 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2272 *
2273 * @param {integer} row The new zero-based cursor row.
2274 * @param {integer} row The new zero-based cursor column.
2275 */
2276hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2277 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002278 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002279 } else {
rginda87b86462011-12-14 13:48:03 -08002280 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002281 }
rginda87b86462011-12-14 13:48:03 -08002282};
rginda8ba33642011-12-14 12:31:31 -08002283
Evan Jones2600d4f2016-12-06 09:29:36 -05002284/**
2285 * Move the cursor relative to its current position.
2286 *
2287 * @param {number} row
2288 * @param {number} column
2289 */
rginda87b86462011-12-14 13:48:03 -08002290hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2291 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002292 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2293 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002294 this.screen_.setCursorPosition(row, column);
2295};
2296
Evan Jones2600d4f2016-12-06 09:29:36 -05002297/**
2298 * Move the cursor to the specified position.
2299 *
2300 * @param {number} row
2301 * @param {number} column
2302 */
rginda87b86462011-12-14 13:48:03 -08002303hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002304 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2305 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002306 this.screen_.setCursorPosition(row, column);
2307};
2308
2309/**
2310 * Set the cursor column.
2311 *
2312 * @param {integer} column The new zero-based cursor column.
2313 */
2314hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002315 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002316};
2317
2318/**
2319 * Return the cursor column.
2320 *
2321 * @return {integer} The zero-based cursor column.
2322 */
2323hterm.Terminal.prototype.getCursorColumn = function() {
2324 return this.screen_.cursorPosition.column;
2325};
2326
2327/**
2328 * Set the cursor row.
2329 *
2330 * The cursor row is relative to the scroll region if the terminal has
2331 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2332 *
2333 * @param {integer} row The new cursor row.
2334 */
rginda87b86462011-12-14 13:48:03 -08002335hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2336 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002337};
2338
2339/**
2340 * Return the cursor row.
2341 *
2342 * @return {integer} The zero-based cursor row.
2343 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002344hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002345 return this.screen_.cursorPosition.row;
2346};
2347
2348/**
2349 * Request that the ScrollPort redraw itself soon.
2350 *
2351 * The redraw will happen asynchronously, soon after the call stack winds down.
2352 * Multiple calls will be coalesced into a single redraw.
2353 */
2354hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002355 if (this.timeouts_.redraw)
2356 return;
rginda8ba33642011-12-14 12:31:31 -08002357
2358 var self = this;
rginda87b86462011-12-14 13:48:03 -08002359 this.timeouts_.redraw = setTimeout(function() {
2360 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002361 self.scrollPort_.redraw_();
2362 }, 0);
2363};
2364
2365/**
2366 * Request that the ScrollPort be scrolled to the bottom.
2367 *
2368 * The scroll will happen asynchronously, soon after the call stack winds down.
2369 * Multiple calls will be coalesced into a single scroll.
2370 *
2371 * This affects the scrollbar position of the ScrollPort, and has nothing to
2372 * do with the VT scroll commands.
2373 */
2374hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2375 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002376 return;
rginda8ba33642011-12-14 12:31:31 -08002377
2378 var self = this;
2379 this.timeouts_.scrollDown = setTimeout(function() {
2380 delete self.timeouts_.scrollDown;
2381 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2382 }, 10);
2383};
2384
2385/**
2386 * Move the cursor up a specified number of rows.
2387 *
2388 * @param {integer} count The number of rows to move the cursor.
2389 */
2390hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002391 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002392};
2393
2394/**
2395 * Move the cursor down a specified number of rows.
2396 *
2397 * @param {integer} count The number of rows to move the cursor.
2398 */
2399hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002400 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002401 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2402 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2403 this.screenSize.height - 1);
2404
rgindacbbd7482012-06-13 15:06:16 -07002405 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002406 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002407 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002408};
2409
2410/**
2411 * Move the cursor left a specified number of columns.
2412 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002413 * If reverse wraparound mode is enabled and the previous row wrapped into
2414 * the current row then we back up through the wraparound as well.
2415 *
rginda8ba33642011-12-14 12:31:31 -08002416 * @param {integer} count The number of columns to move the cursor.
2417 */
2418hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002419 count = count || 1;
2420
2421 if (count < 1)
2422 return;
2423
2424 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002425 if (this.options_.reverseWraparound) {
2426 if (this.screen_.cursorPosition.overflow) {
2427 // If this cursor is in the right margin, consume one count to get it
2428 // back to the last column. This only applies when we're in reverse
2429 // wraparound mode.
2430 count--;
2431 this.clearCursorOverflow();
2432
2433 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002434 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002435 }
2436
Robert Gindabfb32622014-07-17 13:20:27 -07002437 var newRow = this.screen_.cursorPosition.row;
2438 var newColumn = currentColumn - count;
2439 if (newColumn < 0) {
2440 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2441 if (newRow < 0) {
2442 // xterm also wraps from row 0 to the last row.
2443 newRow = this.screenSize.height + newRow % this.screenSize.height;
2444 }
2445 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2446 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002447
Robert Gindabfb32622014-07-17 13:20:27 -07002448 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2449
2450 } else {
2451 var newColumn = Math.max(currentColumn - count, 0);
2452 this.setCursorColumn(newColumn);
2453 }
rginda8ba33642011-12-14 12:31:31 -08002454};
2455
2456/**
2457 * Move the cursor right a specified number of columns.
2458 *
2459 * @param {integer} count The number of columns to move the cursor.
2460 */
2461hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002462 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002463
2464 if (count < 1)
2465 return;
2466
rgindacbbd7482012-06-13 15:06:16 -07002467 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002468 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002469 this.setCursorColumn(column);
2470};
2471
2472/**
2473 * Reverse the foreground and background colors of the terminal.
2474 *
2475 * This only affects text that was drawn with no attributes.
2476 *
2477 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2478 * been drawn with attributes that happen to coincide with the default
2479 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002480 *
2481 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002482 */
2483hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002484 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002485 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002486 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2487 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002488 } else {
rginda9f5222b2012-03-05 11:53:28 -08002489 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2490 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002491 }
2492};
2493
2494/**
rginda87b86462011-12-14 13:48:03 -08002495 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002496 *
2497 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002498 */
2499hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002500 this.cursorNode_.style.backgroundColor =
2501 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002502
2503 var self = this;
2504 setTimeout(function() {
Matheus Fernandes2d733082017-09-11 06:43:01 -04002505 self.restyleCursor_();
rginda6d397402012-01-17 10:58:29 -08002506 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002507
Michael Kelly485ecd12014-06-09 11:41:56 -04002508 // bellSquelchTimeout_ affects both audio and notification bells.
2509 if (this.bellSquelchTimeout_)
2510 return;
2511
Robert Ginda92e18102013-03-14 13:56:37 -07002512 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002513 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002514 this.bellSequelchTimeout_ = setTimeout(function() {
2515 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002516 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002517 } else {
2518 delete this.bellSquelchTimeout_;
2519 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002520
2521 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002522 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002523 this.bellNotificationList_.push(n);
2524 // TODO: Should we try to raise the window here?
2525 n.onclick = function() { self.closeBellNotifications_(); };
2526 }
rginda87b86462011-12-14 13:48:03 -08002527};
2528
2529/**
rginda8ba33642011-12-14 12:31:31 -08002530 * Set the origin mode bit.
2531 *
2532 * If origin mode is on, certain VT cursor and scrolling commands measure their
2533 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2534 * to the top of the addressable screen.
2535 *
2536 * Defaults to off.
2537 *
2538 * @param {boolean} state True to set origin mode, false to unset.
2539 */
2540hterm.Terminal.prototype.setOriginMode = function(state) {
2541 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002542 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002543};
2544
2545/**
2546 * Set the insert mode bit.
2547 *
2548 * If insert mode is on, existing text beyond the cursor position will be
2549 * shifted right to make room for new text. Otherwise, new text overwrites
2550 * any existing text.
2551 *
2552 * Defaults to off.
2553 *
2554 * @param {boolean} state True to set insert mode, false to unset.
2555 */
2556hterm.Terminal.prototype.setInsertMode = function(state) {
2557 this.options_.insertMode = state;
2558};
2559
2560/**
rginda87b86462011-12-14 13:48:03 -08002561 * Set the auto carriage return bit.
2562 *
2563 * If auto carriage return is on then a formfeed character is interpreted
2564 * as a newline, otherwise it's the same as a linefeed. The difference boils
2565 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002566 *
2567 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002568 */
2569hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2570 this.options_.autoCarriageReturn = state;
2571};
2572
2573/**
rginda8ba33642011-12-14 12:31:31 -08002574 * Set the wraparound mode bit.
2575 *
2576 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2577 * to the start of the following row. Otherwise, the cursor is clamped to the
2578 * end of the screen and attempts to write past it are ignored.
2579 *
2580 * Defaults to on.
2581 *
2582 * @param {boolean} state True to set wraparound mode, false to unset.
2583 */
2584hterm.Terminal.prototype.setWraparound = function(state) {
2585 this.options_.wraparound = state;
2586};
2587
2588/**
2589 * Set the reverse-wraparound mode bit.
2590 *
2591 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2592 * to the end of the previous row. Otherwise, the cursor is clamped to column
2593 * 0.
2594 *
2595 * Defaults to off.
2596 *
2597 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2598 */
2599hterm.Terminal.prototype.setReverseWraparound = function(state) {
2600 this.options_.reverseWraparound = state;
2601};
2602
2603/**
2604 * Selects between the primary and alternate screens.
2605 *
2606 * If alternate mode is on, the alternate screen is active. Otherwise the
2607 * primary screen is active.
2608 *
2609 * Swapping screens has no effect on the scrollback buffer.
2610 *
2611 * Each screen maintains its own cursor position.
2612 *
2613 * Defaults to off.
2614 *
2615 * @param {boolean} state True to set alternate mode, false to unset.
2616 */
2617hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002618 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002619 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2620
rginda35c456b2012-02-09 17:29:05 -08002621 if (this.screen_.rowsArray.length &&
2622 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2623 // If the screen changed sizes while we were away, our rowIndexes may
2624 // be incorrect.
2625 var offset = this.scrollbackRows_.length;
2626 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002627 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002628 ary[i].rowIndex = offset + i;
2629 }
2630 }
rginda8ba33642011-12-14 12:31:31 -08002631
rginda35c456b2012-02-09 17:29:05 -08002632 this.realizeWidth_(this.screenSize.width);
2633 this.realizeHeight_(this.screenSize.height);
2634 this.scrollPort_.syncScrollHeight();
2635 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002636
rginda6d397402012-01-17 10:58:29 -08002637 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002638 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002639};
2640
2641/**
2642 * Set the cursor-blink mode bit.
2643 *
2644 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2645 * a visible cursor does not blink.
2646 *
2647 * You should make sure to turn blinking off if you're going to dispose of a
2648 * terminal, otherwise you'll leak a timeout.
2649 *
2650 * Defaults to on.
2651 *
2652 * @param {boolean} state True to set cursor-blink mode, false to unset.
2653 */
2654hterm.Terminal.prototype.setCursorBlink = function(state) {
2655 this.options_.cursorBlink = state;
2656
2657 if (!state && this.timeouts_.cursorBlink) {
2658 clearTimeout(this.timeouts_.cursorBlink);
2659 delete this.timeouts_.cursorBlink;
2660 }
2661
2662 if (this.options_.cursorVisible)
2663 this.setCursorVisible(true);
2664};
2665
2666/**
2667 * Set the cursor-visible mode bit.
2668 *
2669 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2670 *
2671 * Defaults to on.
2672 *
2673 * @param {boolean} state True to set cursor-visible mode, false to unset.
2674 */
2675hterm.Terminal.prototype.setCursorVisible = function(state) {
2676 this.options_.cursorVisible = state;
2677
2678 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002679 if (this.timeouts_.cursorBlink) {
2680 clearTimeout(this.timeouts_.cursorBlink);
2681 delete this.timeouts_.cursorBlink;
2682 }
rginda87b86462011-12-14 13:48:03 -08002683 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002684 return;
2685 }
2686
rginda87b86462011-12-14 13:48:03 -08002687 this.syncCursorPosition_();
2688
2689 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002690
2691 if (this.options_.cursorBlink) {
2692 if (this.timeouts_.cursorBlink)
2693 return;
2694
Robert Gindaea2183e2014-07-17 09:51:51 -07002695 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002696 } else {
2697 if (this.timeouts_.cursorBlink) {
2698 clearTimeout(this.timeouts_.cursorBlink);
2699 delete this.timeouts_.cursorBlink;
2700 }
2701 }
2702};
2703
2704/**
rginda87b86462011-12-14 13:48:03 -08002705 * Synchronizes the visible cursor and document selection with the current
2706 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002707 */
2708hterm.Terminal.prototype.syncCursorPosition_ = function() {
2709 var topRowIndex = this.scrollPort_.getTopRowIndex();
2710 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2711 var cursorRowIndex = this.scrollbackRows_.length +
2712 this.screen_.cursorPosition.row;
2713
2714 if (cursorRowIndex > bottomRowIndex) {
2715 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04002716 this.setCssVar('cursor-offset-row', '-1');
rginda8ba33642011-12-14 12:31:31 -08002717 return;
2718 }
2719
Robert Gindab837c052014-08-11 11:17:51 -07002720 if (this.options_.cursorVisible &&
2721 this.cursorNode_.style.display == 'none') {
2722 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2723 this.cursorNode_.style.display = '';
2724 }
2725
Mike Frysinger44c32202017-08-05 01:13:09 -04002726 // Position the cursor using CSS variable math. If we do the math in JS,
2727 // the float math will end up being more precise than the CSS which will
2728 // cause the cursor tracking to be off.
2729 this.setCssVar(
2730 'cursor-offset-row',
2731 `${cursorRowIndex - topRowIndex} + ` +
2732 `${this.scrollPort_.visibleRowTopMargin}px`);
2733 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08002734
2735 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04002736 '(' + this.screen_.cursorPosition.column +
2737 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08002738 ')');
2739
2740 // Update the caret for a11y purposes.
2741 var selection = this.document_.getSelection();
2742 if (selection && selection.isCollapsed)
2743 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002744};
2745
Robert Gindafb1be6a2013-12-11 11:56:22 -08002746/**
2747 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2748 * and character cell dimensions.
2749 */
Robert Ginda830583c2013-08-07 13:20:46 -07002750hterm.Terminal.prototype.restyleCursor_ = function() {
2751 var shape = this.cursorShape_;
2752
2753 if (this.cursorNode_.getAttribute('focus') == 'false') {
2754 // Always show a block cursor when unfocused.
2755 shape = hterm.Terminal.cursorShape.BLOCK;
2756 }
2757
2758 var style = this.cursorNode_.style;
2759
2760 switch (shape) {
2761 case hterm.Terminal.cursorShape.BEAM:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002762 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002763 style.backgroundColor = 'transparent';
2764 style.borderBottomStyle = null;
2765 style.borderLeftStyle = 'solid';
2766 break;
2767
2768 case hterm.Terminal.cursorShape.UNDERLINE:
2769 style.height = this.scrollPort_.characterSize.baseline + 'px';
2770 style.backgroundColor = 'transparent';
2771 style.borderBottomStyle = 'solid';
2772 // correct the size to put it exactly at the baseline
2773 style.borderLeftStyle = null;
2774 break;
2775
2776 default:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002777 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002778 style.backgroundColor = this.cursorColor_;
2779 style.borderBottomStyle = null;
2780 style.borderLeftStyle = null;
2781 break;
2782 }
2783};
2784
rginda8ba33642011-12-14 12:31:31 -08002785/**
2786 * Synchronizes the visible cursor with the current cursor coordinates.
2787 *
2788 * The sync will happen asynchronously, soon after the call stack winds down.
2789 * Multiple calls will be coalesced into a single sync.
2790 */
2791hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2792 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002793 return;
rginda8ba33642011-12-14 12:31:31 -08002794
2795 var self = this;
2796 this.timeouts_.syncCursor = setTimeout(function() {
2797 self.syncCursorPosition_();
2798 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002799 }, 0);
2800};
2801
rgindacc2996c2012-02-24 14:59:31 -08002802/**
rgindaf522ce02012-04-17 17:49:17 -07002803 * Show or hide the zoom warning.
2804 *
2805 * The zoom warning is a message warning the user that their browser zoom must
2806 * be set to 100% in order for hterm to function properly.
2807 *
2808 * @param {boolean} state True to show the message, false to hide it.
2809 */
2810hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2811 if (!this.zoomWarningNode_) {
2812 if (!state)
2813 return;
2814
2815 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002816 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07002817 this.zoomWarningNode_.style.cssText = (
2818 'color: black;' +
2819 'background-color: #ff2222;' +
2820 'font-size: large;' +
2821 'border-radius: 8px;' +
2822 'opacity: 0.75;' +
2823 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2824 'top: 0.5em;' +
2825 'right: 1.2em;' +
2826 'position: absolute;' +
2827 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002828 '-webkit-user-select: none;' +
2829 '-moz-text-size-adjust: none;' +
2830 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002831
2832 this.zoomWarningNode_.addEventListener('click', function(e) {
2833 this.parentNode.removeChild(this);
2834 });
rgindaf522ce02012-04-17 17:49:17 -07002835 }
2836
Robert Gindab4839c22013-02-28 16:52:10 -08002837 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2838 hterm.zoomWarningMessage,
2839 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2840
rgindaf522ce02012-04-17 17:49:17 -07002841 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2842
2843 if (state) {
2844 if (!this.zoomWarningNode_.parentNode)
2845 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2846 } else if (this.zoomWarningNode_.parentNode) {
2847 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2848 }
2849};
2850
2851/**
rgindacc2996c2012-02-24 14:59:31 -08002852 * Show the terminal overlay for a given amount of time.
2853 *
2854 * The terminal overlay appears in inverse video in a large font, centered
2855 * over the terminal. You should probably keep the overlay message brief,
2856 * since it's in a large font and you probably aren't going to check the size
2857 * of the terminal first.
2858 *
2859 * @param {string} msg The text (not HTML) message to display in the overlay.
2860 * @param {number} opt_timeout The amount of time to wait before fading out
2861 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2862 * stay up forever (or until the next overlay).
2863 */
2864hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002865 if (!this.overlayNode_) {
2866 if (!this.div_)
2867 return;
2868
2869 this.overlayNode_ = this.document_.createElement('div');
2870 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002871 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002872 'font-size: xx-large;' +
2873 'opacity: 0.75;' +
2874 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2875 'position: absolute;' +
2876 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002877 '-webkit-transition: opacity 180ms ease-in;' +
2878 '-moz-user-select: none;' +
2879 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002880
2881 this.overlayNode_.addEventListener('mousedown', function(e) {
2882 e.preventDefault();
2883 e.stopPropagation();
2884 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002885 }
2886
rginda9f5222b2012-03-05 11:53:28 -08002887 this.overlayNode_.style.color = this.prefs_.get('background-color');
2888 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2889 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2890
rgindaf0090c92012-02-10 14:58:52 -08002891 this.overlayNode_.textContent = msg;
2892 this.overlayNode_.style.opacity = '0.75';
2893
2894 if (!this.overlayNode_.parentNode)
2895 this.div_.appendChild(this.overlayNode_);
2896
Robert Ginda97769282013-02-01 15:30:30 -08002897 var divSize = hterm.getClientSize(this.div_);
2898 var overlaySize = hterm.getClientSize(this.overlayNode_);
2899
Robert Ginda8a59f762014-07-23 11:29:55 -07002900 this.overlayNode_.style.top =
2901 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08002902 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07002903 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08002904
rgindaf0090c92012-02-10 14:58:52 -08002905 if (this.overlayTimeout_)
2906 clearTimeout(this.overlayTimeout_);
2907
rgindacc2996c2012-02-24 14:59:31 -08002908 if (opt_timeout === null)
2909 return;
2910
Mike Frysingerb6cfded2017-09-18 00:39:31 -04002911 this.overlayTimeout_ = setTimeout(() => {
2912 this.overlayNode_.style.opacity = '0';
2913 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
2914 }, opt_timeout || 1500);
2915};
2916
2917/**
2918 * Hide the terminal overlay immediately.
2919 *
2920 * Useful when we show an overlay for an event with an unknown end time.
2921 */
2922hterm.Terminal.prototype.hideOverlay = function() {
2923 if (this.overlayTimeout_)
2924 clearTimeout(this.overlayTimeout_);
2925 this.overlayTimeout_ = null;
2926
2927 if (this.overlayNode_.parentNode)
2928 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
2929 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08002930};
2931
rginda4bba5e12012-06-20 16:15:30 -07002932/**
2933 * Paste from the system clipboard to the terminal.
2934 */
2935hterm.Terminal.prototype.paste = function() {
Mike Frysinger4628ad22017-07-20 02:44:20 -04002936 return hterm.pasteFromClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002937};
2938
2939/**
2940 * Copy a string to the system clipboard.
2941 *
2942 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05002943 *
2944 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07002945 */
2946hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002947 if (this.prefs_.get('enable-clipboard-notice'))
2948 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2949
rgindaa09e7332012-08-17 12:49:51 -07002950 var copySource = this.document_.createElement('pre');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002951 copySource.id = 'hterm:copy-to-clipboard-source';
rginda4bba5e12012-06-20 16:15:30 -07002952 copySource.textContent = str;
2953 copySource.style.cssText = (
2954 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002955 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07002956 'position: absolute;' +
2957 'top: -99px');
2958
2959 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002960
rginda4bba5e12012-06-20 16:15:30 -07002961 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002962 var anchorNode = selection.anchorNode;
2963 var anchorOffset = selection.anchorOffset;
2964 var focusNode = selection.focusNode;
2965 var focusOffset = selection.focusOffset;
2966
rginda4bba5e12012-06-20 16:15:30 -07002967 selection.selectAllChildren(copySource);
2968
rgindaa09e7332012-08-17 12:49:51 -07002969 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002970
Rob Spies56953412014-04-28 14:09:47 -07002971 // IE doesn't support selection.extend. This means that the selection
2972 // won't return on IE.
Robert Ginda6f4f0aa2014-07-28 10:21:39 -07002973 if (selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07002974 selection.collapse(anchorNode, anchorOffset);
2975 selection.extend(focusNode, focusOffset);
2976 }
rgindafaa74742012-08-21 13:34:03 -07002977
rginda4bba5e12012-06-20 16:15:30 -07002978 copySource.parentNode.removeChild(copySource);
2979};
2980
Evan Jones2600d4f2016-12-06 09:29:36 -05002981/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04002982 * Display an image.
2983 *
2984 * @param {Object} options The image to display.
2985 * @param {string=} options.name A human readable string for the image.
2986 * @param {string|number=} options.size The size (in bytes).
2987 * @param {boolean=} options.preserveAspectRatio Whether to preserve aspect.
2988 * @param {boolean=} options.inline Whether to display the image inline.
2989 * @param {string|number=} options.width The width of the image.
2990 * @param {string|number=} options.height The height of the image.
2991 * @param {string=} options.align Direction to align the image.
2992 * @param {string} options.uri The source URI for the image.
2993 */
2994hterm.Terminal.prototype.displayImage = function(options) {
2995 // Make sure we're actually given a resource to display.
2996 if (options.uri === undefined)
2997 return;
2998
2999 // Set up the defaults to simplify code below.
3000 if (!options.name)
3001 options.name = '';
3002
3003 // Has the user approved image display yet?
3004 if (this.allowImagesInline !== true) {
3005 this.newLine();
3006 const row = this.getRowNode(this.scrollbackRows_.length +
3007 this.getCursorRow() - 1);
3008
3009 if (this.allowImagesInline === false) {
3010 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3011 'Inline Images Disabled');
3012 return;
3013 }
3014
3015 // Show a prompt.
3016 let button;
3017 const span = this.document_.createElement('span');
3018 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3019 span.style.fontWeight = 'bold';
3020 span.style.borderWidth = '1px';
3021 span.style.borderStyle = 'dashed';
3022 button = this.document_.createElement('span');
3023 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3024 button.style.marginLeft = '1em';
3025 button.style.borderWidth = '1px';
3026 button.style.borderStyle = 'solid';
3027 button.addEventListener('click', () => {
3028 this.prefs_.set('allow-images-inline', false);
3029 });
3030 span.appendChild(button);
3031 button = this.document_.createElement('span');
3032 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3033 'allow this session');
3034 button.style.marginLeft = '1em';
3035 button.style.borderWidth = '1px';
3036 button.style.borderStyle = 'solid';
3037 button.addEventListener('click', () => {
3038 this.allowImagesInline = true;
3039 });
3040 span.appendChild(button);
3041 button = this.document_.createElement('span');
3042 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3043 button.style.marginLeft = '1em';
3044 button.style.borderWidth = '1px';
3045 button.style.borderStyle = 'solid';
3046 button.addEventListener('click', () => {
3047 this.prefs_.set('allow-images-inline', true);
3048 });
3049 span.appendChild(button);
3050
3051 row.appendChild(span);
3052 return;
3053 }
3054
3055 // See if we should show this object directly, or download it.
3056 if (options.inline) {
3057 const io = this.io.push();
3058 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
3059 'Loading $1 ...'), null);
3060
3061 // While we're loading the image, eat all the user's input.
3062 io.onVTKeystroke = io.sendString = () => {};
3063
3064 // Initialize this new image.
3065 const img = this.document_.createElement('img');
3066 img.src = options.uri;
3067 img.title = img.alt = options.name;
3068
3069 // Attach the image to the page to let it load/render. It won't stay here.
3070 // This is needed so it's visible and the DOM can calculate the height. If
3071 // the image is hidden or not in the DOM, the height is always 0.
3072 this.document_.body.appendChild(img);
3073
3074 // Wait for the image to finish loading before we try moving it to the
3075 // right place in the terminal.
3076 img.onload = () => {
3077 // Now that we have the image dimensions, figure out how to show it.
3078 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
3079 img.style.maxWidth = `${this.document_.body.clientWidth}px`;
3080 img.style.maxHeight = `${this.document_.body.clientHeight}px`;
3081
3082 // Parse a width/height specification.
3083 const parseDim = (dim, maxDim, cssVar) => {
3084 if (!dim || dim == 'auto')
3085 return '';
3086
3087 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3088 if (ary) {
3089 if (ary[2] == '%')
3090 return maxDim * parseInt(ary[1]) / 100 + 'px';
3091 else if (ary[2] == 'px')
3092 return dim;
3093 else
3094 return `calc(${dim} * var(${cssVar}))`;
3095 }
3096
3097 return '';
3098 };
3099 img.style.width =
3100 parseDim(options.width, this.document_.body.clientWidth,
3101 '--hterm-charsize-width');
3102 img.style.height =
3103 parseDim(options.height, this.document_.body.clientHeight,
3104 '--hterm-charsize-height');
3105
3106 // Figure out how many rows the image occupies, then add that many.
3107 // XXX: This count will be inaccurate if the font size changes on us.
3108 const padRows = Math.ceil(img.clientHeight /
3109 this.scrollPort_.characterSize.height);
3110 for (let i = 0; i < padRows; ++i)
3111 this.newLine();
3112
3113 // Update the max height in case the user shrinks the character size.
3114 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3115
3116 // Move the image to the last row. This way when we scroll up, it doesn't
3117 // disappear when the first row gets clipped. It will disappear when we
3118 // scroll down and the last row is clipped ...
3119 this.document_.body.removeChild(img);
3120 // Create a wrapper node so we can do an absolute in a relative position.
3121 // This helps with rounding errors between JS & CSS counts.
3122 const div = this.document_.createElement('div');
3123 div.style.position = 'relative';
3124 div.style.textAlign = options.align;
3125 img.style.position = 'absolute';
3126 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3127 div.appendChild(img);
3128 const row = this.getRowNode(this.scrollbackRows_.length +
3129 this.getCursorRow() - 1);
3130 row.appendChild(div);
3131
3132 io.hideOverlay();
3133 io.pop();
3134 };
3135
3136 // If we got a malformed image, give up.
3137 img.onerror = (e) => {
3138 this.document_.body.removeChild(img);
3139 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
3140 'Loading $1 failed ...'));
3141 io.pop();
3142 };
3143 } else {
3144 // We can't use chrome.downloads.download as that requires "downloads"
3145 // permissions, and that works only in extensions, not apps.
3146 const a = this.document_.createElement('a');
3147 a.href = options.uri;
3148 a.download = options.name;
3149 this.document_.body.appendChild(a);
3150 a.click();
3151 a.remove();
3152 }
3153};
3154
3155/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003156 * Returns the selected text, or null if no text is selected.
3157 *
3158 * @return {string|null}
3159 */
rgindaa09e7332012-08-17 12:49:51 -07003160hterm.Terminal.prototype.getSelectionText = function() {
3161 var selection = this.scrollPort_.selection;
3162 selection.sync();
3163
3164 if (selection.isCollapsed)
3165 return null;
3166
3167
3168 // Start offset measures from the beginning of the line.
3169 var startOffset = selection.startOffset;
3170 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003171
Robert Gindafdbb3f22012-09-06 20:23:06 -07003172 if (node.nodeName != 'X-ROW') {
3173 // If the selection doesn't start on an x-row node, then it must be
3174 // somewhere inside the x-row. Add any characters from previous siblings
3175 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003176
3177 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3178 // If node is the text node in a styled span, move up to the span node.
3179 node = node.parentNode;
3180 }
3181
Robert Gindafdbb3f22012-09-06 20:23:06 -07003182 while (node.previousSibling) {
3183 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003184 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003185 }
rgindaa09e7332012-08-17 12:49:51 -07003186 }
3187
3188 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08003189 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
3190 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003191 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003192
Robert Gindafdbb3f22012-09-06 20:23:06 -07003193 if (node.nodeName != 'X-ROW') {
3194 // If the selection doesn't end on an x-row node, then it must be
3195 // somewhere inside the x-row. Add any characters from following siblings
3196 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003197
3198 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3199 // If node is the text node in a styled span, move up to the span node.
3200 node = node.parentNode;
3201 }
3202
Robert Gindafdbb3f22012-09-06 20:23:06 -07003203 while (node.nextSibling) {
3204 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003205 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003206 }
rgindaa09e7332012-08-17 12:49:51 -07003207 }
3208
3209 var rv = this.getRowsText(selection.startRow.rowIndex,
3210 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003211 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003212};
3213
rginda4bba5e12012-06-20 16:15:30 -07003214/**
3215 * Copy the current selection to the system clipboard, then clear it after a
3216 * short delay.
3217 */
3218hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07003219 var text = this.getSelectionText();
3220 if (text != null)
3221 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07003222};
3223
rgindaf0090c92012-02-10 14:58:52 -08003224hterm.Terminal.prototype.overlaySize = function() {
3225 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
3226};
3227
rginda87b86462011-12-14 13:48:03 -08003228/**
3229 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3230 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003231 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003232 */
3233hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08003234 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08003235 this.scrollPort_.scrollRowToBottom(this.getRowCount());
3236
Robert Ginda8cb7d902013-06-20 14:37:18 -07003237 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08003238};
3239
3240/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003241 * Open the selected url.
3242 */
3243hterm.Terminal.prototype.openSelectedUrl_ = function() {
3244 var str = this.getSelectionText();
3245
3246 // If there is no selection, try and expand wherever they clicked.
3247 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003248 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003249 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003250
3251 // If clicking in empty space, return.
3252 if (str == null)
3253 return;
Mike Frysinger70b94692017-01-26 18:57:50 -10003254 }
3255
3256 // Make sure URL is valid before opening.
3257 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
3258 return;
Mike Frysinger43472622017-06-26 18:11:07 -04003259
3260 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003261 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003262 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3263 // We have to whitelist a few protocols that lack authorities and thus
3264 // never use the //. Like mailto.
3265 switch (str.split(':', 1)[0]) {
3266 case 'mailto':
3267 break;
3268 default:
3269 str = 'http://' + str;
3270 break;
3271 }
3272 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003273
Mike Frysinger720fa832017-10-23 01:15:52 -04003274 hterm.openUrl(str);
Mike Frysinger70b94692017-01-26 18:57:50 -10003275}
3276
3277
3278/**
rgindad5613292012-06-19 15:40:37 -07003279 * Add the terminalRow and terminalColumn properties to mouse events and
3280 * then forward on to onMouse().
3281 *
3282 * The terminalRow and terminalColumn properties contain the (row, column)
3283 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003284 *
3285 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003286 */
3287hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003288 if (e.processedByTerminalHandler_) {
3289 // We register our event handlers on the document, as well as the cursor
3290 // and the scroll blocker. Mouse events that occur on the cursor or
3291 // scroll blocker will also appear on the document, but we don't want to
3292 // process them twice.
3293 //
3294 // We can't just prevent bubbling because that has other side effects, so
3295 // we decorate the event object with this property instead.
3296 return;
3297 }
3298
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003299 var reportMouseEvents = (!this.defeatMouseReports_ &&
3300 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3301
rgindafaa74742012-08-21 13:34:03 -07003302 e.processedByTerminalHandler_ = true;
3303
Robert Gindaeda48db2014-07-17 09:25:30 -07003304 // One based row/column stored on the mouse event.
3305 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
3306 this.scrollPort_.characterSize.height) + 1;
3307 e.terminalColumn = parseInt(e.clientX /
3308 this.scrollPort_.characterSize.width) + 1;
3309
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003310 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3311 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003312 return;
3313 }
3314
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003315 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003316 // If the cursor is visible and we're not sending mouse events to the
3317 // host app, then we want to hide the terminal cursor when the mouse
3318 // cursor is over top. This keeps the terminal cursor from interfering
3319 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003320 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3321 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3322 this.cursorNode_.style.display = 'none';
3323 } else if (this.cursorNode_.style.display == 'none') {
3324 this.cursorNode_.style.display = '';
3325 }
3326 }
rgindad5613292012-06-19 15:40:37 -07003327
Robert Ginda928cf632014-03-05 15:07:41 -08003328 if (e.type == 'mousedown') {
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003329 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003330 // If VT mouse reporting is disabled, or has been defeated with
3331 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003332 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003333 this.setSelectionEnabled(true);
3334 } else {
3335 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003336 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003337 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003338 this.setSelectionEnabled(false);
3339 e.preventDefault();
3340 }
3341 }
3342
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003343 if (!reportMouseEvents) {
John Lin2aad22e2018-03-16 13:58:11 +08003344 if (e.type == 'dblclick') {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003345 this.screen_.expandSelection(this.document_.getSelection());
John Lin2aad22e2018-03-16 13:58:11 +08003346 if (this.copyOnSelect)
3347 this.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07003348 }
3349
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003350 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003351 // Debounce this event with the dblclick event. If you try to doubleclick
3352 // a URL to open it, Chrome will fire click then dblclick, but we won't
3353 // have expanded the selection text at the first click event.
3354 clearTimeout(this.timeouts_.openUrl);
3355 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3356 500);
3357 return;
3358 }
3359
Mike Frysinger847577f2017-05-23 23:25:57 -04003360 if (e.type == 'mousedown') {
3361 if ((this.mouseRightClickPaste && e.button == 2 /* right button */) ||
Mike Frysinger2edd3612017-05-24 00:54:39 -04003362 e.button == this.mousePasteButton) {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003363 if (!this.paste())
Mike Frysinger05a57f02017-08-27 17:48:55 -04003364 console.warn('Could not paste manually due to web restrictions');
Mike Frysinger847577f2017-05-23 23:25:57 -04003365 }
3366 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003367
Mike Frysinger2edd3612017-05-24 00:54:39 -04003368 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003369 !this.document_.getSelection().isCollapsed) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003370 this.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003371 }
3372
3373 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3374 this.scrollBlockerNode_.engaged) {
3375 // Disengage the scroll-blocker after one of these events.
3376 this.scrollBlockerNode_.engaged = false;
3377 this.scrollBlockerNode_.style.top = '-99px';
3378 }
3379
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003380 // Emulate arrow key presses via scroll wheel events.
3381 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3382 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003383 if (e.type == 'wheel') {
3384 var delta = this.scrollPort_.scrollWheelDelta(e);
3385 var lines = lib.f.smartFloorDivide(
3386 Math.abs(delta), this.scrollPort_.characterSize.height);
3387
3388 var data = '\x1bO' + (delta < 0 ? 'B' : 'A');
3389 this.io.sendString(data.repeat(lines));
3390
3391 e.preventDefault();
3392 }
3393 }
Robert Ginda928cf632014-03-05 15:07:41 -08003394 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003395 if (!this.scrollBlockerNode_.engaged) {
3396 if (e.type == 'mousedown') {
3397 // Move the scroll-blocker into place if we want to keep the scrollport
3398 // from scrolling.
3399 this.scrollBlockerNode_.engaged = true;
3400 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3401 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3402 } else if (e.type == 'mousemove') {
3403 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3404 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003405 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003406 e.preventDefault();
3407 }
3408 }
Robert Ginda928cf632014-03-05 15:07:41 -08003409
3410 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003411 }
3412
Robert Ginda928cf632014-03-05 15:07:41 -08003413 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3414 // Restore this on mouseup in case it was temporarily defeated with a
3415 // alt-mousedown. Only do this when the selection is empty so that
3416 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003417 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003418 }
rgindad5613292012-06-19 15:40:37 -07003419};
3420
3421/**
3422 * Clients should override this if they care to know about mouse events.
3423 *
3424 * The event parameter will be a normal DOM mouse click event with additional
3425 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003426 *
3427 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003428 */
3429hterm.Terminal.prototype.onMouse = function(e) { };
3430
3431/**
rginda8e92a692012-05-20 19:37:20 -07003432 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003433 *
3434 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003435 */
Rob Spies06533ba2014-04-24 11:20:37 -07003436hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3437 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003438 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003439
3440 if (this.reportFocus) {
3441 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O')
3442 }
3443
Michael Kelly485ecd12014-06-09 11:41:56 -04003444 if (focused === true)
3445 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003446};
3447
3448/**
rginda8ba33642011-12-14 12:31:31 -08003449 * React when the ScrollPort is scrolled.
3450 */
3451hterm.Terminal.prototype.onScroll_ = function() {
3452 this.scheduleSyncCursorPosition_();
3453};
3454
3455/**
rginda9846e2f2012-01-27 13:53:33 -08003456 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003457 *
3458 * @param {Event} e The DOM paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003459 */
3460hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07003461 var data = e.text.replace(/\n/mg, '\r');
Connor Hegartyf525ceb2014-09-03 13:36:03 -07003462 data = this.keyboard.encode(data);
Mike Frysingere8c32c82018-03-11 14:57:28 -07003463 if (this.options_.bracketedPaste) {
3464 // We strip out most escape sequences as they can cause issues (like
3465 // inserting an \x1b[201~ midstream). We pass through whitespace
3466 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
3467 // This matches xterm behavior.
3468 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
3469 data = '\x1b[200~' + filter(data) + '\x1b[201~';
3470 }
Robert Gindaa063b202014-07-21 11:08:25 -07003471
3472 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003473};
3474
3475/**
rgindaa09e7332012-08-17 12:49:51 -07003476 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003477 *
3478 * @param {Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003479 */
3480hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003481 if (!this.useDefaultWindowCopy) {
3482 e.preventDefault();
3483 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3484 }
rgindaa09e7332012-08-17 12:49:51 -07003485};
3486
3487/**
rginda8ba33642011-12-14 12:31:31 -08003488 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003489 *
3490 * Note: This function should not directly contain code that alters the internal
3491 * state of the terminal. That kind of code belongs in realizeWidth or
3492 * realizeHeight, so that it can be executed synchronously in the case of a
3493 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003494 */
3495hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003496 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003497 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003498 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003499 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003500
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003501 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003502 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003503 // gets removed from the document or during the initial load, and we can't
3504 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003505 // This can also happen if called before the scrollPort calculates the
3506 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003507 return;
3508 }
3509
rgindaa8ba17d2012-08-15 14:41:10 -07003510 var isNewSize = (columnCount != this.screenSize.width ||
3511 rowCount != this.screenSize.height);
3512
3513 // We do this even if the size didn't change, just to be sure everything is
3514 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003515 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003516 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003517
3518 if (isNewSize)
3519 this.overlaySize();
3520
Robert Gindafb1be6a2013-12-11 11:56:22 -08003521 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003522 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08003523};
3524
3525/**
3526 * Service the cursor blink timeout.
3527 */
3528hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003529 if (!this.options_.cursorBlink) {
3530 delete this.timeouts_.cursorBlink;
3531 return;
3532 }
3533
Robert Ginda830583c2013-08-07 13:20:46 -07003534 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3535 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003536 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003537 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3538 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003539 } else {
rginda87b86462011-12-14 13:48:03 -08003540 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003541 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3542 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003543 }
3544};
David Reveman8f552492012-03-28 12:18:41 -04003545
3546/**
3547 * Set the scrollbar-visible mode bit.
3548 *
3549 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3550 * Otherwise it will not.
3551 *
3552 * Defaults to on.
3553 *
3554 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3555 */
3556hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3557 this.scrollPort_.setScrollbarVisible(state);
3558};
Michael Kelly485ecd12014-06-09 11:41:56 -04003559
3560/**
Rob Spies49039e52014-12-17 13:40:04 -08003561 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003562 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003563 *
3564 * Defaults to 1.
3565 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003566 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003567 */
3568hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3569 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3570};
3571
3572/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003573 * Close all web notifications created by terminal bells.
3574 */
3575hterm.Terminal.prototype.closeBellNotifications_ = function() {
3576 this.bellNotificationList_.forEach(function(n) {
3577 n.close();
3578 });
3579 this.bellNotificationList_.length = 0;
3580};