blob: a76fcf8ab105bc1fde9689d02e315b5755200ffa [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
Zhu Qunying30d40712017-03-14 16:27:00 -0700129 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800130 this.keyboard = new hterm.Keyboard(this);
131
rginda87b86462011-12-14 13:48:03 -0800132 // General IO interface that can be given to third parties without exposing
133 // the entire terminal object.
134 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800135
rgindad5613292012-06-19 15:40:37 -0700136 // True if mouse-click-drag should scroll the terminal.
137 this.enableMouseDragScroll = true;
138
Robert Ginda57f03b42012-09-13 11:02:48 -0700139 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400140 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700141 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700142
Zhu Qunying30d40712017-03-14 16:27:00 -0700143 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700144 this.useDefaultWindowCopy = false;
145
146 this.clearSelectionAfterCopy = true;
147
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400148 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800149 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700150
Gabriel Holodake8a09be2017-10-10 01:07:11 -0400151 this.reportFocus = false;
152
Robert Ginda57f03b42012-09-13 11:02:48 -0700153 this.setProfile(opt_profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500154 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800155};
156
157/**
Robert Ginda830583c2013-08-07 13:20:46 -0700158 * Possible cursor shapes.
159 */
160hterm.Terminal.cursorShape = {
161 BLOCK: 'BLOCK',
162 BEAM: 'BEAM',
163 UNDERLINE: 'UNDERLINE'
164};
165
166/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700167 * Clients should override this to be notified when the terminal is ready
168 * for use.
169 *
170 * The terminal initialization is asynchronous, and shouldn't be used before
171 * this method is called.
172 */
173hterm.Terminal.prototype.onTerminalReady = function() { };
174
175/**
rginda35c456b2012-02-09 17:29:05 -0800176 * Default tab with of 8 to match xterm.
177 */
178hterm.Terminal.prototype.tabWidth = 8;
179
180/**
rginda9f5222b2012-03-05 11:53:28 -0800181 * Select a preference profile.
182 *
183 * This will load the terminal preferences for the given profile name and
184 * associate subsequent preference changes with the new preference profile.
185 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500186 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800187 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700188 * @param {function} opt_callback Optional callback to invoke when the profile
189 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800190 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700191hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
192 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800193
Robert Ginda57f03b42012-09-13 11:02:48 -0700194 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800195
Robert Ginda57f03b42012-09-13 11:02:48 -0700196 if (this.prefs_)
197 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800198
Robert Ginda57f03b42012-09-13 11:02:48 -0700199 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
200 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800201 'alt-gr-mode': function(v) {
202 if (v == null) {
203 if (navigator.language.toLowerCase() == 'en-us') {
204 v = 'none';
205 } else {
206 v = 'right-alt';
207 }
208 } else if (typeof v == 'string') {
209 v = v.toLowerCase();
210 } else {
211 v = 'none';
212 }
213
214 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v))
215 v = 'none';
216
217 terminal.keyboard.altGrMode = v;
218 },
219
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700220 'alt-backspace-is-meta-backspace': function(v) {
221 terminal.keyboard.altBackspaceIsMetaBackspace = v;
222 },
223
Robert Ginda57f03b42012-09-13 11:02:48 -0700224 'alt-is-meta': function(v) {
225 terminal.keyboard.altIsMeta = v;
226 },
227
228 'alt-sends-what': function(v) {
229 if (!/^(escape|8-bit|browser-key)$/.test(v))
230 v = 'escape';
231
232 terminal.keyboard.altSendsWhat = v;
233 },
234
235 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800236 var ary = v.match(/^lib-resource:(\S+)/);
237 if (ary) {
238 terminal.bellAudio_.setAttribute('src',
239 lib.resource.getDataUrl(ary[1]));
240 } else {
241 terminal.bellAudio_.setAttribute('src', v);
242 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700243 },
244
Michael Kelly485ecd12014-06-09 11:41:56 -0400245 'desktop-notification-bell': function(v) {
246 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700247 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400248 Notification.permission === 'granted';
249 if (!terminal.desktopNotificationBell_) {
250 // Note: We don't call Notification.requestPermission here because
251 // Chrome requires the call be the result of a user action (such as an
252 // onclick handler), and pref listeners are run asynchronously.
253 //
254 // A way of working around this would be to display a dialog in the
255 // terminal with a "click-to-request-permission" button.
256 console.warn('desktop-notification-bell is true but we do not have ' +
257 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400258 }
259 } else {
260 terminal.desktopNotificationBell_ = false;
261 }
262 },
263
Robert Ginda57f03b42012-09-13 11:02:48 -0700264 'background-color': function(v) {
265 terminal.setBackgroundColor(v);
266 },
267
268 'background-image': function(v) {
269 terminal.scrollPort_.setBackgroundImage(v);
270 },
271
272 'background-size': function(v) {
273 terminal.scrollPort_.setBackgroundSize(v);
274 },
275
276 'background-position': function(v) {
277 terminal.scrollPort_.setBackgroundPosition(v);
278 },
279
280 'backspace-sends-backspace': function(v) {
281 terminal.keyboard.backspaceSendsBackspace = v;
282 },
283
Brad Town18654b62015-03-12 00:27:45 -0700284 'character-map-overrides': function(v) {
285 if (!(v == null || v instanceof Object)) {
286 console.warn('Preference character-map-modifications is not an ' +
287 'object: ' + v);
288 return;
289 }
290
Mike Frysinger095d4062017-06-14 00:29:48 -0700291 terminal.vt.characterMaps.reset();
292 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700293 },
294
Robert Ginda57f03b42012-09-13 11:02:48 -0700295 'cursor-blink': function(v) {
296 terminal.setCursorBlink(!!v);
297 },
298
Robert Gindaea2183e2014-07-17 09:51:51 -0700299 'cursor-blink-cycle': function(v) {
300 if (v instanceof Array &&
301 typeof v[0] == 'number' &&
302 typeof v[1] == 'number') {
303 terminal.cursorBlinkCycle_ = v;
304 } else if (typeof v == 'number') {
305 terminal.cursorBlinkCycle_ = [v, v];
306 } else {
307 // Fast blink indicates an error.
308 terminal.cursorBlinkCycle_ = [100, 100];
309 }
310 },
311
Robert Ginda57f03b42012-09-13 11:02:48 -0700312 'cursor-color': function(v) {
313 terminal.setCursorColor(v);
314 },
315
316 'color-palette-overrides': function(v) {
317 if (!(v == null || v instanceof Object || v instanceof Array)) {
318 console.warn('Preference color-palette-overrides is not an array or ' +
319 'object: ' + v);
320 return;
rginda9f5222b2012-03-05 11:53:28 -0800321 }
rginda9f5222b2012-03-05 11:53:28 -0800322
Robert Ginda57f03b42012-09-13 11:02:48 -0700323 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700324
Robert Ginda57f03b42012-09-13 11:02:48 -0700325 if (v) {
326 for (var key in v) {
327 var i = parseInt(key);
328 if (isNaN(i) || i < 0 || i > 255) {
329 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
330 continue;
331 }
332
333 if (v[i]) {
334 var rgb = lib.colors.normalizeCSS(v[i]);
335 if (rgb)
336 lib.colors.colorPalette[i] = rgb;
337 }
338 }
rginda30f20f62012-04-05 16:36:19 -0700339 }
rginda30f20f62012-04-05 16:36:19 -0700340
Evan Jones5f9df812016-12-06 09:38:58 -0500341 terminal.primaryScreen_.textAttributes.resetColorPalette();
Robert Ginda57f03b42012-09-13 11:02:48 -0700342 terminal.alternateScreen_.textAttributes.resetColorPalette();
343 },
rginda30f20f62012-04-05 16:36:19 -0700344
Robert Ginda57f03b42012-09-13 11:02:48 -0700345 'copy-on-select': function(v) {
346 terminal.copyOnSelect = !!v;
347 },
rginda9f5222b2012-03-05 11:53:28 -0800348
Rob Spies0bec09b2014-06-06 15:58:09 -0700349 'use-default-window-copy': function(v) {
350 terminal.useDefaultWindowCopy = !!v;
351 },
352
353 'clear-selection-after-copy': function(v) {
354 terminal.clearSelectionAfterCopy = !!v;
355 },
356
Robert Ginda7e5e9522014-03-14 12:23:58 -0700357 'ctrl-plus-minus-zero-zoom': function(v) {
358 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
359 },
360
Robert Gindafb5a3f92014-05-13 14:12:00 -0700361 'ctrl-c-copy': function(v) {
362 terminal.keyboard.ctrlCCopy = v;
363 },
364
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100365 'ctrl-v-paste': function(v) {
366 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700367 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100368 },
369
Masaya Suzuki273aa982014-05-31 07:25:55 +0900370 'east-asian-ambiguous-as-two-column': function(v) {
371 lib.wc.regardCjkAmbiguous = v;
372 },
373
Robert Ginda57f03b42012-09-13 11:02:48 -0700374 'enable-8-bit-control': function(v) {
375 terminal.vt.enable8BitControl = !!v;
376 },
rginda30f20f62012-04-05 16:36:19 -0700377
Robert Ginda57f03b42012-09-13 11:02:48 -0700378 'enable-bold': function(v) {
379 terminal.syncBoldSafeState();
380 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400381
Robert Ginda3e278d72014-03-25 13:18:51 -0700382 'enable-bold-as-bright': function(v) {
383 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
384 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
385 },
386
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400387 'enable-blink': function(v) {
388 terminal.syncBlinkState();
389 },
390
Robert Ginda57f03b42012-09-13 11:02:48 -0700391 'enable-clipboard-write': function(v) {
392 terminal.vt.enableClipboardWrite = !!v;
393 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400394
Robert Ginda3755e752013-05-31 13:34:09 -0700395 'enable-dec12': function(v) {
396 terminal.vt.enableDec12 = !!v;
397 },
398
Robert Ginda57f03b42012-09-13 11:02:48 -0700399 'font-family': function(v) {
400 terminal.syncFontFamily();
401 },
rginda30f20f62012-04-05 16:36:19 -0700402
Robert Ginda57f03b42012-09-13 11:02:48 -0700403 'font-size': function(v) {
404 terminal.setFontSize(v);
405 },
rginda9875d902012-08-20 16:21:57 -0700406
Robert Ginda57f03b42012-09-13 11:02:48 -0700407 'font-smoothing': function(v) {
408 terminal.syncFontFamily();
409 },
rgindade84e382012-04-20 15:39:31 -0700410
Robert Ginda57f03b42012-09-13 11:02:48 -0700411 'foreground-color': function(v) {
412 terminal.setForegroundColor(v);
413 },
rginda30f20f62012-04-05 16:36:19 -0700414
Robert Ginda57f03b42012-09-13 11:02:48 -0700415 'home-keys-scroll': function(v) {
416 terminal.keyboard.homeKeysScroll = v;
417 },
rginda4bba5e12012-06-20 16:15:30 -0700418
Robert Gindaa8165692015-06-15 14:46:31 -0700419 'keybindings': function(v) {
420 terminal.keyboard.bindings.clear();
421
422 if (!v)
423 return;
424
425 if (!(v instanceof Object)) {
426 console.error('Error in keybindings preference: Expected object');
427 return;
428 }
429
430 try {
431 terminal.keyboard.bindings.addBindings(v);
432 } catch (ex) {
433 console.error('Error in keybindings preference: ' + ex);
434 }
435 },
436
Robert Ginda57f03b42012-09-13 11:02:48 -0700437 'max-string-sequence': function(v) {
438 terminal.vt.maxStringSequence = v;
439 },
rginda11057d52012-04-25 12:29:56 -0700440
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700441 'media-keys-are-fkeys': function(v) {
442 terminal.keyboard.mediaKeysAreFKeys = v;
443 },
444
Robert Ginda57f03b42012-09-13 11:02:48 -0700445 'meta-sends-escape': function(v) {
446 terminal.keyboard.metaSendsEscape = v;
447 },
rginda30f20f62012-04-05 16:36:19 -0700448
Mike Frysinger847577f2017-05-23 23:25:57 -0400449 'mouse-right-click-paste': function(v) {
450 terminal.mouseRightClickPaste = v;
451 },
452
Robert Ginda57f03b42012-09-13 11:02:48 -0700453 'mouse-paste-button': function(v) {
454 terminal.syncMousePasteButton();
455 },
rgindaa8ba17d2012-08-15 14:41:10 -0700456
Robert Gindae76aa9f2014-03-14 12:29:12 -0700457 'page-keys-scroll': function(v) {
458 terminal.keyboard.pageKeysScroll = v;
459 },
460
Robert Ginda40932892012-12-10 17:26:40 -0800461 'pass-alt-number': function(v) {
462 if (v == null) {
463 var osx = window.navigator.userAgent.match(/Mac OS X/);
464
465 // Let Alt-1..9 pass to the browser (to control tab switching) on
466 // non-OS X systems, or if hterm is not opened in an app window.
467 v = (!osx && hterm.windowType != 'popup');
468 }
469
470 terminal.passAltNumber = v;
471 },
472
473 'pass-ctrl-number': function(v) {
474 if (v == null) {
475 var osx = window.navigator.userAgent.match(/Mac OS X/);
476
477 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
478 // non-OS X systems, or if hterm is not opened in an app window.
479 v = (!osx && hterm.windowType != 'popup');
480 }
481
482 terminal.passCtrlNumber = v;
483 },
484
485 'pass-meta-number': function(v) {
486 if (v == null) {
487 var osx = window.navigator.userAgent.match(/Mac OS X/);
488
489 // Let Meta-1..9 pass to the browser (to control tab switching) on
490 // OS X systems, or if hterm is not opened in an app window.
491 v = (osx && hterm.windowType != 'popup');
492 }
493
494 terminal.passMetaNumber = v;
495 },
496
Marius Schilder77857b32014-05-14 16:21:26 -0700497 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700498 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700499 },
500
Robert Ginda8cb7d902013-06-20 14:37:18 -0700501 'receive-encoding': function(v) {
502 if (!(/^(utf-8|raw)$/).test(v)) {
503 console.warn('Invalid value for "receive-encoding": ' + v);
504 v = 'utf-8';
505 }
506
507 terminal.vt.characterEncoding = v;
508 },
509
Robert Ginda57f03b42012-09-13 11:02:48 -0700510 'scroll-on-keystroke': function(v) {
511 terminal.scrollOnKeystroke_ = v;
512 },
rginda9f5222b2012-03-05 11:53:28 -0800513
Robert Ginda57f03b42012-09-13 11:02:48 -0700514 'scroll-on-output': function(v) {
515 terminal.scrollOnOutput_ = v;
516 },
rginda30f20f62012-04-05 16:36:19 -0700517
Robert Ginda57f03b42012-09-13 11:02:48 -0700518 'scrollbar-visible': function(v) {
519 terminal.setScrollbarVisible(v);
520 },
rginda9f5222b2012-03-05 11:53:28 -0800521
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400522 'scroll-wheel-may-send-arrow-keys': function(v) {
523 terminal.scrollWheelArrowKeys_ = v;
524 },
525
Rob Spies49039e52014-12-17 13:40:04 -0800526 'scroll-wheel-move-multiplier': function(v) {
527 terminal.setScrollWheelMoveMultipler(v);
528 },
529
Robert Ginda8cb7d902013-06-20 14:37:18 -0700530 'send-encoding': function(v) {
531 if (!(/^(utf-8|raw)$/).test(v)) {
532 console.warn('Invalid value for "send-encoding": ' + v);
533 v = 'utf-8';
534 }
535
536 terminal.keyboard.characterEncoding = v;
537 },
538
Robert Ginda57f03b42012-09-13 11:02:48 -0700539 'shift-insert-paste': function(v) {
540 terminal.keyboard.shiftInsertPaste = v;
541 },
rginda9f5222b2012-03-05 11:53:28 -0800542
Mike Frysingera7768922017-07-28 15:00:12 -0400543 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400544 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400545 },
546
Robert Gindae76aa9f2014-03-14 12:29:12 -0700547 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400548 terminal.scrollPort_.setUserCssUrl(v);
549 },
550
551 'user-css-text': function(v) {
552 terminal.scrollPort_.setUserCssText(v);
553 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400554
555 'word-break-match-left': function(v) {
556 terminal.primaryScreen_.wordBreakMatchLeft = v;
557 terminal.alternateScreen_.wordBreakMatchLeft = v;
558 },
559
560 'word-break-match-right': function(v) {
561 terminal.primaryScreen_.wordBreakMatchRight = v;
562 terminal.alternateScreen_.wordBreakMatchRight = v;
563 },
564
565 'word-break-match-middle': function(v) {
566 terminal.primaryScreen_.wordBreakMatchMiddle = v;
567 terminal.alternateScreen_.wordBreakMatchMiddle = v;
568 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700569 });
rginda30f20f62012-04-05 16:36:19 -0700570
Robert Ginda57f03b42012-09-13 11:02:48 -0700571 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800572 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700573
574 if (opt_callback)
575 opt_callback();
576 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800577};
578
Rob Spies56953412014-04-28 14:09:47 -0700579
580/**
581 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500582 *
583 * @return {hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700584 */
585hterm.Terminal.prototype.getPrefs = function() {
586 return this.prefs_;
587};
588
Robert Gindaa063b202014-07-21 11:08:25 -0700589/**
590 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500591 *
592 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700593 */
594hterm.Terminal.prototype.setBracketedPaste = function(state) {
595 this.options_.bracketedPaste = state;
596};
Rob Spies56953412014-04-28 14:09:47 -0700597
rginda8e92a692012-05-20 19:37:20 -0700598/**
599 * Set the color for the cursor.
600 *
601 * If you want this setting to persist, set it through prefs_, rather than
602 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500603 *
604 * @param {string} color The color to set.
rginda8e92a692012-05-20 19:37:20 -0700605 */
606hterm.Terminal.prototype.setCursorColor = function(color) {
Robert Ginda830583c2013-08-07 13:20:46 -0700607 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700608 this.cursorNode_.style.backgroundColor = color;
609 this.cursorNode_.style.borderColor = color;
610};
611
612/**
613 * Return the current cursor color as a string.
Evan Jones2600d4f2016-12-06 09:29:36 -0500614 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700615 */
616hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700617 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700618};
619
620/**
rgindad5613292012-06-19 15:40:37 -0700621 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500622 *
623 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700624 */
625hterm.Terminal.prototype.setSelectionEnabled = function(state) {
626 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700627};
628
629/**
rginda8e92a692012-05-20 19:37:20 -0700630 * Set the background color.
631 *
632 * If you want this setting to persist, set it through prefs_, rather than
633 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500634 *
635 * @param {string} color The color to set.
rginda8e92a692012-05-20 19:37:20 -0700636 */
637hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700638 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700639 this.primaryScreen_.textAttributes.setDefaults(
640 this.foregroundColor_, this.backgroundColor_);
641 this.alternateScreen_.textAttributes.setDefaults(
642 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700643 this.scrollPort_.setBackgroundColor(color);
644};
645
rginda9f5222b2012-03-05 11:53:28 -0800646/**
647 * Return the current terminal background color.
648 *
649 * Intended for use by other classes, so we don't have to expose the entire
650 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500651 *
652 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800653 */
654hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700655 return this.backgroundColor_;
656};
657
658/**
659 * Set the foreground color.
660 *
661 * If you want this setting to persist, set it through prefs_, rather than
662 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500663 *
664 * @param {string} color The color to set.
rginda8e92a692012-05-20 19:37:20 -0700665 */
666hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700667 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700668 this.primaryScreen_.textAttributes.setDefaults(
669 this.foregroundColor_, this.backgroundColor_);
670 this.alternateScreen_.textAttributes.setDefaults(
671 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700672 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800673};
674
675/**
676 * Return the current terminal foreground color.
677 *
678 * Intended for use by other classes, so we don't have to expose the entire
679 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500680 *
681 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800682 */
683hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700684 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800685};
686
687/**
rginda87b86462011-12-14 13:48:03 -0800688 * Create a new instance of a terminal command and run it with a given
689 * argument string.
690 *
691 * @param {function} commandClass The constructor for a terminal command.
692 * @param {string} argString The argument string to pass to the command.
693 */
694hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700695 var environment = this.prefs_.get('environment');
696 if (typeof environment != 'object' || environment == null)
697 environment = {};
698
rginda87b86462011-12-14 13:48:03 -0800699 var self = this;
700 this.command = new commandClass(
701 { argString: argString || '',
702 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700703 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800704 onExit: function(code) {
705 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800706 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700707 if (self.prefs_.get('close-on-exit'))
708 window.close();
rginda87b86462011-12-14 13:48:03 -0800709 }
710 });
711
rgindafeaf3142012-01-31 15:14:20 -0800712 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800713 this.command.run();
714};
715
716/**
rgindafeaf3142012-01-31 15:14:20 -0800717 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500718 *
719 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800720 */
721hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700722 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800723};
724
725/**
726 * Install the keyboard handler for this terminal.
727 *
728 * This will prevent the browser from seeing any keystrokes sent to the
729 * terminal.
730 */
731hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700732 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
rgindafeaf3142012-01-31 15:14:20 -0800733}
734
735/**
736 * Uninstall the keyboard handler for this terminal.
737 */
738hterm.Terminal.prototype.uninstallKeyboard = function() {
739 this.keyboard.installKeyboard(null);
740}
741
742/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400743 * Set a CSS variable.
744 *
745 * Normally this is used to set variables in the hterm namespace.
746 *
747 * @param {string} name The variable to set.
748 * @param {string} value The value to assign to the variable.
749 * @param {string?} opt_prefix The variable namespace/prefix to use.
750 */
751hterm.Terminal.prototype.setCssVar = function(name, value,
752 opt_prefix='--hterm-') {
753 this.document_.documentElement.style.setProperty(
754 `${opt_prefix}${name}`, value);
755};
756
757/**
rginda35c456b2012-02-09 17:29:05 -0800758 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800759 *
760 * Call setFontSize(0) to reset to the default font size.
761 *
762 * This function does not modify the font-size preference.
763 *
764 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800765 */
766hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800767 if (px === 0)
768 px = this.prefs_.get('font-size');
769
rginda35c456b2012-02-09 17:29:05 -0800770 this.scrollPort_.setFontSize(px);
Mike Frysingercce97c42017-08-05 01:11:22 -0400771 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
772 this.setCssVar('charsize-height',
773 this.scrollPort_.characterSize.height + 'px');
rginda35c456b2012-02-09 17:29:05 -0800774};
775
776/**
777 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500778 *
779 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800780 */
781hterm.Terminal.prototype.getFontSize = function() {
782 return this.scrollPort_.getFontSize();
783};
784
785/**
rginda8e92a692012-05-20 19:37:20 -0700786 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500787 *
788 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700789 */
790hterm.Terminal.prototype.getFontFamily = function() {
791 return this.scrollPort_.getFontFamily();
792};
793
794/**
rginda35c456b2012-02-09 17:29:05 -0800795 * Set the CSS "font-family" for this terminal.
796 */
rginda9f5222b2012-03-05 11:53:28 -0800797hterm.Terminal.prototype.syncFontFamily = function() {
798 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
799 this.prefs_.get('font-smoothing'));
800 this.syncBoldSafeState();
801};
802
rginda4bba5e12012-06-20 16:15:30 -0700803/**
804 * Set this.mousePasteButton based on the mouse-paste-button pref,
805 * autodetecting if necessary.
806 */
807hterm.Terminal.prototype.syncMousePasteButton = function() {
808 var button = this.prefs_.get('mouse-paste-button');
809 if (typeof button == 'number') {
810 this.mousePasteButton = button;
811 return;
812 }
813
814 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
Mike Frysinger98dd15b2017-05-18 22:52:23 -0400815 if (!ary || ary[1] == 'CrOS') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400816 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700817 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400818 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700819 }
820};
821
822/**
823 * Enable or disable bold based on the enable-bold pref, autodetecting if
824 * necessary.
825 */
rginda9f5222b2012-03-05 11:53:28 -0800826hterm.Terminal.prototype.syncBoldSafeState = function() {
827 var enableBold = this.prefs_.get('enable-bold');
828 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700829 this.primaryScreen_.textAttributes.enableBold = enableBold;
830 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800831 return;
832 }
833
rgindaf7521392012-02-28 17:20:34 -0800834 var normalSize = this.scrollPort_.measureCharacterSize();
835 var boldSize = this.scrollPort_.measureCharacterSize('bold');
836
837 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800838 if (!isBoldSafe) {
839 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700840 'from normal. Font family is: ' +
841 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800842 }
rginda9f5222b2012-03-05 11:53:28 -0800843
Robert Gindaed016262012-10-26 16:27:09 -0700844 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
845 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800846};
847
848/**
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400849 * Enable or disable blink based on the enable-blink pref.
850 */
851hterm.Terminal.prototype.syncBlinkState = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400852 this.setCssVar('node-duration',
853 this.prefs_.get('enable-blink') ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400854};
855
856/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400857 * Set the mouse cursor style based on the current terminal mode.
858 */
859hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400860 this.setCssVar('mouse-cursor-style',
861 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
862 'var(--hterm-mouse-cursor-text)' :
863 'var(--hterm-mouse-cursor-pointer)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400864};
865
866/**
rginda87b86462011-12-14 13:48:03 -0800867 * Return a copy of the current cursor position.
868 *
869 * @return {hterm.RowCol} The RowCol object representing the current position.
870 */
871hterm.Terminal.prototype.saveCursor = function() {
872 return this.screen_.cursorPosition.clone();
873};
874
Evan Jones2600d4f2016-12-06 09:29:36 -0500875/**
876 * Return the current text attributes.
877 *
878 * @return {string}
879 */
rgindaa19afe22012-01-25 15:40:22 -0800880hterm.Terminal.prototype.getTextAttributes = function() {
881 return this.screen_.textAttributes;
882};
883
Evan Jones2600d4f2016-12-06 09:29:36 -0500884/**
885 * Set the text attributes.
886 *
887 * @param {string} textAttributes The attributes to set.
888 */
rginda1a09aa02012-06-18 21:11:25 -0700889hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
890 this.screen_.textAttributes = textAttributes;
891};
892
rginda87b86462011-12-14 13:48:03 -0800893/**
rgindaf522ce02012-04-17 17:49:17 -0700894 * Return the current browser zoom factor applied to the terminal.
895 *
896 * @return {number} The current browser zoom factor.
897 */
898hterm.Terminal.prototype.getZoomFactor = function() {
899 return this.scrollPort_.characterSize.zoomFactor;
900};
901
902/**
rginda9846e2f2012-01-27 13:53:33 -0800903 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500904 *
905 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800906 */
907hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800908 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800909};
910
911/**
rginda87b86462011-12-14 13:48:03 -0800912 * Restore a previously saved cursor position.
913 *
914 * @param {hterm.RowCol} cursor The position to restore.
915 */
916hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700917 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
918 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800919 this.screen_.setCursorPosition(row, column);
920 if (cursor.column > column ||
921 cursor.column == column && cursor.overflow) {
922 this.screen_.cursorPosition.overflow = true;
923 }
rginda87b86462011-12-14 13:48:03 -0800924};
925
926/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400927 * Clear the cursor's overflow flag.
928 */
929hterm.Terminal.prototype.clearCursorOverflow = function() {
930 this.screen_.cursorPosition.overflow = false;
931};
932
933/**
Robert Ginda830583c2013-08-07 13:20:46 -0700934 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -0500935 *
936 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -0700937 */
938hterm.Terminal.prototype.setCursorShape = function(shape) {
939 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -0800940 this.restyleCursor_();
Robert Ginda830583c2013-08-07 13:20:46 -0700941}
942
943/**
944 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -0500945 *
946 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -0700947 */
948hterm.Terminal.prototype.getCursorShape = function() {
949 return this.cursorShape_;
950}
951
952/**
rginda87b86462011-12-14 13:48:03 -0800953 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -0500954 *
955 * @param {number} columnCount
rginda87b86462011-12-14 13:48:03 -0800956 */
957hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800958 if (columnCount == null) {
959 this.div_.style.width = '100%';
960 return;
961 }
962
Robert Ginda26806d12014-07-24 13:44:07 -0700963 this.div_.style.width = Math.ceil(
964 this.scrollPort_.characterSize.width *
965 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400966 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800967 this.scheduleSyncCursorPosition_();
968};
rginda87b86462011-12-14 13:48:03 -0800969
rgindac9bc5502012-01-18 11:48:44 -0800970/**
rginda35c456b2012-02-09 17:29:05 -0800971 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -0500972 *
973 * @param {number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -0800974 */
975hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800976 if (rowCount == null) {
977 this.div_.style.height = '100%';
978 return;
979 }
980
rginda35c456b2012-02-09 17:29:05 -0800981 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700982 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800983 this.realizeSize_(this.screenSize.width, rowCount);
984 this.scheduleSyncCursorPosition_();
985};
986
987/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400988 * Deal with terminal size changes.
989 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500990 * @param {number} columnCount The number of columns.
991 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400992 */
993hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
994 if (columnCount != this.screenSize.width)
995 this.realizeWidth_(columnCount);
996
997 if (rowCount != this.screenSize.height)
998 this.realizeHeight_(rowCount);
999
1000 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -07001001 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001002};
1003
1004/**
rgindac9bc5502012-01-18 11:48:44 -08001005 * Deal with terminal width changes.
1006 *
1007 * This function does what needs to be done when the terminal width changes
1008 * out from under us. It happens here rather than in onResize_() because this
1009 * code may need to run synchronously to handle programmatic changes of
1010 * terminal width.
1011 *
1012 * Relying on the browser to send us an async resize event means we may not be
1013 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001014 *
1015 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001016 */
1017hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001018 if (columnCount <= 0)
1019 throw new Error('Attempt to realize bad width: ' + columnCount);
1020
rgindac9bc5502012-01-18 11:48:44 -08001021 var deltaColumns = columnCount - this.screen_.getWidth();
1022
rginda87b86462011-12-14 13:48:03 -08001023 this.screenSize.width = columnCount;
1024 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001025
1026 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -04001027 if (this.defaultTabStops)
1028 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -08001029 } else {
1030 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -04001031 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -08001032 break;
1033
1034 this.tabStops_.pop();
1035 }
1036 }
1037
1038 this.screen_.setColumnCount(this.screenSize.width);
1039};
1040
1041/**
1042 * Deal with terminal height changes.
1043 *
1044 * This function does what needs to be done when the terminal height changes
1045 * out from under us. It happens here rather than in onResize_() because this
1046 * code may need to run synchronously to handle programmatic changes of
1047 * terminal height.
1048 *
1049 * Relying on the browser to send us an async resize event means we may not be
1050 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001051 *
1052 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001053 */
1054hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001055 if (rowCount <= 0)
1056 throw new Error('Attempt to realize bad height: ' + rowCount);
1057
rgindac9bc5502012-01-18 11:48:44 -08001058 var deltaRows = rowCount - this.screen_.getHeight();
1059
1060 this.screenSize.height = rowCount;
1061
1062 var cursor = this.saveCursor();
1063
1064 if (deltaRows < 0) {
1065 // Screen got smaller.
1066 deltaRows *= -1;
1067 while (deltaRows) {
1068 var lastRow = this.getRowCount() - 1;
1069 if (lastRow - this.scrollbackRows_.length == cursor.row)
1070 break;
1071
1072 if (this.getRowText(lastRow))
1073 break;
1074
1075 this.screen_.popRow();
1076 deltaRows--;
1077 }
1078
1079 var ary = this.screen_.shiftRows(deltaRows);
1080 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1081
1082 // We just removed rows from the top of the screen, we need to update
1083 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001084 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001085 } else if (deltaRows > 0) {
1086 // Screen got larger.
1087
1088 if (deltaRows <= this.scrollbackRows_.length) {
1089 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1090 var rows = this.scrollbackRows_.splice(
1091 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1092 this.screen_.unshiftRows(rows);
1093 deltaRows -= scrollbackCount;
1094 cursor.row += scrollbackCount;
1095 }
1096
1097 if (deltaRows)
1098 this.appendRows_(deltaRows);
1099 }
1100
rginda35c456b2012-02-09 17:29:05 -08001101 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001102 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001103};
1104
1105/**
1106 * Scroll the terminal to the top of the scrollback buffer.
1107 */
1108hterm.Terminal.prototype.scrollHome = function() {
1109 this.scrollPort_.scrollRowToTop(0);
1110};
1111
1112/**
1113 * Scroll the terminal to the end.
1114 */
1115hterm.Terminal.prototype.scrollEnd = function() {
1116 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1117};
1118
1119/**
1120 * Scroll the terminal one page up (minus one line) relative to the current
1121 * position.
1122 */
1123hterm.Terminal.prototype.scrollPageUp = function() {
1124 var i = this.scrollPort_.getTopRowIndex();
1125 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
1126};
1127
1128/**
1129 * Scroll the terminal one page down (minus one line) relative to the current
1130 * position.
1131 */
1132hterm.Terminal.prototype.scrollPageDown = function() {
1133 var i = this.scrollPort_.getTopRowIndex();
1134 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -08001135};
1136
rgindac9bc5502012-01-18 11:48:44 -08001137/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001138 * Scroll the terminal one line up relative to the current position.
1139 */
1140hterm.Terminal.prototype.scrollLineUp = function() {
1141 var i = this.scrollPort_.getTopRowIndex();
1142 this.scrollPort_.scrollRowToTop(i - 1);
1143};
1144
1145/**
1146 * Scroll the terminal one line down relative to the current position.
1147 */
1148hterm.Terminal.prototype.scrollLineDown = function() {
1149 var i = this.scrollPort_.getTopRowIndex();
1150 this.scrollPort_.scrollRowToTop(i + 1);
1151};
1152
1153/**
Robert Ginda40932892012-12-10 17:26:40 -08001154 * Clear primary screen, secondary screen, and the scrollback buffer.
1155 */
1156hterm.Terminal.prototype.wipeContents = function() {
1157 this.scrollbackRows_.length = 0;
1158 this.scrollPort_.resetCache();
1159
1160 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
1161 var bottom = screen.getHeight();
1162 if (bottom > 0) {
1163 this.renumberRows_(0, bottom);
1164 this.clearHome(screen);
1165 }
1166 }.bind(this));
1167
1168 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001169 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001170};
1171
1172/**
rgindac9bc5502012-01-18 11:48:44 -08001173 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001174 *
1175 * Perform a full reset to the default values listed in
1176 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001177 */
rginda87b86462011-12-14 13:48:03 -08001178hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001179 this.vt.reset();
1180
rgindac9bc5502012-01-18 11:48:44 -08001181 this.clearAllTabStops();
1182 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001183
Mike Frysingerd4cb2722017-11-25 12:14:13 -05001184 // We want to make sure to reset the attributes before we clear the screen.
1185 // The attributes might be used to initialize default/empty rows.
rginda9ea433c2012-03-16 11:57:00 -07001186 this.primaryScreen_.textAttributes.reset();
Mike Frysinger84301d02017-11-29 13:28:46 -08001187 this.primaryScreen_.textAttributes.resetColorPalette();
Mike Frysingerd4cb2722017-11-25 12:14:13 -05001188 this.clearHome(this.primaryScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001189
rginda9ea433c2012-03-16 11:57:00 -07001190 this.alternateScreen_.textAttributes.reset();
Mike Frysinger84301d02017-11-29 13:28:46 -08001191 this.alternateScreen_.textAttributes.resetColorPalette();
Mike Frysingerd4cb2722017-11-25 12:14:13 -05001192 this.clearHome(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001193
Mike Frysinger84301d02017-11-29 13:28:46 -08001194 // Reset terminal options to their default values.
1195 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001196 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1197
Mike Frysinger84301d02017-11-29 13:28:46 -08001198 this.setVTScrollRegion(null, null);
1199
1200 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001201};
1202
rgindac9bc5502012-01-18 11:48:44 -08001203/**
1204 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001205 *
1206 * Perform a soft reset to the default values listed in
1207 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001208 */
rginda0f5c0292012-01-13 11:00:13 -08001209hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001210 this.vt.reset();
1211
rgindab8bc8932012-04-27 12:45:03 -07001212 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001213 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001214
Brad Townb62dfdc2015-03-16 19:07:15 -07001215 // We show the cursor on soft reset but do not alter the blink state.
1216 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1217
rgindab8bc8932012-04-27 12:45:03 -07001218 // Xterm also resets the color palette on soft reset, even though it doesn't
1219 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -07001220 this.primaryScreen_.textAttributes.resetColorPalette();
1221 this.alternateScreen_.textAttributes.resetColorPalette();
1222
rgindab8bc8932012-04-27 12:45:03 -07001223 // The xterm man page explicitly says this will happen on soft reset.
1224 this.setVTScrollRegion(null, null);
1225
1226 // Xterm also shows the cursor on soft reset, but does not alter the blink
1227 // state.
rgindaa19afe22012-01-25 15:40:22 -08001228 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001229};
1230
rgindac9bc5502012-01-18 11:48:44 -08001231/**
1232 * Move the cursor forward to the next tab stop, or to the last column
1233 * if no more tab stops are set.
1234 */
1235hterm.Terminal.prototype.forwardTabStop = function() {
1236 var column = this.screen_.cursorPosition.column;
1237
1238 for (var i = 0; i < this.tabStops_.length; i++) {
1239 if (this.tabStops_[i] > column) {
1240 this.setCursorColumn(this.tabStops_[i]);
1241 return;
1242 }
1243 }
1244
David Benjamin66e954d2012-05-05 21:08:12 -04001245 // xterm does not clear the overflow flag on HT or CHT.
1246 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001247 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001248 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001249};
1250
rgindac9bc5502012-01-18 11:48:44 -08001251/**
1252 * Move the cursor backward to the previous tab stop, or to the first column
1253 * if no previous tab stops are set.
1254 */
1255hterm.Terminal.prototype.backwardTabStop = function() {
1256 var column = this.screen_.cursorPosition.column;
1257
1258 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1259 if (this.tabStops_[i] < column) {
1260 this.setCursorColumn(this.tabStops_[i]);
1261 return;
1262 }
1263 }
1264
1265 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001266};
1267
rgindac9bc5502012-01-18 11:48:44 -08001268/**
1269 * Set a tab stop at the given column.
1270 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001271 * @param {integer} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001272 */
1273hterm.Terminal.prototype.setTabStop = function(column) {
1274 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1275 if (this.tabStops_[i] == column)
1276 return;
1277
1278 if (this.tabStops_[i] < column) {
1279 this.tabStops_.splice(i + 1, 0, column);
1280 return;
1281 }
1282 }
1283
1284 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001285};
1286
rgindac9bc5502012-01-18 11:48:44 -08001287/**
1288 * Clear the tab stop at the current cursor position.
1289 *
1290 * No effect if there is no tab stop at the current cursor position.
1291 */
1292hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1293 var column = this.screen_.cursorPosition.column;
1294
1295 var i = this.tabStops_.indexOf(column);
1296 if (i == -1)
1297 return;
1298
1299 this.tabStops_.splice(i, 1);
1300};
1301
1302/**
1303 * Clear all tab stops.
1304 */
1305hterm.Terminal.prototype.clearAllTabStops = function() {
1306 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001307 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001308};
1309
1310/**
1311 * Set up the default tab stops, starting from a given column.
1312 *
1313 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001314 * from the specified column, or 0 if no column is provided. It also flags
1315 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001316 *
1317 * This does not clear the existing tab stops first, use clearAllTabStops
1318 * for that.
1319 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001320 * @param {integer} opt_start Optional starting zero based starting column, useful
rgindac9bc5502012-01-18 11:48:44 -08001321 * for filling out missing tab stops when the terminal is resized.
1322 */
1323hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1324 var start = opt_start || 0;
1325 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001326 // Round start up to a default tab stop.
1327 start = start - 1 - ((start - 1) % w) + w;
1328 for (var i = start; i < this.screenSize.width; i += w) {
1329 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001330 }
David Benjamin66e954d2012-05-05 21:08:12 -04001331
1332 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001333};
1334
rginda6d397402012-01-17 10:58:29 -08001335/**
rginda8ba33642011-12-14 12:31:31 -08001336 * Interpret a sequence of characters.
1337 *
1338 * Incomplete escape sequences are buffered until the next call.
1339 *
1340 * @param {string} str Sequence of characters to interpret or pass through.
1341 */
1342hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001343 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001344 this.scheduleSyncCursorPosition_();
1345};
1346
1347/**
1348 * Take over the given DIV for use as the terminal display.
1349 *
1350 * @param {HTMLDivElement} div The div to use as the terminal display.
1351 */
1352hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001353 this.div_ = div;
1354
rginda8ba33642011-12-14 12:31:31 -08001355 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001356 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001357 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1358 this.scrollPort_.setBackgroundPosition(
1359 this.prefs_.get('background-position'));
Mike Frysinger08bad432017-04-24 00:50:54 -04001360 this.scrollPort_.setUserCssUrl(this.prefs_.get('user-css'));
1361 this.scrollPort_.setUserCssText(this.prefs_.get('user-css-text'));
rginda30f20f62012-04-05 16:36:19 -07001362
rginda0918b652012-04-04 11:26:24 -07001363 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001364
rginda9f5222b2012-03-05 11:53:28 -08001365 this.setFontSize(this.prefs_.get('font-size'));
1366 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001367
David Reveman8f552492012-03-28 12:18:41 -04001368 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001369 this.setScrollWheelMoveMultipler(
1370 this.prefs_.get('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001371
rginda8ba33642011-12-14 12:31:31 -08001372 this.document_ = this.scrollPort_.getDocument();
1373
Evan Jones5f9df812016-12-06 09:38:58 -05001374 this.document_.body.oncontextmenu = function() { return false; };
rginda4bba5e12012-06-20 16:15:30 -07001375
1376 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001377 var screenNode = this.scrollPort_.getScreenNode();
1378 screenNode.addEventListener('mousedown', onMouse);
1379 screenNode.addEventListener('mouseup', onMouse);
1380 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001381 this.scrollPort_.onScrollWheel = onMouse;
1382
Toni Barzic0bfa8922013-11-22 11:18:35 -08001383 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001384 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001385 // Listen for mousedown events on the screenNode as in FF the focus
1386 // events don't bubble.
1387 screenNode.addEventListener('mousedown', function() {
1388 setTimeout(this.onFocusChange_.bind(this, true));
1389 }.bind(this));
1390
Toni Barzic0bfa8922013-11-22 11:18:35 -08001391 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001392 'blur', this.onFocusChange_.bind(this, false));
1393
1394 var style = this.document_.createElement('style');
1395 style.textContent =
1396 ('.cursor-node[focus="false"] {' +
1397 ' box-sizing: border-box;' +
1398 ' background-color: transparent !important;' +
1399 ' border-width: 2px;' +
1400 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001401 '}' +
1402 '.wc-node {' +
1403 ' display: inline-block;' +
1404 ' text-align: center;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001405 ' width: calc(var(--hterm-charsize-width) * 2);' +
Mike Frysinger4036f6e2017-05-31 14:02:55 -04001406 ' line-height: var(--hterm-charsize-height);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001407 '}' +
1408 ':root {' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001409 ' --hterm-charsize-width: ' + this.scrollPort_.characterSize.width + 'px;' +
1410 ' --hterm-charsize-height: ' + this.scrollPort_.characterSize.height + 'px;' +
Mike Frysingera27c0502017-08-23 22:37:10 -04001411 // Default position hides the cursor for when the window is initializing.
1412 ' --hterm-cursor-offset-col: -1;' +
1413 ' --hterm-cursor-offset-row: -1;' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001414 ' --hterm-blink-node-duration: 0.7s;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001415 ' --hterm-mouse-cursor-text: text;' +
1416 ' --hterm-mouse-cursor-pointer: default;' +
1417 ' --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001418 '}' +
1419 '@keyframes blink {' +
1420 ' from { opacity: 1.0; }' +
1421 ' to { opacity: 0.0; }' +
1422 '}' +
1423 '.blink-node {' +
1424 ' animation-name: blink;' +
1425 ' animation-duration: var(--hterm-blink-node-duration);' +
1426 ' animation-iteration-count: infinite;' +
1427 ' animation-timing-function: ease-in-out;' +
1428 ' animation-direction: alternate;' +
rginda8e92a692012-05-20 19:37:20 -07001429 '}');
1430 this.document_.head.appendChild(style);
1431
rginda8ba33642011-12-14 12:31:31 -08001432 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001433 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001434 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001435 this.cursorNode_.style.cssText =
1436 ('position: absolute;' +
Mike Frysinger44c32202017-08-05 01:13:09 -04001437 'left: calc(var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));' +
1438 'top: calc(var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));' +
rginda87b86462011-12-14 13:48:03 -08001439 'display: block;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001440 'width: var(--hterm-charsize-width);' +
1441 'height: var(--hterm-charsize-height);' +
Rob Spies06533ba2014-04-24 11:20:37 -07001442 '-webkit-transition: opacity, background-color 100ms linear;' +
1443 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001444
rginda8e92a692012-05-20 19:37:20 -07001445 this.setCursorColor(this.prefs_.get('cursor-color'));
Robert Gindafb1be6a2013-12-11 11:56:22 -08001446 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1447 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001448
rginda8ba33642011-12-14 12:31:31 -08001449 this.document_.body.appendChild(this.cursorNode_);
1450
rgindad5613292012-06-19 15:40:37 -07001451 // When 'enableMouseDragScroll' is off we reposition this element directly
1452 // under the mouse cursor after a click. This makes Chrome associate
1453 // subsequent mousemove events with the scroll-blocker. Since the
1454 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1455 // events do not cause the scrollport to scroll.
1456 //
1457 // It's a hack, but it's the cleanest way I could find.
1458 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001459 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
rgindad5613292012-06-19 15:40:37 -07001460 this.scrollBlockerNode_.style.cssText =
1461 ('position: absolute;' +
1462 'top: -99px;' +
1463 'display: block;' +
1464 'width: 10px;' +
1465 'height: 10px;');
1466 this.document_.body.appendChild(this.scrollBlockerNode_);
1467
rgindad5613292012-06-19 15:40:37 -07001468 this.scrollPort_.onScrollWheel = onMouse;
1469 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1470 ].forEach(function(event) {
1471 this.scrollBlockerNode_.addEventListener(event, onMouse);
1472 this.cursorNode_.addEventListener(event, onMouse);
1473 this.document_.addEventListener(event, onMouse);
1474 }.bind(this));
1475
1476 this.cursorNode_.addEventListener('mousedown', function() {
1477 setTimeout(this.focus.bind(this));
1478 }.bind(this));
1479
rginda8ba33642011-12-14 12:31:31 -08001480 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001481
rginda87b86462011-12-14 13:48:03 -08001482 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001483 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001484};
1485
rginda0918b652012-04-04 11:26:24 -07001486/**
1487 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001488 *
1489 * @return {HTMLDocument}
rginda0918b652012-04-04 11:26:24 -07001490 */
rginda87b86462011-12-14 13:48:03 -08001491hterm.Terminal.prototype.getDocument = function() {
1492 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001493};
1494
1495/**
rginda0918b652012-04-04 11:26:24 -07001496 * Focus the terminal.
1497 */
1498hterm.Terminal.prototype.focus = function() {
1499 this.scrollPort_.focus();
1500};
1501
1502/**
rginda8ba33642011-12-14 12:31:31 -08001503 * Return the HTML Element for a given row index.
1504 *
1505 * This is a method from the RowProvider interface. The ScrollPort uses
1506 * it to fetch rows on demand as they are scrolled into view.
1507 *
1508 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1509 * pairs to conserve memory.
1510 *
1511 * @param {integer} index The zero-based row index, measured relative to the
1512 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001513 * largest indices.
rginda8ba33642011-12-14 12:31:31 -08001514 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1515 */
1516hterm.Terminal.prototype.getRowNode = function(index) {
1517 if (index < this.scrollbackRows_.length)
1518 return this.scrollbackRows_[index];
1519
1520 var screenIndex = index - this.scrollbackRows_.length;
1521 return this.screen_.rowsArray[screenIndex];
1522};
1523
1524/**
1525 * Return the text content for a given range of rows.
1526 *
1527 * This is a method from the RowProvider interface. The ScrollPort uses
1528 * it to fetch text content on demand when the user attempts to copy their
1529 * selection to the clipboard.
1530 *
1531 * @param {integer} start The zero-based row index to start from, measured
1532 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001533 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001534 * @param {integer} end The zero-based row index to end on, measured
1535 * relative to the start of the scrollback buffer.
1536 * @return {string} A single string containing the text value of the range of
1537 * rows. Lines will be newline delimited, with no trailing newline.
1538 */
1539hterm.Terminal.prototype.getRowsText = function(start, end) {
1540 var ary = [];
1541 for (var i = start; i < end; i++) {
1542 var node = this.getRowNode(i);
1543 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001544 if (i < end - 1 && !node.getAttribute('line-overflow'))
1545 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001546 }
1547
rgindaa09e7332012-08-17 12:49:51 -07001548 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001549};
1550
1551/**
1552 * Return the text content for a given row.
1553 *
1554 * This is a method from the RowProvider interface. The ScrollPort uses
1555 * it to fetch text content on demand when the user attempts to copy their
1556 * selection to the clipboard.
1557 *
1558 * @param {integer} index The zero-based row index to return, measured
1559 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001560 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001561 * @return {string} A string containing the text value of the selected row.
1562 */
1563hterm.Terminal.prototype.getRowText = function(index) {
1564 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001565 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001566};
1567
1568/**
1569 * Return the total number of rows in the addressable screen and in the
1570 * scrollback buffer of this terminal.
1571 *
1572 * This is a method from the RowProvider interface. The ScrollPort uses
1573 * it to compute the size of the scrollbar.
1574 *
1575 * @return {integer} The number of rows in this terminal.
1576 */
1577hterm.Terminal.prototype.getRowCount = function() {
1578 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1579};
1580
1581/**
1582 * Create DOM nodes for new rows and append them to the end of the terminal.
1583 *
1584 * This is the only correct way to add a new DOM node for a row. Notice that
1585 * the new row is appended to the bottom of the list of rows, and does not
1586 * require renumbering (of the rowIndex property) of previous rows.
1587 *
1588 * If you think you want a new blank row somewhere in the middle of the
1589 * terminal, look into moveRows_().
1590 *
1591 * This method does not pay attention to vtScrollTop/Bottom, since you should
1592 * be using moveRows() in cases where they would matter.
1593 *
1594 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001595 *
1596 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001597 */
1598hterm.Terminal.prototype.appendRows_ = function(count) {
1599 var cursorRow = this.screen_.rowsArray.length;
1600 var offset = this.scrollbackRows_.length + cursorRow;
1601 for (var i = 0; i < count; i++) {
1602 var row = this.document_.createElement('x-row');
1603 row.appendChild(this.document_.createTextNode(''));
1604 row.rowIndex = offset + i;
1605 this.screen_.pushRow(row);
1606 }
1607
1608 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1609 if (extraRows > 0) {
1610 var ary = this.screen_.shiftRows(extraRows);
1611 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001612 if (this.scrollPort_.isScrolledEnd)
1613 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001614 }
1615
1616 if (cursorRow >= this.screen_.rowsArray.length)
1617 cursorRow = this.screen_.rowsArray.length - 1;
1618
rginda87b86462011-12-14 13:48:03 -08001619 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001620};
1621
1622/**
1623 * Relocate rows from one part of the addressable screen to another.
1624 *
1625 * This is used to recycle rows during VT scrolls (those which are driven
1626 * by VT commands, rather than by the user manipulating the scrollbar.)
1627 *
1628 * In this case, the blank lines scrolled into the scroll region are made of
1629 * the nodes we scrolled off. These have their rowIndex properties carefully
1630 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001631 *
1632 * @param {number} fromIndex The start index.
1633 * @param {number} count The number of rows to move.
1634 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001635 */
1636hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1637 var ary = this.screen_.removeRows(fromIndex, count);
1638 this.screen_.insertRows(toIndex, ary);
1639
1640 var start, end;
1641 if (fromIndex < toIndex) {
1642 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001643 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001644 } else {
1645 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001646 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001647 }
1648
1649 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001650 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001651};
1652
1653/**
1654 * Renumber the rowIndex property of the given range of rows.
1655 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001656 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001657 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001658 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001659 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001660 *
1661 * @param {number} start The start index.
1662 * @param {number} end The end index.
1663 * @param {hterm.Screen} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001664 */
Robert Ginda40932892012-12-10 17:26:40 -08001665hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1666 var screen = opt_screen || this.screen_;
1667
rginda8ba33642011-12-14 12:31:31 -08001668 var offset = this.scrollbackRows_.length;
1669 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001670 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001671 }
1672};
1673
1674/**
1675 * Print a string to the terminal.
1676 *
1677 * This respects the current insert and wraparound modes. It will add new lines
1678 * to the end of the terminal, scrolling off the top into the scrollback buffer
1679 * if necessary.
1680 *
1681 * The string is *not* parsed for escape codes. Use the interpret() method if
1682 * that's what you're after.
1683 *
1684 * @param{string} str The string to print.
1685 */
1686hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001687 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001688
Ricky Liang48f05cb2013-12-31 23:35:29 +08001689 var strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04001690 // Fun edge case: If the string only contains zero width codepoints (like
1691 // combining characters), we make sure to iterate at least once below.
1692 if (strWidth == 0 && str)
1693 strWidth = 1;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001694
1695 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001696 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1697 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001698 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001699 }
rgindaa19afe22012-01-25 15:40:22 -08001700
Ricky Liang48f05cb2013-12-31 23:35:29 +08001701 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001702 var didOverflow = false;
1703 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001704
rgindaa9abdd82012-08-06 18:05:09 -07001705 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1706 didOverflow = true;
1707 count = this.screenSize.width - this.screen_.cursorPosition.column;
1708 }
rgindaa19afe22012-01-25 15:40:22 -08001709
rgindaa9abdd82012-08-06 18:05:09 -07001710 if (didOverflow && !this.options_.wraparound) {
1711 // If the string overflowed the line but wraparound is off, then the
1712 // last printed character should be the last of the string.
1713 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001714 substr = lib.wc.substr(str, startOffset, count - 1) +
1715 lib.wc.substr(str, strWidth - 1);
1716 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001717 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001718 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001719 }
rgindaa19afe22012-01-25 15:40:22 -08001720
Ricky Liang48f05cb2013-12-31 23:35:29 +08001721 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1722 for (var i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001723 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
1724 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001725
1726 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001727 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001728 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001729 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001730 }
1731 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001732 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07001733 }
1734
1735 this.screen_.maybeClipCurrentRow();
1736 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001737 }
rginda8ba33642011-12-14 12:31:31 -08001738
1739 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001740
rginda9f5222b2012-03-05 11:53:28 -08001741 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001742 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001743};
1744
1745/**
rginda87b86462011-12-14 13:48:03 -08001746 * Set the VT scroll region.
1747 *
rginda87b86462011-12-14 13:48:03 -08001748 * This also resets the cursor position to the absolute (0, 0) position, since
1749 * that's what xterm appears to do.
1750 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001751 * Setting the scroll region to the full height of the terminal will clear
1752 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1753 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1754 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1755 * continue to work as most users would expect.
1756 *
rginda87b86462011-12-14 13:48:03 -08001757 * @param {integer} scrollTop The zero-based top of the scroll region.
1758 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1759 * inclusive.
1760 */
1761hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001762 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001763 this.vtScrollTop_ = null;
1764 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001765 } else {
1766 this.vtScrollTop_ = scrollTop;
1767 this.vtScrollBottom_ = scrollBottom;
1768 }
rginda87b86462011-12-14 13:48:03 -08001769};
1770
1771/**
rginda8ba33642011-12-14 12:31:31 -08001772 * Return the top row index according to the VT.
1773 *
1774 * This will return 0 unless the terminal has been told to restrict scrolling
1775 * to some lower row. It is used for some VT cursor positioning and scrolling
1776 * commands.
1777 *
1778 * @return {integer} The topmost row in the terminal's scroll region.
1779 */
1780hterm.Terminal.prototype.getVTScrollTop = function() {
1781 if (this.vtScrollTop_ != null)
1782 return this.vtScrollTop_;
1783
1784 return 0;
rginda87b86462011-12-14 13:48:03 -08001785};
rginda8ba33642011-12-14 12:31:31 -08001786
1787/**
1788 * Return the bottom row index according to the VT.
1789 *
1790 * This will return the height of the terminal unless the it has been told to
1791 * restrict scrolling to some higher row. It is used for some VT cursor
1792 * positioning and scrolling commands.
1793 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001794 * @return {integer} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001795 */
1796hterm.Terminal.prototype.getVTScrollBottom = function() {
1797 if (this.vtScrollBottom_ != null)
1798 return this.vtScrollBottom_;
1799
rginda87b86462011-12-14 13:48:03 -08001800 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001801}
1802
1803/**
1804 * Process a '\n' character.
1805 *
1806 * If the cursor is on the final row of the terminal this will append a new
1807 * blank row to the screen and scroll the topmost row into the scrollback
1808 * buffer.
1809 *
1810 * Otherwise, this moves the cursor to column zero of the next row.
1811 */
1812hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001813 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1814 this.screen_.rowsArray.length - 1);
1815
1816 if (this.vtScrollBottom_ != null) {
1817 // A VT Scroll region is active, we never append new rows.
1818 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1819 // We're at the end of the VT Scroll Region, perform a VT scroll.
1820 this.vtScrollUp(1);
1821 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1822 } else if (cursorAtEndOfScreen) {
1823 // We're at the end of the screen, the only thing to do is put the
1824 // cursor to column 0.
1825 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1826 } else {
1827 // Anywhere else, advance the cursor row, and reset the column.
1828 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1829 }
1830 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001831 // We're at the end of the screen. Append a new row to the terminal,
1832 // shifting the top row into the scrollback.
1833 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001834 } else {
rginda87b86462011-12-14 13:48:03 -08001835 // Anywhere else in the screen just moves the cursor.
1836 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001837 }
1838};
1839
1840/**
1841 * Like newLine(), except maintain the cursor column.
1842 */
1843hterm.Terminal.prototype.lineFeed = function() {
1844 var column = this.screen_.cursorPosition.column;
1845 this.newLine();
1846 this.setCursorColumn(column);
1847};
1848
1849/**
rginda87b86462011-12-14 13:48:03 -08001850 * If autoCarriageReturn is set then newLine(), else lineFeed().
1851 */
1852hterm.Terminal.prototype.formFeed = function() {
1853 if (this.options_.autoCarriageReturn) {
1854 this.newLine();
1855 } else {
1856 this.lineFeed();
1857 }
1858};
1859
1860/**
1861 * Move the cursor up one row, possibly inserting a blank line.
1862 *
1863 * The cursor column is not changed.
1864 */
1865hterm.Terminal.prototype.reverseLineFeed = function() {
1866 var scrollTop = this.getVTScrollTop();
1867 var currentRow = this.screen_.cursorPosition.row;
1868
1869 if (currentRow == scrollTop) {
1870 this.insertLines(1);
1871 } else {
1872 this.setAbsoluteCursorRow(currentRow - 1);
1873 }
1874};
1875
1876/**
rginda8ba33642011-12-14 12:31:31 -08001877 * Replace all characters to the left of the current cursor with the space
1878 * character.
1879 *
1880 * TODO(rginda): This should probably *remove* the characters (not just replace
1881 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001882 * position.
rginda8ba33642011-12-14 12:31:31 -08001883 */
1884hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001885 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001886 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04001887 const count = cursor.column + 1;
1888 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08001889 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001890};
1891
1892/**
David Benjamin684a9b72012-05-01 17:19:58 -04001893 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001894 *
1895 * The cursor position is unchanged.
1896 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001897 * If the current background color is not the default background color this
1898 * will insert spaces rather than delete. This is unfortunate because the
1899 * trailing space will affect text selection, but it's difficult to come up
1900 * with a way to style empty space that wouldn't trip up the hterm.Screen
1901 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001902 *
1903 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1904 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1905 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05001906 *
1907 * @param {number} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08001908 */
1909hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001910 if (this.screen_.cursorPosition.overflow)
1911 return;
1912
Robert Ginda7fd57082012-09-25 14:41:47 -07001913 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1914 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001915
1916 if (this.screen_.textAttributes.background ===
1917 this.screen_.textAttributes.DEFAULT_COLOR) {
1918 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08001919 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07001920 this.screen_.cursorPosition.column + count) {
1921 this.screen_.deleteChars(count);
1922 this.clearCursorOverflow();
1923 return;
1924 }
1925 }
1926
rginda87b86462011-12-14 13:48:03 -08001927 var cursor = this.saveCursor();
Mike Frysinger6380bed2017-08-24 18:46:39 -04001928 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08001929 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001930 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001931};
1932
1933/**
1934 * Erase the current line.
1935 *
1936 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001937 */
1938hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001939 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001940 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001941 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001942 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001943};
1944
1945/**
David Benjamina08d78f2012-05-05 00:28:49 -04001946 * Erase all characters from the start of the screen to the current cursor
1947 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001948 *
1949 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001950 */
1951hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001952 var cursor = this.saveCursor();
1953
1954 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001955
David Benjamina08d78f2012-05-05 00:28:49 -04001956 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001957 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001958 this.screen_.clearCursorRow();
1959 }
1960
rginda87b86462011-12-14 13:48:03 -08001961 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001962 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001963};
1964
1965/**
1966 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001967 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001968 *
1969 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001970 */
1971hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001972 var cursor = this.saveCursor();
1973
1974 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001975
David Benjamina08d78f2012-05-05 00:28:49 -04001976 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001977 for (var i = cursor.row + 1; i <= bottom; i++) {
1978 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001979 this.screen_.clearCursorRow();
1980 }
1981
rginda87b86462011-12-14 13:48:03 -08001982 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001983 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001984};
1985
1986/**
1987 * Fill the terminal with a given character.
1988 *
1989 * This methods does not respect the VT scroll region.
1990 *
1991 * @param {string} ch The character to use for the fill.
1992 */
1993hterm.Terminal.prototype.fill = function(ch) {
1994 var cursor = this.saveCursor();
1995
1996 this.setAbsoluteCursorPosition(0, 0);
1997 for (var row = 0; row < this.screenSize.height; row++) {
1998 for (var col = 0; col < this.screenSize.width; col++) {
1999 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002000 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002001 }
2002 }
2003
2004 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002005};
2006
2007/**
rginda9ea433c2012-03-16 11:57:00 -07002008 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002009 *
rginda9ea433c2012-03-16 11:57:00 -07002010 * This does not respect the scroll region.
2011 *
2012 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2013 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002014 */
rginda9ea433c2012-03-16 11:57:00 -07002015hterm.Terminal.prototype.clearHome = function(opt_screen) {
2016 var screen = opt_screen || this.screen_;
2017 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002018
rginda11057d52012-04-25 12:29:56 -07002019 if (bottom == 0) {
2020 // Empty screen, nothing to do.
2021 return;
2022 }
2023
rgindae4d29232012-01-19 10:47:13 -08002024 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002025 screen.setCursorPosition(i, 0);
2026 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002027 }
2028
rginda9ea433c2012-03-16 11:57:00 -07002029 screen.setCursorPosition(0, 0);
2030};
2031
2032/**
2033 * Erase the entire display without changing the cursor position.
2034 *
2035 * The cursor position is unchanged. This does not respect the scroll
2036 * region.
2037 *
2038 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2039 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002040 */
2041hterm.Terminal.prototype.clear = function(opt_screen) {
2042 var screen = opt_screen || this.screen_;
2043 var cursor = screen.cursorPosition.clone();
2044 this.clearHome(screen);
2045 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002046};
2047
2048/**
2049 * VT command to insert lines at the current cursor row.
2050 *
2051 * This respects the current scroll region. Rows pushed off the bottom are
2052 * lost (they won't show up in the scrollback buffer).
2053 *
rginda8ba33642011-12-14 12:31:31 -08002054 * @param {integer} count The number of lines to insert.
2055 */
2056hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002057 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002058
2059 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002060 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002061
Robert Ginda579186b2012-09-26 11:40:04 -07002062 // The moveCount is the number of rows we need to relocate to make room for
2063 // the new row(s). The count is the distance to move them.
2064 var moveCount = bottom - cursorRow - count + 1;
2065 if (moveCount)
2066 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002067
Robert Ginda579186b2012-09-26 11:40:04 -07002068 for (var i = count - 1; i >= 0; i--) {
2069 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002070 this.screen_.clearCursorRow();
2071 }
rginda8ba33642011-12-14 12:31:31 -08002072};
2073
2074/**
2075 * VT command to delete lines at the current cursor row.
2076 *
2077 * New rows are added to the bottom of scroll region to take their place. New
2078 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002079 *
2080 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002081 */
2082hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002083 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002084
rginda87b86462011-12-14 13:48:03 -08002085 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002086 var bottom = this.getVTScrollBottom();
2087
rginda87b86462011-12-14 13:48:03 -08002088 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002089 count = Math.min(count, maxCount);
2090
rginda87b86462011-12-14 13:48:03 -08002091 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002092 if (count != maxCount)
2093 this.moveRows_(top, count, moveStart);
2094
2095 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002096 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002097 this.screen_.clearCursorRow();
2098 }
2099
rginda87b86462011-12-14 13:48:03 -08002100 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002101 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002102};
2103
2104/**
2105 * Inserts the given number of spaces at the current cursor position.
2106 *
rginda87b86462011-12-14 13:48:03 -08002107 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002108 *
2109 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002110 */
2111hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002112 var cursor = this.saveCursor();
2113
rgindacbbd7482012-06-13 15:06:16 -07002114 var ws = lib.f.getWhitespace(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002115 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002116 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002117
2118 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002119 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002120};
2121
2122/**
2123 * Forward-delete the specified number of characters starting at the cursor
2124 * position.
2125 *
2126 * @param {integer} count The number of characters to delete.
2127 */
2128hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002129 var deleted = this.screen_.deleteChars(count);
2130 if (deleted && !this.screen_.textAttributes.isDefault()) {
2131 var cursor = this.saveCursor();
2132 this.setCursorColumn(this.screenSize.width - deleted);
2133 this.screen_.insertString(lib.f.getWhitespace(deleted));
2134 this.restoreCursor(cursor);
2135 }
2136
David Benjamin54e8bf62012-06-01 22:31:40 -04002137 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002138};
2139
2140/**
2141 * Shift rows in the scroll region upwards by a given number of lines.
2142 *
2143 * New rows are inserted at the bottom of the scroll region to fill the
2144 * vacated rows. The new rows not filled out with the current text attributes.
2145 *
2146 * This function does not affect the scrollback rows at all. Rows shifted
2147 * off the top are lost.
2148 *
rginda87b86462011-12-14 13:48:03 -08002149 * The cursor position is not altered.
2150 *
rginda8ba33642011-12-14 12:31:31 -08002151 * @param {integer} count The number of rows to scroll.
2152 */
2153hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002154 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002155
rginda87b86462011-12-14 13:48:03 -08002156 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002157 this.deleteLines(count);
2158
rginda87b86462011-12-14 13:48:03 -08002159 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002160};
2161
2162/**
2163 * Shift rows below the cursor down by a given number of lines.
2164 *
2165 * This function respects the current scroll region.
2166 *
2167 * New rows are inserted at the top of the scroll region to fill the
2168 * vacated rows. The new rows not filled out with the current text attributes.
2169 *
2170 * This function does not affect the scrollback rows at all. Rows shifted
2171 * off the bottom are lost.
2172 *
2173 * @param {integer} count The number of rows to scroll.
2174 */
2175hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08002176 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002177
rginda87b86462011-12-14 13:48:03 -08002178 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08002179 this.insertLines(opt_count);
2180
rginda87b86462011-12-14 13:48:03 -08002181 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002182};
2183
rginda87b86462011-12-14 13:48:03 -08002184
rginda8ba33642011-12-14 12:31:31 -08002185/**
2186 * Set the cursor position.
2187 *
2188 * The cursor row is relative to the scroll region if the terminal has
2189 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2190 *
2191 * @param {integer} row The new zero-based cursor row.
2192 * @param {integer} row The new zero-based cursor column.
2193 */
2194hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2195 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002196 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002197 } else {
rginda87b86462011-12-14 13:48:03 -08002198 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002199 }
rginda87b86462011-12-14 13:48:03 -08002200};
rginda8ba33642011-12-14 12:31:31 -08002201
Evan Jones2600d4f2016-12-06 09:29:36 -05002202/**
2203 * Move the cursor relative to its current position.
2204 *
2205 * @param {number} row
2206 * @param {number} column
2207 */
rginda87b86462011-12-14 13:48:03 -08002208hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2209 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002210 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2211 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002212 this.screen_.setCursorPosition(row, column);
2213};
2214
Evan Jones2600d4f2016-12-06 09:29:36 -05002215/**
2216 * Move the cursor to the specified position.
2217 *
2218 * @param {number} row
2219 * @param {number} column
2220 */
rginda87b86462011-12-14 13:48:03 -08002221hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002222 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2223 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002224 this.screen_.setCursorPosition(row, column);
2225};
2226
2227/**
2228 * Set the cursor column.
2229 *
2230 * @param {integer} column The new zero-based cursor column.
2231 */
2232hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002233 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002234};
2235
2236/**
2237 * Return the cursor column.
2238 *
2239 * @return {integer} The zero-based cursor column.
2240 */
2241hterm.Terminal.prototype.getCursorColumn = function() {
2242 return this.screen_.cursorPosition.column;
2243};
2244
2245/**
2246 * Set the cursor row.
2247 *
2248 * The cursor row is relative to the scroll region if the terminal has
2249 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2250 *
2251 * @param {integer} row The new cursor row.
2252 */
rginda87b86462011-12-14 13:48:03 -08002253hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2254 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002255};
2256
2257/**
2258 * Return the cursor row.
2259 *
2260 * @return {integer} The zero-based cursor row.
2261 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002262hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002263 return this.screen_.cursorPosition.row;
2264};
2265
2266/**
2267 * Request that the ScrollPort redraw itself soon.
2268 *
2269 * The redraw will happen asynchronously, soon after the call stack winds down.
2270 * Multiple calls will be coalesced into a single redraw.
2271 */
2272hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002273 if (this.timeouts_.redraw)
2274 return;
rginda8ba33642011-12-14 12:31:31 -08002275
2276 var self = this;
rginda87b86462011-12-14 13:48:03 -08002277 this.timeouts_.redraw = setTimeout(function() {
2278 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002279 self.scrollPort_.redraw_();
2280 }, 0);
2281};
2282
2283/**
2284 * Request that the ScrollPort be scrolled to the bottom.
2285 *
2286 * The scroll will happen asynchronously, soon after the call stack winds down.
2287 * Multiple calls will be coalesced into a single scroll.
2288 *
2289 * This affects the scrollbar position of the ScrollPort, and has nothing to
2290 * do with the VT scroll commands.
2291 */
2292hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2293 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002294 return;
rginda8ba33642011-12-14 12:31:31 -08002295
2296 var self = this;
2297 this.timeouts_.scrollDown = setTimeout(function() {
2298 delete self.timeouts_.scrollDown;
2299 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2300 }, 10);
2301};
2302
2303/**
2304 * Move the cursor up a specified number of rows.
2305 *
2306 * @param {integer} count The number of rows to move the cursor.
2307 */
2308hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002309 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002310};
2311
2312/**
2313 * Move the cursor down a specified number of rows.
2314 *
2315 * @param {integer} count The number of rows to move the cursor.
2316 */
2317hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002318 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002319 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2320 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2321 this.screenSize.height - 1);
2322
rgindacbbd7482012-06-13 15:06:16 -07002323 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002324 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002325 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002326};
2327
2328/**
2329 * Move the cursor left a specified number of columns.
2330 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002331 * If reverse wraparound mode is enabled and the previous row wrapped into
2332 * the current row then we back up through the wraparound as well.
2333 *
rginda8ba33642011-12-14 12:31:31 -08002334 * @param {integer} count The number of columns to move the cursor.
2335 */
2336hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002337 count = count || 1;
2338
2339 if (count < 1)
2340 return;
2341
2342 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002343 if (this.options_.reverseWraparound) {
2344 if (this.screen_.cursorPosition.overflow) {
2345 // If this cursor is in the right margin, consume one count to get it
2346 // back to the last column. This only applies when we're in reverse
2347 // wraparound mode.
2348 count--;
2349 this.clearCursorOverflow();
2350
2351 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002352 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002353 }
2354
Robert Gindabfb32622014-07-17 13:20:27 -07002355 var newRow = this.screen_.cursorPosition.row;
2356 var newColumn = currentColumn - count;
2357 if (newColumn < 0) {
2358 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2359 if (newRow < 0) {
2360 // xterm also wraps from row 0 to the last row.
2361 newRow = this.screenSize.height + newRow % this.screenSize.height;
2362 }
2363 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2364 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002365
Robert Gindabfb32622014-07-17 13:20:27 -07002366 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2367
2368 } else {
2369 var newColumn = Math.max(currentColumn - count, 0);
2370 this.setCursorColumn(newColumn);
2371 }
rginda8ba33642011-12-14 12:31:31 -08002372};
2373
2374/**
2375 * Move the cursor right a specified number of columns.
2376 *
2377 * @param {integer} count The number of columns to move the cursor.
2378 */
2379hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002380 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002381
2382 if (count < 1)
2383 return;
2384
rgindacbbd7482012-06-13 15:06:16 -07002385 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002386 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002387 this.setCursorColumn(column);
2388};
2389
2390/**
2391 * Reverse the foreground and background colors of the terminal.
2392 *
2393 * This only affects text that was drawn with no attributes.
2394 *
2395 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2396 * been drawn with attributes that happen to coincide with the default
2397 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002398 *
2399 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002400 */
2401hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002402 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002403 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002404 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2405 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002406 } else {
rginda9f5222b2012-03-05 11:53:28 -08002407 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2408 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002409 }
2410};
2411
2412/**
rginda87b86462011-12-14 13:48:03 -08002413 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002414 *
2415 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002416 */
2417hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002418 this.cursorNode_.style.backgroundColor =
2419 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002420
2421 var self = this;
2422 setTimeout(function() {
Matheus Fernandes2d733082017-09-11 06:43:01 -04002423 self.restyleCursor_();
rginda6d397402012-01-17 10:58:29 -08002424 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002425
Michael Kelly485ecd12014-06-09 11:41:56 -04002426 // bellSquelchTimeout_ affects both audio and notification bells.
2427 if (this.bellSquelchTimeout_)
2428 return;
2429
Robert Ginda92e18102013-03-14 13:56:37 -07002430 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002431 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002432 this.bellSequelchTimeout_ = setTimeout(function() {
2433 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002434 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002435 } else {
2436 delete this.bellSquelchTimeout_;
2437 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002438
2439 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002440 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002441 this.bellNotificationList_.push(n);
2442 // TODO: Should we try to raise the window here?
2443 n.onclick = function() { self.closeBellNotifications_(); };
2444 }
rginda87b86462011-12-14 13:48:03 -08002445};
2446
2447/**
rginda8ba33642011-12-14 12:31:31 -08002448 * Set the origin mode bit.
2449 *
2450 * If origin mode is on, certain VT cursor and scrolling commands measure their
2451 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2452 * to the top of the addressable screen.
2453 *
2454 * Defaults to off.
2455 *
2456 * @param {boolean} state True to set origin mode, false to unset.
2457 */
2458hterm.Terminal.prototype.setOriginMode = function(state) {
2459 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002460 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002461};
2462
2463/**
2464 * Set the insert mode bit.
2465 *
2466 * If insert mode is on, existing text beyond the cursor position will be
2467 * shifted right to make room for new text. Otherwise, new text overwrites
2468 * any existing text.
2469 *
2470 * Defaults to off.
2471 *
2472 * @param {boolean} state True to set insert mode, false to unset.
2473 */
2474hterm.Terminal.prototype.setInsertMode = function(state) {
2475 this.options_.insertMode = state;
2476};
2477
2478/**
rginda87b86462011-12-14 13:48:03 -08002479 * Set the auto carriage return bit.
2480 *
2481 * If auto carriage return is on then a formfeed character is interpreted
2482 * as a newline, otherwise it's the same as a linefeed. The difference boils
2483 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002484 *
2485 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002486 */
2487hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2488 this.options_.autoCarriageReturn = state;
2489};
2490
2491/**
rginda8ba33642011-12-14 12:31:31 -08002492 * Set the wraparound mode bit.
2493 *
2494 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2495 * to the start of the following row. Otherwise, the cursor is clamped to the
2496 * end of the screen and attempts to write past it are ignored.
2497 *
2498 * Defaults to on.
2499 *
2500 * @param {boolean} state True to set wraparound mode, false to unset.
2501 */
2502hterm.Terminal.prototype.setWraparound = function(state) {
2503 this.options_.wraparound = state;
2504};
2505
2506/**
2507 * Set the reverse-wraparound mode bit.
2508 *
2509 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2510 * to the end of the previous row. Otherwise, the cursor is clamped to column
2511 * 0.
2512 *
2513 * Defaults to off.
2514 *
2515 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2516 */
2517hterm.Terminal.prototype.setReverseWraparound = function(state) {
2518 this.options_.reverseWraparound = state;
2519};
2520
2521/**
2522 * Selects between the primary and alternate screens.
2523 *
2524 * If alternate mode is on, the alternate screen is active. Otherwise the
2525 * primary screen is active.
2526 *
2527 * Swapping screens has no effect on the scrollback buffer.
2528 *
2529 * Each screen maintains its own cursor position.
2530 *
2531 * Defaults to off.
2532 *
2533 * @param {boolean} state True to set alternate mode, false to unset.
2534 */
2535hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002536 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002537 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2538
rginda35c456b2012-02-09 17:29:05 -08002539 if (this.screen_.rowsArray.length &&
2540 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2541 // If the screen changed sizes while we were away, our rowIndexes may
2542 // be incorrect.
2543 var offset = this.scrollbackRows_.length;
2544 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002545 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002546 ary[i].rowIndex = offset + i;
2547 }
2548 }
rginda8ba33642011-12-14 12:31:31 -08002549
rginda35c456b2012-02-09 17:29:05 -08002550 this.realizeWidth_(this.screenSize.width);
2551 this.realizeHeight_(this.screenSize.height);
2552 this.scrollPort_.syncScrollHeight();
2553 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002554
rginda6d397402012-01-17 10:58:29 -08002555 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002556 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002557};
2558
2559/**
2560 * Set the cursor-blink mode bit.
2561 *
2562 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2563 * a visible cursor does not blink.
2564 *
2565 * You should make sure to turn blinking off if you're going to dispose of a
2566 * terminal, otherwise you'll leak a timeout.
2567 *
2568 * Defaults to on.
2569 *
2570 * @param {boolean} state True to set cursor-blink mode, false to unset.
2571 */
2572hterm.Terminal.prototype.setCursorBlink = function(state) {
2573 this.options_.cursorBlink = state;
2574
2575 if (!state && this.timeouts_.cursorBlink) {
2576 clearTimeout(this.timeouts_.cursorBlink);
2577 delete this.timeouts_.cursorBlink;
2578 }
2579
2580 if (this.options_.cursorVisible)
2581 this.setCursorVisible(true);
2582};
2583
2584/**
2585 * Set the cursor-visible mode bit.
2586 *
2587 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2588 *
2589 * Defaults to on.
2590 *
2591 * @param {boolean} state True to set cursor-visible mode, false to unset.
2592 */
2593hterm.Terminal.prototype.setCursorVisible = function(state) {
2594 this.options_.cursorVisible = state;
2595
2596 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002597 if (this.timeouts_.cursorBlink) {
2598 clearTimeout(this.timeouts_.cursorBlink);
2599 delete this.timeouts_.cursorBlink;
2600 }
rginda87b86462011-12-14 13:48:03 -08002601 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002602 return;
2603 }
2604
rginda87b86462011-12-14 13:48:03 -08002605 this.syncCursorPosition_();
2606
2607 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002608
2609 if (this.options_.cursorBlink) {
2610 if (this.timeouts_.cursorBlink)
2611 return;
2612
Robert Gindaea2183e2014-07-17 09:51:51 -07002613 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002614 } else {
2615 if (this.timeouts_.cursorBlink) {
2616 clearTimeout(this.timeouts_.cursorBlink);
2617 delete this.timeouts_.cursorBlink;
2618 }
2619 }
2620};
2621
2622/**
rginda87b86462011-12-14 13:48:03 -08002623 * Synchronizes the visible cursor and document selection with the current
2624 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002625 */
2626hterm.Terminal.prototype.syncCursorPosition_ = function() {
2627 var topRowIndex = this.scrollPort_.getTopRowIndex();
2628 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2629 var cursorRowIndex = this.scrollbackRows_.length +
2630 this.screen_.cursorPosition.row;
2631
2632 if (cursorRowIndex > bottomRowIndex) {
2633 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04002634 this.setCssVar('cursor-offset-row', '-1');
rginda8ba33642011-12-14 12:31:31 -08002635 return;
2636 }
2637
Robert Gindab837c052014-08-11 11:17:51 -07002638 if (this.options_.cursorVisible &&
2639 this.cursorNode_.style.display == 'none') {
2640 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2641 this.cursorNode_.style.display = '';
2642 }
2643
Mike Frysinger44c32202017-08-05 01:13:09 -04002644 // Position the cursor using CSS variable math. If we do the math in JS,
2645 // the float math will end up being more precise than the CSS which will
2646 // cause the cursor tracking to be off.
2647 this.setCssVar(
2648 'cursor-offset-row',
2649 `${cursorRowIndex - topRowIndex} + ` +
2650 `${this.scrollPort_.visibleRowTopMargin}px`);
2651 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08002652
2653 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04002654 '(' + this.screen_.cursorPosition.column +
2655 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08002656 ')');
2657
2658 // Update the caret for a11y purposes.
2659 var selection = this.document_.getSelection();
2660 if (selection && selection.isCollapsed)
2661 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002662};
2663
Robert Gindafb1be6a2013-12-11 11:56:22 -08002664/**
2665 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2666 * and character cell dimensions.
2667 */
Robert Ginda830583c2013-08-07 13:20:46 -07002668hterm.Terminal.prototype.restyleCursor_ = function() {
2669 var shape = this.cursorShape_;
2670
2671 if (this.cursorNode_.getAttribute('focus') == 'false') {
2672 // Always show a block cursor when unfocused.
2673 shape = hterm.Terminal.cursorShape.BLOCK;
2674 }
2675
2676 var style = this.cursorNode_.style;
2677
2678 switch (shape) {
2679 case hterm.Terminal.cursorShape.BEAM:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002680 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002681 style.backgroundColor = 'transparent';
2682 style.borderBottomStyle = null;
2683 style.borderLeftStyle = 'solid';
2684 break;
2685
2686 case hterm.Terminal.cursorShape.UNDERLINE:
2687 style.height = this.scrollPort_.characterSize.baseline + 'px';
2688 style.backgroundColor = 'transparent';
2689 style.borderBottomStyle = 'solid';
2690 // correct the size to put it exactly at the baseline
2691 style.borderLeftStyle = null;
2692 break;
2693
2694 default:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002695 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002696 style.backgroundColor = this.cursorColor_;
2697 style.borderBottomStyle = null;
2698 style.borderLeftStyle = null;
2699 break;
2700 }
2701};
2702
rginda8ba33642011-12-14 12:31:31 -08002703/**
2704 * Synchronizes the visible cursor with the current cursor coordinates.
2705 *
2706 * The sync will happen asynchronously, soon after the call stack winds down.
2707 * Multiple calls will be coalesced into a single sync.
2708 */
2709hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2710 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002711 return;
rginda8ba33642011-12-14 12:31:31 -08002712
2713 var self = this;
2714 this.timeouts_.syncCursor = setTimeout(function() {
2715 self.syncCursorPosition_();
2716 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002717 }, 0);
2718};
2719
rgindacc2996c2012-02-24 14:59:31 -08002720/**
rgindaf522ce02012-04-17 17:49:17 -07002721 * Show or hide the zoom warning.
2722 *
2723 * The zoom warning is a message warning the user that their browser zoom must
2724 * be set to 100% in order for hterm to function properly.
2725 *
2726 * @param {boolean} state True to show the message, false to hide it.
2727 */
2728hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2729 if (!this.zoomWarningNode_) {
2730 if (!state)
2731 return;
2732
2733 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002734 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07002735 this.zoomWarningNode_.style.cssText = (
2736 'color: black;' +
2737 'background-color: #ff2222;' +
2738 'font-size: large;' +
2739 'border-radius: 8px;' +
2740 'opacity: 0.75;' +
2741 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2742 'top: 0.5em;' +
2743 'right: 1.2em;' +
2744 'position: absolute;' +
2745 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002746 '-webkit-user-select: none;' +
2747 '-moz-text-size-adjust: none;' +
2748 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002749
2750 this.zoomWarningNode_.addEventListener('click', function(e) {
2751 this.parentNode.removeChild(this);
2752 });
rgindaf522ce02012-04-17 17:49:17 -07002753 }
2754
Robert Gindab4839c22013-02-28 16:52:10 -08002755 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2756 hterm.zoomWarningMessage,
2757 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2758
rgindaf522ce02012-04-17 17:49:17 -07002759 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2760
2761 if (state) {
2762 if (!this.zoomWarningNode_.parentNode)
2763 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2764 } else if (this.zoomWarningNode_.parentNode) {
2765 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2766 }
2767};
2768
2769/**
rgindacc2996c2012-02-24 14:59:31 -08002770 * Show the terminal overlay for a given amount of time.
2771 *
2772 * The terminal overlay appears in inverse video in a large font, centered
2773 * over the terminal. You should probably keep the overlay message brief,
2774 * since it's in a large font and you probably aren't going to check the size
2775 * of the terminal first.
2776 *
2777 * @param {string} msg The text (not HTML) message to display in the overlay.
2778 * @param {number} opt_timeout The amount of time to wait before fading out
2779 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2780 * stay up forever (or until the next overlay).
2781 */
2782hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002783 if (!this.overlayNode_) {
2784 if (!this.div_)
2785 return;
2786
2787 this.overlayNode_ = this.document_.createElement('div');
2788 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002789 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002790 'font-size: xx-large;' +
2791 'opacity: 0.75;' +
2792 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2793 'position: absolute;' +
2794 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002795 '-webkit-transition: opacity 180ms ease-in;' +
2796 '-moz-user-select: none;' +
2797 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002798
2799 this.overlayNode_.addEventListener('mousedown', function(e) {
2800 e.preventDefault();
2801 e.stopPropagation();
2802 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002803 }
2804
rginda9f5222b2012-03-05 11:53:28 -08002805 this.overlayNode_.style.color = this.prefs_.get('background-color');
2806 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2807 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2808
rgindaf0090c92012-02-10 14:58:52 -08002809 this.overlayNode_.textContent = msg;
2810 this.overlayNode_.style.opacity = '0.75';
2811
2812 if (!this.overlayNode_.parentNode)
2813 this.div_.appendChild(this.overlayNode_);
2814
Robert Ginda97769282013-02-01 15:30:30 -08002815 var divSize = hterm.getClientSize(this.div_);
2816 var overlaySize = hterm.getClientSize(this.overlayNode_);
2817
Robert Ginda8a59f762014-07-23 11:29:55 -07002818 this.overlayNode_.style.top =
2819 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08002820 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07002821 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08002822
rgindaf0090c92012-02-10 14:58:52 -08002823 if (this.overlayTimeout_)
2824 clearTimeout(this.overlayTimeout_);
2825
rgindacc2996c2012-02-24 14:59:31 -08002826 if (opt_timeout === null)
2827 return;
2828
Mike Frysingerb6cfded2017-09-18 00:39:31 -04002829 this.overlayTimeout_ = setTimeout(() => {
2830 this.overlayNode_.style.opacity = '0';
2831 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
2832 }, opt_timeout || 1500);
2833};
2834
2835/**
2836 * Hide the terminal overlay immediately.
2837 *
2838 * Useful when we show an overlay for an event with an unknown end time.
2839 */
2840hterm.Terminal.prototype.hideOverlay = function() {
2841 if (this.overlayTimeout_)
2842 clearTimeout(this.overlayTimeout_);
2843 this.overlayTimeout_ = null;
2844
2845 if (this.overlayNode_.parentNode)
2846 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
2847 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08002848};
2849
rginda4bba5e12012-06-20 16:15:30 -07002850/**
2851 * Paste from the system clipboard to the terminal.
2852 */
2853hterm.Terminal.prototype.paste = function() {
Mike Frysinger4628ad22017-07-20 02:44:20 -04002854 return hterm.pasteFromClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002855};
2856
2857/**
2858 * Copy a string to the system clipboard.
2859 *
2860 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05002861 *
2862 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07002863 */
2864hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002865 if (this.prefs_.get('enable-clipboard-notice'))
2866 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2867
rgindaa09e7332012-08-17 12:49:51 -07002868 var copySource = this.document_.createElement('pre');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002869 copySource.id = 'hterm:copy-to-clipboard-source';
rginda4bba5e12012-06-20 16:15:30 -07002870 copySource.textContent = str;
2871 copySource.style.cssText = (
2872 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002873 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07002874 'position: absolute;' +
2875 'top: -99px');
2876
2877 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002878
rginda4bba5e12012-06-20 16:15:30 -07002879 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002880 var anchorNode = selection.anchorNode;
2881 var anchorOffset = selection.anchorOffset;
2882 var focusNode = selection.focusNode;
2883 var focusOffset = selection.focusOffset;
2884
rginda4bba5e12012-06-20 16:15:30 -07002885 selection.selectAllChildren(copySource);
2886
rgindaa09e7332012-08-17 12:49:51 -07002887 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002888
Rob Spies56953412014-04-28 14:09:47 -07002889 // IE doesn't support selection.extend. This means that the selection
2890 // won't return on IE.
Robert Ginda6f4f0aa2014-07-28 10:21:39 -07002891 if (selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07002892 selection.collapse(anchorNode, anchorOffset);
2893 selection.extend(focusNode, focusOffset);
2894 }
rgindafaa74742012-08-21 13:34:03 -07002895
rginda4bba5e12012-06-20 16:15:30 -07002896 copySource.parentNode.removeChild(copySource);
2897};
2898
Evan Jones2600d4f2016-12-06 09:29:36 -05002899/**
2900 * Returns the selected text, or null if no text is selected.
2901 *
2902 * @return {string|null}
2903 */
rgindaa09e7332012-08-17 12:49:51 -07002904hterm.Terminal.prototype.getSelectionText = function() {
2905 var selection = this.scrollPort_.selection;
2906 selection.sync();
2907
2908 if (selection.isCollapsed)
2909 return null;
2910
2911
2912 // Start offset measures from the beginning of the line.
2913 var startOffset = selection.startOffset;
2914 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002915
Robert Gindafdbb3f22012-09-06 20:23:06 -07002916 if (node.nodeName != 'X-ROW') {
2917 // If the selection doesn't start on an x-row node, then it must be
2918 // somewhere inside the x-row. Add any characters from previous siblings
2919 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002920
2921 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2922 // If node is the text node in a styled span, move up to the span node.
2923 node = node.parentNode;
2924 }
2925
Robert Gindafdbb3f22012-09-06 20:23:06 -07002926 while (node.previousSibling) {
2927 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002928 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002929 }
rgindaa09e7332012-08-17 12:49:51 -07002930 }
2931
2932 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002933 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
2934 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05002935 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002936
Robert Gindafdbb3f22012-09-06 20:23:06 -07002937 if (node.nodeName != 'X-ROW') {
2938 // If the selection doesn't end on an x-row node, then it must be
2939 // somewhere inside the x-row. Add any characters from following siblings
2940 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002941
2942 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2943 // If node is the text node in a styled span, move up to the span node.
2944 node = node.parentNode;
2945 }
2946
Robert Gindafdbb3f22012-09-06 20:23:06 -07002947 while (node.nextSibling) {
2948 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002949 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002950 }
rgindaa09e7332012-08-17 12:49:51 -07002951 }
2952
2953 var rv = this.getRowsText(selection.startRow.rowIndex,
2954 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002955 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002956};
2957
rginda4bba5e12012-06-20 16:15:30 -07002958/**
2959 * Copy the current selection to the system clipboard, then clear it after a
2960 * short delay.
2961 */
2962hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002963 var text = this.getSelectionText();
2964 if (text != null)
2965 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002966};
2967
rgindaf0090c92012-02-10 14:58:52 -08002968hterm.Terminal.prototype.overlaySize = function() {
2969 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2970};
2971
rginda87b86462011-12-14 13:48:03 -08002972/**
2973 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2974 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07002975 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08002976 */
2977hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002978 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002979 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2980
Robert Ginda8cb7d902013-06-20 14:37:18 -07002981 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08002982};
2983
2984/**
Mike Frysinger70b94692017-01-26 18:57:50 -10002985 * Launches url in a new tab.
2986 *
2987 * @param {string} url URL to launch in a new tab.
2988 */
2989hterm.Terminal.prototype.openUrl = function(url) {
Mike Frysingerac437a12017-07-13 02:35:59 -04002990 if (window.chrome && window.chrome.browser) {
2991 // For Chrome v2 apps, we need to use this API to properly open windows.
2992 chrome.browser.openTab({'url': url});
2993 } else {
2994 var win = window.open(url, '_blank');
2995 win.focus();
2996 }
Mike Frysinger70b94692017-01-26 18:57:50 -10002997}
2998
2999/**
3000 * Open the selected url.
3001 */
3002hterm.Terminal.prototype.openSelectedUrl_ = function() {
3003 var str = this.getSelectionText();
3004
3005 // If there is no selection, try and expand wherever they clicked.
3006 if (str == null) {
3007 this.screen_.expandSelection(this.document_.getSelection());
3008 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003009
3010 // If clicking in empty space, return.
3011 if (str == null)
3012 return;
Mike Frysinger70b94692017-01-26 18:57:50 -10003013 }
3014
3015 // Make sure URL is valid before opening.
3016 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
3017 return;
Mike Frysinger43472622017-06-26 18:11:07 -04003018
3019 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003020 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003021 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3022 // We have to whitelist a few protocols that lack authorities and thus
3023 // never use the //. Like mailto.
3024 switch (str.split(':', 1)[0]) {
3025 case 'mailto':
3026 break;
3027 default:
3028 str = 'http://' + str;
3029 break;
3030 }
3031 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003032
3033 this.openUrl(str);
3034}
3035
3036
3037/**
rgindad5613292012-06-19 15:40:37 -07003038 * Add the terminalRow and terminalColumn properties to mouse events and
3039 * then forward on to onMouse().
3040 *
3041 * The terminalRow and terminalColumn properties contain the (row, column)
3042 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003043 *
3044 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003045 */
3046hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003047 if (e.processedByTerminalHandler_) {
3048 // We register our event handlers on the document, as well as the cursor
3049 // and the scroll blocker. Mouse events that occur on the cursor or
3050 // scroll blocker will also appear on the document, but we don't want to
3051 // process them twice.
3052 //
3053 // We can't just prevent bubbling because that has other side effects, so
3054 // we decorate the event object with this property instead.
3055 return;
3056 }
3057
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003058 var reportMouseEvents = (!this.defeatMouseReports_ &&
3059 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3060
rgindafaa74742012-08-21 13:34:03 -07003061 e.processedByTerminalHandler_ = true;
3062
Robert Gindaeda48db2014-07-17 09:25:30 -07003063 // One based row/column stored on the mouse event.
3064 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
3065 this.scrollPort_.characterSize.height) + 1;
3066 e.terminalColumn = parseInt(e.clientX /
3067 this.scrollPort_.characterSize.width) + 1;
3068
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003069 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3070 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003071 return;
3072 }
3073
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003074 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003075 // If the cursor is visible and we're not sending mouse events to the
3076 // host app, then we want to hide the terminal cursor when the mouse
3077 // cursor is over top. This keeps the terminal cursor from interfering
3078 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003079 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3080 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3081 this.cursorNode_.style.display = 'none';
3082 } else if (this.cursorNode_.style.display == 'none') {
3083 this.cursorNode_.style.display = '';
3084 }
3085 }
rgindad5613292012-06-19 15:40:37 -07003086
Robert Ginda928cf632014-03-05 15:07:41 -08003087 if (e.type == 'mousedown') {
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003088 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003089 // If VT mouse reporting is disabled, or has been defeated with
3090 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003091 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003092 this.setSelectionEnabled(true);
3093 } else {
3094 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003095 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003096 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003097 this.setSelectionEnabled(false);
3098 e.preventDefault();
3099 }
3100 }
3101
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003102 if (!reportMouseEvents) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003103 if (e.type == 'dblclick' && this.copyOnSelect) {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003104 this.screen_.expandSelection(this.document_.getSelection());
Robert Ginda15ed4902016-07-12 10:43:22 -07003105 this.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07003106 }
3107
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003108 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003109 // Debounce this event with the dblclick event. If you try to doubleclick
3110 // a URL to open it, Chrome will fire click then dblclick, but we won't
3111 // have expanded the selection text at the first click event.
3112 clearTimeout(this.timeouts_.openUrl);
3113 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3114 500);
3115 return;
3116 }
3117
Mike Frysinger847577f2017-05-23 23:25:57 -04003118 if (e.type == 'mousedown') {
3119 if ((this.mouseRightClickPaste && e.button == 2 /* right button */) ||
Mike Frysinger2edd3612017-05-24 00:54:39 -04003120 e.button == this.mousePasteButton) {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003121 if (!this.paste())
Mike Frysinger05a57f02017-08-27 17:48:55 -04003122 console.warn('Could not paste manually due to web restrictions');
Mike Frysinger847577f2017-05-23 23:25:57 -04003123 }
3124 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003125
Mike Frysinger2edd3612017-05-24 00:54:39 -04003126 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003127 !this.document_.getSelection().isCollapsed) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003128 this.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003129 }
3130
3131 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3132 this.scrollBlockerNode_.engaged) {
3133 // Disengage the scroll-blocker after one of these events.
3134 this.scrollBlockerNode_.engaged = false;
3135 this.scrollBlockerNode_.style.top = '-99px';
3136 }
3137
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003138 // Emulate arrow key presses via scroll wheel events.
3139 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3140 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003141 if (e.type == 'wheel') {
3142 var delta = this.scrollPort_.scrollWheelDelta(e);
3143 var lines = lib.f.smartFloorDivide(
3144 Math.abs(delta), this.scrollPort_.characterSize.height);
3145
3146 var data = '\x1bO' + (delta < 0 ? 'B' : 'A');
3147 this.io.sendString(data.repeat(lines));
3148
3149 e.preventDefault();
3150 }
3151 }
Robert Ginda928cf632014-03-05 15:07:41 -08003152 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003153 if (!this.scrollBlockerNode_.engaged) {
3154 if (e.type == 'mousedown') {
3155 // Move the scroll-blocker into place if we want to keep the scrollport
3156 // from scrolling.
3157 this.scrollBlockerNode_.engaged = true;
3158 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3159 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3160 } else if (e.type == 'mousemove') {
3161 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3162 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003163 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003164 e.preventDefault();
3165 }
3166 }
Robert Ginda928cf632014-03-05 15:07:41 -08003167
3168 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003169 }
3170
Robert Ginda928cf632014-03-05 15:07:41 -08003171 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3172 // Restore this on mouseup in case it was temporarily defeated with a
3173 // alt-mousedown. Only do this when the selection is empty so that
3174 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003175 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003176 }
rgindad5613292012-06-19 15:40:37 -07003177};
3178
3179/**
3180 * Clients should override this if they care to know about mouse events.
3181 *
3182 * The event parameter will be a normal DOM mouse click event with additional
3183 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003184 *
3185 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003186 */
3187hterm.Terminal.prototype.onMouse = function(e) { };
3188
3189/**
rginda8e92a692012-05-20 19:37:20 -07003190 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003191 *
3192 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003193 */
Rob Spies06533ba2014-04-24 11:20:37 -07003194hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3195 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003196 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003197
3198 if (this.reportFocus) {
3199 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O')
3200 }
3201
Michael Kelly485ecd12014-06-09 11:41:56 -04003202 if (focused === true)
3203 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003204};
3205
3206/**
rginda8ba33642011-12-14 12:31:31 -08003207 * React when the ScrollPort is scrolled.
3208 */
3209hterm.Terminal.prototype.onScroll_ = function() {
3210 this.scheduleSyncCursorPosition_();
3211};
3212
3213/**
rginda9846e2f2012-01-27 13:53:33 -08003214 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003215 *
3216 * @param {Event} e The DOM paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003217 */
3218hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07003219 var data = e.text.replace(/\n/mg, '\r');
Connor Hegartyf525ceb2014-09-03 13:36:03 -07003220 data = this.keyboard.encode(data);
Robert Gindaa063b202014-07-21 11:08:25 -07003221 if (this.options_.bracketedPaste)
3222 data = '\x1b[200~' + data + '\x1b[201~';
3223
3224 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003225};
3226
3227/**
rgindaa09e7332012-08-17 12:49:51 -07003228 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003229 *
3230 * @param {Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003231 */
3232hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003233 if (!this.useDefaultWindowCopy) {
3234 e.preventDefault();
3235 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3236 }
rgindaa09e7332012-08-17 12:49:51 -07003237};
3238
3239/**
rginda8ba33642011-12-14 12:31:31 -08003240 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003241 *
3242 * Note: This function should not directly contain code that alters the internal
3243 * state of the terminal. That kind of code belongs in realizeWidth or
3244 * realizeHeight, so that it can be executed synchronously in the case of a
3245 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003246 */
3247hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003248 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003249 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003250 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003251 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003252
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003253 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003254 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003255 // gets removed from the document or during the initial load, and we can't
3256 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003257 // This can also happen if called before the scrollPort calculates the
3258 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003259 return;
3260 }
3261
rgindaa8ba17d2012-08-15 14:41:10 -07003262 var isNewSize = (columnCount != this.screenSize.width ||
3263 rowCount != this.screenSize.height);
3264
3265 // We do this even if the size didn't change, just to be sure everything is
3266 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003267 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003268 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003269
3270 if (isNewSize)
3271 this.overlaySize();
3272
Robert Gindafb1be6a2013-12-11 11:56:22 -08003273 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003274 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08003275};
3276
3277/**
3278 * Service the cursor blink timeout.
3279 */
3280hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003281 if (!this.options_.cursorBlink) {
3282 delete this.timeouts_.cursorBlink;
3283 return;
3284 }
3285
Robert Ginda830583c2013-08-07 13:20:46 -07003286 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3287 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003288 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003289 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3290 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003291 } else {
rginda87b86462011-12-14 13:48:03 -08003292 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003293 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3294 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003295 }
3296};
David Reveman8f552492012-03-28 12:18:41 -04003297
3298/**
3299 * Set the scrollbar-visible mode bit.
3300 *
3301 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3302 * Otherwise it will not.
3303 *
3304 * Defaults to on.
3305 *
3306 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3307 */
3308hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3309 this.scrollPort_.setScrollbarVisible(state);
3310};
Michael Kelly485ecd12014-06-09 11:41:56 -04003311
3312/**
Rob Spies49039e52014-12-17 13:40:04 -08003313 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003314 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003315 *
3316 * Defaults to 1.
3317 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003318 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003319 */
3320hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3321 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3322};
3323
3324/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003325 * Close all web notifications created by terminal bells.
3326 */
3327hterm.Terminal.prototype.closeBellNotifications_ = function() {
3328 this.bellNotificationList_.forEach(function(n) {
3329 n.close();
3330 });
3331 this.bellNotificationList_.length = 0;
3332};