blob: ec721604008993ae736e2f6a7461754932306c74 [file] [log] [blame]
rginda87b86462011-12-14 13:48:03 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rginda8ba33642011-12-14 12:31:31 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rgindacbbd7482012-06-13 15:06:16 -07005'use strict';
6
Masaya Suzuki273aa982014-05-31 07:25:55 +09007lib.rtdep('lib.colors', 'lib.PreferenceManager', 'lib.resource', 'lib.wc',
Rob Spiesf4e90e82015-01-28 12:10:13 -08008 'lib.f', 'hterm.Keyboard', 'hterm.Options', 'hterm.PreferenceManager',
Ricky Liang48f05cb2013-12-31 23:35:29 +08009 'hterm.Screen', 'hterm.ScrollPort', 'hterm.Size',
10 'hterm.TextAttributes', 'hterm.VT');
rgindacbbd7482012-06-13 15:06:16 -070011
rginda8ba33642011-12-14 12:31:31 -080012/**
13 * Constructor for the Terminal class.
14 *
15 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
16 * classes to provide the complete terminal functionality.
17 *
18 * There are a number of lower-level Terminal methods that can be called
19 * directly to manipulate the cursor, text, scroll region, and other terminal
20 * attributes. However, the primary method is interpret(), which parses VT
21 * escape sequences and invokes the appropriate Terminal methods.
22 *
23 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
24 *
25 * TODO(rginda): Eventually we're going to need to support characters which are
26 * displayed twice as wide as standard latin characters. This is to support
27 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080028 *
Robert Ginda57f03b42012-09-13 11:02:48 -070029 * @param {string} opt_profileId Optional preference profile name. If not
rginda9f5222b2012-03-05 11:53:28 -080030 * provided, defaults to 'default'.
rginda8ba33642011-12-14 12:31:31 -080031 */
Robert Ginda57f03b42012-09-13 11:02:48 -070032hterm.Terminal = function(opt_profileId) {
33 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080034
rginda8ba33642011-12-14 12:31:31 -080035 // Two screen instances.
36 this.primaryScreen_ = new hterm.Screen();
37 this.alternateScreen_ = new hterm.Screen();
38
39 // The "current" screen.
40 this.screen_ = this.primaryScreen_;
41
rginda8ba33642011-12-14 12:31:31 -080042 // The local notion of the screen size. ScreenBuffers also have a size which
43 // indicates their present size. During size changes, the two may disagree.
44 // Also, the inactive screen's size is not altered until it is made the active
45 // screen.
46 this.screenSize = new hterm.Size(0, 0);
47
rginda8ba33642011-12-14 12:31:31 -080048 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080049 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080050 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
51 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080052 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070053 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080054
rginda87b86462011-12-14 13:48:03 -080055 // The div that contains this terminal.
56 this.div_ = null;
57
rgindac9bc5502012-01-18 11:48:44 -080058 // The document that contains the scrollPort. Defaulted to the global
59 // document here so that the terminal is functional even if it hasn't been
60 // inserted into a document yet, but re-set in decorate().
61 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080062
rginda8ba33642011-12-14 12:31:31 -080063 // The rows that have scrolled off screen and are no longer addressable.
64 this.scrollbackRows_ = [];
65
rgindac9bc5502012-01-18 11:48:44 -080066 // Saved tab stops.
67 this.tabStops_ = [];
68
David Benjamin66e954d2012-05-05 21:08:12 -040069 // Keep track of whether default tab stops have been erased; after a TBC
70 // clears all tab stops, defaults aren't restored on resize until a reset.
71 this.defaultTabStops = true;
72
rginda8ba33642011-12-14 12:31:31 -080073 // The VT's notion of the top and bottom rows. Used during some VT
74 // cursor positioning and scrolling commands.
75 this.vtScrollTop_ = null;
76 this.vtScrollBottom_ = null;
77
78 // The DIV element for the visible cursor.
79 this.cursorNode_ = null;
80
Robert Ginda830583c2013-08-07 13:20:46 -070081 // The current cursor shape of the terminal.
82 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
83
84 // The current color of the cursor.
85 this.cursorColor_ = null;
86
Robert Gindaea2183e2014-07-17 09:51:51 -070087 // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded.
88 this.cursorBlinkCycle_ = [100, 100];
89
90 // Pre-bound onCursorBlink_ handler, so we don't have to do this for each
91 // cursor on/off servicing.
92 this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
93
rginda9f5222b2012-03-05 11:53:28 -080094 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070095 // each output and keystroke. They are initialized by the preference manager.
Robert Ginda8cb7d902013-06-20 14:37:18 -070096 this.backgroundColor_ = null;
97 this.foregroundColor_ = null;
Robert Ginda57f03b42012-09-13 11:02:48 -070098 this.scrollOnOutput_ = null;
99 this.scrollOnKeystroke_ = null;
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400100 this.scrollWheelArrowKeys_ = null;
rginda9f5222b2012-03-05 11:53:28 -0800101
Robert Ginda6aec7eb2015-06-16 10:31:30 -0700102 // True if we should override mouse event reporting to allow local selection.
103 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -0800104
rgindaf0090c92012-02-10 14:58:52 -0800105 // Terminal bell sound.
106 this.bellAudio_ = this.document_.createElement('audio');
Mike Frysingerd826f1a2017-07-06 16:20:06 -0400107 this.bellAudio_.id = 'hterm:bell-audio';
rgindaf0090c92012-02-10 14:58:52 -0800108 this.bellAudio_.setAttribute('preload', 'auto');
109
Michael Kelly485ecd12014-06-09 11:41:56 -0400110 // All terminal bell notifications that have been generated (not necessarily
111 // shown).
112 this.bellNotificationList_ = [];
113
114 // Whether we have permission to display notifications.
115 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400116
rginda6d397402012-01-17 10:58:29 -0800117 // Cursor position and attributes saved with DECSC.
118 this.savedOptions_ = {};
119
rginda8ba33642011-12-14 12:31:31 -0800120 // The current mode bits for the terminal.
121 this.options_ = new hterm.Options();
122
123 // Timeouts we might need to clear.
124 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800125
126 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800127 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800128
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800129 this.saveCursorAndState(true);
130
Zhu Qunying30d40712017-03-14 16:27:00 -0700131 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800132 this.keyboard = new hterm.Keyboard(this);
133
rginda87b86462011-12-14 13:48:03 -0800134 // General IO interface that can be given to third parties without exposing
135 // the entire terminal object.
136 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800137
rgindad5613292012-06-19 15:40:37 -0700138 // True if mouse-click-drag should scroll the terminal.
139 this.enableMouseDragScroll = true;
140
Robert Ginda57f03b42012-09-13 11:02:48 -0700141 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400142 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700143 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700144
Zhu Qunying30d40712017-03-14 16:27:00 -0700145 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700146 this.useDefaultWindowCopy = false;
147
148 this.clearSelectionAfterCopy = true;
149
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400150 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800151 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700152
Gabriel Holodake8a09be2017-10-10 01:07:11 -0400153 this.reportFocus = false;
154
Robert Ginda57f03b42012-09-13 11:02:48 -0700155 this.setProfile(opt_profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500156 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800157};
158
159/**
Robert Ginda830583c2013-08-07 13:20:46 -0700160 * Possible cursor shapes.
161 */
162hterm.Terminal.cursorShape = {
163 BLOCK: 'BLOCK',
164 BEAM: 'BEAM',
165 UNDERLINE: 'UNDERLINE'
166};
167
168/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700169 * Clients should override this to be notified when the terminal is ready
170 * for use.
171 *
172 * The terminal initialization is asynchronous, and shouldn't be used before
173 * this method is called.
174 */
175hterm.Terminal.prototype.onTerminalReady = function() { };
176
177/**
rginda35c456b2012-02-09 17:29:05 -0800178 * Default tab with of 8 to match xterm.
179 */
180hterm.Terminal.prototype.tabWidth = 8;
181
182/**
rginda9f5222b2012-03-05 11:53:28 -0800183 * Select a preference profile.
184 *
185 * This will load the terminal preferences for the given profile name and
186 * associate subsequent preference changes with the new preference profile.
187 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500188 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800189 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700190 * @param {function} opt_callback Optional callback to invoke when the profile
191 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800192 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700193hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
194 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800195
Robert Ginda57f03b42012-09-13 11:02:48 -0700196 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800197
Robert Ginda57f03b42012-09-13 11:02:48 -0700198 if (this.prefs_)
199 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800200
Robert Ginda57f03b42012-09-13 11:02:48 -0700201 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
202 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800203 'alt-gr-mode': function(v) {
204 if (v == null) {
205 if (navigator.language.toLowerCase() == 'en-us') {
206 v = 'none';
207 } else {
208 v = 'right-alt';
209 }
210 } else if (typeof v == 'string') {
211 v = v.toLowerCase();
212 } else {
213 v = 'none';
214 }
215
216 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v))
217 v = 'none';
218
219 terminal.keyboard.altGrMode = v;
220 },
221
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700222 'alt-backspace-is-meta-backspace': function(v) {
223 terminal.keyboard.altBackspaceIsMetaBackspace = v;
224 },
225
Robert Ginda57f03b42012-09-13 11:02:48 -0700226 'alt-is-meta': function(v) {
227 terminal.keyboard.altIsMeta = v;
228 },
229
230 'alt-sends-what': function(v) {
231 if (!/^(escape|8-bit|browser-key)$/.test(v))
232 v = 'escape';
233
234 terminal.keyboard.altSendsWhat = v;
235 },
236
237 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800238 var ary = v.match(/^lib-resource:(\S+)/);
239 if (ary) {
240 terminal.bellAudio_.setAttribute('src',
241 lib.resource.getDataUrl(ary[1]));
242 } else {
243 terminal.bellAudio_.setAttribute('src', v);
244 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700245 },
246
Michael Kelly485ecd12014-06-09 11:41:56 -0400247 'desktop-notification-bell': function(v) {
248 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700249 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400250 Notification.permission === 'granted';
251 if (!terminal.desktopNotificationBell_) {
252 // Note: We don't call Notification.requestPermission here because
253 // Chrome requires the call be the result of a user action (such as an
254 // onclick handler), and pref listeners are run asynchronously.
255 //
256 // A way of working around this would be to display a dialog in the
257 // terminal with a "click-to-request-permission" button.
258 console.warn('desktop-notification-bell is true but we do not have ' +
259 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400260 }
261 } else {
262 terminal.desktopNotificationBell_ = false;
263 }
264 },
265
Robert Ginda57f03b42012-09-13 11:02:48 -0700266 'background-color': function(v) {
267 terminal.setBackgroundColor(v);
268 },
269
270 'background-image': function(v) {
271 terminal.scrollPort_.setBackgroundImage(v);
272 },
273
274 'background-size': function(v) {
275 terminal.scrollPort_.setBackgroundSize(v);
276 },
277
278 'background-position': function(v) {
279 terminal.scrollPort_.setBackgroundPosition(v);
280 },
281
282 'backspace-sends-backspace': function(v) {
283 terminal.keyboard.backspaceSendsBackspace = v;
284 },
285
Brad Town18654b62015-03-12 00:27:45 -0700286 'character-map-overrides': function(v) {
287 if (!(v == null || v instanceof Object)) {
288 console.warn('Preference character-map-modifications is not an ' +
289 'object: ' + v);
290 return;
291 }
292
Mike Frysinger095d4062017-06-14 00:29:48 -0700293 terminal.vt.characterMaps.reset();
294 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700295 },
296
Robert Ginda57f03b42012-09-13 11:02:48 -0700297 'cursor-blink': function(v) {
298 terminal.setCursorBlink(!!v);
299 },
300
Robert Gindaea2183e2014-07-17 09:51:51 -0700301 'cursor-blink-cycle': function(v) {
302 if (v instanceof Array &&
303 typeof v[0] == 'number' &&
304 typeof v[1] == 'number') {
305 terminal.cursorBlinkCycle_ = v;
306 } else if (typeof v == 'number') {
307 terminal.cursorBlinkCycle_ = [v, v];
308 } else {
309 // Fast blink indicates an error.
310 terminal.cursorBlinkCycle_ = [100, 100];
311 }
312 },
313
Robert Ginda57f03b42012-09-13 11:02:48 -0700314 'cursor-color': function(v) {
315 terminal.setCursorColor(v);
316 },
317
318 'color-palette-overrides': function(v) {
319 if (!(v == null || v instanceof Object || v instanceof Array)) {
320 console.warn('Preference color-palette-overrides is not an array or ' +
321 'object: ' + v);
322 return;
rginda9f5222b2012-03-05 11:53:28 -0800323 }
rginda9f5222b2012-03-05 11:53:28 -0800324
Robert Ginda57f03b42012-09-13 11:02:48 -0700325 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700326
Robert Ginda57f03b42012-09-13 11:02:48 -0700327 if (v) {
328 for (var key in v) {
329 var i = parseInt(key);
330 if (isNaN(i) || i < 0 || i > 255) {
331 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
332 continue;
333 }
334
335 if (v[i]) {
336 var rgb = lib.colors.normalizeCSS(v[i]);
337 if (rgb)
338 lib.colors.colorPalette[i] = rgb;
339 }
340 }
rginda30f20f62012-04-05 16:36:19 -0700341 }
rginda30f20f62012-04-05 16:36:19 -0700342
Evan Jones5f9df812016-12-06 09:38:58 -0500343 terminal.primaryScreen_.textAttributes.resetColorPalette();
Robert Ginda57f03b42012-09-13 11:02:48 -0700344 terminal.alternateScreen_.textAttributes.resetColorPalette();
345 },
rginda30f20f62012-04-05 16:36:19 -0700346
Robert Ginda57f03b42012-09-13 11:02:48 -0700347 'copy-on-select': function(v) {
348 terminal.copyOnSelect = !!v;
349 },
rginda9f5222b2012-03-05 11:53:28 -0800350
Rob Spies0bec09b2014-06-06 15:58:09 -0700351 'use-default-window-copy': function(v) {
352 terminal.useDefaultWindowCopy = !!v;
353 },
354
355 'clear-selection-after-copy': function(v) {
356 terminal.clearSelectionAfterCopy = !!v;
357 },
358
Robert Ginda7e5e9522014-03-14 12:23:58 -0700359 'ctrl-plus-minus-zero-zoom': function(v) {
360 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
361 },
362
Robert Gindafb5a3f92014-05-13 14:12:00 -0700363 'ctrl-c-copy': function(v) {
364 terminal.keyboard.ctrlCCopy = v;
365 },
366
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100367 'ctrl-v-paste': function(v) {
368 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700369 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100370 },
371
Masaya Suzuki273aa982014-05-31 07:25:55 +0900372 'east-asian-ambiguous-as-two-column': function(v) {
373 lib.wc.regardCjkAmbiguous = v;
374 },
375
Robert Ginda57f03b42012-09-13 11:02:48 -0700376 'enable-8-bit-control': function(v) {
377 terminal.vt.enable8BitControl = !!v;
378 },
rginda30f20f62012-04-05 16:36:19 -0700379
Robert Ginda57f03b42012-09-13 11:02:48 -0700380 'enable-bold': function(v) {
381 terminal.syncBoldSafeState();
382 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400383
Robert Ginda3e278d72014-03-25 13:18:51 -0700384 'enable-bold-as-bright': function(v) {
385 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
386 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
387 },
388
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400389 'enable-blink': function(v) {
390 terminal.syncBlinkState();
391 },
392
Robert Ginda57f03b42012-09-13 11:02:48 -0700393 'enable-clipboard-write': function(v) {
394 terminal.vt.enableClipboardWrite = !!v;
395 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400396
Robert Ginda3755e752013-05-31 13:34:09 -0700397 'enable-dec12': function(v) {
398 terminal.vt.enableDec12 = !!v;
399 },
400
Robert Ginda57f03b42012-09-13 11:02:48 -0700401 'font-family': function(v) {
402 terminal.syncFontFamily();
403 },
rginda30f20f62012-04-05 16:36:19 -0700404
Robert Ginda57f03b42012-09-13 11:02:48 -0700405 'font-size': function(v) {
406 terminal.setFontSize(v);
407 },
rginda9875d902012-08-20 16:21:57 -0700408
Robert Ginda57f03b42012-09-13 11:02:48 -0700409 'font-smoothing': function(v) {
410 terminal.syncFontFamily();
411 },
rgindade84e382012-04-20 15:39:31 -0700412
Robert Ginda57f03b42012-09-13 11:02:48 -0700413 'foreground-color': function(v) {
414 terminal.setForegroundColor(v);
415 },
rginda30f20f62012-04-05 16:36:19 -0700416
Robert Ginda57f03b42012-09-13 11:02:48 -0700417 'home-keys-scroll': function(v) {
418 terminal.keyboard.homeKeysScroll = v;
419 },
rginda4bba5e12012-06-20 16:15:30 -0700420
Robert Gindaa8165692015-06-15 14:46:31 -0700421 'keybindings': function(v) {
422 terminal.keyboard.bindings.clear();
423
424 if (!v)
425 return;
426
427 if (!(v instanceof Object)) {
428 console.error('Error in keybindings preference: Expected object');
429 return;
430 }
431
432 try {
433 terminal.keyboard.bindings.addBindings(v);
434 } catch (ex) {
435 console.error('Error in keybindings preference: ' + ex);
436 }
437 },
438
Robert Ginda57f03b42012-09-13 11:02:48 -0700439 'max-string-sequence': function(v) {
440 terminal.vt.maxStringSequence = v;
441 },
rginda11057d52012-04-25 12:29:56 -0700442
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700443 'media-keys-are-fkeys': function(v) {
444 terminal.keyboard.mediaKeysAreFKeys = v;
445 },
446
Robert Ginda57f03b42012-09-13 11:02:48 -0700447 'meta-sends-escape': function(v) {
448 terminal.keyboard.metaSendsEscape = v;
449 },
rginda30f20f62012-04-05 16:36:19 -0700450
Mike Frysinger847577f2017-05-23 23:25:57 -0400451 'mouse-right-click-paste': function(v) {
452 terminal.mouseRightClickPaste = v;
453 },
454
Robert Ginda57f03b42012-09-13 11:02:48 -0700455 'mouse-paste-button': function(v) {
456 terminal.syncMousePasteButton();
457 },
rgindaa8ba17d2012-08-15 14:41:10 -0700458
Robert Gindae76aa9f2014-03-14 12:29:12 -0700459 'page-keys-scroll': function(v) {
460 terminal.keyboard.pageKeysScroll = v;
461 },
462
Robert Ginda40932892012-12-10 17:26:40 -0800463 'pass-alt-number': function(v) {
464 if (v == null) {
465 var osx = window.navigator.userAgent.match(/Mac OS X/);
466
467 // Let Alt-1..9 pass to the browser (to control tab switching) on
468 // non-OS X systems, or if hterm is not opened in an app window.
469 v = (!osx && hterm.windowType != 'popup');
470 }
471
472 terminal.passAltNumber = v;
473 },
474
475 'pass-ctrl-number': function(v) {
476 if (v == null) {
477 var osx = window.navigator.userAgent.match(/Mac OS X/);
478
479 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
480 // non-OS X systems, or if hterm is not opened in an app window.
481 v = (!osx && hterm.windowType != 'popup');
482 }
483
484 terminal.passCtrlNumber = v;
485 },
486
487 'pass-meta-number': function(v) {
488 if (v == null) {
489 var osx = window.navigator.userAgent.match(/Mac OS X/);
490
491 // Let Meta-1..9 pass to the browser (to control tab switching) on
492 // OS X systems, or if hterm is not opened in an app window.
493 v = (osx && hterm.windowType != 'popup');
494 }
495
496 terminal.passMetaNumber = v;
497 },
498
Marius Schilder77857b32014-05-14 16:21:26 -0700499 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700500 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700501 },
502
Robert Ginda8cb7d902013-06-20 14:37:18 -0700503 'receive-encoding': function(v) {
504 if (!(/^(utf-8|raw)$/).test(v)) {
505 console.warn('Invalid value for "receive-encoding": ' + v);
506 v = 'utf-8';
507 }
508
509 terminal.vt.characterEncoding = v;
510 },
511
Robert Ginda57f03b42012-09-13 11:02:48 -0700512 'scroll-on-keystroke': function(v) {
513 terminal.scrollOnKeystroke_ = v;
514 },
rginda9f5222b2012-03-05 11:53:28 -0800515
Robert Ginda57f03b42012-09-13 11:02:48 -0700516 'scroll-on-output': function(v) {
517 terminal.scrollOnOutput_ = v;
518 },
rginda30f20f62012-04-05 16:36:19 -0700519
Robert Ginda57f03b42012-09-13 11:02:48 -0700520 'scrollbar-visible': function(v) {
521 terminal.setScrollbarVisible(v);
522 },
rginda9f5222b2012-03-05 11:53:28 -0800523
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400524 'scroll-wheel-may-send-arrow-keys': function(v) {
525 terminal.scrollWheelArrowKeys_ = v;
526 },
527
Rob Spies49039e52014-12-17 13:40:04 -0800528 'scroll-wheel-move-multiplier': function(v) {
529 terminal.setScrollWheelMoveMultipler(v);
530 },
531
Robert Ginda8cb7d902013-06-20 14:37:18 -0700532 'send-encoding': function(v) {
533 if (!(/^(utf-8|raw)$/).test(v)) {
534 console.warn('Invalid value for "send-encoding": ' + v);
535 v = 'utf-8';
536 }
537
538 terminal.keyboard.characterEncoding = v;
539 },
540
Robert Ginda57f03b42012-09-13 11:02:48 -0700541 'shift-insert-paste': function(v) {
542 terminal.keyboard.shiftInsertPaste = v;
543 },
rginda9f5222b2012-03-05 11:53:28 -0800544
Mike Frysingera7768922017-07-28 15:00:12 -0400545 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400546 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400547 },
548
Robert Gindae76aa9f2014-03-14 12:29:12 -0700549 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400550 terminal.scrollPort_.setUserCssUrl(v);
551 },
552
553 'user-css-text': function(v) {
554 terminal.scrollPort_.setUserCssText(v);
555 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400556
557 'word-break-match-left': function(v) {
558 terminal.primaryScreen_.wordBreakMatchLeft = v;
559 terminal.alternateScreen_.wordBreakMatchLeft = v;
560 },
561
562 'word-break-match-right': function(v) {
563 terminal.primaryScreen_.wordBreakMatchRight = v;
564 terminal.alternateScreen_.wordBreakMatchRight = v;
565 },
566
567 'word-break-match-middle': function(v) {
568 terminal.primaryScreen_.wordBreakMatchMiddle = v;
569 terminal.alternateScreen_.wordBreakMatchMiddle = v;
570 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700571 });
rginda30f20f62012-04-05 16:36:19 -0700572
Robert Ginda57f03b42012-09-13 11:02:48 -0700573 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800574 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700575
576 if (opt_callback)
577 opt_callback();
578 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800579};
580
Rob Spies56953412014-04-28 14:09:47 -0700581
582/**
583 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500584 *
585 * @return {hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700586 */
587hterm.Terminal.prototype.getPrefs = function() {
588 return this.prefs_;
589};
590
Robert Gindaa063b202014-07-21 11:08:25 -0700591/**
592 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500593 *
594 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700595 */
596hterm.Terminal.prototype.setBracketedPaste = function(state) {
597 this.options_.bracketedPaste = state;
598};
Rob Spies56953412014-04-28 14:09:47 -0700599
rginda8e92a692012-05-20 19:37:20 -0700600/**
601 * Set the color for the cursor.
602 *
603 * If you want this setting to persist, set it through prefs_, rather than
604 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500605 *
606 * @param {string} color The color to set.
rginda8e92a692012-05-20 19:37:20 -0700607 */
608hterm.Terminal.prototype.setCursorColor = function(color) {
Robert Ginda830583c2013-08-07 13:20:46 -0700609 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700610 this.cursorNode_.style.backgroundColor = color;
611 this.cursorNode_.style.borderColor = color;
612};
613
614/**
615 * Return the current cursor color as a string.
Evan Jones2600d4f2016-12-06 09:29:36 -0500616 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700617 */
618hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700619 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700620};
621
622/**
rgindad5613292012-06-19 15:40:37 -0700623 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500624 *
625 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700626 */
627hterm.Terminal.prototype.setSelectionEnabled = function(state) {
628 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700629};
630
631/**
rginda8e92a692012-05-20 19:37:20 -0700632 * Set the background color.
633 *
634 * If you want this setting to persist, set it through prefs_, rather than
635 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500636 *
637 * @param {string} color The color to set.
rginda8e92a692012-05-20 19:37:20 -0700638 */
639hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700640 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700641 this.primaryScreen_.textAttributes.setDefaults(
642 this.foregroundColor_, this.backgroundColor_);
643 this.alternateScreen_.textAttributes.setDefaults(
644 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700645 this.scrollPort_.setBackgroundColor(color);
646};
647
rginda9f5222b2012-03-05 11:53:28 -0800648/**
649 * Return the current terminal background color.
650 *
651 * Intended for use by other classes, so we don't have to expose the entire
652 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500653 *
654 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800655 */
656hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700657 return this.backgroundColor_;
658};
659
660/**
661 * Set the foreground color.
662 *
663 * If you want this setting to persist, set it through prefs_, rather than
664 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500665 *
666 * @param {string} color The color to set.
rginda8e92a692012-05-20 19:37:20 -0700667 */
668hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700669 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700670 this.primaryScreen_.textAttributes.setDefaults(
671 this.foregroundColor_, this.backgroundColor_);
672 this.alternateScreen_.textAttributes.setDefaults(
673 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700674 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800675};
676
677/**
678 * Return the current terminal foreground color.
679 *
680 * Intended for use by other classes, so we don't have to expose the entire
681 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500682 *
683 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800684 */
685hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700686 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800687};
688
689/**
rginda87b86462011-12-14 13:48:03 -0800690 * Create a new instance of a terminal command and run it with a given
691 * argument string.
692 *
693 * @param {function} commandClass The constructor for a terminal command.
694 * @param {string} argString The argument string to pass to the command.
695 */
696hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700697 var environment = this.prefs_.get('environment');
698 if (typeof environment != 'object' || environment == null)
699 environment = {};
700
rginda87b86462011-12-14 13:48:03 -0800701 var self = this;
702 this.command = new commandClass(
703 { argString: argString || '',
704 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700705 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800706 onExit: function(code) {
707 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800708 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700709 if (self.prefs_.get('close-on-exit'))
710 window.close();
rginda87b86462011-12-14 13:48:03 -0800711 }
712 });
713
rgindafeaf3142012-01-31 15:14:20 -0800714 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800715 this.command.run();
716};
717
718/**
rgindafeaf3142012-01-31 15:14:20 -0800719 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500720 *
721 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800722 */
723hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700724 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800725};
726
727/**
728 * Install the keyboard handler for this terminal.
729 *
730 * This will prevent the browser from seeing any keystrokes sent to the
731 * terminal.
732 */
733hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700734 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
rgindafeaf3142012-01-31 15:14:20 -0800735}
736
737/**
738 * Uninstall the keyboard handler for this terminal.
739 */
740hterm.Terminal.prototype.uninstallKeyboard = function() {
741 this.keyboard.installKeyboard(null);
742}
743
744/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400745 * Set a CSS variable.
746 *
747 * Normally this is used to set variables in the hterm namespace.
748 *
749 * @param {string} name The variable to set.
750 * @param {string} value The value to assign to the variable.
751 * @param {string?} opt_prefix The variable namespace/prefix to use.
752 */
753hterm.Terminal.prototype.setCssVar = function(name, value,
754 opt_prefix='--hterm-') {
755 this.document_.documentElement.style.setProperty(
756 `${opt_prefix}${name}`, value);
757};
758
759/**
rginda35c456b2012-02-09 17:29:05 -0800760 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800761 *
762 * Call setFontSize(0) to reset to the default font size.
763 *
764 * This function does not modify the font-size preference.
765 *
766 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800767 */
768hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800769 if (px === 0)
770 px = this.prefs_.get('font-size');
771
rginda35c456b2012-02-09 17:29:05 -0800772 this.scrollPort_.setFontSize(px);
Mike Frysingercce97c42017-08-05 01:11:22 -0400773 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
774 this.setCssVar('charsize-height',
775 this.scrollPort_.characterSize.height + 'px');
rginda35c456b2012-02-09 17:29:05 -0800776};
777
778/**
779 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500780 *
781 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800782 */
783hterm.Terminal.prototype.getFontSize = function() {
784 return this.scrollPort_.getFontSize();
785};
786
787/**
rginda8e92a692012-05-20 19:37:20 -0700788 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500789 *
790 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700791 */
792hterm.Terminal.prototype.getFontFamily = function() {
793 return this.scrollPort_.getFontFamily();
794};
795
796/**
rginda35c456b2012-02-09 17:29:05 -0800797 * Set the CSS "font-family" for this terminal.
798 */
rginda9f5222b2012-03-05 11:53:28 -0800799hterm.Terminal.prototype.syncFontFamily = function() {
800 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
801 this.prefs_.get('font-smoothing'));
802 this.syncBoldSafeState();
803};
804
rginda4bba5e12012-06-20 16:15:30 -0700805/**
806 * Set this.mousePasteButton based on the mouse-paste-button pref,
807 * autodetecting if necessary.
808 */
809hterm.Terminal.prototype.syncMousePasteButton = function() {
810 var button = this.prefs_.get('mouse-paste-button');
811 if (typeof button == 'number') {
812 this.mousePasteButton = button;
813 return;
814 }
815
816 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
Mike Frysinger98dd15b2017-05-18 22:52:23 -0400817 if (!ary || ary[1] == 'CrOS') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400818 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700819 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400820 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700821 }
822};
823
824/**
825 * Enable or disable bold based on the enable-bold pref, autodetecting if
826 * necessary.
827 */
rginda9f5222b2012-03-05 11:53:28 -0800828hterm.Terminal.prototype.syncBoldSafeState = function() {
829 var enableBold = this.prefs_.get('enable-bold');
830 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700831 this.primaryScreen_.textAttributes.enableBold = enableBold;
832 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800833 return;
834 }
835
rgindaf7521392012-02-28 17:20:34 -0800836 var normalSize = this.scrollPort_.measureCharacterSize();
837 var boldSize = this.scrollPort_.measureCharacterSize('bold');
838
839 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800840 if (!isBoldSafe) {
841 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700842 'from normal. Font family is: ' +
843 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800844 }
rginda9f5222b2012-03-05 11:53:28 -0800845
Robert Gindaed016262012-10-26 16:27:09 -0700846 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
847 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800848};
849
850/**
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400851 * Enable or disable blink based on the enable-blink pref.
852 */
853hterm.Terminal.prototype.syncBlinkState = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400854 this.setCssVar('node-duration',
855 this.prefs_.get('enable-blink') ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400856};
857
858/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400859 * Set the mouse cursor style based on the current terminal mode.
860 */
861hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400862 this.setCssVar('mouse-cursor-style',
863 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
864 'var(--hterm-mouse-cursor-text)' :
865 'var(--hterm-mouse-cursor-pointer)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400866};
867
868/**
rginda87b86462011-12-14 13:48:03 -0800869 * Return a copy of the current cursor position.
870 *
871 * @return {hterm.RowCol} The RowCol object representing the current position.
872 */
873hterm.Terminal.prototype.saveCursor = function() {
874 return this.screen_.cursorPosition.clone();
875};
876
Evan Jones2600d4f2016-12-06 09:29:36 -0500877/**
878 * Return the current text attributes.
879 *
880 * @return {string}
881 */
rgindaa19afe22012-01-25 15:40:22 -0800882hterm.Terminal.prototype.getTextAttributes = function() {
883 return this.screen_.textAttributes;
884};
885
Evan Jones2600d4f2016-12-06 09:29:36 -0500886/**
887 * Set the text attributes.
888 *
889 * @param {string} textAttributes The attributes to set.
890 */
rginda1a09aa02012-06-18 21:11:25 -0700891hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
892 this.screen_.textAttributes = textAttributes;
893};
894
rginda87b86462011-12-14 13:48:03 -0800895/**
rgindaf522ce02012-04-17 17:49:17 -0700896 * Return the current browser zoom factor applied to the terminal.
897 *
898 * @return {number} The current browser zoom factor.
899 */
900hterm.Terminal.prototype.getZoomFactor = function() {
901 return this.scrollPort_.characterSize.zoomFactor;
902};
903
904/**
rginda9846e2f2012-01-27 13:53:33 -0800905 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500906 *
907 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800908 */
909hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800910 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800911};
912
913/**
rginda87b86462011-12-14 13:48:03 -0800914 * Restore a previously saved cursor position.
915 *
916 * @param {hterm.RowCol} cursor The position to restore.
917 */
918hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700919 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
920 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800921 this.screen_.setCursorPosition(row, column);
922 if (cursor.column > column ||
923 cursor.column == column && cursor.overflow) {
924 this.screen_.cursorPosition.overflow = true;
925 }
rginda87b86462011-12-14 13:48:03 -0800926};
927
928/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400929 * Clear the cursor's overflow flag.
930 */
931hterm.Terminal.prototype.clearCursorOverflow = function() {
932 this.screen_.cursorPosition.overflow = false;
933};
934
935/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800936 * Save the current cursor state to the corresponding screens.
937 *
938 * See the hterm.Screen.CursorState class for more details.
939 *
940 * @param {boolean=} both If true, update both screens, else only update the
941 * current screen.
942 */
943hterm.Terminal.prototype.saveCursorAndState = function(both) {
944 if (both) {
945 this.primaryScreen_.saveCursorAndState(this.vt);
946 this.alternateScreen_.saveCursorAndState(this.vt);
947 } else
948 this.screen_.saveCursorAndState(this.vt);
949};
950
951/**
952 * Restore the saved cursor state in the corresponding screens.
953 *
954 * See the hterm.Screen.CursorState class for more details.
955 *
956 * @param {boolean=} both If true, update both screens, else only update the
957 * current screen.
958 */
959hterm.Terminal.prototype.restoreCursorAndState = function(both) {
960 if (both) {
961 this.primaryScreen_.restoreCursorAndState(this.vt);
962 this.alternateScreen_.restoreCursorAndState(this.vt);
963 } else
964 this.screen_.restoreCursorAndState(this.vt);
965};
966
967/**
Robert Ginda830583c2013-08-07 13:20:46 -0700968 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -0500969 *
970 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -0700971 */
972hterm.Terminal.prototype.setCursorShape = function(shape) {
973 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -0800974 this.restyleCursor_();
Robert Ginda830583c2013-08-07 13:20:46 -0700975}
976
977/**
978 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -0500979 *
980 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -0700981 */
982hterm.Terminal.prototype.getCursorShape = function() {
983 return this.cursorShape_;
984}
985
986/**
rginda87b86462011-12-14 13:48:03 -0800987 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -0500988 *
989 * @param {number} columnCount
rginda87b86462011-12-14 13:48:03 -0800990 */
991hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800992 if (columnCount == null) {
993 this.div_.style.width = '100%';
994 return;
995 }
996
Robert Ginda26806d12014-07-24 13:44:07 -0700997 this.div_.style.width = Math.ceil(
998 this.scrollPort_.characterSize.width *
999 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001000 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001001 this.scheduleSyncCursorPosition_();
1002};
rginda87b86462011-12-14 13:48:03 -08001003
rgindac9bc5502012-01-18 11:48:44 -08001004/**
rginda35c456b2012-02-09 17:29:05 -08001005 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001006 *
1007 * @param {number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001008 */
1009hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001010 if (rowCount == null) {
1011 this.div_.style.height = '100%';
1012 return;
1013 }
1014
rginda35c456b2012-02-09 17:29:05 -08001015 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -07001016 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -08001017 this.realizeSize_(this.screenSize.width, rowCount);
1018 this.scheduleSyncCursorPosition_();
1019};
1020
1021/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001022 * Deal with terminal size changes.
1023 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001024 * @param {number} columnCount The number of columns.
1025 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001026 */
1027hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
1028 if (columnCount != this.screenSize.width)
1029 this.realizeWidth_(columnCount);
1030
1031 if (rowCount != this.screenSize.height)
1032 this.realizeHeight_(rowCount);
1033
1034 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -07001035 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001036};
1037
1038/**
rgindac9bc5502012-01-18 11:48:44 -08001039 * Deal with terminal width changes.
1040 *
1041 * This function does what needs to be done when the terminal width changes
1042 * out from under us. It happens here rather than in onResize_() because this
1043 * code may need to run synchronously to handle programmatic changes of
1044 * terminal width.
1045 *
1046 * Relying on the browser to send us an async resize event means we may not be
1047 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001048 *
1049 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001050 */
1051hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001052 if (columnCount <= 0)
1053 throw new Error('Attempt to realize bad width: ' + columnCount);
1054
rgindac9bc5502012-01-18 11:48:44 -08001055 var deltaColumns = columnCount - this.screen_.getWidth();
1056
rginda87b86462011-12-14 13:48:03 -08001057 this.screenSize.width = columnCount;
1058 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001059
1060 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -04001061 if (this.defaultTabStops)
1062 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -08001063 } else {
1064 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -04001065 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -08001066 break;
1067
1068 this.tabStops_.pop();
1069 }
1070 }
1071
1072 this.screen_.setColumnCount(this.screenSize.width);
1073};
1074
1075/**
1076 * Deal with terminal height changes.
1077 *
1078 * This function does what needs to be done when the terminal height changes
1079 * out from under us. It happens here rather than in onResize_() because this
1080 * code may need to run synchronously to handle programmatic changes of
1081 * terminal height.
1082 *
1083 * Relying on the browser to send us an async resize event means we may not be
1084 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001085 *
1086 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001087 */
1088hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001089 if (rowCount <= 0)
1090 throw new Error('Attempt to realize bad height: ' + rowCount);
1091
rgindac9bc5502012-01-18 11:48:44 -08001092 var deltaRows = rowCount - this.screen_.getHeight();
1093
1094 this.screenSize.height = rowCount;
1095
1096 var cursor = this.saveCursor();
1097
1098 if (deltaRows < 0) {
1099 // Screen got smaller.
1100 deltaRows *= -1;
1101 while (deltaRows) {
1102 var lastRow = this.getRowCount() - 1;
1103 if (lastRow - this.scrollbackRows_.length == cursor.row)
1104 break;
1105
1106 if (this.getRowText(lastRow))
1107 break;
1108
1109 this.screen_.popRow();
1110 deltaRows--;
1111 }
1112
1113 var ary = this.screen_.shiftRows(deltaRows);
1114 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1115
1116 // We just removed rows from the top of the screen, we need to update
1117 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001118 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001119 } else if (deltaRows > 0) {
1120 // Screen got larger.
1121
1122 if (deltaRows <= this.scrollbackRows_.length) {
1123 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1124 var rows = this.scrollbackRows_.splice(
1125 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1126 this.screen_.unshiftRows(rows);
1127 deltaRows -= scrollbackCount;
1128 cursor.row += scrollbackCount;
1129 }
1130
1131 if (deltaRows)
1132 this.appendRows_(deltaRows);
1133 }
1134
rginda35c456b2012-02-09 17:29:05 -08001135 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001136 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001137};
1138
1139/**
1140 * Scroll the terminal to the top of the scrollback buffer.
1141 */
1142hterm.Terminal.prototype.scrollHome = function() {
1143 this.scrollPort_.scrollRowToTop(0);
1144};
1145
1146/**
1147 * Scroll the terminal to the end.
1148 */
1149hterm.Terminal.prototype.scrollEnd = function() {
1150 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1151};
1152
1153/**
1154 * Scroll the terminal one page up (minus one line) relative to the current
1155 * position.
1156 */
1157hterm.Terminal.prototype.scrollPageUp = function() {
1158 var i = this.scrollPort_.getTopRowIndex();
1159 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
1160};
1161
1162/**
1163 * Scroll the terminal one page down (minus one line) relative to the current
1164 * position.
1165 */
1166hterm.Terminal.prototype.scrollPageDown = function() {
1167 var i = this.scrollPort_.getTopRowIndex();
1168 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -08001169};
1170
rgindac9bc5502012-01-18 11:48:44 -08001171/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001172 * Scroll the terminal one line up relative to the current position.
1173 */
1174hterm.Terminal.prototype.scrollLineUp = function() {
1175 var i = this.scrollPort_.getTopRowIndex();
1176 this.scrollPort_.scrollRowToTop(i - 1);
1177};
1178
1179/**
1180 * Scroll the terminal one line down relative to the current position.
1181 */
1182hterm.Terminal.prototype.scrollLineDown = function() {
1183 var i = this.scrollPort_.getTopRowIndex();
1184 this.scrollPort_.scrollRowToTop(i + 1);
1185};
1186
1187/**
Robert Ginda40932892012-12-10 17:26:40 -08001188 * Clear primary screen, secondary screen, and the scrollback buffer.
1189 */
1190hterm.Terminal.prototype.wipeContents = function() {
1191 this.scrollbackRows_.length = 0;
1192 this.scrollPort_.resetCache();
1193
1194 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
1195 var bottom = screen.getHeight();
1196 if (bottom > 0) {
1197 this.renumberRows_(0, bottom);
1198 this.clearHome(screen);
1199 }
1200 }.bind(this));
1201
1202 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001203 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001204};
1205
1206/**
rgindac9bc5502012-01-18 11:48:44 -08001207 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001208 *
1209 * Perform a full reset to the default values listed in
1210 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001211 */
rginda87b86462011-12-14 13:48:03 -08001212hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001213 this.vt.reset();
1214
rgindac9bc5502012-01-18 11:48:44 -08001215 this.clearAllTabStops();
1216 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001217
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001218 const resetScreen = (screen) => {
1219 // We want to make sure to reset the attributes before we clear the screen.
1220 // The attributes might be used to initialize default/empty rows.
1221 screen.textAttributes.reset();
1222 screen.textAttributes.resetColorPalette();
1223 this.clearHome(screen);
1224 screen.saveCursorAndState(this.vt);
1225 };
1226 resetScreen(this.primaryScreen_);
1227 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001228
Mike Frysinger84301d02017-11-29 13:28:46 -08001229 // Reset terminal options to their default values.
1230 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001231 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1232
Mike Frysinger84301d02017-11-29 13:28:46 -08001233 this.setVTScrollRegion(null, null);
1234
1235 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001236};
1237
rgindac9bc5502012-01-18 11:48:44 -08001238/**
1239 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001240 *
1241 * Perform a soft reset to the default values listed in
1242 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001243 */
rginda0f5c0292012-01-13 11:00:13 -08001244hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001245 this.vt.reset();
1246
rgindab8bc8932012-04-27 12:45:03 -07001247 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001248 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001249
Brad Townb62dfdc2015-03-16 19:07:15 -07001250 // We show the cursor on soft reset but do not alter the blink state.
1251 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1252
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001253 const resetScreen = (screen) => {
1254 // Xterm also resets the color palette on soft reset, even though it doesn't
1255 // seem to be documented anywhere.
1256 screen.textAttributes.reset();
1257 screen.textAttributes.resetColorPalette();
1258 screen.saveCursorAndState(this.vt);
1259 };
1260 resetScreen(this.primaryScreen_);
1261 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001262
rgindab8bc8932012-04-27 12:45:03 -07001263 // The xterm man page explicitly says this will happen on soft reset.
1264 this.setVTScrollRegion(null, null);
1265
1266 // Xterm also shows the cursor on soft reset, but does not alter the blink
1267 // state.
rgindaa19afe22012-01-25 15:40:22 -08001268 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001269};
1270
rgindac9bc5502012-01-18 11:48:44 -08001271/**
1272 * Move the cursor forward to the next tab stop, or to the last column
1273 * if no more tab stops are set.
1274 */
1275hterm.Terminal.prototype.forwardTabStop = function() {
1276 var column = this.screen_.cursorPosition.column;
1277
1278 for (var i = 0; i < this.tabStops_.length; i++) {
1279 if (this.tabStops_[i] > column) {
1280 this.setCursorColumn(this.tabStops_[i]);
1281 return;
1282 }
1283 }
1284
David Benjamin66e954d2012-05-05 21:08:12 -04001285 // xterm does not clear the overflow flag on HT or CHT.
1286 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001287 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001288 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001289};
1290
rgindac9bc5502012-01-18 11:48:44 -08001291/**
1292 * Move the cursor backward to the previous tab stop, or to the first column
1293 * if no previous tab stops are set.
1294 */
1295hterm.Terminal.prototype.backwardTabStop = function() {
1296 var column = this.screen_.cursorPosition.column;
1297
1298 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1299 if (this.tabStops_[i] < column) {
1300 this.setCursorColumn(this.tabStops_[i]);
1301 return;
1302 }
1303 }
1304
1305 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001306};
1307
rgindac9bc5502012-01-18 11:48:44 -08001308/**
1309 * Set a tab stop at the given column.
1310 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001311 * @param {integer} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001312 */
1313hterm.Terminal.prototype.setTabStop = function(column) {
1314 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1315 if (this.tabStops_[i] == column)
1316 return;
1317
1318 if (this.tabStops_[i] < column) {
1319 this.tabStops_.splice(i + 1, 0, column);
1320 return;
1321 }
1322 }
1323
1324 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001325};
1326
rgindac9bc5502012-01-18 11:48:44 -08001327/**
1328 * Clear the tab stop at the current cursor position.
1329 *
1330 * No effect if there is no tab stop at the current cursor position.
1331 */
1332hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1333 var column = this.screen_.cursorPosition.column;
1334
1335 var i = this.tabStops_.indexOf(column);
1336 if (i == -1)
1337 return;
1338
1339 this.tabStops_.splice(i, 1);
1340};
1341
1342/**
1343 * Clear all tab stops.
1344 */
1345hterm.Terminal.prototype.clearAllTabStops = function() {
1346 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001347 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001348};
1349
1350/**
1351 * Set up the default tab stops, starting from a given column.
1352 *
1353 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001354 * from the specified column, or 0 if no column is provided. It also flags
1355 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001356 *
1357 * This does not clear the existing tab stops first, use clearAllTabStops
1358 * for that.
1359 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001360 * @param {integer} opt_start Optional starting zero based starting column, useful
rgindac9bc5502012-01-18 11:48:44 -08001361 * for filling out missing tab stops when the terminal is resized.
1362 */
1363hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1364 var start = opt_start || 0;
1365 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001366 // Round start up to a default tab stop.
1367 start = start - 1 - ((start - 1) % w) + w;
1368 for (var i = start; i < this.screenSize.width; i += w) {
1369 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001370 }
David Benjamin66e954d2012-05-05 21:08:12 -04001371
1372 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001373};
1374
rginda6d397402012-01-17 10:58:29 -08001375/**
rginda8ba33642011-12-14 12:31:31 -08001376 * Interpret a sequence of characters.
1377 *
1378 * Incomplete escape sequences are buffered until the next call.
1379 *
1380 * @param {string} str Sequence of characters to interpret or pass through.
1381 */
1382hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001383 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001384 this.scheduleSyncCursorPosition_();
1385};
1386
1387/**
1388 * Take over the given DIV for use as the terminal display.
1389 *
1390 * @param {HTMLDivElement} div The div to use as the terminal display.
1391 */
1392hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001393 this.div_ = div;
1394
rginda8ba33642011-12-14 12:31:31 -08001395 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001396 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001397 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1398 this.scrollPort_.setBackgroundPosition(
1399 this.prefs_.get('background-position'));
Mike Frysinger08bad432017-04-24 00:50:54 -04001400 this.scrollPort_.setUserCssUrl(this.prefs_.get('user-css'));
1401 this.scrollPort_.setUserCssText(this.prefs_.get('user-css-text'));
rginda30f20f62012-04-05 16:36:19 -07001402
rginda0918b652012-04-04 11:26:24 -07001403 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001404
rginda9f5222b2012-03-05 11:53:28 -08001405 this.setFontSize(this.prefs_.get('font-size'));
1406 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001407
David Reveman8f552492012-03-28 12:18:41 -04001408 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001409 this.setScrollWheelMoveMultipler(
1410 this.prefs_.get('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001411
rginda8ba33642011-12-14 12:31:31 -08001412 this.document_ = this.scrollPort_.getDocument();
1413
Evan Jones5f9df812016-12-06 09:38:58 -05001414 this.document_.body.oncontextmenu = function() { return false; };
rginda4bba5e12012-06-20 16:15:30 -07001415
1416 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001417 var screenNode = this.scrollPort_.getScreenNode();
1418 screenNode.addEventListener('mousedown', onMouse);
1419 screenNode.addEventListener('mouseup', onMouse);
1420 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001421 this.scrollPort_.onScrollWheel = onMouse;
1422
Toni Barzic0bfa8922013-11-22 11:18:35 -08001423 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001424 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001425 // Listen for mousedown events on the screenNode as in FF the focus
1426 // events don't bubble.
1427 screenNode.addEventListener('mousedown', function() {
1428 setTimeout(this.onFocusChange_.bind(this, true));
1429 }.bind(this));
1430
Toni Barzic0bfa8922013-11-22 11:18:35 -08001431 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001432 'blur', this.onFocusChange_.bind(this, false));
1433
1434 var style = this.document_.createElement('style');
1435 style.textContent =
1436 ('.cursor-node[focus="false"] {' +
1437 ' box-sizing: border-box;' +
1438 ' background-color: transparent !important;' +
1439 ' border-width: 2px;' +
1440 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001441 '}' +
1442 '.wc-node {' +
1443 ' display: inline-block;' +
1444 ' text-align: center;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001445 ' width: calc(var(--hterm-charsize-width) * 2);' +
Mike Frysinger4036f6e2017-05-31 14:02:55 -04001446 ' line-height: var(--hterm-charsize-height);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001447 '}' +
1448 ':root {' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001449 ' --hterm-charsize-width: ' + this.scrollPort_.characterSize.width + 'px;' +
1450 ' --hterm-charsize-height: ' + this.scrollPort_.characterSize.height + 'px;' +
Mike Frysingera27c0502017-08-23 22:37:10 -04001451 // Default position hides the cursor for when the window is initializing.
1452 ' --hterm-cursor-offset-col: -1;' +
1453 ' --hterm-cursor-offset-row: -1;' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001454 ' --hterm-blink-node-duration: 0.7s;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001455 ' --hterm-mouse-cursor-text: text;' +
1456 ' --hterm-mouse-cursor-pointer: default;' +
1457 ' --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001458 '}' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001459 '.uri-node:hover {' +
1460 ' text-decoration: underline;' +
1461 ' cursor: pointer;' +
1462 '}' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001463 '@keyframes blink {' +
1464 ' from { opacity: 1.0; }' +
1465 ' to { opacity: 0.0; }' +
1466 '}' +
1467 '.blink-node {' +
1468 ' animation-name: blink;' +
1469 ' animation-duration: var(--hterm-blink-node-duration);' +
1470 ' animation-iteration-count: infinite;' +
1471 ' animation-timing-function: ease-in-out;' +
1472 ' animation-direction: alternate;' +
rginda8e92a692012-05-20 19:37:20 -07001473 '}');
1474 this.document_.head.appendChild(style);
1475
rginda8ba33642011-12-14 12:31:31 -08001476 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001477 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001478 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001479 this.cursorNode_.style.cssText =
1480 ('position: absolute;' +
Mike Frysinger44c32202017-08-05 01:13:09 -04001481 'left: calc(var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));' +
1482 'top: calc(var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));' +
rginda87b86462011-12-14 13:48:03 -08001483 'display: block;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001484 'width: var(--hterm-charsize-width);' +
1485 'height: var(--hterm-charsize-height);' +
Rob Spies06533ba2014-04-24 11:20:37 -07001486 '-webkit-transition: opacity, background-color 100ms linear;' +
1487 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001488
rginda8e92a692012-05-20 19:37:20 -07001489 this.setCursorColor(this.prefs_.get('cursor-color'));
Robert Gindafb1be6a2013-12-11 11:56:22 -08001490 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1491 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001492
rginda8ba33642011-12-14 12:31:31 -08001493 this.document_.body.appendChild(this.cursorNode_);
1494
rgindad5613292012-06-19 15:40:37 -07001495 // When 'enableMouseDragScroll' is off we reposition this element directly
1496 // under the mouse cursor after a click. This makes Chrome associate
1497 // subsequent mousemove events with the scroll-blocker. Since the
1498 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1499 // events do not cause the scrollport to scroll.
1500 //
1501 // It's a hack, but it's the cleanest way I could find.
1502 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001503 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
rgindad5613292012-06-19 15:40:37 -07001504 this.scrollBlockerNode_.style.cssText =
1505 ('position: absolute;' +
1506 'top: -99px;' +
1507 'display: block;' +
1508 'width: 10px;' +
1509 'height: 10px;');
1510 this.document_.body.appendChild(this.scrollBlockerNode_);
1511
rgindad5613292012-06-19 15:40:37 -07001512 this.scrollPort_.onScrollWheel = onMouse;
1513 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1514 ].forEach(function(event) {
1515 this.scrollBlockerNode_.addEventListener(event, onMouse);
1516 this.cursorNode_.addEventListener(event, onMouse);
1517 this.document_.addEventListener(event, onMouse);
1518 }.bind(this));
1519
1520 this.cursorNode_.addEventListener('mousedown', function() {
1521 setTimeout(this.focus.bind(this));
1522 }.bind(this));
1523
rginda8ba33642011-12-14 12:31:31 -08001524 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001525
rginda87b86462011-12-14 13:48:03 -08001526 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001527 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001528};
1529
rginda0918b652012-04-04 11:26:24 -07001530/**
1531 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001532 *
1533 * @return {HTMLDocument}
rginda0918b652012-04-04 11:26:24 -07001534 */
rginda87b86462011-12-14 13:48:03 -08001535hterm.Terminal.prototype.getDocument = function() {
1536 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001537};
1538
1539/**
rginda0918b652012-04-04 11:26:24 -07001540 * Focus the terminal.
1541 */
1542hterm.Terminal.prototype.focus = function() {
1543 this.scrollPort_.focus();
1544};
1545
1546/**
rginda8ba33642011-12-14 12:31:31 -08001547 * Return the HTML Element for a given row index.
1548 *
1549 * This is a method from the RowProvider interface. The ScrollPort uses
1550 * it to fetch rows on demand as they are scrolled into view.
1551 *
1552 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1553 * pairs to conserve memory.
1554 *
1555 * @param {integer} index The zero-based row index, measured relative to the
1556 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001557 * largest indices.
rginda8ba33642011-12-14 12:31:31 -08001558 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1559 */
1560hterm.Terminal.prototype.getRowNode = function(index) {
1561 if (index < this.scrollbackRows_.length)
1562 return this.scrollbackRows_[index];
1563
1564 var screenIndex = index - this.scrollbackRows_.length;
1565 return this.screen_.rowsArray[screenIndex];
1566};
1567
1568/**
1569 * Return the text content for a given range of rows.
1570 *
1571 * This is a method from the RowProvider interface. The ScrollPort uses
1572 * it to fetch text content on demand when the user attempts to copy their
1573 * selection to the clipboard.
1574 *
1575 * @param {integer} start The zero-based row index to start from, measured
1576 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001577 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001578 * @param {integer} end The zero-based row index to end on, measured
1579 * relative to the start of the scrollback buffer.
1580 * @return {string} A single string containing the text value of the range of
1581 * rows. Lines will be newline delimited, with no trailing newline.
1582 */
1583hterm.Terminal.prototype.getRowsText = function(start, end) {
1584 var ary = [];
1585 for (var i = start; i < end; i++) {
1586 var node = this.getRowNode(i);
1587 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001588 if (i < end - 1 && !node.getAttribute('line-overflow'))
1589 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001590 }
1591
rgindaa09e7332012-08-17 12:49:51 -07001592 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001593};
1594
1595/**
1596 * Return the text content for a given row.
1597 *
1598 * This is a method from the RowProvider interface. The ScrollPort uses
1599 * it to fetch text content on demand when the user attempts to copy their
1600 * selection to the clipboard.
1601 *
1602 * @param {integer} index The zero-based row index to return, measured
1603 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001604 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001605 * @return {string} A string containing the text value of the selected row.
1606 */
1607hterm.Terminal.prototype.getRowText = function(index) {
1608 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001609 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001610};
1611
1612/**
1613 * Return the total number of rows in the addressable screen and in the
1614 * scrollback buffer of this terminal.
1615 *
1616 * This is a method from the RowProvider interface. The ScrollPort uses
1617 * it to compute the size of the scrollbar.
1618 *
1619 * @return {integer} The number of rows in this terminal.
1620 */
1621hterm.Terminal.prototype.getRowCount = function() {
1622 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1623};
1624
1625/**
1626 * Create DOM nodes for new rows and append them to the end of the terminal.
1627 *
1628 * This is the only correct way to add a new DOM node for a row. Notice that
1629 * the new row is appended to the bottom of the list of rows, and does not
1630 * require renumbering (of the rowIndex property) of previous rows.
1631 *
1632 * If you think you want a new blank row somewhere in the middle of the
1633 * terminal, look into moveRows_().
1634 *
1635 * This method does not pay attention to vtScrollTop/Bottom, since you should
1636 * be using moveRows() in cases where they would matter.
1637 *
1638 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001639 *
1640 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001641 */
1642hterm.Terminal.prototype.appendRows_ = function(count) {
1643 var cursorRow = this.screen_.rowsArray.length;
1644 var offset = this.scrollbackRows_.length + cursorRow;
1645 for (var i = 0; i < count; i++) {
1646 var row = this.document_.createElement('x-row');
1647 row.appendChild(this.document_.createTextNode(''));
1648 row.rowIndex = offset + i;
1649 this.screen_.pushRow(row);
1650 }
1651
1652 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1653 if (extraRows > 0) {
1654 var ary = this.screen_.shiftRows(extraRows);
1655 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001656 if (this.scrollPort_.isScrolledEnd)
1657 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001658 }
1659
1660 if (cursorRow >= this.screen_.rowsArray.length)
1661 cursorRow = this.screen_.rowsArray.length - 1;
1662
rginda87b86462011-12-14 13:48:03 -08001663 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001664};
1665
1666/**
1667 * Relocate rows from one part of the addressable screen to another.
1668 *
1669 * This is used to recycle rows during VT scrolls (those which are driven
1670 * by VT commands, rather than by the user manipulating the scrollbar.)
1671 *
1672 * In this case, the blank lines scrolled into the scroll region are made of
1673 * the nodes we scrolled off. These have their rowIndex properties carefully
1674 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001675 *
1676 * @param {number} fromIndex The start index.
1677 * @param {number} count The number of rows to move.
1678 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001679 */
1680hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1681 var ary = this.screen_.removeRows(fromIndex, count);
1682 this.screen_.insertRows(toIndex, ary);
1683
1684 var start, end;
1685 if (fromIndex < toIndex) {
1686 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001687 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001688 } else {
1689 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001690 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001691 }
1692
1693 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001694 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001695};
1696
1697/**
1698 * Renumber the rowIndex property of the given range of rows.
1699 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001700 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001701 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001702 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001703 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001704 *
1705 * @param {number} start The start index.
1706 * @param {number} end The end index.
1707 * @param {hterm.Screen} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001708 */
Robert Ginda40932892012-12-10 17:26:40 -08001709hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1710 var screen = opt_screen || this.screen_;
1711
rginda8ba33642011-12-14 12:31:31 -08001712 var offset = this.scrollbackRows_.length;
1713 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001714 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001715 }
1716};
1717
1718/**
1719 * Print a string to the terminal.
1720 *
1721 * This respects the current insert and wraparound modes. It will add new lines
1722 * to the end of the terminal, scrolling off the top into the scrollback buffer
1723 * if necessary.
1724 *
1725 * The string is *not* parsed for escape codes. Use the interpret() method if
1726 * that's what you're after.
1727 *
1728 * @param{string} str The string to print.
1729 */
1730hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001731 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001732
Ricky Liang48f05cb2013-12-31 23:35:29 +08001733 var strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04001734 // Fun edge case: If the string only contains zero width codepoints (like
1735 // combining characters), we make sure to iterate at least once below.
1736 if (strWidth == 0 && str)
1737 strWidth = 1;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001738
1739 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001740 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1741 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001742 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001743 }
rgindaa19afe22012-01-25 15:40:22 -08001744
Ricky Liang48f05cb2013-12-31 23:35:29 +08001745 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001746 var didOverflow = false;
1747 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001748
rgindaa9abdd82012-08-06 18:05:09 -07001749 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1750 didOverflow = true;
1751 count = this.screenSize.width - this.screen_.cursorPosition.column;
1752 }
rgindaa19afe22012-01-25 15:40:22 -08001753
rgindaa9abdd82012-08-06 18:05:09 -07001754 if (didOverflow && !this.options_.wraparound) {
1755 // If the string overflowed the line but wraparound is off, then the
1756 // last printed character should be the last of the string.
1757 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001758 substr = lib.wc.substr(str, startOffset, count - 1) +
1759 lib.wc.substr(str, strWidth - 1);
1760 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001761 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001762 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001763 }
rgindaa19afe22012-01-25 15:40:22 -08001764
Ricky Liang48f05cb2013-12-31 23:35:29 +08001765 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1766 for (var i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001767 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
1768 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001769
1770 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001771 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001772 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001773 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001774 }
1775 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001776 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07001777 }
1778
1779 this.screen_.maybeClipCurrentRow();
1780 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001781 }
rginda8ba33642011-12-14 12:31:31 -08001782
1783 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001784
rginda9f5222b2012-03-05 11:53:28 -08001785 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001786 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001787};
1788
1789/**
rginda87b86462011-12-14 13:48:03 -08001790 * Set the VT scroll region.
1791 *
rginda87b86462011-12-14 13:48:03 -08001792 * This also resets the cursor position to the absolute (0, 0) position, since
1793 * that's what xterm appears to do.
1794 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001795 * Setting the scroll region to the full height of the terminal will clear
1796 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1797 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1798 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1799 * continue to work as most users would expect.
1800 *
rginda87b86462011-12-14 13:48:03 -08001801 * @param {integer} scrollTop The zero-based top of the scroll region.
1802 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1803 * inclusive.
1804 */
1805hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001806 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001807 this.vtScrollTop_ = null;
1808 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001809 } else {
1810 this.vtScrollTop_ = scrollTop;
1811 this.vtScrollBottom_ = scrollBottom;
1812 }
rginda87b86462011-12-14 13:48:03 -08001813};
1814
1815/**
rginda8ba33642011-12-14 12:31:31 -08001816 * Return the top row index according to the VT.
1817 *
1818 * This will return 0 unless the terminal has been told to restrict scrolling
1819 * to some lower row. It is used for some VT cursor positioning and scrolling
1820 * commands.
1821 *
1822 * @return {integer} The topmost row in the terminal's scroll region.
1823 */
1824hterm.Terminal.prototype.getVTScrollTop = function() {
1825 if (this.vtScrollTop_ != null)
1826 return this.vtScrollTop_;
1827
1828 return 0;
rginda87b86462011-12-14 13:48:03 -08001829};
rginda8ba33642011-12-14 12:31:31 -08001830
1831/**
1832 * Return the bottom row index according to the VT.
1833 *
1834 * This will return the height of the terminal unless the it has been told to
1835 * restrict scrolling to some higher row. It is used for some VT cursor
1836 * positioning and scrolling commands.
1837 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001838 * @return {integer} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001839 */
1840hterm.Terminal.prototype.getVTScrollBottom = function() {
1841 if (this.vtScrollBottom_ != null)
1842 return this.vtScrollBottom_;
1843
rginda87b86462011-12-14 13:48:03 -08001844 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001845}
1846
1847/**
1848 * Process a '\n' character.
1849 *
1850 * If the cursor is on the final row of the terminal this will append a new
1851 * blank row to the screen and scroll the topmost row into the scrollback
1852 * buffer.
1853 *
1854 * Otherwise, this moves the cursor to column zero of the next row.
1855 */
1856hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001857 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1858 this.screen_.rowsArray.length - 1);
1859
1860 if (this.vtScrollBottom_ != null) {
1861 // A VT Scroll region is active, we never append new rows.
1862 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1863 // We're at the end of the VT Scroll Region, perform a VT scroll.
1864 this.vtScrollUp(1);
1865 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1866 } else if (cursorAtEndOfScreen) {
1867 // We're at the end of the screen, the only thing to do is put the
1868 // cursor to column 0.
1869 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1870 } else {
1871 // Anywhere else, advance the cursor row, and reset the column.
1872 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1873 }
1874 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001875 // We're at the end of the screen. Append a new row to the terminal,
1876 // shifting the top row into the scrollback.
1877 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001878 } else {
rginda87b86462011-12-14 13:48:03 -08001879 // Anywhere else in the screen just moves the cursor.
1880 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001881 }
1882};
1883
1884/**
1885 * Like newLine(), except maintain the cursor column.
1886 */
1887hterm.Terminal.prototype.lineFeed = function() {
1888 var column = this.screen_.cursorPosition.column;
1889 this.newLine();
1890 this.setCursorColumn(column);
1891};
1892
1893/**
rginda87b86462011-12-14 13:48:03 -08001894 * If autoCarriageReturn is set then newLine(), else lineFeed().
1895 */
1896hterm.Terminal.prototype.formFeed = function() {
1897 if (this.options_.autoCarriageReturn) {
1898 this.newLine();
1899 } else {
1900 this.lineFeed();
1901 }
1902};
1903
1904/**
1905 * Move the cursor up one row, possibly inserting a blank line.
1906 *
1907 * The cursor column is not changed.
1908 */
1909hterm.Terminal.prototype.reverseLineFeed = function() {
1910 var scrollTop = this.getVTScrollTop();
1911 var currentRow = this.screen_.cursorPosition.row;
1912
1913 if (currentRow == scrollTop) {
1914 this.insertLines(1);
1915 } else {
1916 this.setAbsoluteCursorRow(currentRow - 1);
1917 }
1918};
1919
1920/**
rginda8ba33642011-12-14 12:31:31 -08001921 * Replace all characters to the left of the current cursor with the space
1922 * character.
1923 *
1924 * TODO(rginda): This should probably *remove* the characters (not just replace
1925 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001926 * position.
rginda8ba33642011-12-14 12:31:31 -08001927 */
1928hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001929 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001930 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04001931 const count = cursor.column + 1;
1932 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08001933 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001934};
1935
1936/**
David Benjamin684a9b72012-05-01 17:19:58 -04001937 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001938 *
1939 * The cursor position is unchanged.
1940 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001941 * If the current background color is not the default background color this
1942 * will insert spaces rather than delete. This is unfortunate because the
1943 * trailing space will affect text selection, but it's difficult to come up
1944 * with a way to style empty space that wouldn't trip up the hterm.Screen
1945 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001946 *
1947 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1948 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1949 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05001950 *
1951 * @param {number} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08001952 */
1953hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001954 if (this.screen_.cursorPosition.overflow)
1955 return;
1956
Robert Ginda7fd57082012-09-25 14:41:47 -07001957 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1958 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001959
1960 if (this.screen_.textAttributes.background ===
1961 this.screen_.textAttributes.DEFAULT_COLOR) {
1962 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08001963 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07001964 this.screen_.cursorPosition.column + count) {
1965 this.screen_.deleteChars(count);
1966 this.clearCursorOverflow();
1967 return;
1968 }
1969 }
1970
rginda87b86462011-12-14 13:48:03 -08001971 var cursor = this.saveCursor();
Mike Frysinger6380bed2017-08-24 18:46:39 -04001972 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08001973 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001974 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001975};
1976
1977/**
1978 * Erase the current line.
1979 *
1980 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001981 */
1982hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001983 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001984 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001985 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001986 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001987};
1988
1989/**
David Benjamina08d78f2012-05-05 00:28:49 -04001990 * Erase all characters from the start of the screen to the current cursor
1991 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001992 *
1993 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001994 */
1995hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001996 var cursor = this.saveCursor();
1997
1998 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001999
David Benjamina08d78f2012-05-05 00:28:49 -04002000 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002001 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002002 this.screen_.clearCursorRow();
2003 }
2004
rginda87b86462011-12-14 13:48:03 -08002005 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002006 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002007};
2008
2009/**
2010 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002011 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002012 *
2013 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002014 */
2015hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08002016 var cursor = this.saveCursor();
2017
2018 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002019
David Benjamina08d78f2012-05-05 00:28:49 -04002020 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08002021 for (var i = cursor.row + 1; i <= bottom; i++) {
2022 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002023 this.screen_.clearCursorRow();
2024 }
2025
rginda87b86462011-12-14 13:48:03 -08002026 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002027 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002028};
2029
2030/**
2031 * Fill the terminal with a given character.
2032 *
2033 * This methods does not respect the VT scroll region.
2034 *
2035 * @param {string} ch The character to use for the fill.
2036 */
2037hterm.Terminal.prototype.fill = function(ch) {
2038 var cursor = this.saveCursor();
2039
2040 this.setAbsoluteCursorPosition(0, 0);
2041 for (var row = 0; row < this.screenSize.height; row++) {
2042 for (var col = 0; col < this.screenSize.width; col++) {
2043 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002044 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002045 }
2046 }
2047
2048 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002049};
2050
2051/**
rginda9ea433c2012-03-16 11:57:00 -07002052 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002053 *
rginda9ea433c2012-03-16 11:57:00 -07002054 * This does not respect the scroll region.
2055 *
2056 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2057 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002058 */
rginda9ea433c2012-03-16 11:57:00 -07002059hterm.Terminal.prototype.clearHome = function(opt_screen) {
2060 var screen = opt_screen || this.screen_;
2061 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002062
rginda11057d52012-04-25 12:29:56 -07002063 if (bottom == 0) {
2064 // Empty screen, nothing to do.
2065 return;
2066 }
2067
rgindae4d29232012-01-19 10:47:13 -08002068 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002069 screen.setCursorPosition(i, 0);
2070 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002071 }
2072
rginda9ea433c2012-03-16 11:57:00 -07002073 screen.setCursorPosition(0, 0);
2074};
2075
2076/**
2077 * Erase the entire display without changing the cursor position.
2078 *
2079 * The cursor position is unchanged. This does not respect the scroll
2080 * region.
2081 *
2082 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2083 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002084 */
2085hterm.Terminal.prototype.clear = function(opt_screen) {
2086 var screen = opt_screen || this.screen_;
2087 var cursor = screen.cursorPosition.clone();
2088 this.clearHome(screen);
2089 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002090};
2091
2092/**
2093 * VT command to insert lines at the current cursor row.
2094 *
2095 * This respects the current scroll region. Rows pushed off the bottom are
2096 * lost (they won't show up in the scrollback buffer).
2097 *
rginda8ba33642011-12-14 12:31:31 -08002098 * @param {integer} count The number of lines to insert.
2099 */
2100hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002101 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002102
2103 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002104 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002105
Robert Ginda579186b2012-09-26 11:40:04 -07002106 // The moveCount is the number of rows we need to relocate to make room for
2107 // the new row(s). The count is the distance to move them.
2108 var moveCount = bottom - cursorRow - count + 1;
2109 if (moveCount)
2110 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002111
Robert Ginda579186b2012-09-26 11:40:04 -07002112 for (var i = count - 1; i >= 0; i--) {
2113 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002114 this.screen_.clearCursorRow();
2115 }
rginda8ba33642011-12-14 12:31:31 -08002116};
2117
2118/**
2119 * VT command to delete lines at the current cursor row.
2120 *
2121 * New rows are added to the bottom of scroll region to take their place. New
2122 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002123 *
2124 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002125 */
2126hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002127 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002128
rginda87b86462011-12-14 13:48:03 -08002129 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002130 var bottom = this.getVTScrollBottom();
2131
rginda87b86462011-12-14 13:48:03 -08002132 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002133 count = Math.min(count, maxCount);
2134
rginda87b86462011-12-14 13:48:03 -08002135 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002136 if (count != maxCount)
2137 this.moveRows_(top, count, moveStart);
2138
2139 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002140 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002141 this.screen_.clearCursorRow();
2142 }
2143
rginda87b86462011-12-14 13:48:03 -08002144 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002145 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002146};
2147
2148/**
2149 * Inserts the given number of spaces at the current cursor position.
2150 *
rginda87b86462011-12-14 13:48:03 -08002151 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002152 *
2153 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002154 */
2155hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002156 var cursor = this.saveCursor();
2157
rgindacbbd7482012-06-13 15:06:16 -07002158 var ws = lib.f.getWhitespace(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002159 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002160 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002161
2162 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002163 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002164};
2165
2166/**
2167 * Forward-delete the specified number of characters starting at the cursor
2168 * position.
2169 *
2170 * @param {integer} count The number of characters to delete.
2171 */
2172hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002173 var deleted = this.screen_.deleteChars(count);
2174 if (deleted && !this.screen_.textAttributes.isDefault()) {
2175 var cursor = this.saveCursor();
2176 this.setCursorColumn(this.screenSize.width - deleted);
2177 this.screen_.insertString(lib.f.getWhitespace(deleted));
2178 this.restoreCursor(cursor);
2179 }
2180
David Benjamin54e8bf62012-06-01 22:31:40 -04002181 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002182};
2183
2184/**
2185 * Shift rows in the scroll region upwards by a given number of lines.
2186 *
2187 * New rows are inserted at the bottom of the scroll region to fill the
2188 * vacated rows. The new rows not filled out with the current text attributes.
2189 *
2190 * This function does not affect the scrollback rows at all. Rows shifted
2191 * off the top are lost.
2192 *
rginda87b86462011-12-14 13:48:03 -08002193 * The cursor position is not altered.
2194 *
rginda8ba33642011-12-14 12:31:31 -08002195 * @param {integer} count The number of rows to scroll.
2196 */
2197hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002198 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002199
rginda87b86462011-12-14 13:48:03 -08002200 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002201 this.deleteLines(count);
2202
rginda87b86462011-12-14 13:48:03 -08002203 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002204};
2205
2206/**
2207 * Shift rows below the cursor down by a given number of lines.
2208 *
2209 * This function respects the current scroll region.
2210 *
2211 * New rows are inserted at the top of the scroll region to fill the
2212 * vacated rows. The new rows not filled out with the current text attributes.
2213 *
2214 * This function does not affect the scrollback rows at all. Rows shifted
2215 * off the bottom are lost.
2216 *
2217 * @param {integer} count The number of rows to scroll.
2218 */
2219hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08002220 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002221
rginda87b86462011-12-14 13:48:03 -08002222 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08002223 this.insertLines(opt_count);
2224
rginda87b86462011-12-14 13:48:03 -08002225 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002226};
2227
rginda87b86462011-12-14 13:48:03 -08002228
rginda8ba33642011-12-14 12:31:31 -08002229/**
2230 * Set the cursor position.
2231 *
2232 * The cursor row is relative to the scroll region if the terminal has
2233 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2234 *
2235 * @param {integer} row The new zero-based cursor row.
2236 * @param {integer} row The new zero-based cursor column.
2237 */
2238hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2239 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002240 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002241 } else {
rginda87b86462011-12-14 13:48:03 -08002242 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002243 }
rginda87b86462011-12-14 13:48:03 -08002244};
rginda8ba33642011-12-14 12:31:31 -08002245
Evan Jones2600d4f2016-12-06 09:29:36 -05002246/**
2247 * Move the cursor relative to its current position.
2248 *
2249 * @param {number} row
2250 * @param {number} column
2251 */
rginda87b86462011-12-14 13:48:03 -08002252hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2253 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002254 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2255 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002256 this.screen_.setCursorPosition(row, column);
2257};
2258
Evan Jones2600d4f2016-12-06 09:29:36 -05002259/**
2260 * Move the cursor to the specified position.
2261 *
2262 * @param {number} row
2263 * @param {number} column
2264 */
rginda87b86462011-12-14 13:48:03 -08002265hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002266 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2267 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002268 this.screen_.setCursorPosition(row, column);
2269};
2270
2271/**
2272 * Set the cursor column.
2273 *
2274 * @param {integer} column The new zero-based cursor column.
2275 */
2276hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002277 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002278};
2279
2280/**
2281 * Return the cursor column.
2282 *
2283 * @return {integer} The zero-based cursor column.
2284 */
2285hterm.Terminal.prototype.getCursorColumn = function() {
2286 return this.screen_.cursorPosition.column;
2287};
2288
2289/**
2290 * Set the cursor row.
2291 *
2292 * The cursor row is relative to the scroll region if the terminal has
2293 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2294 *
2295 * @param {integer} row The new cursor row.
2296 */
rginda87b86462011-12-14 13:48:03 -08002297hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2298 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002299};
2300
2301/**
2302 * Return the cursor row.
2303 *
2304 * @return {integer} The zero-based cursor row.
2305 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002306hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002307 return this.screen_.cursorPosition.row;
2308};
2309
2310/**
2311 * Request that the ScrollPort redraw itself soon.
2312 *
2313 * The redraw will happen asynchronously, soon after the call stack winds down.
2314 * Multiple calls will be coalesced into a single redraw.
2315 */
2316hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002317 if (this.timeouts_.redraw)
2318 return;
rginda8ba33642011-12-14 12:31:31 -08002319
2320 var self = this;
rginda87b86462011-12-14 13:48:03 -08002321 this.timeouts_.redraw = setTimeout(function() {
2322 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002323 self.scrollPort_.redraw_();
2324 }, 0);
2325};
2326
2327/**
2328 * Request that the ScrollPort be scrolled to the bottom.
2329 *
2330 * The scroll will happen asynchronously, soon after the call stack winds down.
2331 * Multiple calls will be coalesced into a single scroll.
2332 *
2333 * This affects the scrollbar position of the ScrollPort, and has nothing to
2334 * do with the VT scroll commands.
2335 */
2336hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2337 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002338 return;
rginda8ba33642011-12-14 12:31:31 -08002339
2340 var self = this;
2341 this.timeouts_.scrollDown = setTimeout(function() {
2342 delete self.timeouts_.scrollDown;
2343 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2344 }, 10);
2345};
2346
2347/**
2348 * Move the cursor up a specified number of rows.
2349 *
2350 * @param {integer} count The number of rows to move the cursor.
2351 */
2352hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002353 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002354};
2355
2356/**
2357 * Move the cursor down a specified number of rows.
2358 *
2359 * @param {integer} count The number of rows to move the cursor.
2360 */
2361hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002362 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002363 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2364 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2365 this.screenSize.height - 1);
2366
rgindacbbd7482012-06-13 15:06:16 -07002367 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002368 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002369 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002370};
2371
2372/**
2373 * Move the cursor left a specified number of columns.
2374 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002375 * If reverse wraparound mode is enabled and the previous row wrapped into
2376 * the current row then we back up through the wraparound as well.
2377 *
rginda8ba33642011-12-14 12:31:31 -08002378 * @param {integer} count The number of columns to move the cursor.
2379 */
2380hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002381 count = count || 1;
2382
2383 if (count < 1)
2384 return;
2385
2386 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002387 if (this.options_.reverseWraparound) {
2388 if (this.screen_.cursorPosition.overflow) {
2389 // If this cursor is in the right margin, consume one count to get it
2390 // back to the last column. This only applies when we're in reverse
2391 // wraparound mode.
2392 count--;
2393 this.clearCursorOverflow();
2394
2395 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002396 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002397 }
2398
Robert Gindabfb32622014-07-17 13:20:27 -07002399 var newRow = this.screen_.cursorPosition.row;
2400 var newColumn = currentColumn - count;
2401 if (newColumn < 0) {
2402 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2403 if (newRow < 0) {
2404 // xterm also wraps from row 0 to the last row.
2405 newRow = this.screenSize.height + newRow % this.screenSize.height;
2406 }
2407 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2408 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002409
Robert Gindabfb32622014-07-17 13:20:27 -07002410 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2411
2412 } else {
2413 var newColumn = Math.max(currentColumn - count, 0);
2414 this.setCursorColumn(newColumn);
2415 }
rginda8ba33642011-12-14 12:31:31 -08002416};
2417
2418/**
2419 * Move the cursor right a specified number of columns.
2420 *
2421 * @param {integer} count The number of columns to move the cursor.
2422 */
2423hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002424 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002425
2426 if (count < 1)
2427 return;
2428
rgindacbbd7482012-06-13 15:06:16 -07002429 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002430 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002431 this.setCursorColumn(column);
2432};
2433
2434/**
2435 * Reverse the foreground and background colors of the terminal.
2436 *
2437 * This only affects text that was drawn with no attributes.
2438 *
2439 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2440 * been drawn with attributes that happen to coincide with the default
2441 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002442 *
2443 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002444 */
2445hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002446 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002447 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002448 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2449 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002450 } else {
rginda9f5222b2012-03-05 11:53:28 -08002451 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2452 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002453 }
2454};
2455
2456/**
rginda87b86462011-12-14 13:48:03 -08002457 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002458 *
2459 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002460 */
2461hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002462 this.cursorNode_.style.backgroundColor =
2463 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002464
2465 var self = this;
2466 setTimeout(function() {
Matheus Fernandes2d733082017-09-11 06:43:01 -04002467 self.restyleCursor_();
rginda6d397402012-01-17 10:58:29 -08002468 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002469
Michael Kelly485ecd12014-06-09 11:41:56 -04002470 // bellSquelchTimeout_ affects both audio and notification bells.
2471 if (this.bellSquelchTimeout_)
2472 return;
2473
Robert Ginda92e18102013-03-14 13:56:37 -07002474 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002475 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002476 this.bellSequelchTimeout_ = setTimeout(function() {
2477 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002478 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002479 } else {
2480 delete this.bellSquelchTimeout_;
2481 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002482
2483 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002484 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002485 this.bellNotificationList_.push(n);
2486 // TODO: Should we try to raise the window here?
2487 n.onclick = function() { self.closeBellNotifications_(); };
2488 }
rginda87b86462011-12-14 13:48:03 -08002489};
2490
2491/**
rginda8ba33642011-12-14 12:31:31 -08002492 * Set the origin mode bit.
2493 *
2494 * If origin mode is on, certain VT cursor and scrolling commands measure their
2495 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2496 * to the top of the addressable screen.
2497 *
2498 * Defaults to off.
2499 *
2500 * @param {boolean} state True to set origin mode, false to unset.
2501 */
2502hterm.Terminal.prototype.setOriginMode = function(state) {
2503 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002504 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002505};
2506
2507/**
2508 * Set the insert mode bit.
2509 *
2510 * If insert mode is on, existing text beyond the cursor position will be
2511 * shifted right to make room for new text. Otherwise, new text overwrites
2512 * any existing text.
2513 *
2514 * Defaults to off.
2515 *
2516 * @param {boolean} state True to set insert mode, false to unset.
2517 */
2518hterm.Terminal.prototype.setInsertMode = function(state) {
2519 this.options_.insertMode = state;
2520};
2521
2522/**
rginda87b86462011-12-14 13:48:03 -08002523 * Set the auto carriage return bit.
2524 *
2525 * If auto carriage return is on then a formfeed character is interpreted
2526 * as a newline, otherwise it's the same as a linefeed. The difference boils
2527 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002528 *
2529 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002530 */
2531hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2532 this.options_.autoCarriageReturn = state;
2533};
2534
2535/**
rginda8ba33642011-12-14 12:31:31 -08002536 * Set the wraparound mode bit.
2537 *
2538 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2539 * to the start of the following row. Otherwise, the cursor is clamped to the
2540 * end of the screen and attempts to write past it are ignored.
2541 *
2542 * Defaults to on.
2543 *
2544 * @param {boolean} state True to set wraparound mode, false to unset.
2545 */
2546hterm.Terminal.prototype.setWraparound = function(state) {
2547 this.options_.wraparound = state;
2548};
2549
2550/**
2551 * Set the reverse-wraparound mode bit.
2552 *
2553 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2554 * to the end of the previous row. Otherwise, the cursor is clamped to column
2555 * 0.
2556 *
2557 * Defaults to off.
2558 *
2559 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2560 */
2561hterm.Terminal.prototype.setReverseWraparound = function(state) {
2562 this.options_.reverseWraparound = state;
2563};
2564
2565/**
2566 * Selects between the primary and alternate screens.
2567 *
2568 * If alternate mode is on, the alternate screen is active. Otherwise the
2569 * primary screen is active.
2570 *
2571 * Swapping screens has no effect on the scrollback buffer.
2572 *
2573 * Each screen maintains its own cursor position.
2574 *
2575 * Defaults to off.
2576 *
2577 * @param {boolean} state True to set alternate mode, false to unset.
2578 */
2579hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002580 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002581 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2582
rginda35c456b2012-02-09 17:29:05 -08002583 if (this.screen_.rowsArray.length &&
2584 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2585 // If the screen changed sizes while we were away, our rowIndexes may
2586 // be incorrect.
2587 var offset = this.scrollbackRows_.length;
2588 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002589 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002590 ary[i].rowIndex = offset + i;
2591 }
2592 }
rginda8ba33642011-12-14 12:31:31 -08002593
rginda35c456b2012-02-09 17:29:05 -08002594 this.realizeWidth_(this.screenSize.width);
2595 this.realizeHeight_(this.screenSize.height);
2596 this.scrollPort_.syncScrollHeight();
2597 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002598
rginda6d397402012-01-17 10:58:29 -08002599 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002600 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002601};
2602
2603/**
2604 * Set the cursor-blink mode bit.
2605 *
2606 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2607 * a visible cursor does not blink.
2608 *
2609 * You should make sure to turn blinking off if you're going to dispose of a
2610 * terminal, otherwise you'll leak a timeout.
2611 *
2612 * Defaults to on.
2613 *
2614 * @param {boolean} state True to set cursor-blink mode, false to unset.
2615 */
2616hterm.Terminal.prototype.setCursorBlink = function(state) {
2617 this.options_.cursorBlink = state;
2618
2619 if (!state && this.timeouts_.cursorBlink) {
2620 clearTimeout(this.timeouts_.cursorBlink);
2621 delete this.timeouts_.cursorBlink;
2622 }
2623
2624 if (this.options_.cursorVisible)
2625 this.setCursorVisible(true);
2626};
2627
2628/**
2629 * Set the cursor-visible mode bit.
2630 *
2631 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2632 *
2633 * Defaults to on.
2634 *
2635 * @param {boolean} state True to set cursor-visible mode, false to unset.
2636 */
2637hterm.Terminal.prototype.setCursorVisible = function(state) {
2638 this.options_.cursorVisible = state;
2639
2640 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002641 if (this.timeouts_.cursorBlink) {
2642 clearTimeout(this.timeouts_.cursorBlink);
2643 delete this.timeouts_.cursorBlink;
2644 }
rginda87b86462011-12-14 13:48:03 -08002645 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002646 return;
2647 }
2648
rginda87b86462011-12-14 13:48:03 -08002649 this.syncCursorPosition_();
2650
2651 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002652
2653 if (this.options_.cursorBlink) {
2654 if (this.timeouts_.cursorBlink)
2655 return;
2656
Robert Gindaea2183e2014-07-17 09:51:51 -07002657 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002658 } else {
2659 if (this.timeouts_.cursorBlink) {
2660 clearTimeout(this.timeouts_.cursorBlink);
2661 delete this.timeouts_.cursorBlink;
2662 }
2663 }
2664};
2665
2666/**
rginda87b86462011-12-14 13:48:03 -08002667 * Synchronizes the visible cursor and document selection with the current
2668 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002669 */
2670hterm.Terminal.prototype.syncCursorPosition_ = function() {
2671 var topRowIndex = this.scrollPort_.getTopRowIndex();
2672 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2673 var cursorRowIndex = this.scrollbackRows_.length +
2674 this.screen_.cursorPosition.row;
2675
2676 if (cursorRowIndex > bottomRowIndex) {
2677 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04002678 this.setCssVar('cursor-offset-row', '-1');
rginda8ba33642011-12-14 12:31:31 -08002679 return;
2680 }
2681
Robert Gindab837c052014-08-11 11:17:51 -07002682 if (this.options_.cursorVisible &&
2683 this.cursorNode_.style.display == 'none') {
2684 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2685 this.cursorNode_.style.display = '';
2686 }
2687
Mike Frysinger44c32202017-08-05 01:13:09 -04002688 // Position the cursor using CSS variable math. If we do the math in JS,
2689 // the float math will end up being more precise than the CSS which will
2690 // cause the cursor tracking to be off.
2691 this.setCssVar(
2692 'cursor-offset-row',
2693 `${cursorRowIndex - topRowIndex} + ` +
2694 `${this.scrollPort_.visibleRowTopMargin}px`);
2695 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08002696
2697 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04002698 '(' + this.screen_.cursorPosition.column +
2699 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08002700 ')');
2701
2702 // Update the caret for a11y purposes.
2703 var selection = this.document_.getSelection();
2704 if (selection && selection.isCollapsed)
2705 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002706};
2707
Robert Gindafb1be6a2013-12-11 11:56:22 -08002708/**
2709 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2710 * and character cell dimensions.
2711 */
Robert Ginda830583c2013-08-07 13:20:46 -07002712hterm.Terminal.prototype.restyleCursor_ = function() {
2713 var shape = this.cursorShape_;
2714
2715 if (this.cursorNode_.getAttribute('focus') == 'false') {
2716 // Always show a block cursor when unfocused.
2717 shape = hterm.Terminal.cursorShape.BLOCK;
2718 }
2719
2720 var style = this.cursorNode_.style;
2721
2722 switch (shape) {
2723 case hterm.Terminal.cursorShape.BEAM:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002724 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002725 style.backgroundColor = 'transparent';
2726 style.borderBottomStyle = null;
2727 style.borderLeftStyle = 'solid';
2728 break;
2729
2730 case hterm.Terminal.cursorShape.UNDERLINE:
2731 style.height = this.scrollPort_.characterSize.baseline + 'px';
2732 style.backgroundColor = 'transparent';
2733 style.borderBottomStyle = 'solid';
2734 // correct the size to put it exactly at the baseline
2735 style.borderLeftStyle = null;
2736 break;
2737
2738 default:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002739 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002740 style.backgroundColor = this.cursorColor_;
2741 style.borderBottomStyle = null;
2742 style.borderLeftStyle = null;
2743 break;
2744 }
2745};
2746
rginda8ba33642011-12-14 12:31:31 -08002747/**
2748 * Synchronizes the visible cursor with the current cursor coordinates.
2749 *
2750 * The sync will happen asynchronously, soon after the call stack winds down.
2751 * Multiple calls will be coalesced into a single sync.
2752 */
2753hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2754 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002755 return;
rginda8ba33642011-12-14 12:31:31 -08002756
2757 var self = this;
2758 this.timeouts_.syncCursor = setTimeout(function() {
2759 self.syncCursorPosition_();
2760 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002761 }, 0);
2762};
2763
rgindacc2996c2012-02-24 14:59:31 -08002764/**
rgindaf522ce02012-04-17 17:49:17 -07002765 * Show or hide the zoom warning.
2766 *
2767 * The zoom warning is a message warning the user that their browser zoom must
2768 * be set to 100% in order for hterm to function properly.
2769 *
2770 * @param {boolean} state True to show the message, false to hide it.
2771 */
2772hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2773 if (!this.zoomWarningNode_) {
2774 if (!state)
2775 return;
2776
2777 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002778 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07002779 this.zoomWarningNode_.style.cssText = (
2780 'color: black;' +
2781 'background-color: #ff2222;' +
2782 'font-size: large;' +
2783 'border-radius: 8px;' +
2784 'opacity: 0.75;' +
2785 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2786 'top: 0.5em;' +
2787 'right: 1.2em;' +
2788 'position: absolute;' +
2789 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002790 '-webkit-user-select: none;' +
2791 '-moz-text-size-adjust: none;' +
2792 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002793
2794 this.zoomWarningNode_.addEventListener('click', function(e) {
2795 this.parentNode.removeChild(this);
2796 });
rgindaf522ce02012-04-17 17:49:17 -07002797 }
2798
Robert Gindab4839c22013-02-28 16:52:10 -08002799 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2800 hterm.zoomWarningMessage,
2801 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2802
rgindaf522ce02012-04-17 17:49:17 -07002803 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2804
2805 if (state) {
2806 if (!this.zoomWarningNode_.parentNode)
2807 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2808 } else if (this.zoomWarningNode_.parentNode) {
2809 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2810 }
2811};
2812
2813/**
rgindacc2996c2012-02-24 14:59:31 -08002814 * Show the terminal overlay for a given amount of time.
2815 *
2816 * The terminal overlay appears in inverse video in a large font, centered
2817 * over the terminal. You should probably keep the overlay message brief,
2818 * since it's in a large font and you probably aren't going to check the size
2819 * of the terminal first.
2820 *
2821 * @param {string} msg The text (not HTML) message to display in the overlay.
2822 * @param {number} opt_timeout The amount of time to wait before fading out
2823 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2824 * stay up forever (or until the next overlay).
2825 */
2826hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002827 if (!this.overlayNode_) {
2828 if (!this.div_)
2829 return;
2830
2831 this.overlayNode_ = this.document_.createElement('div');
2832 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002833 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002834 'font-size: xx-large;' +
2835 'opacity: 0.75;' +
2836 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2837 'position: absolute;' +
2838 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002839 '-webkit-transition: opacity 180ms ease-in;' +
2840 '-moz-user-select: none;' +
2841 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002842
2843 this.overlayNode_.addEventListener('mousedown', function(e) {
2844 e.preventDefault();
2845 e.stopPropagation();
2846 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002847 }
2848
rginda9f5222b2012-03-05 11:53:28 -08002849 this.overlayNode_.style.color = this.prefs_.get('background-color');
2850 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2851 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2852
rgindaf0090c92012-02-10 14:58:52 -08002853 this.overlayNode_.textContent = msg;
2854 this.overlayNode_.style.opacity = '0.75';
2855
2856 if (!this.overlayNode_.parentNode)
2857 this.div_.appendChild(this.overlayNode_);
2858
Robert Ginda97769282013-02-01 15:30:30 -08002859 var divSize = hterm.getClientSize(this.div_);
2860 var overlaySize = hterm.getClientSize(this.overlayNode_);
2861
Robert Ginda8a59f762014-07-23 11:29:55 -07002862 this.overlayNode_.style.top =
2863 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08002864 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07002865 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08002866
rgindaf0090c92012-02-10 14:58:52 -08002867 if (this.overlayTimeout_)
2868 clearTimeout(this.overlayTimeout_);
2869
rgindacc2996c2012-02-24 14:59:31 -08002870 if (opt_timeout === null)
2871 return;
2872
Mike Frysingerb6cfded2017-09-18 00:39:31 -04002873 this.overlayTimeout_ = setTimeout(() => {
2874 this.overlayNode_.style.opacity = '0';
2875 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
2876 }, opt_timeout || 1500);
2877};
2878
2879/**
2880 * Hide the terminal overlay immediately.
2881 *
2882 * Useful when we show an overlay for an event with an unknown end time.
2883 */
2884hterm.Terminal.prototype.hideOverlay = function() {
2885 if (this.overlayTimeout_)
2886 clearTimeout(this.overlayTimeout_);
2887 this.overlayTimeout_ = null;
2888
2889 if (this.overlayNode_.parentNode)
2890 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
2891 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08002892};
2893
rginda4bba5e12012-06-20 16:15:30 -07002894/**
2895 * Paste from the system clipboard to the terminal.
2896 */
2897hterm.Terminal.prototype.paste = function() {
Mike Frysinger4628ad22017-07-20 02:44:20 -04002898 return hterm.pasteFromClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002899};
2900
2901/**
2902 * Copy a string to the system clipboard.
2903 *
2904 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05002905 *
2906 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07002907 */
2908hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002909 if (this.prefs_.get('enable-clipboard-notice'))
2910 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2911
rgindaa09e7332012-08-17 12:49:51 -07002912 var copySource = this.document_.createElement('pre');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002913 copySource.id = 'hterm:copy-to-clipboard-source';
rginda4bba5e12012-06-20 16:15:30 -07002914 copySource.textContent = str;
2915 copySource.style.cssText = (
2916 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002917 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07002918 'position: absolute;' +
2919 'top: -99px');
2920
2921 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002922
rginda4bba5e12012-06-20 16:15:30 -07002923 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002924 var anchorNode = selection.anchorNode;
2925 var anchorOffset = selection.anchorOffset;
2926 var focusNode = selection.focusNode;
2927 var focusOffset = selection.focusOffset;
2928
rginda4bba5e12012-06-20 16:15:30 -07002929 selection.selectAllChildren(copySource);
2930
rgindaa09e7332012-08-17 12:49:51 -07002931 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002932
Rob Spies56953412014-04-28 14:09:47 -07002933 // IE doesn't support selection.extend. This means that the selection
2934 // won't return on IE.
Robert Ginda6f4f0aa2014-07-28 10:21:39 -07002935 if (selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07002936 selection.collapse(anchorNode, anchorOffset);
2937 selection.extend(focusNode, focusOffset);
2938 }
rgindafaa74742012-08-21 13:34:03 -07002939
rginda4bba5e12012-06-20 16:15:30 -07002940 copySource.parentNode.removeChild(copySource);
2941};
2942
Evan Jones2600d4f2016-12-06 09:29:36 -05002943/**
2944 * Returns the selected text, or null if no text is selected.
2945 *
2946 * @return {string|null}
2947 */
rgindaa09e7332012-08-17 12:49:51 -07002948hterm.Terminal.prototype.getSelectionText = function() {
2949 var selection = this.scrollPort_.selection;
2950 selection.sync();
2951
2952 if (selection.isCollapsed)
2953 return null;
2954
2955
2956 // Start offset measures from the beginning of the line.
2957 var startOffset = selection.startOffset;
2958 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002959
Robert Gindafdbb3f22012-09-06 20:23:06 -07002960 if (node.nodeName != 'X-ROW') {
2961 // If the selection doesn't start on an x-row node, then it must be
2962 // somewhere inside the x-row. Add any characters from previous siblings
2963 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002964
2965 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2966 // If node is the text node in a styled span, move up to the span node.
2967 node = node.parentNode;
2968 }
2969
Robert Gindafdbb3f22012-09-06 20:23:06 -07002970 while (node.previousSibling) {
2971 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002972 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002973 }
rgindaa09e7332012-08-17 12:49:51 -07002974 }
2975
2976 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002977 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
2978 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05002979 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002980
Robert Gindafdbb3f22012-09-06 20:23:06 -07002981 if (node.nodeName != 'X-ROW') {
2982 // If the selection doesn't end on an x-row node, then it must be
2983 // somewhere inside the x-row. Add any characters from following siblings
2984 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002985
2986 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2987 // If node is the text node in a styled span, move up to the span node.
2988 node = node.parentNode;
2989 }
2990
Robert Gindafdbb3f22012-09-06 20:23:06 -07002991 while (node.nextSibling) {
2992 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002993 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002994 }
rgindaa09e7332012-08-17 12:49:51 -07002995 }
2996
2997 var rv = this.getRowsText(selection.startRow.rowIndex,
2998 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002999 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003000};
3001
rginda4bba5e12012-06-20 16:15:30 -07003002/**
3003 * Copy the current selection to the system clipboard, then clear it after a
3004 * short delay.
3005 */
3006hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07003007 var text = this.getSelectionText();
3008 if (text != null)
3009 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07003010};
3011
rgindaf0090c92012-02-10 14:58:52 -08003012hterm.Terminal.prototype.overlaySize = function() {
3013 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
3014};
3015
rginda87b86462011-12-14 13:48:03 -08003016/**
3017 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3018 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003019 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003020 */
3021hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08003022 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08003023 this.scrollPort_.scrollRowToBottom(this.getRowCount());
3024
Robert Ginda8cb7d902013-06-20 14:37:18 -07003025 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08003026};
3027
3028/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003029 * Open the selected url.
3030 */
3031hterm.Terminal.prototype.openSelectedUrl_ = function() {
3032 var str = this.getSelectionText();
3033
3034 // If there is no selection, try and expand wherever they clicked.
3035 if (str == null) {
3036 this.screen_.expandSelection(this.document_.getSelection());
3037 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003038
3039 // If clicking in empty space, return.
3040 if (str == null)
3041 return;
Mike Frysinger70b94692017-01-26 18:57:50 -10003042 }
3043
3044 // Make sure URL is valid before opening.
3045 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
3046 return;
Mike Frysinger43472622017-06-26 18:11:07 -04003047
3048 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003049 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003050 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3051 // We have to whitelist a few protocols that lack authorities and thus
3052 // never use the //. Like mailto.
3053 switch (str.split(':', 1)[0]) {
3054 case 'mailto':
3055 break;
3056 default:
3057 str = 'http://' + str;
3058 break;
3059 }
3060 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003061
Mike Frysinger720fa832017-10-23 01:15:52 -04003062 hterm.openUrl(str);
Mike Frysinger70b94692017-01-26 18:57:50 -10003063}
3064
3065
3066/**
rgindad5613292012-06-19 15:40:37 -07003067 * Add the terminalRow and terminalColumn properties to mouse events and
3068 * then forward on to onMouse().
3069 *
3070 * The terminalRow and terminalColumn properties contain the (row, column)
3071 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003072 *
3073 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003074 */
3075hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003076 if (e.processedByTerminalHandler_) {
3077 // We register our event handlers on the document, as well as the cursor
3078 // and the scroll blocker. Mouse events that occur on the cursor or
3079 // scroll blocker will also appear on the document, but we don't want to
3080 // process them twice.
3081 //
3082 // We can't just prevent bubbling because that has other side effects, so
3083 // we decorate the event object with this property instead.
3084 return;
3085 }
3086
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003087 var reportMouseEvents = (!this.defeatMouseReports_ &&
3088 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3089
rgindafaa74742012-08-21 13:34:03 -07003090 e.processedByTerminalHandler_ = true;
3091
Robert Gindaeda48db2014-07-17 09:25:30 -07003092 // One based row/column stored on the mouse event.
3093 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
3094 this.scrollPort_.characterSize.height) + 1;
3095 e.terminalColumn = parseInt(e.clientX /
3096 this.scrollPort_.characterSize.width) + 1;
3097
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003098 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3099 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003100 return;
3101 }
3102
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003103 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003104 // If the cursor is visible and we're not sending mouse events to the
3105 // host app, then we want to hide the terminal cursor when the mouse
3106 // cursor is over top. This keeps the terminal cursor from interfering
3107 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003108 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3109 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3110 this.cursorNode_.style.display = 'none';
3111 } else if (this.cursorNode_.style.display == 'none') {
3112 this.cursorNode_.style.display = '';
3113 }
3114 }
rgindad5613292012-06-19 15:40:37 -07003115
Robert Ginda928cf632014-03-05 15:07:41 -08003116 if (e.type == 'mousedown') {
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003117 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003118 // If VT mouse reporting is disabled, or has been defeated with
3119 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003120 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003121 this.setSelectionEnabled(true);
3122 } else {
3123 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003124 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003125 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003126 this.setSelectionEnabled(false);
3127 e.preventDefault();
3128 }
3129 }
3130
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003131 if (!reportMouseEvents) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003132 if (e.type == 'dblclick' && this.copyOnSelect) {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003133 this.screen_.expandSelection(this.document_.getSelection());
Robert Ginda15ed4902016-07-12 10:43:22 -07003134 this.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07003135 }
3136
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003137 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003138 // Debounce this event with the dblclick event. If you try to doubleclick
3139 // a URL to open it, Chrome will fire click then dblclick, but we won't
3140 // have expanded the selection text at the first click event.
3141 clearTimeout(this.timeouts_.openUrl);
3142 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3143 500);
3144 return;
3145 }
3146
Mike Frysinger847577f2017-05-23 23:25:57 -04003147 if (e.type == 'mousedown') {
3148 if ((this.mouseRightClickPaste && e.button == 2 /* right button */) ||
Mike Frysinger2edd3612017-05-24 00:54:39 -04003149 e.button == this.mousePasteButton) {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003150 if (!this.paste())
Mike Frysinger05a57f02017-08-27 17:48:55 -04003151 console.warn('Could not paste manually due to web restrictions');
Mike Frysinger847577f2017-05-23 23:25:57 -04003152 }
3153 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003154
Mike Frysinger2edd3612017-05-24 00:54:39 -04003155 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003156 !this.document_.getSelection().isCollapsed) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003157 this.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003158 }
3159
3160 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3161 this.scrollBlockerNode_.engaged) {
3162 // Disengage the scroll-blocker after one of these events.
3163 this.scrollBlockerNode_.engaged = false;
3164 this.scrollBlockerNode_.style.top = '-99px';
3165 }
3166
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003167 // Emulate arrow key presses via scroll wheel events.
3168 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3169 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003170 if (e.type == 'wheel') {
3171 var delta = this.scrollPort_.scrollWheelDelta(e);
3172 var lines = lib.f.smartFloorDivide(
3173 Math.abs(delta), this.scrollPort_.characterSize.height);
3174
3175 var data = '\x1bO' + (delta < 0 ? 'B' : 'A');
3176 this.io.sendString(data.repeat(lines));
3177
3178 e.preventDefault();
3179 }
3180 }
Robert Ginda928cf632014-03-05 15:07:41 -08003181 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003182 if (!this.scrollBlockerNode_.engaged) {
3183 if (e.type == 'mousedown') {
3184 // Move the scroll-blocker into place if we want to keep the scrollport
3185 // from scrolling.
3186 this.scrollBlockerNode_.engaged = true;
3187 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3188 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3189 } else if (e.type == 'mousemove') {
3190 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3191 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003192 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003193 e.preventDefault();
3194 }
3195 }
Robert Ginda928cf632014-03-05 15:07:41 -08003196
3197 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003198 }
3199
Robert Ginda928cf632014-03-05 15:07:41 -08003200 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3201 // Restore this on mouseup in case it was temporarily defeated with a
3202 // alt-mousedown. Only do this when the selection is empty so that
3203 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003204 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003205 }
rgindad5613292012-06-19 15:40:37 -07003206};
3207
3208/**
3209 * Clients should override this if they care to know about mouse events.
3210 *
3211 * The event parameter will be a normal DOM mouse click event with additional
3212 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003213 *
3214 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003215 */
3216hterm.Terminal.prototype.onMouse = function(e) { };
3217
3218/**
rginda8e92a692012-05-20 19:37:20 -07003219 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003220 *
3221 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003222 */
Rob Spies06533ba2014-04-24 11:20:37 -07003223hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3224 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003225 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003226
3227 if (this.reportFocus) {
3228 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O')
3229 }
3230
Michael Kelly485ecd12014-06-09 11:41:56 -04003231 if (focused === true)
3232 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003233};
3234
3235/**
rginda8ba33642011-12-14 12:31:31 -08003236 * React when the ScrollPort is scrolled.
3237 */
3238hterm.Terminal.prototype.onScroll_ = function() {
3239 this.scheduleSyncCursorPosition_();
3240};
3241
3242/**
rginda9846e2f2012-01-27 13:53:33 -08003243 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003244 *
3245 * @param {Event} e The DOM paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003246 */
3247hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07003248 var data = e.text.replace(/\n/mg, '\r');
Connor Hegartyf525ceb2014-09-03 13:36:03 -07003249 data = this.keyboard.encode(data);
Robert Gindaa063b202014-07-21 11:08:25 -07003250 if (this.options_.bracketedPaste)
3251 data = '\x1b[200~' + data + '\x1b[201~';
3252
3253 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003254};
3255
3256/**
rgindaa09e7332012-08-17 12:49:51 -07003257 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003258 *
3259 * @param {Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003260 */
3261hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003262 if (!this.useDefaultWindowCopy) {
3263 e.preventDefault();
3264 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3265 }
rgindaa09e7332012-08-17 12:49:51 -07003266};
3267
3268/**
rginda8ba33642011-12-14 12:31:31 -08003269 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003270 *
3271 * Note: This function should not directly contain code that alters the internal
3272 * state of the terminal. That kind of code belongs in realizeWidth or
3273 * realizeHeight, so that it can be executed synchronously in the case of a
3274 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003275 */
3276hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003277 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003278 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003279 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003280 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003281
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003282 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003283 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003284 // gets removed from the document or during the initial load, and we can't
3285 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003286 // This can also happen if called before the scrollPort calculates the
3287 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003288 return;
3289 }
3290
rgindaa8ba17d2012-08-15 14:41:10 -07003291 var isNewSize = (columnCount != this.screenSize.width ||
3292 rowCount != this.screenSize.height);
3293
3294 // We do this even if the size didn't change, just to be sure everything is
3295 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003296 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003297 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003298
3299 if (isNewSize)
3300 this.overlaySize();
3301
Robert Gindafb1be6a2013-12-11 11:56:22 -08003302 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003303 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08003304};
3305
3306/**
3307 * Service the cursor blink timeout.
3308 */
3309hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003310 if (!this.options_.cursorBlink) {
3311 delete this.timeouts_.cursorBlink;
3312 return;
3313 }
3314
Robert Ginda830583c2013-08-07 13:20:46 -07003315 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3316 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003317 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003318 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3319 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003320 } else {
rginda87b86462011-12-14 13:48:03 -08003321 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003322 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3323 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003324 }
3325};
David Reveman8f552492012-03-28 12:18:41 -04003326
3327/**
3328 * Set the scrollbar-visible mode bit.
3329 *
3330 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3331 * Otherwise it will not.
3332 *
3333 * Defaults to on.
3334 *
3335 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3336 */
3337hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3338 this.scrollPort_.setScrollbarVisible(state);
3339};
Michael Kelly485ecd12014-06-09 11:41:56 -04003340
3341/**
Rob Spies49039e52014-12-17 13:40:04 -08003342 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003343 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003344 *
3345 * Defaults to 1.
3346 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003347 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003348 */
3349hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3350 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3351};
3352
3353/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003354 * Close all web notifications created by terminal bells.
3355 */
3356hterm.Terminal.prototype.closeBellNotifications_ = function() {
3357 this.bellNotificationList_.forEach(function(n) {
3358 n.close();
3359 });
3360 this.bellNotificationList_.length = 0;
3361};