blob: 921a4435ff9b2b2991112e47e52e48f2a2d040d5 [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) {
393 terminal.syncBlinkState();
394 },
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) {
409 terminal.setFontSize(v);
410 },
rginda9875d902012-08-20 16:21:57 -0700411
Robert Ginda57f03b42012-09-13 11:02:48 -0700412 'font-smoothing': function(v) {
413 terminal.syncFontFamily();
414 },
rgindade84e382012-04-20 15:39:31 -0700415
Robert Ginda57f03b42012-09-13 11:02:48 -0700416 'foreground-color': function(v) {
417 terminal.setForegroundColor(v);
418 },
rginda30f20f62012-04-05 16:36:19 -0700419
Robert Ginda57f03b42012-09-13 11:02:48 -0700420 'home-keys-scroll': function(v) {
421 terminal.keyboard.homeKeysScroll = v;
422 },
rginda4bba5e12012-06-20 16:15:30 -0700423
Robert Gindaa8165692015-06-15 14:46:31 -0700424 'keybindings': function(v) {
425 terminal.keyboard.bindings.clear();
426
427 if (!v)
428 return;
429
430 if (!(v instanceof Object)) {
431 console.error('Error in keybindings preference: Expected object');
432 return;
433 }
434
435 try {
436 terminal.keyboard.bindings.addBindings(v);
437 } catch (ex) {
438 console.error('Error in keybindings preference: ' + ex);
439 }
440 },
441
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700442 'media-keys-are-fkeys': function(v) {
443 terminal.keyboard.mediaKeysAreFKeys = v;
444 },
445
Robert Ginda57f03b42012-09-13 11:02:48 -0700446 'meta-sends-escape': function(v) {
447 terminal.keyboard.metaSendsEscape = v;
448 },
rginda30f20f62012-04-05 16:36:19 -0700449
Mike Frysinger847577f2017-05-23 23:25:57 -0400450 'mouse-right-click-paste': function(v) {
451 terminal.mouseRightClickPaste = v;
452 },
453
Robert Ginda57f03b42012-09-13 11:02:48 -0700454 'mouse-paste-button': function(v) {
455 terminal.syncMousePasteButton();
456 },
rgindaa8ba17d2012-08-15 14:41:10 -0700457
Robert Gindae76aa9f2014-03-14 12:29:12 -0700458 'page-keys-scroll': function(v) {
459 terminal.keyboard.pageKeysScroll = v;
460 },
461
Robert Ginda40932892012-12-10 17:26:40 -0800462 'pass-alt-number': function(v) {
463 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800464 // Let Alt-1..9 pass to the browser (to control tab switching) on
465 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500466 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800467 }
468
469 terminal.passAltNumber = v;
470 },
471
472 'pass-ctrl-number': function(v) {
473 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800474 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
475 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500476 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800477 }
478
479 terminal.passCtrlNumber = v;
480 },
481
482 'pass-meta-number': function(v) {
483 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800484 // Let Meta-1..9 pass to the browser (to control tab switching) on
485 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500486 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800487 }
488
489 terminal.passMetaNumber = v;
490 },
491
Marius Schilder77857b32014-05-14 16:21:26 -0700492 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700493 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700494 },
495
Robert Ginda8cb7d902013-06-20 14:37:18 -0700496 'receive-encoding': function(v) {
497 if (!(/^(utf-8|raw)$/).test(v)) {
498 console.warn('Invalid value for "receive-encoding": ' + v);
499 v = 'utf-8';
500 }
501
502 terminal.vt.characterEncoding = v;
503 },
504
Robert Ginda57f03b42012-09-13 11:02:48 -0700505 'scroll-on-keystroke': function(v) {
506 terminal.scrollOnKeystroke_ = v;
507 },
rginda9f5222b2012-03-05 11:53:28 -0800508
Robert Ginda57f03b42012-09-13 11:02:48 -0700509 'scroll-on-output': function(v) {
510 terminal.scrollOnOutput_ = v;
511 },
rginda30f20f62012-04-05 16:36:19 -0700512
Robert Ginda57f03b42012-09-13 11:02:48 -0700513 'scrollbar-visible': function(v) {
514 terminal.setScrollbarVisible(v);
515 },
rginda9f5222b2012-03-05 11:53:28 -0800516
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400517 'scroll-wheel-may-send-arrow-keys': function(v) {
518 terminal.scrollWheelArrowKeys_ = v;
519 },
520
Rob Spies49039e52014-12-17 13:40:04 -0800521 'scroll-wheel-move-multiplier': function(v) {
522 terminal.setScrollWheelMoveMultipler(v);
523 },
524
Robert Ginda8cb7d902013-06-20 14:37:18 -0700525 'send-encoding': function(v) {
526 if (!(/^(utf-8|raw)$/).test(v)) {
527 console.warn('Invalid value for "send-encoding": ' + v);
528 v = 'utf-8';
529 }
530
531 terminal.keyboard.characterEncoding = v;
532 },
533
Robert Ginda57f03b42012-09-13 11:02:48 -0700534 'shift-insert-paste': function(v) {
535 terminal.keyboard.shiftInsertPaste = v;
536 },
rginda9f5222b2012-03-05 11:53:28 -0800537
Mike Frysingera7768922017-07-28 15:00:12 -0400538 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400539 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400540 },
541
Robert Gindae76aa9f2014-03-14 12:29:12 -0700542 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400543 terminal.scrollPort_.setUserCssUrl(v);
544 },
545
546 'user-css-text': function(v) {
547 terminal.scrollPort_.setUserCssText(v);
548 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400549
550 'word-break-match-left': function(v) {
551 terminal.primaryScreen_.wordBreakMatchLeft = v;
552 terminal.alternateScreen_.wordBreakMatchLeft = v;
553 },
554
555 'word-break-match-right': function(v) {
556 terminal.primaryScreen_.wordBreakMatchRight = v;
557 terminal.alternateScreen_.wordBreakMatchRight = v;
558 },
559
560 'word-break-match-middle': function(v) {
561 terminal.primaryScreen_.wordBreakMatchMiddle = v;
562 terminal.alternateScreen_.wordBreakMatchMiddle = v;
563 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400564
565 'allow-images-inline': function(v) {
566 terminal.allowImagesInline = v;
567 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700568 });
rginda30f20f62012-04-05 16:36:19 -0700569
Robert Ginda57f03b42012-09-13 11:02:48 -0700570 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800571 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700572
573 if (opt_callback)
574 opt_callback();
575 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800576};
577
Rob Spies56953412014-04-28 14:09:47 -0700578
579/**
580 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500581 *
582 * @return {hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700583 */
584hterm.Terminal.prototype.getPrefs = function() {
585 return this.prefs_;
586};
587
Robert Gindaa063b202014-07-21 11:08:25 -0700588/**
589 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500590 *
591 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700592 */
593hterm.Terminal.prototype.setBracketedPaste = function(state) {
594 this.options_.bracketedPaste = state;
595};
Rob Spies56953412014-04-28 14:09:47 -0700596
rginda8e92a692012-05-20 19:37:20 -0700597/**
598 * Set the color for the cursor.
599 *
600 * If you want this setting to persist, set it through prefs_, rather than
601 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500602 *
603 * @param {string} color The color to set.
rginda8e92a692012-05-20 19:37:20 -0700604 */
605hterm.Terminal.prototype.setCursorColor = function(color) {
Robert Ginda830583c2013-08-07 13:20:46 -0700606 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700607 this.cursorNode_.style.backgroundColor = color;
608 this.cursorNode_.style.borderColor = color;
609};
610
611/**
612 * Return the current cursor color as a string.
Evan Jones2600d4f2016-12-06 09:29:36 -0500613 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700614 */
615hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700616 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700617};
618
619/**
rgindad5613292012-06-19 15:40:37 -0700620 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500621 *
622 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700623 */
624hterm.Terminal.prototype.setSelectionEnabled = function(state) {
625 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700626};
627
628/**
rginda8e92a692012-05-20 19:37:20 -0700629 * Set the background color.
630 *
631 * If you want this setting to persist, set it through prefs_, rather than
632 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500633 *
634 * @param {string} color The color to set.
rginda8e92a692012-05-20 19:37:20 -0700635 */
636hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700637 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700638 this.primaryScreen_.textAttributes.setDefaults(
639 this.foregroundColor_, this.backgroundColor_);
640 this.alternateScreen_.textAttributes.setDefaults(
641 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700642 this.scrollPort_.setBackgroundColor(color);
643};
644
rginda9f5222b2012-03-05 11:53:28 -0800645/**
646 * Return the current terminal background color.
647 *
648 * Intended for use by other classes, so we don't have to expose the entire
649 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500650 *
651 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800652 */
653hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700654 return this.backgroundColor_;
655};
656
657/**
658 * Set the foreground color.
659 *
660 * If you want this setting to persist, set it through prefs_, rather than
661 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500662 *
663 * @param {string} color The color to set.
rginda8e92a692012-05-20 19:37:20 -0700664 */
665hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700666 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700667 this.primaryScreen_.textAttributes.setDefaults(
668 this.foregroundColor_, this.backgroundColor_);
669 this.alternateScreen_.textAttributes.setDefaults(
670 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700671 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800672};
673
674/**
675 * Return the current terminal foreground color.
676 *
677 * Intended for use by other classes, so we don't have to expose the entire
678 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500679 *
680 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800681 */
682hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700683 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800684};
685
686/**
rginda87b86462011-12-14 13:48:03 -0800687 * Create a new instance of a terminal command and run it with a given
688 * argument string.
689 *
690 * @param {function} commandClass The constructor for a terminal command.
691 * @param {string} argString The argument string to pass to the command.
692 */
693hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700694 var environment = this.prefs_.get('environment');
695 if (typeof environment != 'object' || environment == null)
696 environment = {};
697
rginda87b86462011-12-14 13:48:03 -0800698 var self = this;
699 this.command = new commandClass(
700 { argString: argString || '',
701 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700702 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800703 onExit: function(code) {
704 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800705 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700706 if (self.prefs_.get('close-on-exit'))
707 window.close();
rginda87b86462011-12-14 13:48:03 -0800708 }
709 });
710
rgindafeaf3142012-01-31 15:14:20 -0800711 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800712 this.command.run();
713};
714
715/**
rgindafeaf3142012-01-31 15:14:20 -0800716 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500717 *
718 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800719 */
720hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700721 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800722};
723
724/**
725 * Install the keyboard handler for this terminal.
726 *
727 * This will prevent the browser from seeing any keystrokes sent to the
728 * terminal.
729 */
730hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700731 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
rgindafeaf3142012-01-31 15:14:20 -0800732}
733
734/**
735 * Uninstall the keyboard handler for this terminal.
736 */
737hterm.Terminal.prototype.uninstallKeyboard = function() {
738 this.keyboard.installKeyboard(null);
739}
740
741/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400742 * Set a CSS variable.
743 *
744 * Normally this is used to set variables in the hterm namespace.
745 *
746 * @param {string} name The variable to set.
747 * @param {string} value The value to assign to the variable.
748 * @param {string?} opt_prefix The variable namespace/prefix to use.
749 */
750hterm.Terminal.prototype.setCssVar = function(name, value,
751 opt_prefix='--hterm-') {
752 this.document_.documentElement.style.setProperty(
753 `${opt_prefix}${name}`, value);
754};
755
756/**
rginda35c456b2012-02-09 17:29:05 -0800757 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800758 *
759 * Call setFontSize(0) to reset to the default font size.
760 *
761 * This function does not modify the font-size preference.
762 *
763 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800764 */
765hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800766 if (px === 0)
767 px = this.prefs_.get('font-size');
768
rginda35c456b2012-02-09 17:29:05 -0800769 this.scrollPort_.setFontSize(px);
Mike Frysingercce97c42017-08-05 01:11:22 -0400770 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
771 this.setCssVar('charsize-height',
772 this.scrollPort_.characterSize.height + 'px');
rginda35c456b2012-02-09 17:29:05 -0800773};
774
775/**
776 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500777 *
778 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800779 */
780hterm.Terminal.prototype.getFontSize = function() {
781 return this.scrollPort_.getFontSize();
782};
783
784/**
rginda8e92a692012-05-20 19:37:20 -0700785 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500786 *
787 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700788 */
789hterm.Terminal.prototype.getFontFamily = function() {
790 return this.scrollPort_.getFontFamily();
791};
792
793/**
rginda35c456b2012-02-09 17:29:05 -0800794 * Set the CSS "font-family" for this terminal.
795 */
rginda9f5222b2012-03-05 11:53:28 -0800796hterm.Terminal.prototype.syncFontFamily = function() {
797 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
798 this.prefs_.get('font-smoothing'));
799 this.syncBoldSafeState();
800};
801
rginda4bba5e12012-06-20 16:15:30 -0700802/**
803 * Set this.mousePasteButton based on the mouse-paste-button pref,
804 * autodetecting if necessary.
805 */
806hterm.Terminal.prototype.syncMousePasteButton = function() {
807 var button = this.prefs_.get('mouse-paste-button');
808 if (typeof button == 'number') {
809 this.mousePasteButton = button;
810 return;
811 }
812
Mike Frysingeree81a002017-12-12 16:14:53 -0500813 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400814 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700815 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400816 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700817 }
818};
819
820/**
821 * Enable or disable bold based on the enable-bold pref, autodetecting if
822 * necessary.
823 */
rginda9f5222b2012-03-05 11:53:28 -0800824hterm.Terminal.prototype.syncBoldSafeState = function() {
825 var enableBold = this.prefs_.get('enable-bold');
826 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700827 this.primaryScreen_.textAttributes.enableBold = enableBold;
828 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800829 return;
830 }
831
rgindaf7521392012-02-28 17:20:34 -0800832 var normalSize = this.scrollPort_.measureCharacterSize();
833 var boldSize = this.scrollPort_.measureCharacterSize('bold');
834
835 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800836 if (!isBoldSafe) {
837 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700838 'from normal. Font family is: ' +
839 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800840 }
rginda9f5222b2012-03-05 11:53:28 -0800841
Robert Gindaed016262012-10-26 16:27:09 -0700842 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
843 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800844};
845
846/**
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400847 * Enable or disable blink based on the enable-blink pref.
848 */
849hterm.Terminal.prototype.syncBlinkState = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400850 this.setCssVar('node-duration',
851 this.prefs_.get('enable-blink') ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400852};
853
854/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400855 * Set the mouse cursor style based on the current terminal mode.
856 */
857hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400858 this.setCssVar('mouse-cursor-style',
859 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
860 'var(--hterm-mouse-cursor-text)' :
861 'var(--hterm-mouse-cursor-pointer)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400862};
863
864/**
rginda87b86462011-12-14 13:48:03 -0800865 * Return a copy of the current cursor position.
866 *
867 * @return {hterm.RowCol} The RowCol object representing the current position.
868 */
869hterm.Terminal.prototype.saveCursor = function() {
870 return this.screen_.cursorPosition.clone();
871};
872
Evan Jones2600d4f2016-12-06 09:29:36 -0500873/**
874 * Return the current text attributes.
875 *
876 * @return {string}
877 */
rgindaa19afe22012-01-25 15:40:22 -0800878hterm.Terminal.prototype.getTextAttributes = function() {
879 return this.screen_.textAttributes;
880};
881
Evan Jones2600d4f2016-12-06 09:29:36 -0500882/**
883 * Set the text attributes.
884 *
885 * @param {string} textAttributes The attributes to set.
886 */
rginda1a09aa02012-06-18 21:11:25 -0700887hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
888 this.screen_.textAttributes = textAttributes;
889};
890
rginda87b86462011-12-14 13:48:03 -0800891/**
rgindaf522ce02012-04-17 17:49:17 -0700892 * Return the current browser zoom factor applied to the terminal.
893 *
894 * @return {number} The current browser zoom factor.
895 */
896hterm.Terminal.prototype.getZoomFactor = function() {
897 return this.scrollPort_.characterSize.zoomFactor;
898};
899
900/**
rginda9846e2f2012-01-27 13:53:33 -0800901 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500902 *
903 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800904 */
905hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800906 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800907};
908
909/**
rginda87b86462011-12-14 13:48:03 -0800910 * Restore a previously saved cursor position.
911 *
912 * @param {hterm.RowCol} cursor The position to restore.
913 */
914hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700915 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
916 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800917 this.screen_.setCursorPosition(row, column);
918 if (cursor.column > column ||
919 cursor.column == column && cursor.overflow) {
920 this.screen_.cursorPosition.overflow = true;
921 }
rginda87b86462011-12-14 13:48:03 -0800922};
923
924/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400925 * Clear the cursor's overflow flag.
926 */
927hterm.Terminal.prototype.clearCursorOverflow = function() {
928 this.screen_.cursorPosition.overflow = false;
929};
930
931/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800932 * Save the current cursor state to the corresponding screens.
933 *
934 * See the hterm.Screen.CursorState class for more details.
935 *
936 * @param {boolean=} both If true, update both screens, else only update the
937 * current screen.
938 */
939hterm.Terminal.prototype.saveCursorAndState = function(both) {
940 if (both) {
941 this.primaryScreen_.saveCursorAndState(this.vt);
942 this.alternateScreen_.saveCursorAndState(this.vt);
943 } else
944 this.screen_.saveCursorAndState(this.vt);
945};
946
947/**
948 * Restore the saved cursor state in the corresponding screens.
949 *
950 * See the hterm.Screen.CursorState class for more details.
951 *
952 * @param {boolean=} both If true, update both screens, else only update the
953 * current screen.
954 */
955hterm.Terminal.prototype.restoreCursorAndState = function(both) {
956 if (both) {
957 this.primaryScreen_.restoreCursorAndState(this.vt);
958 this.alternateScreen_.restoreCursorAndState(this.vt);
959 } else
960 this.screen_.restoreCursorAndState(this.vt);
961};
962
963/**
Robert Ginda830583c2013-08-07 13:20:46 -0700964 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -0500965 *
966 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -0700967 */
968hterm.Terminal.prototype.setCursorShape = function(shape) {
969 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -0800970 this.restyleCursor_();
Robert Ginda830583c2013-08-07 13:20:46 -0700971}
972
973/**
974 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -0500975 *
976 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -0700977 */
978hterm.Terminal.prototype.getCursorShape = function() {
979 return this.cursorShape_;
980}
981
982/**
rginda87b86462011-12-14 13:48:03 -0800983 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -0500984 *
985 * @param {number} columnCount
rginda87b86462011-12-14 13:48:03 -0800986 */
987hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800988 if (columnCount == null) {
989 this.div_.style.width = '100%';
990 return;
991 }
992
Robert Ginda26806d12014-07-24 13:44:07 -0700993 this.div_.style.width = Math.ceil(
994 this.scrollPort_.characterSize.width *
995 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400996 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800997 this.scheduleSyncCursorPosition_();
998};
rginda87b86462011-12-14 13:48:03 -0800999
rgindac9bc5502012-01-18 11:48:44 -08001000/**
rginda35c456b2012-02-09 17:29:05 -08001001 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001002 *
1003 * @param {number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001004 */
1005hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001006 if (rowCount == null) {
1007 this.div_.style.height = '100%';
1008 return;
1009 }
1010
rginda35c456b2012-02-09 17:29:05 -08001011 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -07001012 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -08001013 this.realizeSize_(this.screenSize.width, rowCount);
1014 this.scheduleSyncCursorPosition_();
1015};
1016
1017/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001018 * Deal with terminal size changes.
1019 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001020 * @param {number} columnCount The number of columns.
1021 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001022 */
1023hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
1024 if (columnCount != this.screenSize.width)
1025 this.realizeWidth_(columnCount);
1026
1027 if (rowCount != this.screenSize.height)
1028 this.realizeHeight_(rowCount);
1029
1030 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -07001031 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001032};
1033
1034/**
rgindac9bc5502012-01-18 11:48:44 -08001035 * Deal with terminal width changes.
1036 *
1037 * This function does what needs to be done when the terminal width changes
1038 * out from under us. It happens here rather than in onResize_() because this
1039 * code may need to run synchronously to handle programmatic changes of
1040 * terminal width.
1041 *
1042 * Relying on the browser to send us an async resize event means we may not be
1043 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001044 *
1045 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001046 */
1047hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001048 if (columnCount <= 0)
1049 throw new Error('Attempt to realize bad width: ' + columnCount);
1050
rgindac9bc5502012-01-18 11:48:44 -08001051 var deltaColumns = columnCount - this.screen_.getWidth();
1052
rginda87b86462011-12-14 13:48:03 -08001053 this.screenSize.width = columnCount;
1054 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001055
1056 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -04001057 if (this.defaultTabStops)
1058 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -08001059 } else {
1060 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -04001061 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -08001062 break;
1063
1064 this.tabStops_.pop();
1065 }
1066 }
1067
1068 this.screen_.setColumnCount(this.screenSize.width);
1069};
1070
1071/**
1072 * Deal with terminal height changes.
1073 *
1074 * This function does what needs to be done when the terminal height changes
1075 * out from under us. It happens here rather than in onResize_() because this
1076 * code may need to run synchronously to handle programmatic changes of
1077 * terminal height.
1078 *
1079 * Relying on the browser to send us an async resize event means we may not be
1080 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001081 *
1082 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001083 */
1084hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001085 if (rowCount <= 0)
1086 throw new Error('Attempt to realize bad height: ' + rowCount);
1087
rgindac9bc5502012-01-18 11:48:44 -08001088 var deltaRows = rowCount - this.screen_.getHeight();
1089
1090 this.screenSize.height = rowCount;
1091
1092 var cursor = this.saveCursor();
1093
1094 if (deltaRows < 0) {
1095 // Screen got smaller.
1096 deltaRows *= -1;
1097 while (deltaRows) {
1098 var lastRow = this.getRowCount() - 1;
1099 if (lastRow - this.scrollbackRows_.length == cursor.row)
1100 break;
1101
1102 if (this.getRowText(lastRow))
1103 break;
1104
1105 this.screen_.popRow();
1106 deltaRows--;
1107 }
1108
1109 var ary = this.screen_.shiftRows(deltaRows);
1110 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1111
1112 // We just removed rows from the top of the screen, we need to update
1113 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001114 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001115 } else if (deltaRows > 0) {
1116 // Screen got larger.
1117
1118 if (deltaRows <= this.scrollbackRows_.length) {
1119 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1120 var rows = this.scrollbackRows_.splice(
1121 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1122 this.screen_.unshiftRows(rows);
1123 deltaRows -= scrollbackCount;
1124 cursor.row += scrollbackCount;
1125 }
1126
1127 if (deltaRows)
1128 this.appendRows_(deltaRows);
1129 }
1130
rginda35c456b2012-02-09 17:29:05 -08001131 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001132 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001133};
1134
1135/**
1136 * Scroll the terminal to the top of the scrollback buffer.
1137 */
1138hterm.Terminal.prototype.scrollHome = function() {
1139 this.scrollPort_.scrollRowToTop(0);
1140};
1141
1142/**
1143 * Scroll the terminal to the end.
1144 */
1145hterm.Terminal.prototype.scrollEnd = function() {
1146 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1147};
1148
1149/**
1150 * Scroll the terminal one page up (minus one line) relative to the current
1151 * position.
1152 */
1153hterm.Terminal.prototype.scrollPageUp = function() {
1154 var i = this.scrollPort_.getTopRowIndex();
1155 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
1156};
1157
1158/**
1159 * Scroll the terminal one page down (minus one line) relative to the current
1160 * position.
1161 */
1162hterm.Terminal.prototype.scrollPageDown = function() {
1163 var i = this.scrollPort_.getTopRowIndex();
1164 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -08001165};
1166
rgindac9bc5502012-01-18 11:48:44 -08001167/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001168 * Scroll the terminal one line up relative to the current position.
1169 */
1170hterm.Terminal.prototype.scrollLineUp = function() {
1171 var i = this.scrollPort_.getTopRowIndex();
1172 this.scrollPort_.scrollRowToTop(i - 1);
1173};
1174
1175/**
1176 * Scroll the terminal one line down relative to the current position.
1177 */
1178hterm.Terminal.prototype.scrollLineDown = function() {
1179 var i = this.scrollPort_.getTopRowIndex();
1180 this.scrollPort_.scrollRowToTop(i + 1);
1181};
1182
1183/**
Robert Ginda40932892012-12-10 17:26:40 -08001184 * Clear primary screen, secondary screen, and the scrollback buffer.
1185 */
1186hterm.Terminal.prototype.wipeContents = function() {
1187 this.scrollbackRows_.length = 0;
1188 this.scrollPort_.resetCache();
1189
1190 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
1191 var bottom = screen.getHeight();
1192 if (bottom > 0) {
1193 this.renumberRows_(0, bottom);
1194 this.clearHome(screen);
1195 }
1196 }.bind(this));
1197
1198 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001199 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001200};
1201
1202/**
rgindac9bc5502012-01-18 11:48:44 -08001203 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001204 *
1205 * Perform a full reset to the default values listed in
1206 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001207 */
rginda87b86462011-12-14 13:48:03 -08001208hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001209 this.vt.reset();
1210
rgindac9bc5502012-01-18 11:48:44 -08001211 this.clearAllTabStops();
1212 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001213
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001214 const resetScreen = (screen) => {
1215 // We want to make sure to reset the attributes before we clear the screen.
1216 // The attributes might be used to initialize default/empty rows.
1217 screen.textAttributes.reset();
1218 screen.textAttributes.resetColorPalette();
1219 this.clearHome(screen);
1220 screen.saveCursorAndState(this.vt);
1221 };
1222 resetScreen(this.primaryScreen_);
1223 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001224
Mike Frysinger84301d02017-11-29 13:28:46 -08001225 // Reset terminal options to their default values.
1226 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001227 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1228
Mike Frysinger84301d02017-11-29 13:28:46 -08001229 this.setVTScrollRegion(null, null);
1230
1231 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001232};
1233
rgindac9bc5502012-01-18 11:48:44 -08001234/**
1235 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001236 *
1237 * Perform a soft reset to the default values listed in
1238 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001239 */
rginda0f5c0292012-01-13 11:00:13 -08001240hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001241 this.vt.reset();
1242
rgindab8bc8932012-04-27 12:45:03 -07001243 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001244 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001245
Brad Townb62dfdc2015-03-16 19:07:15 -07001246 // We show the cursor on soft reset but do not alter the blink state.
1247 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1248
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001249 const resetScreen = (screen) => {
1250 // Xterm also resets the color palette on soft reset, even though it doesn't
1251 // seem to be documented anywhere.
1252 screen.textAttributes.reset();
1253 screen.textAttributes.resetColorPalette();
1254 screen.saveCursorAndState(this.vt);
1255 };
1256 resetScreen(this.primaryScreen_);
1257 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001258
rgindab8bc8932012-04-27 12:45:03 -07001259 // The xterm man page explicitly says this will happen on soft reset.
1260 this.setVTScrollRegion(null, null);
1261
1262 // Xterm also shows the cursor on soft reset, but does not alter the blink
1263 // state.
rgindaa19afe22012-01-25 15:40:22 -08001264 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001265};
1266
rgindac9bc5502012-01-18 11:48:44 -08001267/**
1268 * Move the cursor forward to the next tab stop, or to the last column
1269 * if no more tab stops are set.
1270 */
1271hterm.Terminal.prototype.forwardTabStop = function() {
1272 var column = this.screen_.cursorPosition.column;
1273
1274 for (var i = 0; i < this.tabStops_.length; i++) {
1275 if (this.tabStops_[i] > column) {
1276 this.setCursorColumn(this.tabStops_[i]);
1277 return;
1278 }
1279 }
1280
David Benjamin66e954d2012-05-05 21:08:12 -04001281 // xterm does not clear the overflow flag on HT or CHT.
1282 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001283 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001284 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001285};
1286
rgindac9bc5502012-01-18 11:48:44 -08001287/**
1288 * Move the cursor backward to the previous tab stop, or to the first column
1289 * if no previous tab stops are set.
1290 */
1291hterm.Terminal.prototype.backwardTabStop = function() {
1292 var column = this.screen_.cursorPosition.column;
1293
1294 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1295 if (this.tabStops_[i] < column) {
1296 this.setCursorColumn(this.tabStops_[i]);
1297 return;
1298 }
1299 }
1300
1301 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001302};
1303
rgindac9bc5502012-01-18 11:48:44 -08001304/**
1305 * Set a tab stop at the given column.
1306 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001307 * @param {integer} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001308 */
1309hterm.Terminal.prototype.setTabStop = function(column) {
1310 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1311 if (this.tabStops_[i] == column)
1312 return;
1313
1314 if (this.tabStops_[i] < column) {
1315 this.tabStops_.splice(i + 1, 0, column);
1316 return;
1317 }
1318 }
1319
1320 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001321};
1322
rgindac9bc5502012-01-18 11:48:44 -08001323/**
1324 * Clear the tab stop at the current cursor position.
1325 *
1326 * No effect if there is no tab stop at the current cursor position.
1327 */
1328hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1329 var column = this.screen_.cursorPosition.column;
1330
1331 var i = this.tabStops_.indexOf(column);
1332 if (i == -1)
1333 return;
1334
1335 this.tabStops_.splice(i, 1);
1336};
1337
1338/**
1339 * Clear all tab stops.
1340 */
1341hterm.Terminal.prototype.clearAllTabStops = function() {
1342 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001343 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001344};
1345
1346/**
1347 * Set up the default tab stops, starting from a given column.
1348 *
1349 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001350 * from the specified column, or 0 if no column is provided. It also flags
1351 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001352 *
1353 * This does not clear the existing tab stops first, use clearAllTabStops
1354 * for that.
1355 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001356 * @param {integer} opt_start Optional starting zero based starting column, useful
rgindac9bc5502012-01-18 11:48:44 -08001357 * for filling out missing tab stops when the terminal is resized.
1358 */
1359hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1360 var start = opt_start || 0;
1361 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001362 // Round start up to a default tab stop.
1363 start = start - 1 - ((start - 1) % w) + w;
1364 for (var i = start; i < this.screenSize.width; i += w) {
1365 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001366 }
David Benjamin66e954d2012-05-05 21:08:12 -04001367
1368 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001369};
1370
rginda6d397402012-01-17 10:58:29 -08001371/**
rginda8ba33642011-12-14 12:31:31 -08001372 * Interpret a sequence of characters.
1373 *
1374 * Incomplete escape sequences are buffered until the next call.
1375 *
1376 * @param {string} str Sequence of characters to interpret or pass through.
1377 */
1378hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001379 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001380 this.scheduleSyncCursorPosition_();
1381};
1382
1383/**
1384 * Take over the given DIV for use as the terminal display.
1385 *
1386 * @param {HTMLDivElement} div The div to use as the terminal display.
1387 */
1388hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001389 this.div_ = div;
1390
rginda8ba33642011-12-14 12:31:31 -08001391 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001392 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001393 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1394 this.scrollPort_.setBackgroundPosition(
1395 this.prefs_.get('background-position'));
Mike Frysinger08bad432017-04-24 00:50:54 -04001396 this.scrollPort_.setUserCssUrl(this.prefs_.get('user-css'));
1397 this.scrollPort_.setUserCssText(this.prefs_.get('user-css-text'));
rginda30f20f62012-04-05 16:36:19 -07001398
rginda0918b652012-04-04 11:26:24 -07001399 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001400
rginda9f5222b2012-03-05 11:53:28 -08001401 this.setFontSize(this.prefs_.get('font-size'));
1402 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001403
David Reveman8f552492012-03-28 12:18:41 -04001404 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001405 this.setScrollWheelMoveMultipler(
1406 this.prefs_.get('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001407
rginda8ba33642011-12-14 12:31:31 -08001408 this.document_ = this.scrollPort_.getDocument();
1409
Evan Jones5f9df812016-12-06 09:38:58 -05001410 this.document_.body.oncontextmenu = function() { return false; };
rginda4bba5e12012-06-20 16:15:30 -07001411
1412 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001413 var screenNode = this.scrollPort_.getScreenNode();
1414 screenNode.addEventListener('mousedown', onMouse);
1415 screenNode.addEventListener('mouseup', onMouse);
1416 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001417 this.scrollPort_.onScrollWheel = onMouse;
1418
Toni Barzic0bfa8922013-11-22 11:18:35 -08001419 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001420 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001421 // Listen for mousedown events on the screenNode as in FF the focus
1422 // events don't bubble.
1423 screenNode.addEventListener('mousedown', function() {
1424 setTimeout(this.onFocusChange_.bind(this, true));
1425 }.bind(this));
1426
Toni Barzic0bfa8922013-11-22 11:18:35 -08001427 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001428 'blur', this.onFocusChange_.bind(this, false));
1429
1430 var style = this.document_.createElement('style');
1431 style.textContent =
1432 ('.cursor-node[focus="false"] {' +
1433 ' box-sizing: border-box;' +
1434 ' background-color: transparent !important;' +
1435 ' border-width: 2px;' +
1436 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001437 '}' +
1438 '.wc-node {' +
1439 ' display: inline-block;' +
1440 ' text-align: center;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001441 ' width: calc(var(--hterm-charsize-width) * 2);' +
Mike Frysinger4036f6e2017-05-31 14:02:55 -04001442 ' line-height: var(--hterm-charsize-height);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001443 '}' +
1444 ':root {' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001445 ' --hterm-charsize-width: ' + this.scrollPort_.characterSize.width + 'px;' +
1446 ' --hterm-charsize-height: ' + this.scrollPort_.characterSize.height + 'px;' +
Mike Frysingera27c0502017-08-23 22:37:10 -04001447 // Default position hides the cursor for when the window is initializing.
1448 ' --hterm-cursor-offset-col: -1;' +
1449 ' --hterm-cursor-offset-row: -1;' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001450 ' --hterm-blink-node-duration: 0.7s;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001451 ' --hterm-mouse-cursor-text: text;' +
1452 ' --hterm-mouse-cursor-pointer: default;' +
1453 ' --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001454 '}' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001455 '.uri-node:hover {' +
1456 ' text-decoration: underline;' +
1457 ' cursor: pointer;' +
1458 '}' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001459 '@keyframes blink {' +
1460 ' from { opacity: 1.0; }' +
1461 ' to { opacity: 0.0; }' +
1462 '}' +
1463 '.blink-node {' +
1464 ' animation-name: blink;' +
1465 ' animation-duration: var(--hterm-blink-node-duration);' +
1466 ' animation-iteration-count: infinite;' +
1467 ' animation-timing-function: ease-in-out;' +
1468 ' animation-direction: alternate;' +
rginda8e92a692012-05-20 19:37:20 -07001469 '}');
1470 this.document_.head.appendChild(style);
1471
rginda8ba33642011-12-14 12:31:31 -08001472 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001473 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001474 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001475 this.cursorNode_.style.cssText =
1476 ('position: absolute;' +
Mike Frysinger44c32202017-08-05 01:13:09 -04001477 'left: calc(var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));' +
1478 'top: calc(var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));' +
rginda87b86462011-12-14 13:48:03 -08001479 'display: block;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001480 'width: var(--hterm-charsize-width);' +
1481 'height: var(--hterm-charsize-height);' +
Rob Spies06533ba2014-04-24 11:20:37 -07001482 '-webkit-transition: opacity, background-color 100ms linear;' +
1483 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001484
rginda8e92a692012-05-20 19:37:20 -07001485 this.setCursorColor(this.prefs_.get('cursor-color'));
Robert Gindafb1be6a2013-12-11 11:56:22 -08001486 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1487 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001488
rginda8ba33642011-12-14 12:31:31 -08001489 this.document_.body.appendChild(this.cursorNode_);
1490
rgindad5613292012-06-19 15:40:37 -07001491 // When 'enableMouseDragScroll' is off we reposition this element directly
1492 // under the mouse cursor after a click. This makes Chrome associate
1493 // subsequent mousemove events with the scroll-blocker. Since the
1494 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1495 // events do not cause the scrollport to scroll.
1496 //
1497 // It's a hack, but it's the cleanest way I could find.
1498 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001499 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
rgindad5613292012-06-19 15:40:37 -07001500 this.scrollBlockerNode_.style.cssText =
1501 ('position: absolute;' +
1502 'top: -99px;' +
1503 'display: block;' +
1504 'width: 10px;' +
1505 'height: 10px;');
1506 this.document_.body.appendChild(this.scrollBlockerNode_);
1507
rgindad5613292012-06-19 15:40:37 -07001508 this.scrollPort_.onScrollWheel = onMouse;
1509 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1510 ].forEach(function(event) {
1511 this.scrollBlockerNode_.addEventListener(event, onMouse);
1512 this.cursorNode_.addEventListener(event, onMouse);
1513 this.document_.addEventListener(event, onMouse);
1514 }.bind(this));
1515
1516 this.cursorNode_.addEventListener('mousedown', function() {
1517 setTimeout(this.focus.bind(this));
1518 }.bind(this));
1519
rginda8ba33642011-12-14 12:31:31 -08001520 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001521
rginda87b86462011-12-14 13:48:03 -08001522 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001523 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001524};
1525
rginda0918b652012-04-04 11:26:24 -07001526/**
1527 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001528 *
1529 * @return {HTMLDocument}
rginda0918b652012-04-04 11:26:24 -07001530 */
rginda87b86462011-12-14 13:48:03 -08001531hterm.Terminal.prototype.getDocument = function() {
1532 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001533};
1534
1535/**
rginda0918b652012-04-04 11:26:24 -07001536 * Focus the terminal.
1537 */
1538hterm.Terminal.prototype.focus = function() {
1539 this.scrollPort_.focus();
1540};
1541
1542/**
rginda8ba33642011-12-14 12:31:31 -08001543 * Return the HTML Element for a given row index.
1544 *
1545 * This is a method from the RowProvider interface. The ScrollPort uses
1546 * it to fetch rows on demand as they are scrolled into view.
1547 *
1548 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1549 * pairs to conserve memory.
1550 *
1551 * @param {integer} index The zero-based row index, measured relative to the
1552 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001553 * largest indices.
rginda8ba33642011-12-14 12:31:31 -08001554 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1555 */
1556hterm.Terminal.prototype.getRowNode = function(index) {
1557 if (index < this.scrollbackRows_.length)
1558 return this.scrollbackRows_[index];
1559
1560 var screenIndex = index - this.scrollbackRows_.length;
1561 return this.screen_.rowsArray[screenIndex];
1562};
1563
1564/**
1565 * Return the text content for a given range of rows.
1566 *
1567 * This is a method from the RowProvider interface. The ScrollPort uses
1568 * it to fetch text content on demand when the user attempts to copy their
1569 * selection to the clipboard.
1570 *
1571 * @param {integer} start The zero-based row index to start from, measured
1572 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001573 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001574 * @param {integer} end The zero-based row index to end on, measured
1575 * relative to the start of the scrollback buffer.
1576 * @return {string} A single string containing the text value of the range of
1577 * rows. Lines will be newline delimited, with no trailing newline.
1578 */
1579hterm.Terminal.prototype.getRowsText = function(start, end) {
1580 var ary = [];
1581 for (var i = start; i < end; i++) {
1582 var node = this.getRowNode(i);
1583 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001584 if (i < end - 1 && !node.getAttribute('line-overflow'))
1585 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001586 }
1587
rgindaa09e7332012-08-17 12:49:51 -07001588 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001589};
1590
1591/**
1592 * Return the text content for a given row.
1593 *
1594 * This is a method from the RowProvider interface. The ScrollPort uses
1595 * it to fetch text content on demand when the user attempts to copy their
1596 * selection to the clipboard.
1597 *
1598 * @param {integer} index The zero-based row index to return, measured
1599 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001600 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001601 * @return {string} A string containing the text value of the selected row.
1602 */
1603hterm.Terminal.prototype.getRowText = function(index) {
1604 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001605 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001606};
1607
1608/**
1609 * Return the total number of rows in the addressable screen and in the
1610 * scrollback buffer of this terminal.
1611 *
1612 * This is a method from the RowProvider interface. The ScrollPort uses
1613 * it to compute the size of the scrollbar.
1614 *
1615 * @return {integer} The number of rows in this terminal.
1616 */
1617hterm.Terminal.prototype.getRowCount = function() {
1618 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1619};
1620
1621/**
1622 * Create DOM nodes for new rows and append them to the end of the terminal.
1623 *
1624 * This is the only correct way to add a new DOM node for a row. Notice that
1625 * the new row is appended to the bottom of the list of rows, and does not
1626 * require renumbering (of the rowIndex property) of previous rows.
1627 *
1628 * If you think you want a new blank row somewhere in the middle of the
1629 * terminal, look into moveRows_().
1630 *
1631 * This method does not pay attention to vtScrollTop/Bottom, since you should
1632 * be using moveRows() in cases where they would matter.
1633 *
1634 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001635 *
1636 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001637 */
1638hterm.Terminal.prototype.appendRows_ = function(count) {
1639 var cursorRow = this.screen_.rowsArray.length;
1640 var offset = this.scrollbackRows_.length + cursorRow;
1641 for (var i = 0; i < count; i++) {
1642 var row = this.document_.createElement('x-row');
1643 row.appendChild(this.document_.createTextNode(''));
1644 row.rowIndex = offset + i;
1645 this.screen_.pushRow(row);
1646 }
1647
1648 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1649 if (extraRows > 0) {
1650 var ary = this.screen_.shiftRows(extraRows);
1651 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001652 if (this.scrollPort_.isScrolledEnd)
1653 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001654 }
1655
1656 if (cursorRow >= this.screen_.rowsArray.length)
1657 cursorRow = this.screen_.rowsArray.length - 1;
1658
rginda87b86462011-12-14 13:48:03 -08001659 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001660};
1661
1662/**
1663 * Relocate rows from one part of the addressable screen to another.
1664 *
1665 * This is used to recycle rows during VT scrolls (those which are driven
1666 * by VT commands, rather than by the user manipulating the scrollbar.)
1667 *
1668 * In this case, the blank lines scrolled into the scroll region are made of
1669 * the nodes we scrolled off. These have their rowIndex properties carefully
1670 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001671 *
1672 * @param {number} fromIndex The start index.
1673 * @param {number} count The number of rows to move.
1674 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001675 */
1676hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1677 var ary = this.screen_.removeRows(fromIndex, count);
1678 this.screen_.insertRows(toIndex, ary);
1679
1680 var start, end;
1681 if (fromIndex < toIndex) {
1682 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001683 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001684 } else {
1685 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001686 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001687 }
1688
1689 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001690 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001691};
1692
1693/**
1694 * Renumber the rowIndex property of the given range of rows.
1695 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001696 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001697 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001698 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001699 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001700 *
1701 * @param {number} start The start index.
1702 * @param {number} end The end index.
1703 * @param {hterm.Screen} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001704 */
Robert Ginda40932892012-12-10 17:26:40 -08001705hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1706 var screen = opt_screen || this.screen_;
1707
rginda8ba33642011-12-14 12:31:31 -08001708 var offset = this.scrollbackRows_.length;
1709 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001710 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001711 }
1712};
1713
1714/**
1715 * Print a string to the terminal.
1716 *
1717 * This respects the current insert and wraparound modes. It will add new lines
1718 * to the end of the terminal, scrolling off the top into the scrollback buffer
1719 * if necessary.
1720 *
1721 * The string is *not* parsed for escape codes. Use the interpret() method if
1722 * that's what you're after.
1723 *
1724 * @param{string} str The string to print.
1725 */
1726hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001727 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001728
Ricky Liang48f05cb2013-12-31 23:35:29 +08001729 var strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04001730 // Fun edge case: If the string only contains zero width codepoints (like
1731 // combining characters), we make sure to iterate at least once below.
1732 if (strWidth == 0 && str)
1733 strWidth = 1;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001734
1735 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001736 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1737 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001738 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001739 }
rgindaa19afe22012-01-25 15:40:22 -08001740
Ricky Liang48f05cb2013-12-31 23:35:29 +08001741 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001742 var didOverflow = false;
1743 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001744
rgindaa9abdd82012-08-06 18:05:09 -07001745 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1746 didOverflow = true;
1747 count = this.screenSize.width - this.screen_.cursorPosition.column;
1748 }
rgindaa19afe22012-01-25 15:40:22 -08001749
rgindaa9abdd82012-08-06 18:05:09 -07001750 if (didOverflow && !this.options_.wraparound) {
1751 // If the string overflowed the line but wraparound is off, then the
1752 // last printed character should be the last of the string.
1753 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001754 substr = lib.wc.substr(str, startOffset, count - 1) +
1755 lib.wc.substr(str, strWidth - 1);
1756 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001757 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001758 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001759 }
rgindaa19afe22012-01-25 15:40:22 -08001760
Ricky Liang48f05cb2013-12-31 23:35:29 +08001761 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1762 for (var i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001763 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
1764 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001765
1766 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001767 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001768 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001769 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001770 }
1771 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001772 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07001773 }
1774
1775 this.screen_.maybeClipCurrentRow();
1776 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001777 }
rginda8ba33642011-12-14 12:31:31 -08001778
1779 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001780
rginda9f5222b2012-03-05 11:53:28 -08001781 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001782 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001783};
1784
1785/**
rginda87b86462011-12-14 13:48:03 -08001786 * Set the VT scroll region.
1787 *
rginda87b86462011-12-14 13:48:03 -08001788 * This also resets the cursor position to the absolute (0, 0) position, since
1789 * that's what xterm appears to do.
1790 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001791 * Setting the scroll region to the full height of the terminal will clear
1792 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1793 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1794 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1795 * continue to work as most users would expect.
1796 *
rginda87b86462011-12-14 13:48:03 -08001797 * @param {integer} scrollTop The zero-based top of the scroll region.
1798 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1799 * inclusive.
1800 */
1801hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001802 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001803 this.vtScrollTop_ = null;
1804 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001805 } else {
1806 this.vtScrollTop_ = scrollTop;
1807 this.vtScrollBottom_ = scrollBottom;
1808 }
rginda87b86462011-12-14 13:48:03 -08001809};
1810
1811/**
rginda8ba33642011-12-14 12:31:31 -08001812 * Return the top row index according to the VT.
1813 *
1814 * This will return 0 unless the terminal has been told to restrict scrolling
1815 * to some lower row. It is used for some VT cursor positioning and scrolling
1816 * commands.
1817 *
1818 * @return {integer} The topmost row in the terminal's scroll region.
1819 */
1820hterm.Terminal.prototype.getVTScrollTop = function() {
1821 if (this.vtScrollTop_ != null)
1822 return this.vtScrollTop_;
1823
1824 return 0;
rginda87b86462011-12-14 13:48:03 -08001825};
rginda8ba33642011-12-14 12:31:31 -08001826
1827/**
1828 * Return the bottom row index according to the VT.
1829 *
1830 * This will return the height of the terminal unless the it has been told to
1831 * restrict scrolling to some higher row. It is used for some VT cursor
1832 * positioning and scrolling commands.
1833 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001834 * @return {integer} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001835 */
1836hterm.Terminal.prototype.getVTScrollBottom = function() {
1837 if (this.vtScrollBottom_ != null)
1838 return this.vtScrollBottom_;
1839
rginda87b86462011-12-14 13:48:03 -08001840 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001841}
1842
1843/**
1844 * Process a '\n' character.
1845 *
1846 * If the cursor is on the final row of the terminal this will append a new
1847 * blank row to the screen and scroll the topmost row into the scrollback
1848 * buffer.
1849 *
1850 * Otherwise, this moves the cursor to column zero of the next row.
1851 */
1852hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001853 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1854 this.screen_.rowsArray.length - 1);
1855
1856 if (this.vtScrollBottom_ != null) {
1857 // A VT Scroll region is active, we never append new rows.
1858 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1859 // We're at the end of the VT Scroll Region, perform a VT scroll.
1860 this.vtScrollUp(1);
1861 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1862 } else if (cursorAtEndOfScreen) {
1863 // We're at the end of the screen, the only thing to do is put the
1864 // cursor to column 0.
1865 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1866 } else {
1867 // Anywhere else, advance the cursor row, and reset the column.
1868 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1869 }
1870 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001871 // We're at the end of the screen. Append a new row to the terminal,
1872 // shifting the top row into the scrollback.
1873 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001874 } else {
rginda87b86462011-12-14 13:48:03 -08001875 // Anywhere else in the screen just moves the cursor.
1876 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001877 }
1878};
1879
1880/**
1881 * Like newLine(), except maintain the cursor column.
1882 */
1883hterm.Terminal.prototype.lineFeed = function() {
1884 var column = this.screen_.cursorPosition.column;
1885 this.newLine();
1886 this.setCursorColumn(column);
1887};
1888
1889/**
rginda87b86462011-12-14 13:48:03 -08001890 * If autoCarriageReturn is set then newLine(), else lineFeed().
1891 */
1892hterm.Terminal.prototype.formFeed = function() {
1893 if (this.options_.autoCarriageReturn) {
1894 this.newLine();
1895 } else {
1896 this.lineFeed();
1897 }
1898};
1899
1900/**
1901 * Move the cursor up one row, possibly inserting a blank line.
1902 *
1903 * The cursor column is not changed.
1904 */
1905hterm.Terminal.prototype.reverseLineFeed = function() {
1906 var scrollTop = this.getVTScrollTop();
1907 var currentRow = this.screen_.cursorPosition.row;
1908
1909 if (currentRow == scrollTop) {
1910 this.insertLines(1);
1911 } else {
1912 this.setAbsoluteCursorRow(currentRow - 1);
1913 }
1914};
1915
1916/**
rginda8ba33642011-12-14 12:31:31 -08001917 * Replace all characters to the left of the current cursor with the space
1918 * character.
1919 *
1920 * TODO(rginda): This should probably *remove* the characters (not just replace
1921 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001922 * position.
rginda8ba33642011-12-14 12:31:31 -08001923 */
1924hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001925 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001926 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04001927 const count = cursor.column + 1;
1928 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08001929 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001930};
1931
1932/**
David Benjamin684a9b72012-05-01 17:19:58 -04001933 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001934 *
1935 * The cursor position is unchanged.
1936 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001937 * If the current background color is not the default background color this
1938 * will insert spaces rather than delete. This is unfortunate because the
1939 * trailing space will affect text selection, but it's difficult to come up
1940 * with a way to style empty space that wouldn't trip up the hterm.Screen
1941 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001942 *
1943 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1944 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1945 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05001946 *
1947 * @param {number} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08001948 */
1949hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001950 if (this.screen_.cursorPosition.overflow)
1951 return;
1952
Robert Ginda7fd57082012-09-25 14:41:47 -07001953 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1954 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001955
1956 if (this.screen_.textAttributes.background ===
1957 this.screen_.textAttributes.DEFAULT_COLOR) {
1958 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08001959 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07001960 this.screen_.cursorPosition.column + count) {
1961 this.screen_.deleteChars(count);
1962 this.clearCursorOverflow();
1963 return;
1964 }
1965 }
1966
rginda87b86462011-12-14 13:48:03 -08001967 var cursor = this.saveCursor();
Mike Frysinger6380bed2017-08-24 18:46:39 -04001968 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08001969 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001970 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001971};
1972
1973/**
1974 * Erase the current line.
1975 *
1976 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001977 */
1978hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001979 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001980 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001981 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001982 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001983};
1984
1985/**
David Benjamina08d78f2012-05-05 00:28:49 -04001986 * Erase all characters from the start of the screen to the current cursor
1987 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001988 *
1989 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001990 */
1991hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001992 var cursor = this.saveCursor();
1993
1994 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001995
David Benjamina08d78f2012-05-05 00:28:49 -04001996 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001997 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001998 this.screen_.clearCursorRow();
1999 }
2000
rginda87b86462011-12-14 13:48:03 -08002001 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002002 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002003};
2004
2005/**
2006 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002007 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002008 *
2009 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002010 */
2011hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08002012 var cursor = this.saveCursor();
2013
2014 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002015
David Benjamina08d78f2012-05-05 00:28:49 -04002016 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08002017 for (var i = cursor.row + 1; i <= bottom; i++) {
2018 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002019 this.screen_.clearCursorRow();
2020 }
2021
rginda87b86462011-12-14 13:48:03 -08002022 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002023 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002024};
2025
2026/**
2027 * Fill the terminal with a given character.
2028 *
2029 * This methods does not respect the VT scroll region.
2030 *
2031 * @param {string} ch The character to use for the fill.
2032 */
2033hterm.Terminal.prototype.fill = function(ch) {
2034 var cursor = this.saveCursor();
2035
2036 this.setAbsoluteCursorPosition(0, 0);
2037 for (var row = 0; row < this.screenSize.height; row++) {
2038 for (var col = 0; col < this.screenSize.width; col++) {
2039 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002040 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002041 }
2042 }
2043
2044 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002045};
2046
2047/**
rginda9ea433c2012-03-16 11:57:00 -07002048 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002049 *
rginda9ea433c2012-03-16 11:57:00 -07002050 * This does not respect the scroll region.
2051 *
2052 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2053 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002054 */
rginda9ea433c2012-03-16 11:57:00 -07002055hterm.Terminal.prototype.clearHome = function(opt_screen) {
2056 var screen = opt_screen || this.screen_;
2057 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002058
rginda11057d52012-04-25 12:29:56 -07002059 if (bottom == 0) {
2060 // Empty screen, nothing to do.
2061 return;
2062 }
2063
rgindae4d29232012-01-19 10:47:13 -08002064 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002065 screen.setCursorPosition(i, 0);
2066 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002067 }
2068
rginda9ea433c2012-03-16 11:57:00 -07002069 screen.setCursorPosition(0, 0);
2070};
2071
2072/**
2073 * Erase the entire display without changing the cursor position.
2074 *
2075 * The cursor position is unchanged. This does not respect the scroll
2076 * region.
2077 *
2078 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2079 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002080 */
2081hterm.Terminal.prototype.clear = function(opt_screen) {
2082 var screen = opt_screen || this.screen_;
2083 var cursor = screen.cursorPosition.clone();
2084 this.clearHome(screen);
2085 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002086};
2087
2088/**
2089 * VT command to insert lines at the current cursor row.
2090 *
2091 * This respects the current scroll region. Rows pushed off the bottom are
2092 * lost (they won't show up in the scrollback buffer).
2093 *
rginda8ba33642011-12-14 12:31:31 -08002094 * @param {integer} count The number of lines to insert.
2095 */
2096hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002097 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002098
2099 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002100 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002101
Robert Ginda579186b2012-09-26 11:40:04 -07002102 // The moveCount is the number of rows we need to relocate to make room for
2103 // the new row(s). The count is the distance to move them.
2104 var moveCount = bottom - cursorRow - count + 1;
2105 if (moveCount)
2106 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002107
Robert Ginda579186b2012-09-26 11:40:04 -07002108 for (var i = count - 1; i >= 0; i--) {
2109 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002110 this.screen_.clearCursorRow();
2111 }
rginda8ba33642011-12-14 12:31:31 -08002112};
2113
2114/**
2115 * VT command to delete lines at the current cursor row.
2116 *
2117 * New rows are added to the bottom of scroll region to take their place. New
2118 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002119 *
2120 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002121 */
2122hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002123 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002124
rginda87b86462011-12-14 13:48:03 -08002125 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002126 var bottom = this.getVTScrollBottom();
2127
rginda87b86462011-12-14 13:48:03 -08002128 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002129 count = Math.min(count, maxCount);
2130
rginda87b86462011-12-14 13:48:03 -08002131 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002132 if (count != maxCount)
2133 this.moveRows_(top, count, moveStart);
2134
2135 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002136 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002137 this.screen_.clearCursorRow();
2138 }
2139
rginda87b86462011-12-14 13:48:03 -08002140 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002141 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002142};
2143
2144/**
2145 * Inserts the given number of spaces at the current cursor position.
2146 *
rginda87b86462011-12-14 13:48:03 -08002147 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002148 *
2149 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002150 */
2151hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002152 var cursor = this.saveCursor();
2153
rgindacbbd7482012-06-13 15:06:16 -07002154 var ws = lib.f.getWhitespace(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002155 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002156 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002157
2158 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002159 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002160};
2161
2162/**
2163 * Forward-delete the specified number of characters starting at the cursor
2164 * position.
2165 *
2166 * @param {integer} count The number of characters to delete.
2167 */
2168hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002169 var deleted = this.screen_.deleteChars(count);
2170 if (deleted && !this.screen_.textAttributes.isDefault()) {
2171 var cursor = this.saveCursor();
2172 this.setCursorColumn(this.screenSize.width - deleted);
2173 this.screen_.insertString(lib.f.getWhitespace(deleted));
2174 this.restoreCursor(cursor);
2175 }
2176
David Benjamin54e8bf62012-06-01 22:31:40 -04002177 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002178};
2179
2180/**
2181 * Shift rows in the scroll region upwards by a given number of lines.
2182 *
2183 * New rows are inserted at the bottom of the scroll region to fill the
2184 * vacated rows. The new rows not filled out with the current text attributes.
2185 *
2186 * This function does not affect the scrollback rows at all. Rows shifted
2187 * off the top are lost.
2188 *
rginda87b86462011-12-14 13:48:03 -08002189 * The cursor position is not altered.
2190 *
rginda8ba33642011-12-14 12:31:31 -08002191 * @param {integer} count The number of rows to scroll.
2192 */
2193hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002194 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002195
rginda87b86462011-12-14 13:48:03 -08002196 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002197 this.deleteLines(count);
2198
rginda87b86462011-12-14 13:48:03 -08002199 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002200};
2201
2202/**
2203 * Shift rows below the cursor down by a given number of lines.
2204 *
2205 * This function respects the current scroll region.
2206 *
2207 * New rows are inserted at the top of the scroll region to fill the
2208 * vacated rows. The new rows not filled out with the current text attributes.
2209 *
2210 * This function does not affect the scrollback rows at all. Rows shifted
2211 * off the bottom are lost.
2212 *
2213 * @param {integer} count The number of rows to scroll.
2214 */
2215hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08002216 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002217
rginda87b86462011-12-14 13:48:03 -08002218 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08002219 this.insertLines(opt_count);
2220
rginda87b86462011-12-14 13:48:03 -08002221 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002222};
2223
rginda87b86462011-12-14 13:48:03 -08002224
rginda8ba33642011-12-14 12:31:31 -08002225/**
2226 * Set the cursor position.
2227 *
2228 * The cursor row is relative to the scroll region if the terminal has
2229 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2230 *
2231 * @param {integer} row The new zero-based cursor row.
2232 * @param {integer} row The new zero-based cursor column.
2233 */
2234hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2235 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002236 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002237 } else {
rginda87b86462011-12-14 13:48:03 -08002238 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002239 }
rginda87b86462011-12-14 13:48:03 -08002240};
rginda8ba33642011-12-14 12:31:31 -08002241
Evan Jones2600d4f2016-12-06 09:29:36 -05002242/**
2243 * Move the cursor relative to its current position.
2244 *
2245 * @param {number} row
2246 * @param {number} column
2247 */
rginda87b86462011-12-14 13:48:03 -08002248hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2249 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002250 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2251 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002252 this.screen_.setCursorPosition(row, column);
2253};
2254
Evan Jones2600d4f2016-12-06 09:29:36 -05002255/**
2256 * Move the cursor to the specified position.
2257 *
2258 * @param {number} row
2259 * @param {number} column
2260 */
rginda87b86462011-12-14 13:48:03 -08002261hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002262 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2263 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002264 this.screen_.setCursorPosition(row, column);
2265};
2266
2267/**
2268 * Set the cursor column.
2269 *
2270 * @param {integer} column The new zero-based cursor column.
2271 */
2272hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002273 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002274};
2275
2276/**
2277 * Return the cursor column.
2278 *
2279 * @return {integer} The zero-based cursor column.
2280 */
2281hterm.Terminal.prototype.getCursorColumn = function() {
2282 return this.screen_.cursorPosition.column;
2283};
2284
2285/**
2286 * Set the cursor row.
2287 *
2288 * The cursor row is relative to the scroll region if the terminal has
2289 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2290 *
2291 * @param {integer} row The new cursor row.
2292 */
rginda87b86462011-12-14 13:48:03 -08002293hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2294 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002295};
2296
2297/**
2298 * Return the cursor row.
2299 *
2300 * @return {integer} The zero-based cursor row.
2301 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002302hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002303 return this.screen_.cursorPosition.row;
2304};
2305
2306/**
2307 * Request that the ScrollPort redraw itself soon.
2308 *
2309 * The redraw will happen asynchronously, soon after the call stack winds down.
2310 * Multiple calls will be coalesced into a single redraw.
2311 */
2312hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002313 if (this.timeouts_.redraw)
2314 return;
rginda8ba33642011-12-14 12:31:31 -08002315
2316 var self = this;
rginda87b86462011-12-14 13:48:03 -08002317 this.timeouts_.redraw = setTimeout(function() {
2318 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002319 self.scrollPort_.redraw_();
2320 }, 0);
2321};
2322
2323/**
2324 * Request that the ScrollPort be scrolled to the bottom.
2325 *
2326 * The scroll will happen asynchronously, soon after the call stack winds down.
2327 * Multiple calls will be coalesced into a single scroll.
2328 *
2329 * This affects the scrollbar position of the ScrollPort, and has nothing to
2330 * do with the VT scroll commands.
2331 */
2332hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2333 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002334 return;
rginda8ba33642011-12-14 12:31:31 -08002335
2336 var self = this;
2337 this.timeouts_.scrollDown = setTimeout(function() {
2338 delete self.timeouts_.scrollDown;
2339 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2340 }, 10);
2341};
2342
2343/**
2344 * Move the cursor up a specified number of rows.
2345 *
2346 * @param {integer} count The number of rows to move the cursor.
2347 */
2348hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002349 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002350};
2351
2352/**
2353 * Move the cursor down a specified number of rows.
2354 *
2355 * @param {integer} count The number of rows to move the cursor.
2356 */
2357hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002358 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002359 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2360 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2361 this.screenSize.height - 1);
2362
rgindacbbd7482012-06-13 15:06:16 -07002363 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002364 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002365 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002366};
2367
2368/**
2369 * Move the cursor left a specified number of columns.
2370 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002371 * If reverse wraparound mode is enabled and the previous row wrapped into
2372 * the current row then we back up through the wraparound as well.
2373 *
rginda8ba33642011-12-14 12:31:31 -08002374 * @param {integer} count The number of columns to move the cursor.
2375 */
2376hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002377 count = count || 1;
2378
2379 if (count < 1)
2380 return;
2381
2382 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002383 if (this.options_.reverseWraparound) {
2384 if (this.screen_.cursorPosition.overflow) {
2385 // If this cursor is in the right margin, consume one count to get it
2386 // back to the last column. This only applies when we're in reverse
2387 // wraparound mode.
2388 count--;
2389 this.clearCursorOverflow();
2390
2391 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002392 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002393 }
2394
Robert Gindabfb32622014-07-17 13:20:27 -07002395 var newRow = this.screen_.cursorPosition.row;
2396 var newColumn = currentColumn - count;
2397 if (newColumn < 0) {
2398 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2399 if (newRow < 0) {
2400 // xterm also wraps from row 0 to the last row.
2401 newRow = this.screenSize.height + newRow % this.screenSize.height;
2402 }
2403 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2404 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002405
Robert Gindabfb32622014-07-17 13:20:27 -07002406 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2407
2408 } else {
2409 var newColumn = Math.max(currentColumn - count, 0);
2410 this.setCursorColumn(newColumn);
2411 }
rginda8ba33642011-12-14 12:31:31 -08002412};
2413
2414/**
2415 * Move the cursor right a specified number of columns.
2416 *
2417 * @param {integer} count The number of columns to move the cursor.
2418 */
2419hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002420 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002421
2422 if (count < 1)
2423 return;
2424
rgindacbbd7482012-06-13 15:06:16 -07002425 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002426 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002427 this.setCursorColumn(column);
2428};
2429
2430/**
2431 * Reverse the foreground and background colors of the terminal.
2432 *
2433 * This only affects text that was drawn with no attributes.
2434 *
2435 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2436 * been drawn with attributes that happen to coincide with the default
2437 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002438 *
2439 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002440 */
2441hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002442 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002443 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002444 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2445 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002446 } else {
rginda9f5222b2012-03-05 11:53:28 -08002447 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2448 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002449 }
2450};
2451
2452/**
rginda87b86462011-12-14 13:48:03 -08002453 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002454 *
2455 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002456 */
2457hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002458 this.cursorNode_.style.backgroundColor =
2459 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002460
2461 var self = this;
2462 setTimeout(function() {
Matheus Fernandes2d733082017-09-11 06:43:01 -04002463 self.restyleCursor_();
rginda6d397402012-01-17 10:58:29 -08002464 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002465
Michael Kelly485ecd12014-06-09 11:41:56 -04002466 // bellSquelchTimeout_ affects both audio and notification bells.
2467 if (this.bellSquelchTimeout_)
2468 return;
2469
Robert Ginda92e18102013-03-14 13:56:37 -07002470 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002471 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002472 this.bellSequelchTimeout_ = setTimeout(function() {
2473 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002474 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002475 } else {
2476 delete this.bellSquelchTimeout_;
2477 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002478
2479 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002480 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002481 this.bellNotificationList_.push(n);
2482 // TODO: Should we try to raise the window here?
2483 n.onclick = function() { self.closeBellNotifications_(); };
2484 }
rginda87b86462011-12-14 13:48:03 -08002485};
2486
2487/**
rginda8ba33642011-12-14 12:31:31 -08002488 * Set the origin mode bit.
2489 *
2490 * If origin mode is on, certain VT cursor and scrolling commands measure their
2491 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2492 * to the top of the addressable screen.
2493 *
2494 * Defaults to off.
2495 *
2496 * @param {boolean} state True to set origin mode, false to unset.
2497 */
2498hterm.Terminal.prototype.setOriginMode = function(state) {
2499 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002500 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002501};
2502
2503/**
2504 * Set the insert mode bit.
2505 *
2506 * If insert mode is on, existing text beyond the cursor position will be
2507 * shifted right to make room for new text. Otherwise, new text overwrites
2508 * any existing text.
2509 *
2510 * Defaults to off.
2511 *
2512 * @param {boolean} state True to set insert mode, false to unset.
2513 */
2514hterm.Terminal.prototype.setInsertMode = function(state) {
2515 this.options_.insertMode = state;
2516};
2517
2518/**
rginda87b86462011-12-14 13:48:03 -08002519 * Set the auto carriage return bit.
2520 *
2521 * If auto carriage return is on then a formfeed character is interpreted
2522 * as a newline, otherwise it's the same as a linefeed. The difference boils
2523 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002524 *
2525 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002526 */
2527hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2528 this.options_.autoCarriageReturn = state;
2529};
2530
2531/**
rginda8ba33642011-12-14 12:31:31 -08002532 * Set the wraparound mode bit.
2533 *
2534 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2535 * to the start of the following row. Otherwise, the cursor is clamped to the
2536 * end of the screen and attempts to write past it are ignored.
2537 *
2538 * Defaults to on.
2539 *
2540 * @param {boolean} state True to set wraparound mode, false to unset.
2541 */
2542hterm.Terminal.prototype.setWraparound = function(state) {
2543 this.options_.wraparound = state;
2544};
2545
2546/**
2547 * Set the reverse-wraparound mode bit.
2548 *
2549 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2550 * to the end of the previous row. Otherwise, the cursor is clamped to column
2551 * 0.
2552 *
2553 * Defaults to off.
2554 *
2555 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2556 */
2557hterm.Terminal.prototype.setReverseWraparound = function(state) {
2558 this.options_.reverseWraparound = state;
2559};
2560
2561/**
2562 * Selects between the primary and alternate screens.
2563 *
2564 * If alternate mode is on, the alternate screen is active. Otherwise the
2565 * primary screen is active.
2566 *
2567 * Swapping screens has no effect on the scrollback buffer.
2568 *
2569 * Each screen maintains its own cursor position.
2570 *
2571 * Defaults to off.
2572 *
2573 * @param {boolean} state True to set alternate mode, false to unset.
2574 */
2575hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002576 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002577 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2578
rginda35c456b2012-02-09 17:29:05 -08002579 if (this.screen_.rowsArray.length &&
2580 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2581 // If the screen changed sizes while we were away, our rowIndexes may
2582 // be incorrect.
2583 var offset = this.scrollbackRows_.length;
2584 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002585 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002586 ary[i].rowIndex = offset + i;
2587 }
2588 }
rginda8ba33642011-12-14 12:31:31 -08002589
rginda35c456b2012-02-09 17:29:05 -08002590 this.realizeWidth_(this.screenSize.width);
2591 this.realizeHeight_(this.screenSize.height);
2592 this.scrollPort_.syncScrollHeight();
2593 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002594
rginda6d397402012-01-17 10:58:29 -08002595 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002596 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002597};
2598
2599/**
2600 * Set the cursor-blink mode bit.
2601 *
2602 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2603 * a visible cursor does not blink.
2604 *
2605 * You should make sure to turn blinking off if you're going to dispose of a
2606 * terminal, otherwise you'll leak a timeout.
2607 *
2608 * Defaults to on.
2609 *
2610 * @param {boolean} state True to set cursor-blink mode, false to unset.
2611 */
2612hterm.Terminal.prototype.setCursorBlink = function(state) {
2613 this.options_.cursorBlink = state;
2614
2615 if (!state && this.timeouts_.cursorBlink) {
2616 clearTimeout(this.timeouts_.cursorBlink);
2617 delete this.timeouts_.cursorBlink;
2618 }
2619
2620 if (this.options_.cursorVisible)
2621 this.setCursorVisible(true);
2622};
2623
2624/**
2625 * Set the cursor-visible mode bit.
2626 *
2627 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2628 *
2629 * Defaults to on.
2630 *
2631 * @param {boolean} state True to set cursor-visible mode, false to unset.
2632 */
2633hterm.Terminal.prototype.setCursorVisible = function(state) {
2634 this.options_.cursorVisible = state;
2635
2636 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002637 if (this.timeouts_.cursorBlink) {
2638 clearTimeout(this.timeouts_.cursorBlink);
2639 delete this.timeouts_.cursorBlink;
2640 }
rginda87b86462011-12-14 13:48:03 -08002641 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002642 return;
2643 }
2644
rginda87b86462011-12-14 13:48:03 -08002645 this.syncCursorPosition_();
2646
2647 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002648
2649 if (this.options_.cursorBlink) {
2650 if (this.timeouts_.cursorBlink)
2651 return;
2652
Robert Gindaea2183e2014-07-17 09:51:51 -07002653 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002654 } else {
2655 if (this.timeouts_.cursorBlink) {
2656 clearTimeout(this.timeouts_.cursorBlink);
2657 delete this.timeouts_.cursorBlink;
2658 }
2659 }
2660};
2661
2662/**
rginda87b86462011-12-14 13:48:03 -08002663 * Synchronizes the visible cursor and document selection with the current
2664 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002665 */
2666hterm.Terminal.prototype.syncCursorPosition_ = function() {
2667 var topRowIndex = this.scrollPort_.getTopRowIndex();
2668 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2669 var cursorRowIndex = this.scrollbackRows_.length +
2670 this.screen_.cursorPosition.row;
2671
2672 if (cursorRowIndex > bottomRowIndex) {
2673 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04002674 this.setCssVar('cursor-offset-row', '-1');
rginda8ba33642011-12-14 12:31:31 -08002675 return;
2676 }
2677
Robert Gindab837c052014-08-11 11:17:51 -07002678 if (this.options_.cursorVisible &&
2679 this.cursorNode_.style.display == 'none') {
2680 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2681 this.cursorNode_.style.display = '';
2682 }
2683
Mike Frysinger44c32202017-08-05 01:13:09 -04002684 // Position the cursor using CSS variable math. If we do the math in JS,
2685 // the float math will end up being more precise than the CSS which will
2686 // cause the cursor tracking to be off.
2687 this.setCssVar(
2688 'cursor-offset-row',
2689 `${cursorRowIndex - topRowIndex} + ` +
2690 `${this.scrollPort_.visibleRowTopMargin}px`);
2691 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08002692
2693 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04002694 '(' + this.screen_.cursorPosition.column +
2695 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08002696 ')');
2697
2698 // Update the caret for a11y purposes.
2699 var selection = this.document_.getSelection();
2700 if (selection && selection.isCollapsed)
2701 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002702};
2703
Robert Gindafb1be6a2013-12-11 11:56:22 -08002704/**
2705 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2706 * and character cell dimensions.
2707 */
Robert Ginda830583c2013-08-07 13:20:46 -07002708hterm.Terminal.prototype.restyleCursor_ = function() {
2709 var shape = this.cursorShape_;
2710
2711 if (this.cursorNode_.getAttribute('focus') == 'false') {
2712 // Always show a block cursor when unfocused.
2713 shape = hterm.Terminal.cursorShape.BLOCK;
2714 }
2715
2716 var style = this.cursorNode_.style;
2717
2718 switch (shape) {
2719 case hterm.Terminal.cursorShape.BEAM:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002720 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002721 style.backgroundColor = 'transparent';
2722 style.borderBottomStyle = null;
2723 style.borderLeftStyle = 'solid';
2724 break;
2725
2726 case hterm.Terminal.cursorShape.UNDERLINE:
2727 style.height = this.scrollPort_.characterSize.baseline + 'px';
2728 style.backgroundColor = 'transparent';
2729 style.borderBottomStyle = 'solid';
2730 // correct the size to put it exactly at the baseline
2731 style.borderLeftStyle = null;
2732 break;
2733
2734 default:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002735 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002736 style.backgroundColor = this.cursorColor_;
2737 style.borderBottomStyle = null;
2738 style.borderLeftStyle = null;
2739 break;
2740 }
2741};
2742
rginda8ba33642011-12-14 12:31:31 -08002743/**
2744 * Synchronizes the visible cursor with the current cursor coordinates.
2745 *
2746 * The sync will happen asynchronously, soon after the call stack winds down.
2747 * Multiple calls will be coalesced into a single sync.
2748 */
2749hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2750 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002751 return;
rginda8ba33642011-12-14 12:31:31 -08002752
2753 var self = this;
2754 this.timeouts_.syncCursor = setTimeout(function() {
2755 self.syncCursorPosition_();
2756 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002757 }, 0);
2758};
2759
rgindacc2996c2012-02-24 14:59:31 -08002760/**
rgindaf522ce02012-04-17 17:49:17 -07002761 * Show or hide the zoom warning.
2762 *
2763 * The zoom warning is a message warning the user that their browser zoom must
2764 * be set to 100% in order for hterm to function properly.
2765 *
2766 * @param {boolean} state True to show the message, false to hide it.
2767 */
2768hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2769 if (!this.zoomWarningNode_) {
2770 if (!state)
2771 return;
2772
2773 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002774 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07002775 this.zoomWarningNode_.style.cssText = (
2776 'color: black;' +
2777 'background-color: #ff2222;' +
2778 'font-size: large;' +
2779 'border-radius: 8px;' +
2780 'opacity: 0.75;' +
2781 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2782 'top: 0.5em;' +
2783 'right: 1.2em;' +
2784 'position: absolute;' +
2785 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002786 '-webkit-user-select: none;' +
2787 '-moz-text-size-adjust: none;' +
2788 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002789
2790 this.zoomWarningNode_.addEventListener('click', function(e) {
2791 this.parentNode.removeChild(this);
2792 });
rgindaf522ce02012-04-17 17:49:17 -07002793 }
2794
Robert Gindab4839c22013-02-28 16:52:10 -08002795 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2796 hterm.zoomWarningMessage,
2797 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2798
rgindaf522ce02012-04-17 17:49:17 -07002799 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2800
2801 if (state) {
2802 if (!this.zoomWarningNode_.parentNode)
2803 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2804 } else if (this.zoomWarningNode_.parentNode) {
2805 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2806 }
2807};
2808
2809/**
rgindacc2996c2012-02-24 14:59:31 -08002810 * Show the terminal overlay for a given amount of time.
2811 *
2812 * The terminal overlay appears in inverse video in a large font, centered
2813 * over the terminal. You should probably keep the overlay message brief,
2814 * since it's in a large font and you probably aren't going to check the size
2815 * of the terminal first.
2816 *
2817 * @param {string} msg The text (not HTML) message to display in the overlay.
2818 * @param {number} opt_timeout The amount of time to wait before fading out
2819 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2820 * stay up forever (or until the next overlay).
2821 */
2822hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002823 if (!this.overlayNode_) {
2824 if (!this.div_)
2825 return;
2826
2827 this.overlayNode_ = this.document_.createElement('div');
2828 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002829 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002830 'font-size: xx-large;' +
2831 'opacity: 0.75;' +
2832 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2833 'position: absolute;' +
2834 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002835 '-webkit-transition: opacity 180ms ease-in;' +
2836 '-moz-user-select: none;' +
2837 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002838
2839 this.overlayNode_.addEventListener('mousedown', function(e) {
2840 e.preventDefault();
2841 e.stopPropagation();
2842 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002843 }
2844
rginda9f5222b2012-03-05 11:53:28 -08002845 this.overlayNode_.style.color = this.prefs_.get('background-color');
2846 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2847 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2848
rgindaf0090c92012-02-10 14:58:52 -08002849 this.overlayNode_.textContent = msg;
2850 this.overlayNode_.style.opacity = '0.75';
2851
2852 if (!this.overlayNode_.parentNode)
2853 this.div_.appendChild(this.overlayNode_);
2854
Robert Ginda97769282013-02-01 15:30:30 -08002855 var divSize = hterm.getClientSize(this.div_);
2856 var overlaySize = hterm.getClientSize(this.overlayNode_);
2857
Robert Ginda8a59f762014-07-23 11:29:55 -07002858 this.overlayNode_.style.top =
2859 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08002860 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07002861 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08002862
rgindaf0090c92012-02-10 14:58:52 -08002863 if (this.overlayTimeout_)
2864 clearTimeout(this.overlayTimeout_);
2865
rgindacc2996c2012-02-24 14:59:31 -08002866 if (opt_timeout === null)
2867 return;
2868
Mike Frysingerb6cfded2017-09-18 00:39:31 -04002869 this.overlayTimeout_ = setTimeout(() => {
2870 this.overlayNode_.style.opacity = '0';
2871 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
2872 }, opt_timeout || 1500);
2873};
2874
2875/**
2876 * Hide the terminal overlay immediately.
2877 *
2878 * Useful when we show an overlay for an event with an unknown end time.
2879 */
2880hterm.Terminal.prototype.hideOverlay = function() {
2881 if (this.overlayTimeout_)
2882 clearTimeout(this.overlayTimeout_);
2883 this.overlayTimeout_ = null;
2884
2885 if (this.overlayNode_.parentNode)
2886 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
2887 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08002888};
2889
rginda4bba5e12012-06-20 16:15:30 -07002890/**
2891 * Paste from the system clipboard to the terminal.
2892 */
2893hterm.Terminal.prototype.paste = function() {
Mike Frysinger4628ad22017-07-20 02:44:20 -04002894 return hterm.pasteFromClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002895};
2896
2897/**
2898 * Copy a string to the system clipboard.
2899 *
2900 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05002901 *
2902 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07002903 */
2904hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002905 if (this.prefs_.get('enable-clipboard-notice'))
2906 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2907
rgindaa09e7332012-08-17 12:49:51 -07002908 var copySource = this.document_.createElement('pre');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002909 copySource.id = 'hterm:copy-to-clipboard-source';
rginda4bba5e12012-06-20 16:15:30 -07002910 copySource.textContent = str;
2911 copySource.style.cssText = (
2912 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002913 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07002914 'position: absolute;' +
2915 'top: -99px');
2916
2917 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002918
rginda4bba5e12012-06-20 16:15:30 -07002919 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002920 var anchorNode = selection.anchorNode;
2921 var anchorOffset = selection.anchorOffset;
2922 var focusNode = selection.focusNode;
2923 var focusOffset = selection.focusOffset;
2924
rginda4bba5e12012-06-20 16:15:30 -07002925 selection.selectAllChildren(copySource);
2926
rgindaa09e7332012-08-17 12:49:51 -07002927 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002928
Rob Spies56953412014-04-28 14:09:47 -07002929 // IE doesn't support selection.extend. This means that the selection
2930 // won't return on IE.
Robert Ginda6f4f0aa2014-07-28 10:21:39 -07002931 if (selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07002932 selection.collapse(anchorNode, anchorOffset);
2933 selection.extend(focusNode, focusOffset);
2934 }
rgindafaa74742012-08-21 13:34:03 -07002935
rginda4bba5e12012-06-20 16:15:30 -07002936 copySource.parentNode.removeChild(copySource);
2937};
2938
Evan Jones2600d4f2016-12-06 09:29:36 -05002939/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04002940 * Display an image.
2941 *
2942 * @param {Object} options The image to display.
2943 * @param {string=} options.name A human readable string for the image.
2944 * @param {string|number=} options.size The size (in bytes).
2945 * @param {boolean=} options.preserveAspectRatio Whether to preserve aspect.
2946 * @param {boolean=} options.inline Whether to display the image inline.
2947 * @param {string|number=} options.width The width of the image.
2948 * @param {string|number=} options.height The height of the image.
2949 * @param {string=} options.align Direction to align the image.
2950 * @param {string} options.uri The source URI for the image.
2951 */
2952hterm.Terminal.prototype.displayImage = function(options) {
2953 // Make sure we're actually given a resource to display.
2954 if (options.uri === undefined)
2955 return;
2956
2957 // Set up the defaults to simplify code below.
2958 if (!options.name)
2959 options.name = '';
2960
2961 // Has the user approved image display yet?
2962 if (this.allowImagesInline !== true) {
2963 this.newLine();
2964 const row = this.getRowNode(this.scrollbackRows_.length +
2965 this.getCursorRow() - 1);
2966
2967 if (this.allowImagesInline === false) {
2968 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
2969 'Inline Images Disabled');
2970 return;
2971 }
2972
2973 // Show a prompt.
2974 let button;
2975 const span = this.document_.createElement('span');
2976 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
2977 span.style.fontWeight = 'bold';
2978 span.style.borderWidth = '1px';
2979 span.style.borderStyle = 'dashed';
2980 button = this.document_.createElement('span');
2981 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
2982 button.style.marginLeft = '1em';
2983 button.style.borderWidth = '1px';
2984 button.style.borderStyle = 'solid';
2985 button.addEventListener('click', () => {
2986 this.prefs_.set('allow-images-inline', false);
2987 });
2988 span.appendChild(button);
2989 button = this.document_.createElement('span');
2990 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
2991 'allow this session');
2992 button.style.marginLeft = '1em';
2993 button.style.borderWidth = '1px';
2994 button.style.borderStyle = 'solid';
2995 button.addEventListener('click', () => {
2996 this.allowImagesInline = true;
2997 });
2998 span.appendChild(button);
2999 button = this.document_.createElement('span');
3000 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3001 button.style.marginLeft = '1em';
3002 button.style.borderWidth = '1px';
3003 button.style.borderStyle = 'solid';
3004 button.addEventListener('click', () => {
3005 this.prefs_.set('allow-images-inline', true);
3006 });
3007 span.appendChild(button);
3008
3009 row.appendChild(span);
3010 return;
3011 }
3012
3013 // See if we should show this object directly, or download it.
3014 if (options.inline) {
3015 const io = this.io.push();
3016 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
3017 'Loading $1 ...'), null);
3018
3019 // While we're loading the image, eat all the user's input.
3020 io.onVTKeystroke = io.sendString = () => {};
3021
3022 // Initialize this new image.
3023 const img = this.document_.createElement('img');
3024 img.src = options.uri;
3025 img.title = img.alt = options.name;
3026
3027 // Attach the image to the page to let it load/render. It won't stay here.
3028 // This is needed so it's visible and the DOM can calculate the height. If
3029 // the image is hidden or not in the DOM, the height is always 0.
3030 this.document_.body.appendChild(img);
3031
3032 // Wait for the image to finish loading before we try moving it to the
3033 // right place in the terminal.
3034 img.onload = () => {
3035 // Now that we have the image dimensions, figure out how to show it.
3036 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
3037 img.style.maxWidth = `${this.document_.body.clientWidth}px`;
3038 img.style.maxHeight = `${this.document_.body.clientHeight}px`;
3039
3040 // Parse a width/height specification.
3041 const parseDim = (dim, maxDim, cssVar) => {
3042 if (!dim || dim == 'auto')
3043 return '';
3044
3045 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3046 if (ary) {
3047 if (ary[2] == '%')
3048 return maxDim * parseInt(ary[1]) / 100 + 'px';
3049 else if (ary[2] == 'px')
3050 return dim;
3051 else
3052 return `calc(${dim} * var(${cssVar}))`;
3053 }
3054
3055 return '';
3056 };
3057 img.style.width =
3058 parseDim(options.width, this.document_.body.clientWidth,
3059 '--hterm-charsize-width');
3060 img.style.height =
3061 parseDim(options.height, this.document_.body.clientHeight,
3062 '--hterm-charsize-height');
3063
3064 // Figure out how many rows the image occupies, then add that many.
3065 // XXX: This count will be inaccurate if the font size changes on us.
3066 const padRows = Math.ceil(img.clientHeight /
3067 this.scrollPort_.characterSize.height);
3068 for (let i = 0; i < padRows; ++i)
3069 this.newLine();
3070
3071 // Update the max height in case the user shrinks the character size.
3072 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3073
3074 // Move the image to the last row. This way when we scroll up, it doesn't
3075 // disappear when the first row gets clipped. It will disappear when we
3076 // scroll down and the last row is clipped ...
3077 this.document_.body.removeChild(img);
3078 // Create a wrapper node so we can do an absolute in a relative position.
3079 // This helps with rounding errors between JS & CSS counts.
3080 const div = this.document_.createElement('div');
3081 div.style.position = 'relative';
3082 div.style.textAlign = options.align;
3083 img.style.position = 'absolute';
3084 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3085 div.appendChild(img);
3086 const row = this.getRowNode(this.scrollbackRows_.length +
3087 this.getCursorRow() - 1);
3088 row.appendChild(div);
3089
3090 io.hideOverlay();
3091 io.pop();
3092 };
3093
3094 // If we got a malformed image, give up.
3095 img.onerror = (e) => {
3096 this.document_.body.removeChild(img);
3097 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
3098 'Loading $1 failed ...'));
3099 io.pop();
3100 };
3101 } else {
3102 // We can't use chrome.downloads.download as that requires "downloads"
3103 // permissions, and that works only in extensions, not apps.
3104 const a = this.document_.createElement('a');
3105 a.href = options.uri;
3106 a.download = options.name;
3107 this.document_.body.appendChild(a);
3108 a.click();
3109 a.remove();
3110 }
3111};
3112
3113/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003114 * Returns the selected text, or null if no text is selected.
3115 *
3116 * @return {string|null}
3117 */
rgindaa09e7332012-08-17 12:49:51 -07003118hterm.Terminal.prototype.getSelectionText = function() {
3119 var selection = this.scrollPort_.selection;
3120 selection.sync();
3121
3122 if (selection.isCollapsed)
3123 return null;
3124
3125
3126 // Start offset measures from the beginning of the line.
3127 var startOffset = selection.startOffset;
3128 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003129
Robert Gindafdbb3f22012-09-06 20:23:06 -07003130 if (node.nodeName != 'X-ROW') {
3131 // If the selection doesn't start on an x-row node, then it must be
3132 // somewhere inside the x-row. Add any characters from previous siblings
3133 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003134
3135 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3136 // If node is the text node in a styled span, move up to the span node.
3137 node = node.parentNode;
3138 }
3139
Robert Gindafdbb3f22012-09-06 20:23:06 -07003140 while (node.previousSibling) {
3141 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003142 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003143 }
rgindaa09e7332012-08-17 12:49:51 -07003144 }
3145
3146 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08003147 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
3148 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003149 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003150
Robert Gindafdbb3f22012-09-06 20:23:06 -07003151 if (node.nodeName != 'X-ROW') {
3152 // If the selection doesn't end on an x-row node, then it must be
3153 // somewhere inside the x-row. Add any characters from following siblings
3154 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003155
3156 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3157 // If node is the text node in a styled span, move up to the span node.
3158 node = node.parentNode;
3159 }
3160
Robert Gindafdbb3f22012-09-06 20:23:06 -07003161 while (node.nextSibling) {
3162 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003163 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003164 }
rgindaa09e7332012-08-17 12:49:51 -07003165 }
3166
3167 var rv = this.getRowsText(selection.startRow.rowIndex,
3168 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003169 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003170};
3171
rginda4bba5e12012-06-20 16:15:30 -07003172/**
3173 * Copy the current selection to the system clipboard, then clear it after a
3174 * short delay.
3175 */
3176hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07003177 var text = this.getSelectionText();
3178 if (text != null)
3179 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07003180};
3181
rgindaf0090c92012-02-10 14:58:52 -08003182hterm.Terminal.prototype.overlaySize = function() {
3183 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
3184};
3185
rginda87b86462011-12-14 13:48:03 -08003186/**
3187 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3188 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003189 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003190 */
3191hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08003192 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08003193 this.scrollPort_.scrollRowToBottom(this.getRowCount());
3194
Robert Ginda8cb7d902013-06-20 14:37:18 -07003195 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08003196};
3197
3198/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003199 * Open the selected url.
3200 */
3201hterm.Terminal.prototype.openSelectedUrl_ = function() {
3202 var str = this.getSelectionText();
3203
3204 // If there is no selection, try and expand wherever they clicked.
3205 if (str == null) {
3206 this.screen_.expandSelection(this.document_.getSelection());
3207 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003208
3209 // If clicking in empty space, return.
3210 if (str == null)
3211 return;
Mike Frysinger70b94692017-01-26 18:57:50 -10003212 }
3213
3214 // Make sure URL is valid before opening.
3215 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
3216 return;
Mike Frysinger43472622017-06-26 18:11:07 -04003217
3218 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003219 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003220 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3221 // We have to whitelist a few protocols that lack authorities and thus
3222 // never use the //. Like mailto.
3223 switch (str.split(':', 1)[0]) {
3224 case 'mailto':
3225 break;
3226 default:
3227 str = 'http://' + str;
3228 break;
3229 }
3230 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003231
Mike Frysinger720fa832017-10-23 01:15:52 -04003232 hterm.openUrl(str);
Mike Frysinger70b94692017-01-26 18:57:50 -10003233}
3234
3235
3236/**
rgindad5613292012-06-19 15:40:37 -07003237 * Add the terminalRow and terminalColumn properties to mouse events and
3238 * then forward on to onMouse().
3239 *
3240 * The terminalRow and terminalColumn properties contain the (row, column)
3241 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003242 *
3243 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003244 */
3245hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003246 if (e.processedByTerminalHandler_) {
3247 // We register our event handlers on the document, as well as the cursor
3248 // and the scroll blocker. Mouse events that occur on the cursor or
3249 // scroll blocker will also appear on the document, but we don't want to
3250 // process them twice.
3251 //
3252 // We can't just prevent bubbling because that has other side effects, so
3253 // we decorate the event object with this property instead.
3254 return;
3255 }
3256
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003257 var reportMouseEvents = (!this.defeatMouseReports_ &&
3258 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3259
rgindafaa74742012-08-21 13:34:03 -07003260 e.processedByTerminalHandler_ = true;
3261
Robert Gindaeda48db2014-07-17 09:25:30 -07003262 // One based row/column stored on the mouse event.
3263 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
3264 this.scrollPort_.characterSize.height) + 1;
3265 e.terminalColumn = parseInt(e.clientX /
3266 this.scrollPort_.characterSize.width) + 1;
3267
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003268 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3269 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003270 return;
3271 }
3272
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003273 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003274 // If the cursor is visible and we're not sending mouse events to the
3275 // host app, then we want to hide the terminal cursor when the mouse
3276 // cursor is over top. This keeps the terminal cursor from interfering
3277 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003278 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3279 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3280 this.cursorNode_.style.display = 'none';
3281 } else if (this.cursorNode_.style.display == 'none') {
3282 this.cursorNode_.style.display = '';
3283 }
3284 }
rgindad5613292012-06-19 15:40:37 -07003285
Robert Ginda928cf632014-03-05 15:07:41 -08003286 if (e.type == 'mousedown') {
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003287 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003288 // If VT mouse reporting is disabled, or has been defeated with
3289 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003290 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003291 this.setSelectionEnabled(true);
3292 } else {
3293 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003294 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003295 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003296 this.setSelectionEnabled(false);
3297 e.preventDefault();
3298 }
3299 }
3300
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003301 if (!reportMouseEvents) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003302 if (e.type == 'dblclick' && this.copyOnSelect) {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003303 this.screen_.expandSelection(this.document_.getSelection());
Robert Ginda15ed4902016-07-12 10:43:22 -07003304 this.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07003305 }
3306
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003307 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003308 // Debounce this event with the dblclick event. If you try to doubleclick
3309 // a URL to open it, Chrome will fire click then dblclick, but we won't
3310 // have expanded the selection text at the first click event.
3311 clearTimeout(this.timeouts_.openUrl);
3312 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3313 500);
3314 return;
3315 }
3316
Mike Frysinger847577f2017-05-23 23:25:57 -04003317 if (e.type == 'mousedown') {
3318 if ((this.mouseRightClickPaste && e.button == 2 /* right button */) ||
Mike Frysinger2edd3612017-05-24 00:54:39 -04003319 e.button == this.mousePasteButton) {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003320 if (!this.paste())
Mike Frysinger05a57f02017-08-27 17:48:55 -04003321 console.warn('Could not paste manually due to web restrictions');
Mike Frysinger847577f2017-05-23 23:25:57 -04003322 }
3323 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003324
Mike Frysinger2edd3612017-05-24 00:54:39 -04003325 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003326 !this.document_.getSelection().isCollapsed) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003327 this.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003328 }
3329
3330 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3331 this.scrollBlockerNode_.engaged) {
3332 // Disengage the scroll-blocker after one of these events.
3333 this.scrollBlockerNode_.engaged = false;
3334 this.scrollBlockerNode_.style.top = '-99px';
3335 }
3336
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003337 // Emulate arrow key presses via scroll wheel events.
3338 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3339 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003340 if (e.type == 'wheel') {
3341 var delta = this.scrollPort_.scrollWheelDelta(e);
3342 var lines = lib.f.smartFloorDivide(
3343 Math.abs(delta), this.scrollPort_.characterSize.height);
3344
3345 var data = '\x1bO' + (delta < 0 ? 'B' : 'A');
3346 this.io.sendString(data.repeat(lines));
3347
3348 e.preventDefault();
3349 }
3350 }
Robert Ginda928cf632014-03-05 15:07:41 -08003351 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003352 if (!this.scrollBlockerNode_.engaged) {
3353 if (e.type == 'mousedown') {
3354 // Move the scroll-blocker into place if we want to keep the scrollport
3355 // from scrolling.
3356 this.scrollBlockerNode_.engaged = true;
3357 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3358 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3359 } else if (e.type == 'mousemove') {
3360 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3361 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003362 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003363 e.preventDefault();
3364 }
3365 }
Robert Ginda928cf632014-03-05 15:07:41 -08003366
3367 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003368 }
3369
Robert Ginda928cf632014-03-05 15:07:41 -08003370 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3371 // Restore this on mouseup in case it was temporarily defeated with a
3372 // alt-mousedown. Only do this when the selection is empty so that
3373 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003374 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003375 }
rgindad5613292012-06-19 15:40:37 -07003376};
3377
3378/**
3379 * Clients should override this if they care to know about mouse events.
3380 *
3381 * The event parameter will be a normal DOM mouse click event with additional
3382 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003383 *
3384 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003385 */
3386hterm.Terminal.prototype.onMouse = function(e) { };
3387
3388/**
rginda8e92a692012-05-20 19:37:20 -07003389 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003390 *
3391 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003392 */
Rob Spies06533ba2014-04-24 11:20:37 -07003393hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3394 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003395 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003396
3397 if (this.reportFocus) {
3398 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O')
3399 }
3400
Michael Kelly485ecd12014-06-09 11:41:56 -04003401 if (focused === true)
3402 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003403};
3404
3405/**
rginda8ba33642011-12-14 12:31:31 -08003406 * React when the ScrollPort is scrolled.
3407 */
3408hterm.Terminal.prototype.onScroll_ = function() {
3409 this.scheduleSyncCursorPosition_();
3410};
3411
3412/**
rginda9846e2f2012-01-27 13:53:33 -08003413 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003414 *
3415 * @param {Event} e The DOM paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003416 */
3417hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07003418 var data = e.text.replace(/\n/mg, '\r');
Connor Hegartyf525ceb2014-09-03 13:36:03 -07003419 data = this.keyboard.encode(data);
Robert Gindaa063b202014-07-21 11:08:25 -07003420 if (this.options_.bracketedPaste)
3421 data = '\x1b[200~' + data + '\x1b[201~';
3422
3423 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003424};
3425
3426/**
rgindaa09e7332012-08-17 12:49:51 -07003427 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003428 *
3429 * @param {Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003430 */
3431hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003432 if (!this.useDefaultWindowCopy) {
3433 e.preventDefault();
3434 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3435 }
rgindaa09e7332012-08-17 12:49:51 -07003436};
3437
3438/**
rginda8ba33642011-12-14 12:31:31 -08003439 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003440 *
3441 * Note: This function should not directly contain code that alters the internal
3442 * state of the terminal. That kind of code belongs in realizeWidth or
3443 * realizeHeight, so that it can be executed synchronously in the case of a
3444 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003445 */
3446hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003447 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003448 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003449 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003450 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003451
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003452 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003453 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003454 // gets removed from the document or during the initial load, and we can't
3455 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003456 // This can also happen if called before the scrollPort calculates the
3457 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003458 return;
3459 }
3460
rgindaa8ba17d2012-08-15 14:41:10 -07003461 var isNewSize = (columnCount != this.screenSize.width ||
3462 rowCount != this.screenSize.height);
3463
3464 // We do this even if the size didn't change, just to be sure everything is
3465 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003466 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003467 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003468
3469 if (isNewSize)
3470 this.overlaySize();
3471
Robert Gindafb1be6a2013-12-11 11:56:22 -08003472 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003473 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08003474};
3475
3476/**
3477 * Service the cursor blink timeout.
3478 */
3479hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003480 if (!this.options_.cursorBlink) {
3481 delete this.timeouts_.cursorBlink;
3482 return;
3483 }
3484
Robert Ginda830583c2013-08-07 13:20:46 -07003485 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3486 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003487 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003488 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3489 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003490 } else {
rginda87b86462011-12-14 13:48:03 -08003491 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003492 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3493 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003494 }
3495};
David Reveman8f552492012-03-28 12:18:41 -04003496
3497/**
3498 * Set the scrollbar-visible mode bit.
3499 *
3500 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3501 * Otherwise it will not.
3502 *
3503 * Defaults to on.
3504 *
3505 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3506 */
3507hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3508 this.scrollPort_.setScrollbarVisible(state);
3509};
Michael Kelly485ecd12014-06-09 11:41:56 -04003510
3511/**
Rob Spies49039e52014-12-17 13:40:04 -08003512 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003513 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003514 *
3515 * Defaults to 1.
3516 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003517 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003518 */
3519hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3520 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3521};
3522
3523/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003524 * Close all web notifications created by terminal bells.
3525 */
3526hterm.Terminal.prototype.closeBellNotifications_ = function() {
3527 this.bellNotificationList_.forEach(function(n) {
3528 n.close();
3529 });
3530 this.bellNotificationList_.length = 0;
3531};