blob: dc20fcfedf8aa4f38d8929c1dd2bdcb47a654c5b [file] [log] [blame]
rginda87b86462011-12-14 13:48:03 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rginda8ba33642011-12-14 12:31:31 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rgindacbbd7482012-06-13 15:06:16 -07005'use strict';
6
Masaya Suzuki273aa982014-05-31 07:25:55 +09007lib.rtdep('lib.colors', 'lib.PreferenceManager', 'lib.resource', 'lib.wc',
Rob Spiesf4e90e82015-01-28 12:10:13 -08008 'lib.f', 'hterm.Keyboard', 'hterm.Options', 'hterm.PreferenceManager',
Ricky Liang48f05cb2013-12-31 23:35:29 +08009 'hterm.Screen', 'hterm.ScrollPort', 'hterm.Size',
10 'hterm.TextAttributes', 'hterm.VT');
rgindacbbd7482012-06-13 15:06:16 -070011
rginda8ba33642011-12-14 12:31:31 -080012/**
13 * Constructor for the Terminal class.
14 *
15 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
16 * classes to provide the complete terminal functionality.
17 *
18 * There are a number of lower-level Terminal methods that can be called
19 * directly to manipulate the cursor, text, scroll region, and other terminal
20 * attributes. However, the primary method is interpret(), which parses VT
21 * escape sequences and invokes the appropriate Terminal methods.
22 *
23 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
24 *
25 * TODO(rginda): Eventually we're going to need to support characters which are
26 * displayed twice as wide as standard latin characters. This is to support
27 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080028 *
Robert Ginda57f03b42012-09-13 11:02:48 -070029 * @param {string} opt_profileId Optional preference profile name. If not
rginda9f5222b2012-03-05 11:53:28 -080030 * provided, defaults to 'default'.
rginda8ba33642011-12-14 12:31:31 -080031 */
Robert Ginda57f03b42012-09-13 11:02:48 -070032hterm.Terminal = function(opt_profileId) {
33 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080034
rginda8ba33642011-12-14 12:31:31 -080035 // Two screen instances.
36 this.primaryScreen_ = new hterm.Screen();
37 this.alternateScreen_ = new hterm.Screen();
38
39 // The "current" screen.
40 this.screen_ = this.primaryScreen_;
41
rginda8ba33642011-12-14 12:31:31 -080042 // The local notion of the screen size. ScreenBuffers also have a size which
43 // indicates their present size. During size changes, the two may disagree.
44 // Also, the inactive screen's size is not altered until it is made the active
45 // screen.
46 this.screenSize = new hterm.Size(0, 0);
47
rginda8ba33642011-12-14 12:31:31 -080048 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080049 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080050 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
51 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080052 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070053 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080054
rginda87b86462011-12-14 13:48:03 -080055 // The div that contains this terminal.
56 this.div_ = null;
57
rgindac9bc5502012-01-18 11:48:44 -080058 // The document that contains the scrollPort. Defaulted to the global
59 // document here so that the terminal is functional even if it hasn't been
60 // inserted into a document yet, but re-set in decorate().
61 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080062
rginda8ba33642011-12-14 12:31:31 -080063 // The rows that have scrolled off screen and are no longer addressable.
64 this.scrollbackRows_ = [];
65
rgindac9bc5502012-01-18 11:48:44 -080066 // Saved tab stops.
67 this.tabStops_ = [];
68
David Benjamin66e954d2012-05-05 21:08:12 -040069 // Keep track of whether default tab stops have been erased; after a TBC
70 // clears all tab stops, defaults aren't restored on resize until a reset.
71 this.defaultTabStops = true;
72
rginda8ba33642011-12-14 12:31:31 -080073 // The VT's notion of the top and bottom rows. Used during some VT
74 // cursor positioning and scrolling commands.
75 this.vtScrollTop_ = null;
76 this.vtScrollBottom_ = null;
77
78 // The DIV element for the visible cursor.
79 this.cursorNode_ = null;
80
Robert Ginda830583c2013-08-07 13:20:46 -070081 // The current cursor shape of the terminal.
82 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
83
84 // The current color of the cursor.
85 this.cursorColor_ = null;
86
Robert Gindaea2183e2014-07-17 09:51:51 -070087 // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded.
88 this.cursorBlinkCycle_ = [100, 100];
89
90 // Pre-bound onCursorBlink_ handler, so we don't have to do this for each
91 // cursor on/off servicing.
92 this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
93
rginda9f5222b2012-03-05 11:53:28 -080094 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070095 // each output and keystroke. They are initialized by the preference manager.
Robert Ginda8cb7d902013-06-20 14:37:18 -070096 this.backgroundColor_ = null;
97 this.foregroundColor_ = null;
Robert Ginda57f03b42012-09-13 11:02:48 -070098 this.scrollOnOutput_ = null;
99 this.scrollOnKeystroke_ = null;
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400100 this.scrollWheelArrowKeys_ = null;
rginda9f5222b2012-03-05 11:53:28 -0800101
Robert Ginda6aec7eb2015-06-16 10:31:30 -0700102 // True if we should override mouse event reporting to allow local selection.
103 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -0800104
rgindaf0090c92012-02-10 14:58:52 -0800105 // Terminal bell sound.
106 this.bellAudio_ = this.document_.createElement('audio');
Mike Frysingerd826f1a2017-07-06 16:20:06 -0400107 this.bellAudio_.id = 'hterm:bell-audio';
rgindaf0090c92012-02-10 14:58:52 -0800108 this.bellAudio_.setAttribute('preload', 'auto');
109
Michael Kelly485ecd12014-06-09 11:41:56 -0400110 // All terminal bell notifications that have been generated (not necessarily
111 // shown).
112 this.bellNotificationList_ = [];
113
114 // Whether we have permission to display notifications.
115 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400116
rginda6d397402012-01-17 10:58:29 -0800117 // Cursor position and attributes saved with DECSC.
118 this.savedOptions_ = {};
119
rginda8ba33642011-12-14 12:31:31 -0800120 // The current mode bits for the terminal.
121 this.options_ = new hterm.Options();
122
123 // Timeouts we might need to clear.
124 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800125
126 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800127 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800128
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800129 this.saveCursorAndState(true);
130
Zhu Qunying30d40712017-03-14 16:27:00 -0700131 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800132 this.keyboard = new hterm.Keyboard(this);
133
rginda87b86462011-12-14 13:48:03 -0800134 // General IO interface that can be given to third parties without exposing
135 // the entire terminal object.
136 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800137
rgindad5613292012-06-19 15:40:37 -0700138 // True if mouse-click-drag should scroll the terminal.
139 this.enableMouseDragScroll = true;
140
Robert Ginda57f03b42012-09-13 11:02:48 -0700141 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400142 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700143 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700144
Zhu Qunying30d40712017-03-14 16:27:00 -0700145 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700146 this.useDefaultWindowCopy = false;
147
148 this.clearSelectionAfterCopy = true;
149
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400150 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800151 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700152
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400153 // Whether we allow images to be shown.
154 this.allowImagesInline = null;
155
Gabriel Holodake8a09be2017-10-10 01:07:11 -0400156 this.reportFocus = false;
157
Robert Ginda57f03b42012-09-13 11:02:48 -0700158 this.setProfile(opt_profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500159 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800160};
161
162/**
Robert Ginda830583c2013-08-07 13:20:46 -0700163 * Possible cursor shapes.
164 */
165hterm.Terminal.cursorShape = {
166 BLOCK: 'BLOCK',
167 BEAM: 'BEAM',
168 UNDERLINE: 'UNDERLINE'
169};
170
171/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700172 * Clients should override this to be notified when the terminal is ready
173 * for use.
174 *
175 * The terminal initialization is asynchronous, and shouldn't be used before
176 * this method is called.
177 */
178hterm.Terminal.prototype.onTerminalReady = function() { };
179
180/**
rginda35c456b2012-02-09 17:29:05 -0800181 * Default tab with of 8 to match xterm.
182 */
183hterm.Terminal.prototype.tabWidth = 8;
184
185/**
rginda9f5222b2012-03-05 11:53:28 -0800186 * Select a preference profile.
187 *
188 * This will load the terminal preferences for the given profile name and
189 * associate subsequent preference changes with the new preference profile.
190 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500191 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800192 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700193 * @param {function} opt_callback Optional callback to invoke when the profile
194 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800195 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700196hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
197 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800198
Robert Ginda57f03b42012-09-13 11:02:48 -0700199 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800200
Robert Ginda57f03b42012-09-13 11:02:48 -0700201 if (this.prefs_)
202 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800203
Robert Ginda57f03b42012-09-13 11:02:48 -0700204 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
205 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800206 'alt-gr-mode': function(v) {
207 if (v == null) {
208 if (navigator.language.toLowerCase() == 'en-us') {
209 v = 'none';
210 } else {
211 v = 'right-alt';
212 }
213 } else if (typeof v == 'string') {
214 v = v.toLowerCase();
215 } else {
216 v = 'none';
217 }
218
219 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v))
220 v = 'none';
221
222 terminal.keyboard.altGrMode = v;
223 },
224
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700225 'alt-backspace-is-meta-backspace': function(v) {
226 terminal.keyboard.altBackspaceIsMetaBackspace = v;
227 },
228
Robert Ginda57f03b42012-09-13 11:02:48 -0700229 'alt-is-meta': function(v) {
230 terminal.keyboard.altIsMeta = v;
231 },
232
233 'alt-sends-what': function(v) {
234 if (!/^(escape|8-bit|browser-key)$/.test(v))
235 v = 'escape';
236
237 terminal.keyboard.altSendsWhat = v;
238 },
239
240 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800241 var ary = v.match(/^lib-resource:(\S+)/);
242 if (ary) {
243 terminal.bellAudio_.setAttribute('src',
244 lib.resource.getDataUrl(ary[1]));
245 } else {
246 terminal.bellAudio_.setAttribute('src', v);
247 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700248 },
249
Michael Kelly485ecd12014-06-09 11:41:56 -0400250 'desktop-notification-bell': function(v) {
251 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700252 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400253 Notification.permission === 'granted';
254 if (!terminal.desktopNotificationBell_) {
255 // Note: We don't call Notification.requestPermission here because
256 // Chrome requires the call be the result of a user action (such as an
257 // onclick handler), and pref listeners are run asynchronously.
258 //
259 // A way of working around this would be to display a dialog in the
260 // terminal with a "click-to-request-permission" button.
261 console.warn('desktop-notification-bell is true but we do not have ' +
262 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400263 }
264 } else {
265 terminal.desktopNotificationBell_ = false;
266 }
267 },
268
Robert Ginda57f03b42012-09-13 11:02:48 -0700269 'background-color': function(v) {
270 terminal.setBackgroundColor(v);
271 },
272
273 'background-image': function(v) {
274 terminal.scrollPort_.setBackgroundImage(v);
275 },
276
277 'background-size': function(v) {
278 terminal.scrollPort_.setBackgroundSize(v);
279 },
280
281 'background-position': function(v) {
282 terminal.scrollPort_.setBackgroundPosition(v);
283 },
284
285 'backspace-sends-backspace': function(v) {
286 terminal.keyboard.backspaceSendsBackspace = v;
287 },
288
Brad Town18654b62015-03-12 00:27:45 -0700289 'character-map-overrides': function(v) {
290 if (!(v == null || v instanceof Object)) {
291 console.warn('Preference character-map-modifications is not an ' +
292 'object: ' + v);
293 return;
294 }
295
Mike Frysinger095d4062017-06-14 00:29:48 -0700296 terminal.vt.characterMaps.reset();
297 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700298 },
299
Robert Ginda57f03b42012-09-13 11:02:48 -0700300 'cursor-blink': function(v) {
301 terminal.setCursorBlink(!!v);
302 },
303
Robert Gindaea2183e2014-07-17 09:51:51 -0700304 'cursor-blink-cycle': function(v) {
305 if (v instanceof Array &&
306 typeof v[0] == 'number' &&
307 typeof v[1] == 'number') {
308 terminal.cursorBlinkCycle_ = v;
309 } else if (typeof v == 'number') {
310 terminal.cursorBlinkCycle_ = [v, v];
311 } else {
312 // Fast blink indicates an error.
313 terminal.cursorBlinkCycle_ = [100, 100];
314 }
315 },
316
Robert Ginda57f03b42012-09-13 11:02:48 -0700317 'cursor-color': function(v) {
318 terminal.setCursorColor(v);
319 },
320
321 'color-palette-overrides': function(v) {
322 if (!(v == null || v instanceof Object || v instanceof Array)) {
323 console.warn('Preference color-palette-overrides is not an array or ' +
324 'object: ' + v);
325 return;
rginda9f5222b2012-03-05 11:53:28 -0800326 }
rginda9f5222b2012-03-05 11:53:28 -0800327
Robert Ginda57f03b42012-09-13 11:02:48 -0700328 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700329
Robert Ginda57f03b42012-09-13 11:02:48 -0700330 if (v) {
331 for (var key in v) {
332 var i = parseInt(key);
333 if (isNaN(i) || i < 0 || i > 255) {
334 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
335 continue;
336 }
337
338 if (v[i]) {
339 var rgb = lib.colors.normalizeCSS(v[i]);
340 if (rgb)
341 lib.colors.colorPalette[i] = rgb;
342 }
343 }
rginda30f20f62012-04-05 16:36:19 -0700344 }
rginda30f20f62012-04-05 16:36:19 -0700345
Evan Jones5f9df812016-12-06 09:38:58 -0500346 terminal.primaryScreen_.textAttributes.resetColorPalette();
Robert Ginda57f03b42012-09-13 11:02:48 -0700347 terminal.alternateScreen_.textAttributes.resetColorPalette();
348 },
rginda30f20f62012-04-05 16:36:19 -0700349
Robert Ginda57f03b42012-09-13 11:02:48 -0700350 'copy-on-select': function(v) {
351 terminal.copyOnSelect = !!v;
352 },
rginda9f5222b2012-03-05 11:53:28 -0800353
Rob Spies0bec09b2014-06-06 15:58:09 -0700354 'use-default-window-copy': function(v) {
355 terminal.useDefaultWindowCopy = !!v;
356 },
357
358 'clear-selection-after-copy': function(v) {
359 terminal.clearSelectionAfterCopy = !!v;
360 },
361
Robert Ginda7e5e9522014-03-14 12:23:58 -0700362 'ctrl-plus-minus-zero-zoom': function(v) {
363 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
364 },
365
Robert Gindafb5a3f92014-05-13 14:12:00 -0700366 'ctrl-c-copy': function(v) {
367 terminal.keyboard.ctrlCCopy = v;
368 },
369
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100370 'ctrl-v-paste': function(v) {
371 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700372 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100373 },
374
Masaya Suzuki273aa982014-05-31 07:25:55 +0900375 'east-asian-ambiguous-as-two-column': function(v) {
376 lib.wc.regardCjkAmbiguous = v;
377 },
378
Robert Ginda57f03b42012-09-13 11:02:48 -0700379 'enable-8-bit-control': function(v) {
380 terminal.vt.enable8BitControl = !!v;
381 },
rginda30f20f62012-04-05 16:36:19 -0700382
Robert Ginda57f03b42012-09-13 11:02:48 -0700383 'enable-bold': function(v) {
384 terminal.syncBoldSafeState();
385 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400386
Robert Ginda3e278d72014-03-25 13:18:51 -0700387 'enable-bold-as-bright': function(v) {
388 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
389 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
390 },
391
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400392 'enable-blink': function(v) {
393 terminal.syncBlinkState();
394 },
395
Robert Ginda57f03b42012-09-13 11:02:48 -0700396 'enable-clipboard-write': function(v) {
397 terminal.vt.enableClipboardWrite = !!v;
398 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400399
Robert Ginda3755e752013-05-31 13:34:09 -0700400 'enable-dec12': function(v) {
401 terminal.vt.enableDec12 = !!v;
402 },
403
Robert Ginda57f03b42012-09-13 11:02:48 -0700404 'font-family': function(v) {
405 terminal.syncFontFamily();
406 },
rginda30f20f62012-04-05 16:36:19 -0700407
Robert Ginda57f03b42012-09-13 11:02:48 -0700408 'font-size': function(v) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500409 v = parseInt(v);
410 if (v <= 0) {
411 console.error(`Invalid font size: ${v}`);
412 return;
413 }
414
Robert Ginda57f03b42012-09-13 11:02:48 -0700415 terminal.setFontSize(v);
416 },
rginda9875d902012-08-20 16:21:57 -0700417
Robert Ginda57f03b42012-09-13 11:02:48 -0700418 'font-smoothing': function(v) {
419 terminal.syncFontFamily();
420 },
rgindade84e382012-04-20 15:39:31 -0700421
Robert Ginda57f03b42012-09-13 11:02:48 -0700422 'foreground-color': function(v) {
423 terminal.setForegroundColor(v);
424 },
rginda30f20f62012-04-05 16:36:19 -0700425
Robert Ginda57f03b42012-09-13 11:02:48 -0700426 'home-keys-scroll': function(v) {
427 terminal.keyboard.homeKeysScroll = v;
428 },
rginda4bba5e12012-06-20 16:15:30 -0700429
Robert Gindaa8165692015-06-15 14:46:31 -0700430 'keybindings': function(v) {
431 terminal.keyboard.bindings.clear();
432
433 if (!v)
434 return;
435
436 if (!(v instanceof Object)) {
437 console.error('Error in keybindings preference: Expected object');
438 return;
439 }
440
441 try {
442 terminal.keyboard.bindings.addBindings(v);
443 } catch (ex) {
444 console.error('Error in keybindings preference: ' + ex);
445 }
446 },
447
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700448 'media-keys-are-fkeys': function(v) {
449 terminal.keyboard.mediaKeysAreFKeys = v;
450 },
451
Robert Ginda57f03b42012-09-13 11:02:48 -0700452 'meta-sends-escape': function(v) {
453 terminal.keyboard.metaSendsEscape = v;
454 },
rginda30f20f62012-04-05 16:36:19 -0700455
Mike Frysinger847577f2017-05-23 23:25:57 -0400456 'mouse-right-click-paste': function(v) {
457 terminal.mouseRightClickPaste = v;
458 },
459
Robert Ginda57f03b42012-09-13 11:02:48 -0700460 'mouse-paste-button': function(v) {
461 terminal.syncMousePasteButton();
462 },
rgindaa8ba17d2012-08-15 14:41:10 -0700463
Robert Gindae76aa9f2014-03-14 12:29:12 -0700464 'page-keys-scroll': function(v) {
465 terminal.keyboard.pageKeysScroll = v;
466 },
467
Robert Ginda40932892012-12-10 17:26:40 -0800468 'pass-alt-number': function(v) {
469 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800470 // Let Alt-1..9 pass to the browser (to control tab switching) on
471 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500472 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800473 }
474
475 terminal.passAltNumber = v;
476 },
477
478 'pass-ctrl-number': function(v) {
479 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800480 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
481 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500482 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800483 }
484
485 terminal.passCtrlNumber = v;
486 },
487
488 'pass-meta-number': function(v) {
489 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800490 // Let Meta-1..9 pass to the browser (to control tab switching) on
491 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500492 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800493 }
494
495 terminal.passMetaNumber = v;
496 },
497
Marius Schilder77857b32014-05-14 16:21:26 -0700498 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700499 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700500 },
501
Robert Ginda8cb7d902013-06-20 14:37:18 -0700502 'receive-encoding': function(v) {
503 if (!(/^(utf-8|raw)$/).test(v)) {
504 console.warn('Invalid value for "receive-encoding": ' + v);
505 v = 'utf-8';
506 }
507
508 terminal.vt.characterEncoding = v;
509 },
510
Robert Ginda57f03b42012-09-13 11:02:48 -0700511 'scroll-on-keystroke': function(v) {
512 terminal.scrollOnKeystroke_ = v;
513 },
rginda9f5222b2012-03-05 11:53:28 -0800514
Robert Ginda57f03b42012-09-13 11:02:48 -0700515 'scroll-on-output': function(v) {
516 terminal.scrollOnOutput_ = v;
517 },
rginda30f20f62012-04-05 16:36:19 -0700518
Robert Ginda57f03b42012-09-13 11:02:48 -0700519 'scrollbar-visible': function(v) {
520 terminal.setScrollbarVisible(v);
521 },
rginda9f5222b2012-03-05 11:53:28 -0800522
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400523 'scroll-wheel-may-send-arrow-keys': function(v) {
524 terminal.scrollWheelArrowKeys_ = v;
525 },
526
Rob Spies49039e52014-12-17 13:40:04 -0800527 'scroll-wheel-move-multiplier': function(v) {
528 terminal.setScrollWheelMoveMultipler(v);
529 },
530
Robert Ginda8cb7d902013-06-20 14:37:18 -0700531 'send-encoding': function(v) {
532 if (!(/^(utf-8|raw)$/).test(v)) {
533 console.warn('Invalid value for "send-encoding": ' + v);
534 v = 'utf-8';
535 }
536
537 terminal.keyboard.characterEncoding = v;
538 },
539
Robert Ginda57f03b42012-09-13 11:02:48 -0700540 'shift-insert-paste': function(v) {
541 terminal.keyboard.shiftInsertPaste = v;
542 },
rginda9f5222b2012-03-05 11:53:28 -0800543
Mike Frysingera7768922017-07-28 15:00:12 -0400544 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400545 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400546 },
547
Robert Gindae76aa9f2014-03-14 12:29:12 -0700548 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400549 terminal.scrollPort_.setUserCssUrl(v);
550 },
551
552 'user-css-text': function(v) {
553 terminal.scrollPort_.setUserCssText(v);
554 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400555
556 'word-break-match-left': function(v) {
557 terminal.primaryScreen_.wordBreakMatchLeft = v;
558 terminal.alternateScreen_.wordBreakMatchLeft = v;
559 },
560
561 'word-break-match-right': function(v) {
562 terminal.primaryScreen_.wordBreakMatchRight = v;
563 terminal.alternateScreen_.wordBreakMatchRight = v;
564 },
565
566 'word-break-match-middle': function(v) {
567 terminal.primaryScreen_.wordBreakMatchMiddle = v;
568 terminal.alternateScreen_.wordBreakMatchMiddle = v;
569 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400570
571 'allow-images-inline': function(v) {
572 terminal.allowImagesInline = v;
573 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700574 });
rginda30f20f62012-04-05 16:36:19 -0700575
Robert Ginda57f03b42012-09-13 11:02:48 -0700576 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800577 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700578
579 if (opt_callback)
580 opt_callback();
581 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800582};
583
Rob Spies56953412014-04-28 14:09:47 -0700584
585/**
586 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500587 *
588 * @return {hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700589 */
590hterm.Terminal.prototype.getPrefs = function() {
591 return this.prefs_;
592};
593
Robert Gindaa063b202014-07-21 11:08:25 -0700594/**
595 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500596 *
597 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700598 */
599hterm.Terminal.prototype.setBracketedPaste = function(state) {
600 this.options_.bracketedPaste = state;
601};
Rob Spies56953412014-04-28 14:09:47 -0700602
rginda8e92a692012-05-20 19:37:20 -0700603/**
604 * Set the color for the cursor.
605 *
606 * If you want this setting to persist, set it through prefs_, rather than
607 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500608 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500609 * @param {string=} color The color to set. If not defined, we reset to the
610 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700611 */
612hterm.Terminal.prototype.setCursorColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500613 if (color === undefined)
614 color = this.prefs_.get('cursor-color');
615
Robert Ginda830583c2013-08-07 13:20:46 -0700616 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700617 this.cursorNode_.style.backgroundColor = color;
618 this.cursorNode_.style.borderColor = color;
619};
620
621/**
622 * Return the current cursor color as a string.
Evan Jones2600d4f2016-12-06 09:29:36 -0500623 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700624 */
625hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700626 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700627};
628
629/**
rgindad5613292012-06-19 15:40:37 -0700630 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500631 *
632 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700633 */
634hterm.Terminal.prototype.setSelectionEnabled = function(state) {
635 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700636};
637
638/**
rginda8e92a692012-05-20 19:37:20 -0700639 * Set the background color.
640 *
641 * If you want this setting to persist, set it through prefs_, rather than
642 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500643 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500644 * @param {string=} color The color to set. If not defined, we reset to the
645 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700646 */
647hterm.Terminal.prototype.setBackgroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500648 if (color === undefined)
649 color = this.prefs_.get('background-color');
650
rgindacbbd7482012-06-13 15:06:16 -0700651 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700652 this.primaryScreen_.textAttributes.setDefaults(
653 this.foregroundColor_, this.backgroundColor_);
654 this.alternateScreen_.textAttributes.setDefaults(
655 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700656 this.scrollPort_.setBackgroundColor(color);
657};
658
rginda9f5222b2012-03-05 11:53:28 -0800659/**
660 * Return the current terminal background color.
661 *
662 * Intended for use by other classes, so we don't have to expose the entire
663 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500664 *
665 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800666 */
667hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700668 return this.backgroundColor_;
669};
670
671/**
672 * Set the foreground color.
673 *
674 * If you want this setting to persist, set it through prefs_, rather than
675 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500676 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500677 * @param {string=} color The color to set. If not defined, we reset to the
678 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700679 */
680hterm.Terminal.prototype.setForegroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500681 if (color === undefined)
682 color = this.prefs_.get('foreground-color');
683
rgindacbbd7482012-06-13 15:06:16 -0700684 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700685 this.primaryScreen_.textAttributes.setDefaults(
686 this.foregroundColor_, this.backgroundColor_);
687 this.alternateScreen_.textAttributes.setDefaults(
688 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700689 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800690};
691
692/**
693 * Return the current terminal foreground color.
694 *
695 * Intended for use by other classes, so we don't have to expose the entire
696 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500697 *
698 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800699 */
700hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700701 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800702};
703
704/**
rginda87b86462011-12-14 13:48:03 -0800705 * Create a new instance of a terminal command and run it with a given
706 * argument string.
707 *
708 * @param {function} commandClass The constructor for a terminal command.
709 * @param {string} argString The argument string to pass to the command.
710 */
711hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700712 var environment = this.prefs_.get('environment');
713 if (typeof environment != 'object' || environment == null)
714 environment = {};
715
rginda87b86462011-12-14 13:48:03 -0800716 var self = this;
717 this.command = new commandClass(
718 { argString: argString || '',
719 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700720 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800721 onExit: function(code) {
722 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800723 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700724 if (self.prefs_.get('close-on-exit'))
725 window.close();
rginda87b86462011-12-14 13:48:03 -0800726 }
727 });
728
rgindafeaf3142012-01-31 15:14:20 -0800729 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800730 this.command.run();
731};
732
733/**
rgindafeaf3142012-01-31 15:14:20 -0800734 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500735 *
736 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800737 */
738hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700739 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800740};
741
742/**
743 * Install the keyboard handler for this terminal.
744 *
745 * This will prevent the browser from seeing any keystrokes sent to the
746 * terminal.
747 */
748hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700749 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
rgindafeaf3142012-01-31 15:14:20 -0800750}
751
752/**
753 * Uninstall the keyboard handler for this terminal.
754 */
755hterm.Terminal.prototype.uninstallKeyboard = function() {
756 this.keyboard.installKeyboard(null);
757}
758
759/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400760 * Set a CSS variable.
761 *
762 * Normally this is used to set variables in the hterm namespace.
763 *
764 * @param {string} name The variable to set.
765 * @param {string} value The value to assign to the variable.
766 * @param {string?} opt_prefix The variable namespace/prefix to use.
767 */
768hterm.Terminal.prototype.setCssVar = function(name, value,
769 opt_prefix='--hterm-') {
770 this.document_.documentElement.style.setProperty(
771 `${opt_prefix}${name}`, value);
772};
773
774/**
rginda35c456b2012-02-09 17:29:05 -0800775 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800776 *
777 * Call setFontSize(0) to reset to the default font size.
778 *
779 * This function does not modify the font-size preference.
780 *
781 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800782 */
783hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500784 if (px <= 0)
rginda9f5222b2012-03-05 11:53:28 -0800785 px = this.prefs_.get('font-size');
786
rginda35c456b2012-02-09 17:29:05 -0800787 this.scrollPort_.setFontSize(px);
Mike Frysingercce97c42017-08-05 01:11:22 -0400788 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
789 this.setCssVar('charsize-height',
790 this.scrollPort_.characterSize.height + 'px');
rginda35c456b2012-02-09 17:29:05 -0800791};
792
793/**
794 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500795 *
796 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800797 */
798hterm.Terminal.prototype.getFontSize = function() {
799 return this.scrollPort_.getFontSize();
800};
801
802/**
rginda8e92a692012-05-20 19:37:20 -0700803 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500804 *
805 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700806 */
807hterm.Terminal.prototype.getFontFamily = function() {
808 return this.scrollPort_.getFontFamily();
809};
810
811/**
rginda35c456b2012-02-09 17:29:05 -0800812 * Set the CSS "font-family" for this terminal.
813 */
rginda9f5222b2012-03-05 11:53:28 -0800814hterm.Terminal.prototype.syncFontFamily = function() {
815 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
816 this.prefs_.get('font-smoothing'));
817 this.syncBoldSafeState();
818};
819
rginda4bba5e12012-06-20 16:15:30 -0700820/**
821 * Set this.mousePasteButton based on the mouse-paste-button pref,
822 * autodetecting if necessary.
823 */
824hterm.Terminal.prototype.syncMousePasteButton = function() {
825 var button = this.prefs_.get('mouse-paste-button');
826 if (typeof button == 'number') {
827 this.mousePasteButton = button;
828 return;
829 }
830
Mike Frysingeree81a002017-12-12 16:14:53 -0500831 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400832 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700833 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400834 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700835 }
836};
837
838/**
839 * Enable or disable bold based on the enable-bold pref, autodetecting if
840 * necessary.
841 */
rginda9f5222b2012-03-05 11:53:28 -0800842hterm.Terminal.prototype.syncBoldSafeState = function() {
843 var enableBold = this.prefs_.get('enable-bold');
844 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700845 this.primaryScreen_.textAttributes.enableBold = enableBold;
846 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800847 return;
848 }
849
rgindaf7521392012-02-28 17:20:34 -0800850 var normalSize = this.scrollPort_.measureCharacterSize();
851 var boldSize = this.scrollPort_.measureCharacterSize('bold');
852
853 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800854 if (!isBoldSafe) {
855 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700856 'from normal. Font family is: ' +
857 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800858 }
rginda9f5222b2012-03-05 11:53:28 -0800859
Robert Gindaed016262012-10-26 16:27:09 -0700860 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
861 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800862};
863
864/**
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400865 * Enable or disable blink based on the enable-blink pref.
866 */
867hterm.Terminal.prototype.syncBlinkState = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400868 this.setCssVar('node-duration',
869 this.prefs_.get('enable-blink') ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400870};
871
872/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400873 * Set the mouse cursor style based on the current terminal mode.
874 */
875hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400876 this.setCssVar('mouse-cursor-style',
877 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
878 'var(--hterm-mouse-cursor-text)' :
879 'var(--hterm-mouse-cursor-pointer)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400880};
881
882/**
rginda87b86462011-12-14 13:48:03 -0800883 * Return a copy of the current cursor position.
884 *
885 * @return {hterm.RowCol} The RowCol object representing the current position.
886 */
887hterm.Terminal.prototype.saveCursor = function() {
888 return this.screen_.cursorPosition.clone();
889};
890
Evan Jones2600d4f2016-12-06 09:29:36 -0500891/**
892 * Return the current text attributes.
893 *
894 * @return {string}
895 */
rgindaa19afe22012-01-25 15:40:22 -0800896hterm.Terminal.prototype.getTextAttributes = function() {
897 return this.screen_.textAttributes;
898};
899
Evan Jones2600d4f2016-12-06 09:29:36 -0500900/**
901 * Set the text attributes.
902 *
903 * @param {string} textAttributes The attributes to set.
904 */
rginda1a09aa02012-06-18 21:11:25 -0700905hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
906 this.screen_.textAttributes = textAttributes;
907};
908
rginda87b86462011-12-14 13:48:03 -0800909/**
rgindaf522ce02012-04-17 17:49:17 -0700910 * Return the current browser zoom factor applied to the terminal.
911 *
912 * @return {number} The current browser zoom factor.
913 */
914hterm.Terminal.prototype.getZoomFactor = function() {
915 return this.scrollPort_.characterSize.zoomFactor;
916};
917
918/**
rginda9846e2f2012-01-27 13:53:33 -0800919 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500920 *
921 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800922 */
923hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800924 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800925};
926
927/**
rginda87b86462011-12-14 13:48:03 -0800928 * Restore a previously saved cursor position.
929 *
930 * @param {hterm.RowCol} cursor The position to restore.
931 */
932hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700933 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
934 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800935 this.screen_.setCursorPosition(row, column);
936 if (cursor.column > column ||
937 cursor.column == column && cursor.overflow) {
938 this.screen_.cursorPosition.overflow = true;
939 }
rginda87b86462011-12-14 13:48:03 -0800940};
941
942/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400943 * Clear the cursor's overflow flag.
944 */
945hterm.Terminal.prototype.clearCursorOverflow = function() {
946 this.screen_.cursorPosition.overflow = false;
947};
948
949/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800950 * Save the current cursor state to the corresponding screens.
951 *
952 * See the hterm.Screen.CursorState class for more details.
953 *
954 * @param {boolean=} both If true, update both screens, else only update the
955 * current screen.
956 */
957hterm.Terminal.prototype.saveCursorAndState = function(both) {
958 if (both) {
959 this.primaryScreen_.saveCursorAndState(this.vt);
960 this.alternateScreen_.saveCursorAndState(this.vt);
961 } else
962 this.screen_.saveCursorAndState(this.vt);
963};
964
965/**
966 * Restore the saved cursor state in the corresponding screens.
967 *
968 * See the hterm.Screen.CursorState class for more details.
969 *
970 * @param {boolean=} both If true, update both screens, else only update the
971 * current screen.
972 */
973hterm.Terminal.prototype.restoreCursorAndState = function(both) {
974 if (both) {
975 this.primaryScreen_.restoreCursorAndState(this.vt);
976 this.alternateScreen_.restoreCursorAndState(this.vt);
977 } else
978 this.screen_.restoreCursorAndState(this.vt);
979};
980
981/**
Robert Ginda830583c2013-08-07 13:20:46 -0700982 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -0500983 *
984 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -0700985 */
986hterm.Terminal.prototype.setCursorShape = function(shape) {
987 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -0800988 this.restyleCursor_();
Robert Ginda830583c2013-08-07 13:20:46 -0700989}
990
991/**
992 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -0500993 *
994 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -0700995 */
996hterm.Terminal.prototype.getCursorShape = function() {
997 return this.cursorShape_;
998}
999
1000/**
rginda87b86462011-12-14 13:48:03 -08001001 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001002 *
1003 * @param {number} columnCount
rginda87b86462011-12-14 13:48:03 -08001004 */
1005hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001006 if (columnCount == null) {
1007 this.div_.style.width = '100%';
1008 return;
1009 }
1010
Robert Ginda26806d12014-07-24 13:44:07 -07001011 this.div_.style.width = Math.ceil(
1012 this.scrollPort_.characterSize.width *
1013 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001014 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001015 this.scheduleSyncCursorPosition_();
1016};
rginda87b86462011-12-14 13:48:03 -08001017
rgindac9bc5502012-01-18 11:48:44 -08001018/**
rginda35c456b2012-02-09 17:29:05 -08001019 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001020 *
1021 * @param {number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001022 */
1023hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001024 if (rowCount == null) {
1025 this.div_.style.height = '100%';
1026 return;
1027 }
1028
rginda35c456b2012-02-09 17:29:05 -08001029 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -07001030 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -08001031 this.realizeSize_(this.screenSize.width, rowCount);
1032 this.scheduleSyncCursorPosition_();
1033};
1034
1035/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001036 * Deal with terminal size changes.
1037 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001038 * @param {number} columnCount The number of columns.
1039 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001040 */
1041hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
1042 if (columnCount != this.screenSize.width)
1043 this.realizeWidth_(columnCount);
1044
1045 if (rowCount != this.screenSize.height)
1046 this.realizeHeight_(rowCount);
1047
1048 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -07001049 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001050};
1051
1052/**
rgindac9bc5502012-01-18 11:48:44 -08001053 * Deal with terminal width changes.
1054 *
1055 * This function does what needs to be done when the terminal width changes
1056 * out from under us. It happens here rather than in onResize_() because this
1057 * code may need to run synchronously to handle programmatic changes of
1058 * terminal width.
1059 *
1060 * Relying on the browser to send us an async resize event means we may not be
1061 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001062 *
1063 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001064 */
1065hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001066 if (columnCount <= 0)
1067 throw new Error('Attempt to realize bad width: ' + columnCount);
1068
rgindac9bc5502012-01-18 11:48:44 -08001069 var deltaColumns = columnCount - this.screen_.getWidth();
1070
rginda87b86462011-12-14 13:48:03 -08001071 this.screenSize.width = columnCount;
1072 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001073
1074 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -04001075 if (this.defaultTabStops)
1076 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -08001077 } else {
1078 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -04001079 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -08001080 break;
1081
1082 this.tabStops_.pop();
1083 }
1084 }
1085
1086 this.screen_.setColumnCount(this.screenSize.width);
1087};
1088
1089/**
1090 * Deal with terminal height changes.
1091 *
1092 * This function does what needs to be done when the terminal height changes
1093 * out from under us. It happens here rather than in onResize_() because this
1094 * code may need to run synchronously to handle programmatic changes of
1095 * terminal height.
1096 *
1097 * Relying on the browser to send us an async resize event means we may not be
1098 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001099 *
1100 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001101 */
1102hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001103 if (rowCount <= 0)
1104 throw new Error('Attempt to realize bad height: ' + rowCount);
1105
rgindac9bc5502012-01-18 11:48:44 -08001106 var deltaRows = rowCount - this.screen_.getHeight();
1107
1108 this.screenSize.height = rowCount;
1109
1110 var cursor = this.saveCursor();
1111
1112 if (deltaRows < 0) {
1113 // Screen got smaller.
1114 deltaRows *= -1;
1115 while (deltaRows) {
1116 var lastRow = this.getRowCount() - 1;
1117 if (lastRow - this.scrollbackRows_.length == cursor.row)
1118 break;
1119
1120 if (this.getRowText(lastRow))
1121 break;
1122
1123 this.screen_.popRow();
1124 deltaRows--;
1125 }
1126
1127 var ary = this.screen_.shiftRows(deltaRows);
1128 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1129
1130 // We just removed rows from the top of the screen, we need to update
1131 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001132 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001133 } else if (deltaRows > 0) {
1134 // Screen got larger.
1135
1136 if (deltaRows <= this.scrollbackRows_.length) {
1137 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1138 var rows = this.scrollbackRows_.splice(
1139 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1140 this.screen_.unshiftRows(rows);
1141 deltaRows -= scrollbackCount;
1142 cursor.row += scrollbackCount;
1143 }
1144
1145 if (deltaRows)
1146 this.appendRows_(deltaRows);
1147 }
1148
rginda35c456b2012-02-09 17:29:05 -08001149 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001150 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001151};
1152
1153/**
1154 * Scroll the terminal to the top of the scrollback buffer.
1155 */
1156hterm.Terminal.prototype.scrollHome = function() {
1157 this.scrollPort_.scrollRowToTop(0);
1158};
1159
1160/**
1161 * Scroll the terminal to the end.
1162 */
1163hterm.Terminal.prototype.scrollEnd = function() {
1164 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1165};
1166
1167/**
1168 * Scroll the terminal one page up (minus one line) relative to the current
1169 * position.
1170 */
1171hterm.Terminal.prototype.scrollPageUp = function() {
1172 var i = this.scrollPort_.getTopRowIndex();
1173 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
1174};
1175
1176/**
1177 * Scroll the terminal one page down (minus one line) relative to the current
1178 * position.
1179 */
1180hterm.Terminal.prototype.scrollPageDown = function() {
1181 var i = this.scrollPort_.getTopRowIndex();
1182 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -08001183};
1184
rgindac9bc5502012-01-18 11:48:44 -08001185/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001186 * Scroll the terminal one line up relative to the current position.
1187 */
1188hterm.Terminal.prototype.scrollLineUp = function() {
1189 var i = this.scrollPort_.getTopRowIndex();
1190 this.scrollPort_.scrollRowToTop(i - 1);
1191};
1192
1193/**
1194 * Scroll the terminal one line down relative to the current position.
1195 */
1196hterm.Terminal.prototype.scrollLineDown = function() {
1197 var i = this.scrollPort_.getTopRowIndex();
1198 this.scrollPort_.scrollRowToTop(i + 1);
1199};
1200
1201/**
Robert Ginda40932892012-12-10 17:26:40 -08001202 * Clear primary screen, secondary screen, and the scrollback buffer.
1203 */
1204hterm.Terminal.prototype.wipeContents = function() {
1205 this.scrollbackRows_.length = 0;
1206 this.scrollPort_.resetCache();
1207
1208 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
1209 var bottom = screen.getHeight();
1210 if (bottom > 0) {
1211 this.renumberRows_(0, bottom);
1212 this.clearHome(screen);
1213 }
1214 }.bind(this));
1215
1216 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001217 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001218};
1219
1220/**
rgindac9bc5502012-01-18 11:48:44 -08001221 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001222 *
1223 * Perform a full reset to the default values listed in
1224 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001225 */
rginda87b86462011-12-14 13:48:03 -08001226hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001227 this.vt.reset();
1228
rgindac9bc5502012-01-18 11:48:44 -08001229 this.clearAllTabStops();
1230 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001231
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001232 const resetScreen = (screen) => {
1233 // We want to make sure to reset the attributes before we clear the screen.
1234 // The attributes might be used to initialize default/empty rows.
1235 screen.textAttributes.reset();
1236 screen.textAttributes.resetColorPalette();
1237 this.clearHome(screen);
1238 screen.saveCursorAndState(this.vt);
1239 };
1240 resetScreen(this.primaryScreen_);
1241 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001242
Mike Frysinger84301d02017-11-29 13:28:46 -08001243 // Reset terminal options to their default values.
1244 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001245 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1246
Mike Frysinger84301d02017-11-29 13:28:46 -08001247 this.setVTScrollRegion(null, null);
1248
1249 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001250};
1251
rgindac9bc5502012-01-18 11:48:44 -08001252/**
1253 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001254 *
1255 * Perform a soft reset to the default values listed in
1256 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001257 */
rginda0f5c0292012-01-13 11:00:13 -08001258hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001259 this.vt.reset();
1260
rgindab8bc8932012-04-27 12:45:03 -07001261 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001262 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001263
Brad Townb62dfdc2015-03-16 19:07:15 -07001264 // We show the cursor on soft reset but do not alter the blink state.
1265 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1266
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001267 const resetScreen = (screen) => {
1268 // Xterm also resets the color palette on soft reset, even though it doesn't
1269 // seem to be documented anywhere.
1270 screen.textAttributes.reset();
1271 screen.textAttributes.resetColorPalette();
1272 screen.saveCursorAndState(this.vt);
1273 };
1274 resetScreen(this.primaryScreen_);
1275 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001276
rgindab8bc8932012-04-27 12:45:03 -07001277 // The xterm man page explicitly says this will happen on soft reset.
1278 this.setVTScrollRegion(null, null);
1279
1280 // Xterm also shows the cursor on soft reset, but does not alter the blink
1281 // state.
rgindaa19afe22012-01-25 15:40:22 -08001282 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001283};
1284
rgindac9bc5502012-01-18 11:48:44 -08001285/**
1286 * Move the cursor forward to the next tab stop, or to the last column
1287 * if no more tab stops are set.
1288 */
1289hterm.Terminal.prototype.forwardTabStop = function() {
1290 var column = this.screen_.cursorPosition.column;
1291
1292 for (var i = 0; i < this.tabStops_.length; i++) {
1293 if (this.tabStops_[i] > column) {
1294 this.setCursorColumn(this.tabStops_[i]);
1295 return;
1296 }
1297 }
1298
David Benjamin66e954d2012-05-05 21:08:12 -04001299 // xterm does not clear the overflow flag on HT or CHT.
1300 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001301 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001302 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001303};
1304
rgindac9bc5502012-01-18 11:48:44 -08001305/**
1306 * Move the cursor backward to the previous tab stop, or to the first column
1307 * if no previous tab stops are set.
1308 */
1309hterm.Terminal.prototype.backwardTabStop = function() {
1310 var column = this.screen_.cursorPosition.column;
1311
1312 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1313 if (this.tabStops_[i] < column) {
1314 this.setCursorColumn(this.tabStops_[i]);
1315 return;
1316 }
1317 }
1318
1319 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001320};
1321
rgindac9bc5502012-01-18 11:48:44 -08001322/**
1323 * Set a tab stop at the given column.
1324 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001325 * @param {integer} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001326 */
1327hterm.Terminal.prototype.setTabStop = function(column) {
1328 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1329 if (this.tabStops_[i] == column)
1330 return;
1331
1332 if (this.tabStops_[i] < column) {
1333 this.tabStops_.splice(i + 1, 0, column);
1334 return;
1335 }
1336 }
1337
1338 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001339};
1340
rgindac9bc5502012-01-18 11:48:44 -08001341/**
1342 * Clear the tab stop at the current cursor position.
1343 *
1344 * No effect if there is no tab stop at the current cursor position.
1345 */
1346hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1347 var column = this.screen_.cursorPosition.column;
1348
1349 var i = this.tabStops_.indexOf(column);
1350 if (i == -1)
1351 return;
1352
1353 this.tabStops_.splice(i, 1);
1354};
1355
1356/**
1357 * Clear all tab stops.
1358 */
1359hterm.Terminal.prototype.clearAllTabStops = function() {
1360 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001361 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001362};
1363
1364/**
1365 * Set up the default tab stops, starting from a given column.
1366 *
1367 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001368 * from the specified column, or 0 if no column is provided. It also flags
1369 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001370 *
1371 * This does not clear the existing tab stops first, use clearAllTabStops
1372 * for that.
1373 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001374 * @param {integer} opt_start Optional starting zero based starting column, useful
rgindac9bc5502012-01-18 11:48:44 -08001375 * for filling out missing tab stops when the terminal is resized.
1376 */
1377hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1378 var start = opt_start || 0;
1379 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001380 // Round start up to a default tab stop.
1381 start = start - 1 - ((start - 1) % w) + w;
1382 for (var i = start; i < this.screenSize.width; i += w) {
1383 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001384 }
David Benjamin66e954d2012-05-05 21:08:12 -04001385
1386 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001387};
1388
rginda6d397402012-01-17 10:58:29 -08001389/**
rginda8ba33642011-12-14 12:31:31 -08001390 * Interpret a sequence of characters.
1391 *
1392 * Incomplete escape sequences are buffered until the next call.
1393 *
1394 * @param {string} str Sequence of characters to interpret or pass through.
1395 */
1396hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001397 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001398 this.scheduleSyncCursorPosition_();
1399};
1400
1401/**
1402 * Take over the given DIV for use as the terminal display.
1403 *
1404 * @param {HTMLDivElement} div The div to use as the terminal display.
1405 */
1406hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001407 this.div_ = div;
1408
rginda8ba33642011-12-14 12:31:31 -08001409 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001410 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001411 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1412 this.scrollPort_.setBackgroundPosition(
1413 this.prefs_.get('background-position'));
Mike Frysinger08bad432017-04-24 00:50:54 -04001414 this.scrollPort_.setUserCssUrl(this.prefs_.get('user-css'));
1415 this.scrollPort_.setUserCssText(this.prefs_.get('user-css-text'));
rginda30f20f62012-04-05 16:36:19 -07001416
rginda0918b652012-04-04 11:26:24 -07001417 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001418
rginda9f5222b2012-03-05 11:53:28 -08001419 this.setFontSize(this.prefs_.get('font-size'));
1420 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001421
David Reveman8f552492012-03-28 12:18:41 -04001422 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001423 this.setScrollWheelMoveMultipler(
1424 this.prefs_.get('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001425
rginda8ba33642011-12-14 12:31:31 -08001426 this.document_ = this.scrollPort_.getDocument();
1427
Evan Jones5f9df812016-12-06 09:38:58 -05001428 this.document_.body.oncontextmenu = function() { return false; };
rginda4bba5e12012-06-20 16:15:30 -07001429
1430 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001431 var screenNode = this.scrollPort_.getScreenNode();
1432 screenNode.addEventListener('mousedown', onMouse);
1433 screenNode.addEventListener('mouseup', onMouse);
1434 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001435 this.scrollPort_.onScrollWheel = onMouse;
1436
Toni Barzic0bfa8922013-11-22 11:18:35 -08001437 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001438 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001439 // Listen for mousedown events on the screenNode as in FF the focus
1440 // events don't bubble.
1441 screenNode.addEventListener('mousedown', function() {
1442 setTimeout(this.onFocusChange_.bind(this, true));
1443 }.bind(this));
1444
Toni Barzic0bfa8922013-11-22 11:18:35 -08001445 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001446 'blur', this.onFocusChange_.bind(this, false));
1447
1448 var style = this.document_.createElement('style');
1449 style.textContent =
1450 ('.cursor-node[focus="false"] {' +
1451 ' box-sizing: border-box;' +
1452 ' background-color: transparent !important;' +
1453 ' border-width: 2px;' +
1454 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001455 '}' +
1456 '.wc-node {' +
1457 ' display: inline-block;' +
1458 ' text-align: center;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001459 ' width: calc(var(--hterm-charsize-width) * 2);' +
Mike Frysinger4036f6e2017-05-31 14:02:55 -04001460 ' line-height: var(--hterm-charsize-height);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001461 '}' +
1462 ':root {' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001463 ' --hterm-charsize-width: ' + this.scrollPort_.characterSize.width + 'px;' +
1464 ' --hterm-charsize-height: ' + this.scrollPort_.characterSize.height + 'px;' +
Mike Frysingera27c0502017-08-23 22:37:10 -04001465 // Default position hides the cursor for when the window is initializing.
1466 ' --hterm-cursor-offset-col: -1;' +
1467 ' --hterm-cursor-offset-row: -1;' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001468 ' --hterm-blink-node-duration: 0.7s;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001469 ' --hterm-mouse-cursor-text: text;' +
1470 ' --hterm-mouse-cursor-pointer: default;' +
1471 ' --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001472 '}' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001473 '.uri-node:hover {' +
1474 ' text-decoration: underline;' +
1475 ' cursor: pointer;' +
1476 '}' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001477 '@keyframes blink {' +
1478 ' from { opacity: 1.0; }' +
1479 ' to { opacity: 0.0; }' +
1480 '}' +
1481 '.blink-node {' +
1482 ' animation-name: blink;' +
1483 ' animation-duration: var(--hterm-blink-node-duration);' +
1484 ' animation-iteration-count: infinite;' +
1485 ' animation-timing-function: ease-in-out;' +
1486 ' animation-direction: alternate;' +
rginda8e92a692012-05-20 19:37:20 -07001487 '}');
1488 this.document_.head.appendChild(style);
1489
rginda8ba33642011-12-14 12:31:31 -08001490 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001491 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001492 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001493 this.cursorNode_.style.cssText =
1494 ('position: absolute;' +
Mike Frysinger44c32202017-08-05 01:13:09 -04001495 'left: calc(var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));' +
1496 'top: calc(var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));' +
rginda87b86462011-12-14 13:48:03 -08001497 'display: block;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001498 'width: var(--hterm-charsize-width);' +
1499 'height: var(--hterm-charsize-height);' +
Rob Spies06533ba2014-04-24 11:20:37 -07001500 '-webkit-transition: opacity, background-color 100ms linear;' +
1501 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001502
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001503 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001504 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1505 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001506
rginda8ba33642011-12-14 12:31:31 -08001507 this.document_.body.appendChild(this.cursorNode_);
1508
rgindad5613292012-06-19 15:40:37 -07001509 // When 'enableMouseDragScroll' is off we reposition this element directly
1510 // under the mouse cursor after a click. This makes Chrome associate
1511 // subsequent mousemove events with the scroll-blocker. Since the
1512 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1513 // events do not cause the scrollport to scroll.
1514 //
1515 // It's a hack, but it's the cleanest way I could find.
1516 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001517 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
rgindad5613292012-06-19 15:40:37 -07001518 this.scrollBlockerNode_.style.cssText =
1519 ('position: absolute;' +
1520 'top: -99px;' +
1521 'display: block;' +
1522 'width: 10px;' +
1523 'height: 10px;');
1524 this.document_.body.appendChild(this.scrollBlockerNode_);
1525
rgindad5613292012-06-19 15:40:37 -07001526 this.scrollPort_.onScrollWheel = onMouse;
1527 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1528 ].forEach(function(event) {
1529 this.scrollBlockerNode_.addEventListener(event, onMouse);
1530 this.cursorNode_.addEventListener(event, onMouse);
1531 this.document_.addEventListener(event, onMouse);
1532 }.bind(this));
1533
1534 this.cursorNode_.addEventListener('mousedown', function() {
1535 setTimeout(this.focus.bind(this));
1536 }.bind(this));
1537
rginda8ba33642011-12-14 12:31:31 -08001538 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001539
rginda87b86462011-12-14 13:48:03 -08001540 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001541 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001542};
1543
rginda0918b652012-04-04 11:26:24 -07001544/**
1545 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001546 *
1547 * @return {HTMLDocument}
rginda0918b652012-04-04 11:26:24 -07001548 */
rginda87b86462011-12-14 13:48:03 -08001549hterm.Terminal.prototype.getDocument = function() {
1550 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001551};
1552
1553/**
rginda0918b652012-04-04 11:26:24 -07001554 * Focus the terminal.
1555 */
1556hterm.Terminal.prototype.focus = function() {
1557 this.scrollPort_.focus();
1558};
1559
1560/**
rginda8ba33642011-12-14 12:31:31 -08001561 * Return the HTML Element for a given row index.
1562 *
1563 * This is a method from the RowProvider interface. The ScrollPort uses
1564 * it to fetch rows on demand as they are scrolled into view.
1565 *
1566 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1567 * pairs to conserve memory.
1568 *
1569 * @param {integer} index The zero-based row index, measured relative to the
1570 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001571 * largest indices.
rginda8ba33642011-12-14 12:31:31 -08001572 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1573 */
1574hterm.Terminal.prototype.getRowNode = function(index) {
1575 if (index < this.scrollbackRows_.length)
1576 return this.scrollbackRows_[index];
1577
1578 var screenIndex = index - this.scrollbackRows_.length;
1579 return this.screen_.rowsArray[screenIndex];
1580};
1581
1582/**
1583 * Return the text content for a given range of rows.
1584 *
1585 * This is a method from the RowProvider interface. The ScrollPort uses
1586 * it to fetch text content on demand when the user attempts to copy their
1587 * selection to the clipboard.
1588 *
1589 * @param {integer} start The zero-based row index to start from, measured
1590 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001591 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001592 * @param {integer} end The zero-based row index to end on, measured
1593 * relative to the start of the scrollback buffer.
1594 * @return {string} A single string containing the text value of the range of
1595 * rows. Lines will be newline delimited, with no trailing newline.
1596 */
1597hterm.Terminal.prototype.getRowsText = function(start, end) {
1598 var ary = [];
1599 for (var i = start; i < end; i++) {
1600 var node = this.getRowNode(i);
1601 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001602 if (i < end - 1 && !node.getAttribute('line-overflow'))
1603 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001604 }
1605
rgindaa09e7332012-08-17 12:49:51 -07001606 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001607};
1608
1609/**
1610 * Return the text content for a given row.
1611 *
1612 * This is a method from the RowProvider interface. The ScrollPort uses
1613 * it to fetch text content on demand when the user attempts to copy their
1614 * selection to the clipboard.
1615 *
1616 * @param {integer} index The zero-based row index to return, measured
1617 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001618 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001619 * @return {string} A string containing the text value of the selected row.
1620 */
1621hterm.Terminal.prototype.getRowText = function(index) {
1622 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001623 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001624};
1625
1626/**
1627 * Return the total number of rows in the addressable screen and in the
1628 * scrollback buffer of this terminal.
1629 *
1630 * This is a method from the RowProvider interface. The ScrollPort uses
1631 * it to compute the size of the scrollbar.
1632 *
1633 * @return {integer} The number of rows in this terminal.
1634 */
1635hterm.Terminal.prototype.getRowCount = function() {
1636 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1637};
1638
1639/**
1640 * Create DOM nodes for new rows and append them to the end of the terminal.
1641 *
1642 * This is the only correct way to add a new DOM node for a row. Notice that
1643 * the new row is appended to the bottom of the list of rows, and does not
1644 * require renumbering (of the rowIndex property) of previous rows.
1645 *
1646 * If you think you want a new blank row somewhere in the middle of the
1647 * terminal, look into moveRows_().
1648 *
1649 * This method does not pay attention to vtScrollTop/Bottom, since you should
1650 * be using moveRows() in cases where they would matter.
1651 *
1652 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001653 *
1654 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001655 */
1656hterm.Terminal.prototype.appendRows_ = function(count) {
1657 var cursorRow = this.screen_.rowsArray.length;
1658 var offset = this.scrollbackRows_.length + cursorRow;
1659 for (var i = 0; i < count; i++) {
1660 var row = this.document_.createElement('x-row');
1661 row.appendChild(this.document_.createTextNode(''));
1662 row.rowIndex = offset + i;
1663 this.screen_.pushRow(row);
1664 }
1665
1666 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1667 if (extraRows > 0) {
1668 var ary = this.screen_.shiftRows(extraRows);
1669 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001670 if (this.scrollPort_.isScrolledEnd)
1671 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001672 }
1673
1674 if (cursorRow >= this.screen_.rowsArray.length)
1675 cursorRow = this.screen_.rowsArray.length - 1;
1676
rginda87b86462011-12-14 13:48:03 -08001677 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001678};
1679
1680/**
1681 * Relocate rows from one part of the addressable screen to another.
1682 *
1683 * This is used to recycle rows during VT scrolls (those which are driven
1684 * by VT commands, rather than by the user manipulating the scrollbar.)
1685 *
1686 * In this case, the blank lines scrolled into the scroll region are made of
1687 * the nodes we scrolled off. These have their rowIndex properties carefully
1688 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001689 *
1690 * @param {number} fromIndex The start index.
1691 * @param {number} count The number of rows to move.
1692 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001693 */
1694hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1695 var ary = this.screen_.removeRows(fromIndex, count);
1696 this.screen_.insertRows(toIndex, ary);
1697
1698 var start, end;
1699 if (fromIndex < toIndex) {
1700 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001701 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001702 } else {
1703 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001704 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001705 }
1706
1707 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001708 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001709};
1710
1711/**
1712 * Renumber the rowIndex property of the given range of rows.
1713 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001714 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001715 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001716 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001717 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001718 *
1719 * @param {number} start The start index.
1720 * @param {number} end The end index.
1721 * @param {hterm.Screen} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001722 */
Robert Ginda40932892012-12-10 17:26:40 -08001723hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1724 var screen = opt_screen || this.screen_;
1725
rginda8ba33642011-12-14 12:31:31 -08001726 var offset = this.scrollbackRows_.length;
1727 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001728 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001729 }
1730};
1731
1732/**
1733 * Print a string to the terminal.
1734 *
1735 * This respects the current insert and wraparound modes. It will add new lines
1736 * to the end of the terminal, scrolling off the top into the scrollback buffer
1737 * if necessary.
1738 *
1739 * The string is *not* parsed for escape codes. Use the interpret() method if
1740 * that's what you're after.
1741 *
1742 * @param{string} str The string to print.
1743 */
1744hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001745 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001746
Ricky Liang48f05cb2013-12-31 23:35:29 +08001747 var strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04001748 // Fun edge case: If the string only contains zero width codepoints (like
1749 // combining characters), we make sure to iterate at least once below.
1750 if (strWidth == 0 && str)
1751 strWidth = 1;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001752
1753 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001754 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1755 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001756 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001757 }
rgindaa19afe22012-01-25 15:40:22 -08001758
Ricky Liang48f05cb2013-12-31 23:35:29 +08001759 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001760 var didOverflow = false;
1761 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001762
rgindaa9abdd82012-08-06 18:05:09 -07001763 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1764 didOverflow = true;
1765 count = this.screenSize.width - this.screen_.cursorPosition.column;
1766 }
rgindaa19afe22012-01-25 15:40:22 -08001767
rgindaa9abdd82012-08-06 18:05:09 -07001768 if (didOverflow && !this.options_.wraparound) {
1769 // If the string overflowed the line but wraparound is off, then the
1770 // last printed character should be the last of the string.
1771 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001772 substr = lib.wc.substr(str, startOffset, count - 1) +
1773 lib.wc.substr(str, strWidth - 1);
1774 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001775 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001776 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001777 }
rgindaa19afe22012-01-25 15:40:22 -08001778
Ricky Liang48f05cb2013-12-31 23:35:29 +08001779 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1780 for (var i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001781 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
1782 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001783
1784 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001785 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001786 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001787 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001788 }
1789 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001790 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07001791 }
1792
1793 this.screen_.maybeClipCurrentRow();
1794 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001795 }
rginda8ba33642011-12-14 12:31:31 -08001796
1797 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001798
rginda9f5222b2012-03-05 11:53:28 -08001799 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001800 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001801};
1802
1803/**
rginda87b86462011-12-14 13:48:03 -08001804 * Set the VT scroll region.
1805 *
rginda87b86462011-12-14 13:48:03 -08001806 * This also resets the cursor position to the absolute (0, 0) position, since
1807 * that's what xterm appears to do.
1808 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001809 * Setting the scroll region to the full height of the terminal will clear
1810 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1811 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1812 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1813 * continue to work as most users would expect.
1814 *
rginda87b86462011-12-14 13:48:03 -08001815 * @param {integer} scrollTop The zero-based top of the scroll region.
1816 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1817 * inclusive.
1818 */
1819hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001820 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001821 this.vtScrollTop_ = null;
1822 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001823 } else {
1824 this.vtScrollTop_ = scrollTop;
1825 this.vtScrollBottom_ = scrollBottom;
1826 }
rginda87b86462011-12-14 13:48:03 -08001827};
1828
1829/**
rginda8ba33642011-12-14 12:31:31 -08001830 * Return the top row index according to the VT.
1831 *
1832 * This will return 0 unless the terminal has been told to restrict scrolling
1833 * to some lower row. It is used for some VT cursor positioning and scrolling
1834 * commands.
1835 *
1836 * @return {integer} The topmost row in the terminal's scroll region.
1837 */
1838hterm.Terminal.prototype.getVTScrollTop = function() {
1839 if (this.vtScrollTop_ != null)
1840 return this.vtScrollTop_;
1841
1842 return 0;
rginda87b86462011-12-14 13:48:03 -08001843};
rginda8ba33642011-12-14 12:31:31 -08001844
1845/**
1846 * Return the bottom row index according to the VT.
1847 *
1848 * This will return the height of the terminal unless the it has been told to
1849 * restrict scrolling to some higher row. It is used for some VT cursor
1850 * positioning and scrolling commands.
1851 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001852 * @return {integer} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001853 */
1854hterm.Terminal.prototype.getVTScrollBottom = function() {
1855 if (this.vtScrollBottom_ != null)
1856 return this.vtScrollBottom_;
1857
rginda87b86462011-12-14 13:48:03 -08001858 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001859}
1860
1861/**
1862 * Process a '\n' character.
1863 *
1864 * If the cursor is on the final row of the terminal this will append a new
1865 * blank row to the screen and scroll the topmost row into the scrollback
1866 * buffer.
1867 *
1868 * Otherwise, this moves the cursor to column zero of the next row.
1869 */
1870hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001871 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1872 this.screen_.rowsArray.length - 1);
1873
1874 if (this.vtScrollBottom_ != null) {
1875 // A VT Scroll region is active, we never append new rows.
1876 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1877 // We're at the end of the VT Scroll Region, perform a VT scroll.
1878 this.vtScrollUp(1);
1879 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1880 } else if (cursorAtEndOfScreen) {
1881 // We're at the end of the screen, the only thing to do is put the
1882 // cursor to column 0.
1883 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1884 } else {
1885 // Anywhere else, advance the cursor row, and reset the column.
1886 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1887 }
1888 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001889 // We're at the end of the screen. Append a new row to the terminal,
1890 // shifting the top row into the scrollback.
1891 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001892 } else {
rginda87b86462011-12-14 13:48:03 -08001893 // Anywhere else in the screen just moves the cursor.
1894 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001895 }
1896};
1897
1898/**
1899 * Like newLine(), except maintain the cursor column.
1900 */
1901hterm.Terminal.prototype.lineFeed = function() {
1902 var column = this.screen_.cursorPosition.column;
1903 this.newLine();
1904 this.setCursorColumn(column);
1905};
1906
1907/**
rginda87b86462011-12-14 13:48:03 -08001908 * If autoCarriageReturn is set then newLine(), else lineFeed().
1909 */
1910hterm.Terminal.prototype.formFeed = function() {
1911 if (this.options_.autoCarriageReturn) {
1912 this.newLine();
1913 } else {
1914 this.lineFeed();
1915 }
1916};
1917
1918/**
1919 * Move the cursor up one row, possibly inserting a blank line.
1920 *
1921 * The cursor column is not changed.
1922 */
1923hterm.Terminal.prototype.reverseLineFeed = function() {
1924 var scrollTop = this.getVTScrollTop();
1925 var currentRow = this.screen_.cursorPosition.row;
1926
1927 if (currentRow == scrollTop) {
1928 this.insertLines(1);
1929 } else {
1930 this.setAbsoluteCursorRow(currentRow - 1);
1931 }
1932};
1933
1934/**
rginda8ba33642011-12-14 12:31:31 -08001935 * Replace all characters to the left of the current cursor with the space
1936 * character.
1937 *
1938 * TODO(rginda): This should probably *remove* the characters (not just replace
1939 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001940 * position.
rginda8ba33642011-12-14 12:31:31 -08001941 */
1942hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001943 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001944 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04001945 const count = cursor.column + 1;
1946 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08001947 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001948};
1949
1950/**
David Benjamin684a9b72012-05-01 17:19:58 -04001951 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001952 *
1953 * The cursor position is unchanged.
1954 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001955 * If the current background color is not the default background color this
1956 * will insert spaces rather than delete. This is unfortunate because the
1957 * trailing space will affect text selection, but it's difficult to come up
1958 * with a way to style empty space that wouldn't trip up the hterm.Screen
1959 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001960 *
1961 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1962 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1963 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05001964 *
1965 * @param {number} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08001966 */
1967hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001968 if (this.screen_.cursorPosition.overflow)
1969 return;
1970
Robert Ginda7fd57082012-09-25 14:41:47 -07001971 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1972 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001973
1974 if (this.screen_.textAttributes.background ===
1975 this.screen_.textAttributes.DEFAULT_COLOR) {
1976 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08001977 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07001978 this.screen_.cursorPosition.column + count) {
1979 this.screen_.deleteChars(count);
1980 this.clearCursorOverflow();
1981 return;
1982 }
1983 }
1984
rginda87b86462011-12-14 13:48:03 -08001985 var cursor = this.saveCursor();
Mike Frysinger6380bed2017-08-24 18:46:39 -04001986 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08001987 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001988 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001989};
1990
1991/**
1992 * Erase the current line.
1993 *
1994 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001995 */
1996hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001997 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001998 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001999 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002000 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002001};
2002
2003/**
David Benjamina08d78f2012-05-05 00:28:49 -04002004 * Erase all characters from the start of the screen to the current cursor
2005 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002006 *
2007 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002008 */
2009hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08002010 var cursor = this.saveCursor();
2011
2012 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002013
David Benjamina08d78f2012-05-05 00:28:49 -04002014 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002015 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002016 this.screen_.clearCursorRow();
2017 }
2018
rginda87b86462011-12-14 13:48:03 -08002019 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002020 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002021};
2022
2023/**
2024 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002025 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002026 *
2027 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002028 */
2029hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08002030 var cursor = this.saveCursor();
2031
2032 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002033
David Benjamina08d78f2012-05-05 00:28:49 -04002034 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08002035 for (var i = cursor.row + 1; i <= bottom; i++) {
2036 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002037 this.screen_.clearCursorRow();
2038 }
2039
rginda87b86462011-12-14 13:48:03 -08002040 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002041 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002042};
2043
2044/**
2045 * Fill the terminal with a given character.
2046 *
2047 * This methods does not respect the VT scroll region.
2048 *
2049 * @param {string} ch The character to use for the fill.
2050 */
2051hterm.Terminal.prototype.fill = function(ch) {
2052 var cursor = this.saveCursor();
2053
2054 this.setAbsoluteCursorPosition(0, 0);
2055 for (var row = 0; row < this.screenSize.height; row++) {
2056 for (var col = 0; col < this.screenSize.width; col++) {
2057 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002058 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002059 }
2060 }
2061
2062 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002063};
2064
2065/**
rginda9ea433c2012-03-16 11:57:00 -07002066 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002067 *
rginda9ea433c2012-03-16 11:57:00 -07002068 * This does not respect the scroll region.
2069 *
2070 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2071 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002072 */
rginda9ea433c2012-03-16 11:57:00 -07002073hterm.Terminal.prototype.clearHome = function(opt_screen) {
2074 var screen = opt_screen || this.screen_;
2075 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002076
rginda11057d52012-04-25 12:29:56 -07002077 if (bottom == 0) {
2078 // Empty screen, nothing to do.
2079 return;
2080 }
2081
rgindae4d29232012-01-19 10:47:13 -08002082 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002083 screen.setCursorPosition(i, 0);
2084 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002085 }
2086
rginda9ea433c2012-03-16 11:57:00 -07002087 screen.setCursorPosition(0, 0);
2088};
2089
2090/**
2091 * Erase the entire display without changing the cursor position.
2092 *
2093 * The cursor position is unchanged. This does not respect the scroll
2094 * region.
2095 *
2096 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2097 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002098 */
2099hterm.Terminal.prototype.clear = function(opt_screen) {
2100 var screen = opt_screen || this.screen_;
2101 var cursor = screen.cursorPosition.clone();
2102 this.clearHome(screen);
2103 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002104};
2105
2106/**
2107 * VT command to insert lines at the current cursor row.
2108 *
2109 * This respects the current scroll region. Rows pushed off the bottom are
2110 * lost (they won't show up in the scrollback buffer).
2111 *
rginda8ba33642011-12-14 12:31:31 -08002112 * @param {integer} count The number of lines to insert.
2113 */
2114hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002115 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002116
2117 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002118 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002119
Robert Ginda579186b2012-09-26 11:40:04 -07002120 // The moveCount is the number of rows we need to relocate to make room for
2121 // the new row(s). The count is the distance to move them.
2122 var moveCount = bottom - cursorRow - count + 1;
2123 if (moveCount)
2124 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002125
Robert Ginda579186b2012-09-26 11:40:04 -07002126 for (var i = count - 1; i >= 0; i--) {
2127 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002128 this.screen_.clearCursorRow();
2129 }
rginda8ba33642011-12-14 12:31:31 -08002130};
2131
2132/**
2133 * VT command to delete lines at the current cursor row.
2134 *
2135 * New rows are added to the bottom of scroll region to take their place. New
2136 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002137 *
2138 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002139 */
2140hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002141 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002142
rginda87b86462011-12-14 13:48:03 -08002143 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002144 var bottom = this.getVTScrollBottom();
2145
rginda87b86462011-12-14 13:48:03 -08002146 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002147 count = Math.min(count, maxCount);
2148
rginda87b86462011-12-14 13:48:03 -08002149 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002150 if (count != maxCount)
2151 this.moveRows_(top, count, moveStart);
2152
2153 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002154 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002155 this.screen_.clearCursorRow();
2156 }
2157
rginda87b86462011-12-14 13:48:03 -08002158 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002159 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002160};
2161
2162/**
2163 * Inserts the given number of spaces at the current cursor position.
2164 *
rginda87b86462011-12-14 13:48:03 -08002165 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002166 *
2167 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002168 */
2169hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002170 var cursor = this.saveCursor();
2171
rgindacbbd7482012-06-13 15:06:16 -07002172 var ws = lib.f.getWhitespace(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002173 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002174 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002175
2176 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002177 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002178};
2179
2180/**
2181 * Forward-delete the specified number of characters starting at the cursor
2182 * position.
2183 *
2184 * @param {integer} count The number of characters to delete.
2185 */
2186hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002187 var deleted = this.screen_.deleteChars(count);
2188 if (deleted && !this.screen_.textAttributes.isDefault()) {
2189 var cursor = this.saveCursor();
2190 this.setCursorColumn(this.screenSize.width - deleted);
2191 this.screen_.insertString(lib.f.getWhitespace(deleted));
2192 this.restoreCursor(cursor);
2193 }
2194
David Benjamin54e8bf62012-06-01 22:31:40 -04002195 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002196};
2197
2198/**
2199 * Shift rows in the scroll region upwards by a given number of lines.
2200 *
2201 * New rows are inserted at the bottom of the scroll region to fill the
2202 * vacated rows. The new rows not filled out with the current text attributes.
2203 *
2204 * This function does not affect the scrollback rows at all. Rows shifted
2205 * off the top are lost.
2206 *
rginda87b86462011-12-14 13:48:03 -08002207 * The cursor position is not altered.
2208 *
rginda8ba33642011-12-14 12:31:31 -08002209 * @param {integer} count The number of rows to scroll.
2210 */
2211hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002212 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002213
rginda87b86462011-12-14 13:48:03 -08002214 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002215 this.deleteLines(count);
2216
rginda87b86462011-12-14 13:48:03 -08002217 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002218};
2219
2220/**
2221 * Shift rows below the cursor down by a given number of lines.
2222 *
2223 * This function respects the current scroll region.
2224 *
2225 * New rows are inserted at the top of the scroll region to fill the
2226 * vacated rows. The new rows not filled out with the current text attributes.
2227 *
2228 * This function does not affect the scrollback rows at all. Rows shifted
2229 * off the bottom are lost.
2230 *
2231 * @param {integer} count The number of rows to scroll.
2232 */
2233hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08002234 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002235
rginda87b86462011-12-14 13:48:03 -08002236 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08002237 this.insertLines(opt_count);
2238
rginda87b86462011-12-14 13:48:03 -08002239 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002240};
2241
rginda87b86462011-12-14 13:48:03 -08002242
rginda8ba33642011-12-14 12:31:31 -08002243/**
2244 * Set the cursor position.
2245 *
2246 * The cursor row is relative to the scroll region if the terminal has
2247 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2248 *
2249 * @param {integer} row The new zero-based cursor row.
2250 * @param {integer} row The new zero-based cursor column.
2251 */
2252hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2253 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002254 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002255 } else {
rginda87b86462011-12-14 13:48:03 -08002256 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002257 }
rginda87b86462011-12-14 13:48:03 -08002258};
rginda8ba33642011-12-14 12:31:31 -08002259
Evan Jones2600d4f2016-12-06 09:29:36 -05002260/**
2261 * Move the cursor relative to its current position.
2262 *
2263 * @param {number} row
2264 * @param {number} column
2265 */
rginda87b86462011-12-14 13:48:03 -08002266hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2267 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002268 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2269 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002270 this.screen_.setCursorPosition(row, column);
2271};
2272
Evan Jones2600d4f2016-12-06 09:29:36 -05002273/**
2274 * Move the cursor to the specified position.
2275 *
2276 * @param {number} row
2277 * @param {number} column
2278 */
rginda87b86462011-12-14 13:48:03 -08002279hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002280 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2281 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002282 this.screen_.setCursorPosition(row, column);
2283};
2284
2285/**
2286 * Set the cursor column.
2287 *
2288 * @param {integer} column The new zero-based cursor column.
2289 */
2290hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002291 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002292};
2293
2294/**
2295 * Return the cursor column.
2296 *
2297 * @return {integer} The zero-based cursor column.
2298 */
2299hterm.Terminal.prototype.getCursorColumn = function() {
2300 return this.screen_.cursorPosition.column;
2301};
2302
2303/**
2304 * Set the cursor row.
2305 *
2306 * The cursor row is relative to the scroll region if the terminal has
2307 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2308 *
2309 * @param {integer} row The new cursor row.
2310 */
rginda87b86462011-12-14 13:48:03 -08002311hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2312 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002313};
2314
2315/**
2316 * Return the cursor row.
2317 *
2318 * @return {integer} The zero-based cursor row.
2319 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002320hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002321 return this.screen_.cursorPosition.row;
2322};
2323
2324/**
2325 * Request that the ScrollPort redraw itself soon.
2326 *
2327 * The redraw will happen asynchronously, soon after the call stack winds down.
2328 * Multiple calls will be coalesced into a single redraw.
2329 */
2330hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002331 if (this.timeouts_.redraw)
2332 return;
rginda8ba33642011-12-14 12:31:31 -08002333
2334 var self = this;
rginda87b86462011-12-14 13:48:03 -08002335 this.timeouts_.redraw = setTimeout(function() {
2336 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002337 self.scrollPort_.redraw_();
2338 }, 0);
2339};
2340
2341/**
2342 * Request that the ScrollPort be scrolled to the bottom.
2343 *
2344 * The scroll will happen asynchronously, soon after the call stack winds down.
2345 * Multiple calls will be coalesced into a single scroll.
2346 *
2347 * This affects the scrollbar position of the ScrollPort, and has nothing to
2348 * do with the VT scroll commands.
2349 */
2350hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2351 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002352 return;
rginda8ba33642011-12-14 12:31:31 -08002353
2354 var self = this;
2355 this.timeouts_.scrollDown = setTimeout(function() {
2356 delete self.timeouts_.scrollDown;
2357 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2358 }, 10);
2359};
2360
2361/**
2362 * Move the cursor up a specified number of rows.
2363 *
2364 * @param {integer} count The number of rows to move the cursor.
2365 */
2366hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002367 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002368};
2369
2370/**
2371 * Move the cursor down a specified number of rows.
2372 *
2373 * @param {integer} count The number of rows to move the cursor.
2374 */
2375hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002376 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002377 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2378 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2379 this.screenSize.height - 1);
2380
rgindacbbd7482012-06-13 15:06:16 -07002381 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002382 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002383 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002384};
2385
2386/**
2387 * Move the cursor left a specified number of columns.
2388 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002389 * If reverse wraparound mode is enabled and the previous row wrapped into
2390 * the current row then we back up through the wraparound as well.
2391 *
rginda8ba33642011-12-14 12:31:31 -08002392 * @param {integer} count The number of columns to move the cursor.
2393 */
2394hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002395 count = count || 1;
2396
2397 if (count < 1)
2398 return;
2399
2400 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002401 if (this.options_.reverseWraparound) {
2402 if (this.screen_.cursorPosition.overflow) {
2403 // If this cursor is in the right margin, consume one count to get it
2404 // back to the last column. This only applies when we're in reverse
2405 // wraparound mode.
2406 count--;
2407 this.clearCursorOverflow();
2408
2409 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002410 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002411 }
2412
Robert Gindabfb32622014-07-17 13:20:27 -07002413 var newRow = this.screen_.cursorPosition.row;
2414 var newColumn = currentColumn - count;
2415 if (newColumn < 0) {
2416 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2417 if (newRow < 0) {
2418 // xterm also wraps from row 0 to the last row.
2419 newRow = this.screenSize.height + newRow % this.screenSize.height;
2420 }
2421 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2422 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002423
Robert Gindabfb32622014-07-17 13:20:27 -07002424 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2425
2426 } else {
2427 var newColumn = Math.max(currentColumn - count, 0);
2428 this.setCursorColumn(newColumn);
2429 }
rginda8ba33642011-12-14 12:31:31 -08002430};
2431
2432/**
2433 * Move the cursor right a specified number of columns.
2434 *
2435 * @param {integer} count The number of columns to move the cursor.
2436 */
2437hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002438 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002439
2440 if (count < 1)
2441 return;
2442
rgindacbbd7482012-06-13 15:06:16 -07002443 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002444 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002445 this.setCursorColumn(column);
2446};
2447
2448/**
2449 * Reverse the foreground and background colors of the terminal.
2450 *
2451 * This only affects text that was drawn with no attributes.
2452 *
2453 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2454 * been drawn with attributes that happen to coincide with the default
2455 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002456 *
2457 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002458 */
2459hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002460 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002461 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002462 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2463 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002464 } else {
rginda9f5222b2012-03-05 11:53:28 -08002465 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2466 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002467 }
2468};
2469
2470/**
rginda87b86462011-12-14 13:48:03 -08002471 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002472 *
2473 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002474 */
2475hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002476 this.cursorNode_.style.backgroundColor =
2477 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002478
2479 var self = this;
2480 setTimeout(function() {
Matheus Fernandes2d733082017-09-11 06:43:01 -04002481 self.restyleCursor_();
rginda6d397402012-01-17 10:58:29 -08002482 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002483
Michael Kelly485ecd12014-06-09 11:41:56 -04002484 // bellSquelchTimeout_ affects both audio and notification bells.
2485 if (this.bellSquelchTimeout_)
2486 return;
2487
Robert Ginda92e18102013-03-14 13:56:37 -07002488 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002489 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002490 this.bellSequelchTimeout_ = setTimeout(function() {
2491 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002492 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002493 } else {
2494 delete this.bellSquelchTimeout_;
2495 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002496
2497 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002498 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002499 this.bellNotificationList_.push(n);
2500 // TODO: Should we try to raise the window here?
2501 n.onclick = function() { self.closeBellNotifications_(); };
2502 }
rginda87b86462011-12-14 13:48:03 -08002503};
2504
2505/**
rginda8ba33642011-12-14 12:31:31 -08002506 * Set the origin mode bit.
2507 *
2508 * If origin mode is on, certain VT cursor and scrolling commands measure their
2509 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2510 * to the top of the addressable screen.
2511 *
2512 * Defaults to off.
2513 *
2514 * @param {boolean} state True to set origin mode, false to unset.
2515 */
2516hterm.Terminal.prototype.setOriginMode = function(state) {
2517 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002518 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002519};
2520
2521/**
2522 * Set the insert mode bit.
2523 *
2524 * If insert mode is on, existing text beyond the cursor position will be
2525 * shifted right to make room for new text. Otherwise, new text overwrites
2526 * any existing text.
2527 *
2528 * Defaults to off.
2529 *
2530 * @param {boolean} state True to set insert mode, false to unset.
2531 */
2532hterm.Terminal.prototype.setInsertMode = function(state) {
2533 this.options_.insertMode = state;
2534};
2535
2536/**
rginda87b86462011-12-14 13:48:03 -08002537 * Set the auto carriage return bit.
2538 *
2539 * If auto carriage return is on then a formfeed character is interpreted
2540 * as a newline, otherwise it's the same as a linefeed. The difference boils
2541 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002542 *
2543 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002544 */
2545hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2546 this.options_.autoCarriageReturn = state;
2547};
2548
2549/**
rginda8ba33642011-12-14 12:31:31 -08002550 * Set the wraparound mode bit.
2551 *
2552 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2553 * to the start of the following row. Otherwise, the cursor is clamped to the
2554 * end of the screen and attempts to write past it are ignored.
2555 *
2556 * Defaults to on.
2557 *
2558 * @param {boolean} state True to set wraparound mode, false to unset.
2559 */
2560hterm.Terminal.prototype.setWraparound = function(state) {
2561 this.options_.wraparound = state;
2562};
2563
2564/**
2565 * Set the reverse-wraparound mode bit.
2566 *
2567 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2568 * to the end of the previous row. Otherwise, the cursor is clamped to column
2569 * 0.
2570 *
2571 * Defaults to off.
2572 *
2573 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2574 */
2575hterm.Terminal.prototype.setReverseWraparound = function(state) {
2576 this.options_.reverseWraparound = state;
2577};
2578
2579/**
2580 * Selects between the primary and alternate screens.
2581 *
2582 * If alternate mode is on, the alternate screen is active. Otherwise the
2583 * primary screen is active.
2584 *
2585 * Swapping screens has no effect on the scrollback buffer.
2586 *
2587 * Each screen maintains its own cursor position.
2588 *
2589 * Defaults to off.
2590 *
2591 * @param {boolean} state True to set alternate mode, false to unset.
2592 */
2593hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002594 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002595 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2596
rginda35c456b2012-02-09 17:29:05 -08002597 if (this.screen_.rowsArray.length &&
2598 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2599 // If the screen changed sizes while we were away, our rowIndexes may
2600 // be incorrect.
2601 var offset = this.scrollbackRows_.length;
2602 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002603 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002604 ary[i].rowIndex = offset + i;
2605 }
2606 }
rginda8ba33642011-12-14 12:31:31 -08002607
rginda35c456b2012-02-09 17:29:05 -08002608 this.realizeWidth_(this.screenSize.width);
2609 this.realizeHeight_(this.screenSize.height);
2610 this.scrollPort_.syncScrollHeight();
2611 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002612
rginda6d397402012-01-17 10:58:29 -08002613 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002614 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002615};
2616
2617/**
2618 * Set the cursor-blink mode bit.
2619 *
2620 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2621 * a visible cursor does not blink.
2622 *
2623 * You should make sure to turn blinking off if you're going to dispose of a
2624 * terminal, otherwise you'll leak a timeout.
2625 *
2626 * Defaults to on.
2627 *
2628 * @param {boolean} state True to set cursor-blink mode, false to unset.
2629 */
2630hterm.Terminal.prototype.setCursorBlink = function(state) {
2631 this.options_.cursorBlink = state;
2632
2633 if (!state && this.timeouts_.cursorBlink) {
2634 clearTimeout(this.timeouts_.cursorBlink);
2635 delete this.timeouts_.cursorBlink;
2636 }
2637
2638 if (this.options_.cursorVisible)
2639 this.setCursorVisible(true);
2640};
2641
2642/**
2643 * Set the cursor-visible mode bit.
2644 *
2645 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2646 *
2647 * Defaults to on.
2648 *
2649 * @param {boolean} state True to set cursor-visible mode, false to unset.
2650 */
2651hterm.Terminal.prototype.setCursorVisible = function(state) {
2652 this.options_.cursorVisible = state;
2653
2654 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002655 if (this.timeouts_.cursorBlink) {
2656 clearTimeout(this.timeouts_.cursorBlink);
2657 delete this.timeouts_.cursorBlink;
2658 }
rginda87b86462011-12-14 13:48:03 -08002659 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002660 return;
2661 }
2662
rginda87b86462011-12-14 13:48:03 -08002663 this.syncCursorPosition_();
2664
2665 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002666
2667 if (this.options_.cursorBlink) {
2668 if (this.timeouts_.cursorBlink)
2669 return;
2670
Robert Gindaea2183e2014-07-17 09:51:51 -07002671 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002672 } else {
2673 if (this.timeouts_.cursorBlink) {
2674 clearTimeout(this.timeouts_.cursorBlink);
2675 delete this.timeouts_.cursorBlink;
2676 }
2677 }
2678};
2679
2680/**
rginda87b86462011-12-14 13:48:03 -08002681 * Synchronizes the visible cursor and document selection with the current
2682 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002683 */
2684hterm.Terminal.prototype.syncCursorPosition_ = function() {
2685 var topRowIndex = this.scrollPort_.getTopRowIndex();
2686 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2687 var cursorRowIndex = this.scrollbackRows_.length +
2688 this.screen_.cursorPosition.row;
2689
2690 if (cursorRowIndex > bottomRowIndex) {
2691 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04002692 this.setCssVar('cursor-offset-row', '-1');
rginda8ba33642011-12-14 12:31:31 -08002693 return;
2694 }
2695
Robert Gindab837c052014-08-11 11:17:51 -07002696 if (this.options_.cursorVisible &&
2697 this.cursorNode_.style.display == 'none') {
2698 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2699 this.cursorNode_.style.display = '';
2700 }
2701
Mike Frysinger44c32202017-08-05 01:13:09 -04002702 // Position the cursor using CSS variable math. If we do the math in JS,
2703 // the float math will end up being more precise than the CSS which will
2704 // cause the cursor tracking to be off.
2705 this.setCssVar(
2706 'cursor-offset-row',
2707 `${cursorRowIndex - topRowIndex} + ` +
2708 `${this.scrollPort_.visibleRowTopMargin}px`);
2709 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08002710
2711 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04002712 '(' + this.screen_.cursorPosition.column +
2713 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08002714 ')');
2715
2716 // Update the caret for a11y purposes.
2717 var selection = this.document_.getSelection();
2718 if (selection && selection.isCollapsed)
2719 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002720};
2721
Robert Gindafb1be6a2013-12-11 11:56:22 -08002722/**
2723 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2724 * and character cell dimensions.
2725 */
Robert Ginda830583c2013-08-07 13:20:46 -07002726hterm.Terminal.prototype.restyleCursor_ = function() {
2727 var shape = this.cursorShape_;
2728
2729 if (this.cursorNode_.getAttribute('focus') == 'false') {
2730 // Always show a block cursor when unfocused.
2731 shape = hterm.Terminal.cursorShape.BLOCK;
2732 }
2733
2734 var style = this.cursorNode_.style;
2735
2736 switch (shape) {
2737 case hterm.Terminal.cursorShape.BEAM:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002738 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002739 style.backgroundColor = 'transparent';
2740 style.borderBottomStyle = null;
2741 style.borderLeftStyle = 'solid';
2742 break;
2743
2744 case hterm.Terminal.cursorShape.UNDERLINE:
2745 style.height = this.scrollPort_.characterSize.baseline + 'px';
2746 style.backgroundColor = 'transparent';
2747 style.borderBottomStyle = 'solid';
2748 // correct the size to put it exactly at the baseline
2749 style.borderLeftStyle = null;
2750 break;
2751
2752 default:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002753 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002754 style.backgroundColor = this.cursorColor_;
2755 style.borderBottomStyle = null;
2756 style.borderLeftStyle = null;
2757 break;
2758 }
2759};
2760
rginda8ba33642011-12-14 12:31:31 -08002761/**
2762 * Synchronizes the visible cursor with the current cursor coordinates.
2763 *
2764 * The sync will happen asynchronously, soon after the call stack winds down.
2765 * Multiple calls will be coalesced into a single sync.
2766 */
2767hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2768 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002769 return;
rginda8ba33642011-12-14 12:31:31 -08002770
2771 var self = this;
2772 this.timeouts_.syncCursor = setTimeout(function() {
2773 self.syncCursorPosition_();
2774 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002775 }, 0);
2776};
2777
rgindacc2996c2012-02-24 14:59:31 -08002778/**
rgindaf522ce02012-04-17 17:49:17 -07002779 * Show or hide the zoom warning.
2780 *
2781 * The zoom warning is a message warning the user that their browser zoom must
2782 * be set to 100% in order for hterm to function properly.
2783 *
2784 * @param {boolean} state True to show the message, false to hide it.
2785 */
2786hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2787 if (!this.zoomWarningNode_) {
2788 if (!state)
2789 return;
2790
2791 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002792 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07002793 this.zoomWarningNode_.style.cssText = (
2794 'color: black;' +
2795 'background-color: #ff2222;' +
2796 'font-size: large;' +
2797 'border-radius: 8px;' +
2798 'opacity: 0.75;' +
2799 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2800 'top: 0.5em;' +
2801 'right: 1.2em;' +
2802 'position: absolute;' +
2803 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002804 '-webkit-user-select: none;' +
2805 '-moz-text-size-adjust: none;' +
2806 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002807
2808 this.zoomWarningNode_.addEventListener('click', function(e) {
2809 this.parentNode.removeChild(this);
2810 });
rgindaf522ce02012-04-17 17:49:17 -07002811 }
2812
Robert Gindab4839c22013-02-28 16:52:10 -08002813 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2814 hterm.zoomWarningMessage,
2815 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2816
rgindaf522ce02012-04-17 17:49:17 -07002817 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2818
2819 if (state) {
2820 if (!this.zoomWarningNode_.parentNode)
2821 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2822 } else if (this.zoomWarningNode_.parentNode) {
2823 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2824 }
2825};
2826
2827/**
rgindacc2996c2012-02-24 14:59:31 -08002828 * Show the terminal overlay for a given amount of time.
2829 *
2830 * The terminal overlay appears in inverse video in a large font, centered
2831 * over the terminal. You should probably keep the overlay message brief,
2832 * since it's in a large font and you probably aren't going to check the size
2833 * of the terminal first.
2834 *
2835 * @param {string} msg The text (not HTML) message to display in the overlay.
2836 * @param {number} opt_timeout The amount of time to wait before fading out
2837 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2838 * stay up forever (or until the next overlay).
2839 */
2840hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002841 if (!this.overlayNode_) {
2842 if (!this.div_)
2843 return;
2844
2845 this.overlayNode_ = this.document_.createElement('div');
2846 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002847 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002848 'font-size: xx-large;' +
2849 'opacity: 0.75;' +
2850 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2851 'position: absolute;' +
2852 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002853 '-webkit-transition: opacity 180ms ease-in;' +
2854 '-moz-user-select: none;' +
2855 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002856
2857 this.overlayNode_.addEventListener('mousedown', function(e) {
2858 e.preventDefault();
2859 e.stopPropagation();
2860 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002861 }
2862
rginda9f5222b2012-03-05 11:53:28 -08002863 this.overlayNode_.style.color = this.prefs_.get('background-color');
2864 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2865 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2866
rgindaf0090c92012-02-10 14:58:52 -08002867 this.overlayNode_.textContent = msg;
2868 this.overlayNode_.style.opacity = '0.75';
2869
2870 if (!this.overlayNode_.parentNode)
2871 this.div_.appendChild(this.overlayNode_);
2872
Robert Ginda97769282013-02-01 15:30:30 -08002873 var divSize = hterm.getClientSize(this.div_);
2874 var overlaySize = hterm.getClientSize(this.overlayNode_);
2875
Robert Ginda8a59f762014-07-23 11:29:55 -07002876 this.overlayNode_.style.top =
2877 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08002878 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07002879 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08002880
rgindaf0090c92012-02-10 14:58:52 -08002881 if (this.overlayTimeout_)
2882 clearTimeout(this.overlayTimeout_);
2883
rgindacc2996c2012-02-24 14:59:31 -08002884 if (opt_timeout === null)
2885 return;
2886
Mike Frysingerb6cfded2017-09-18 00:39:31 -04002887 this.overlayTimeout_ = setTimeout(() => {
2888 this.overlayNode_.style.opacity = '0';
2889 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
2890 }, opt_timeout || 1500);
2891};
2892
2893/**
2894 * Hide the terminal overlay immediately.
2895 *
2896 * Useful when we show an overlay for an event with an unknown end time.
2897 */
2898hterm.Terminal.prototype.hideOverlay = function() {
2899 if (this.overlayTimeout_)
2900 clearTimeout(this.overlayTimeout_);
2901 this.overlayTimeout_ = null;
2902
2903 if (this.overlayNode_.parentNode)
2904 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
2905 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08002906};
2907
rginda4bba5e12012-06-20 16:15:30 -07002908/**
2909 * Paste from the system clipboard to the terminal.
2910 */
2911hterm.Terminal.prototype.paste = function() {
Mike Frysinger4628ad22017-07-20 02:44:20 -04002912 return hterm.pasteFromClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002913};
2914
2915/**
2916 * Copy a string to the system clipboard.
2917 *
2918 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05002919 *
2920 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07002921 */
2922hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002923 if (this.prefs_.get('enable-clipboard-notice'))
2924 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2925
rgindaa09e7332012-08-17 12:49:51 -07002926 var copySource = this.document_.createElement('pre');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002927 copySource.id = 'hterm:copy-to-clipboard-source';
rginda4bba5e12012-06-20 16:15:30 -07002928 copySource.textContent = str;
2929 copySource.style.cssText = (
2930 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002931 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07002932 'position: absolute;' +
2933 'top: -99px');
2934
2935 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002936
rginda4bba5e12012-06-20 16:15:30 -07002937 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002938 var anchorNode = selection.anchorNode;
2939 var anchorOffset = selection.anchorOffset;
2940 var focusNode = selection.focusNode;
2941 var focusOffset = selection.focusOffset;
2942
rginda4bba5e12012-06-20 16:15:30 -07002943 selection.selectAllChildren(copySource);
2944
rgindaa09e7332012-08-17 12:49:51 -07002945 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002946
Rob Spies56953412014-04-28 14:09:47 -07002947 // IE doesn't support selection.extend. This means that the selection
2948 // won't return on IE.
Robert Ginda6f4f0aa2014-07-28 10:21:39 -07002949 if (selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07002950 selection.collapse(anchorNode, anchorOffset);
2951 selection.extend(focusNode, focusOffset);
2952 }
rgindafaa74742012-08-21 13:34:03 -07002953
rginda4bba5e12012-06-20 16:15:30 -07002954 copySource.parentNode.removeChild(copySource);
2955};
2956
Evan Jones2600d4f2016-12-06 09:29:36 -05002957/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04002958 * Display an image.
2959 *
2960 * @param {Object} options The image to display.
2961 * @param {string=} options.name A human readable string for the image.
2962 * @param {string|number=} options.size The size (in bytes).
2963 * @param {boolean=} options.preserveAspectRatio Whether to preserve aspect.
2964 * @param {boolean=} options.inline Whether to display the image inline.
2965 * @param {string|number=} options.width The width of the image.
2966 * @param {string|number=} options.height The height of the image.
2967 * @param {string=} options.align Direction to align the image.
2968 * @param {string} options.uri The source URI for the image.
2969 */
2970hterm.Terminal.prototype.displayImage = function(options) {
2971 // Make sure we're actually given a resource to display.
2972 if (options.uri === undefined)
2973 return;
2974
2975 // Set up the defaults to simplify code below.
2976 if (!options.name)
2977 options.name = '';
2978
2979 // Has the user approved image display yet?
2980 if (this.allowImagesInline !== true) {
2981 this.newLine();
2982 const row = this.getRowNode(this.scrollbackRows_.length +
2983 this.getCursorRow() - 1);
2984
2985 if (this.allowImagesInline === false) {
2986 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
2987 'Inline Images Disabled');
2988 return;
2989 }
2990
2991 // Show a prompt.
2992 let button;
2993 const span = this.document_.createElement('span');
2994 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
2995 span.style.fontWeight = 'bold';
2996 span.style.borderWidth = '1px';
2997 span.style.borderStyle = 'dashed';
2998 button = this.document_.createElement('span');
2999 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3000 button.style.marginLeft = '1em';
3001 button.style.borderWidth = '1px';
3002 button.style.borderStyle = 'solid';
3003 button.addEventListener('click', () => {
3004 this.prefs_.set('allow-images-inline', false);
3005 });
3006 span.appendChild(button);
3007 button = this.document_.createElement('span');
3008 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3009 'allow this session');
3010 button.style.marginLeft = '1em';
3011 button.style.borderWidth = '1px';
3012 button.style.borderStyle = 'solid';
3013 button.addEventListener('click', () => {
3014 this.allowImagesInline = true;
3015 });
3016 span.appendChild(button);
3017 button = this.document_.createElement('span');
3018 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3019 button.style.marginLeft = '1em';
3020 button.style.borderWidth = '1px';
3021 button.style.borderStyle = 'solid';
3022 button.addEventListener('click', () => {
3023 this.prefs_.set('allow-images-inline', true);
3024 });
3025 span.appendChild(button);
3026
3027 row.appendChild(span);
3028 return;
3029 }
3030
3031 // See if we should show this object directly, or download it.
3032 if (options.inline) {
3033 const io = this.io.push();
3034 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
3035 'Loading $1 ...'), null);
3036
3037 // While we're loading the image, eat all the user's input.
3038 io.onVTKeystroke = io.sendString = () => {};
3039
3040 // Initialize this new image.
3041 const img = this.document_.createElement('img');
3042 img.src = options.uri;
3043 img.title = img.alt = options.name;
3044
3045 // Attach the image to the page to let it load/render. It won't stay here.
3046 // This is needed so it's visible and the DOM can calculate the height. If
3047 // the image is hidden or not in the DOM, the height is always 0.
3048 this.document_.body.appendChild(img);
3049
3050 // Wait for the image to finish loading before we try moving it to the
3051 // right place in the terminal.
3052 img.onload = () => {
3053 // Now that we have the image dimensions, figure out how to show it.
3054 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
3055 img.style.maxWidth = `${this.document_.body.clientWidth}px`;
3056 img.style.maxHeight = `${this.document_.body.clientHeight}px`;
3057
3058 // Parse a width/height specification.
3059 const parseDim = (dim, maxDim, cssVar) => {
3060 if (!dim || dim == 'auto')
3061 return '';
3062
3063 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3064 if (ary) {
3065 if (ary[2] == '%')
3066 return maxDim * parseInt(ary[1]) / 100 + 'px';
3067 else if (ary[2] == 'px')
3068 return dim;
3069 else
3070 return `calc(${dim} * var(${cssVar}))`;
3071 }
3072
3073 return '';
3074 };
3075 img.style.width =
3076 parseDim(options.width, this.document_.body.clientWidth,
3077 '--hterm-charsize-width');
3078 img.style.height =
3079 parseDim(options.height, this.document_.body.clientHeight,
3080 '--hterm-charsize-height');
3081
3082 // Figure out how many rows the image occupies, then add that many.
3083 // XXX: This count will be inaccurate if the font size changes on us.
3084 const padRows = Math.ceil(img.clientHeight /
3085 this.scrollPort_.characterSize.height);
3086 for (let i = 0; i < padRows; ++i)
3087 this.newLine();
3088
3089 // Update the max height in case the user shrinks the character size.
3090 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3091
3092 // Move the image to the last row. This way when we scroll up, it doesn't
3093 // disappear when the first row gets clipped. It will disappear when we
3094 // scroll down and the last row is clipped ...
3095 this.document_.body.removeChild(img);
3096 // Create a wrapper node so we can do an absolute in a relative position.
3097 // This helps with rounding errors between JS & CSS counts.
3098 const div = this.document_.createElement('div');
3099 div.style.position = 'relative';
3100 div.style.textAlign = options.align;
3101 img.style.position = 'absolute';
3102 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3103 div.appendChild(img);
3104 const row = this.getRowNode(this.scrollbackRows_.length +
3105 this.getCursorRow() - 1);
3106 row.appendChild(div);
3107
3108 io.hideOverlay();
3109 io.pop();
3110 };
3111
3112 // If we got a malformed image, give up.
3113 img.onerror = (e) => {
3114 this.document_.body.removeChild(img);
3115 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
3116 'Loading $1 failed ...'));
3117 io.pop();
3118 };
3119 } else {
3120 // We can't use chrome.downloads.download as that requires "downloads"
3121 // permissions, and that works only in extensions, not apps.
3122 const a = this.document_.createElement('a');
3123 a.href = options.uri;
3124 a.download = options.name;
3125 this.document_.body.appendChild(a);
3126 a.click();
3127 a.remove();
3128 }
3129};
3130
3131/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003132 * Returns the selected text, or null if no text is selected.
3133 *
3134 * @return {string|null}
3135 */
rgindaa09e7332012-08-17 12:49:51 -07003136hterm.Terminal.prototype.getSelectionText = function() {
3137 var selection = this.scrollPort_.selection;
3138 selection.sync();
3139
3140 if (selection.isCollapsed)
3141 return null;
3142
3143
3144 // Start offset measures from the beginning of the line.
3145 var startOffset = selection.startOffset;
3146 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003147
Robert Gindafdbb3f22012-09-06 20:23:06 -07003148 if (node.nodeName != 'X-ROW') {
3149 // If the selection doesn't start on an x-row node, then it must be
3150 // somewhere inside the x-row. Add any characters from previous siblings
3151 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003152
3153 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3154 // If node is the text node in a styled span, move up to the span node.
3155 node = node.parentNode;
3156 }
3157
Robert Gindafdbb3f22012-09-06 20:23:06 -07003158 while (node.previousSibling) {
3159 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003160 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003161 }
rgindaa09e7332012-08-17 12:49:51 -07003162 }
3163
3164 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08003165 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
3166 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003167 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003168
Robert Gindafdbb3f22012-09-06 20:23:06 -07003169 if (node.nodeName != 'X-ROW') {
3170 // If the selection doesn't end on an x-row node, then it must be
3171 // somewhere inside the x-row. Add any characters from following siblings
3172 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003173
3174 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3175 // If node is the text node in a styled span, move up to the span node.
3176 node = node.parentNode;
3177 }
3178
Robert Gindafdbb3f22012-09-06 20:23:06 -07003179 while (node.nextSibling) {
3180 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003181 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003182 }
rgindaa09e7332012-08-17 12:49:51 -07003183 }
3184
3185 var rv = this.getRowsText(selection.startRow.rowIndex,
3186 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003187 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003188};
3189
rginda4bba5e12012-06-20 16:15:30 -07003190/**
3191 * Copy the current selection to the system clipboard, then clear it after a
3192 * short delay.
3193 */
3194hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07003195 var text = this.getSelectionText();
3196 if (text != null)
3197 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07003198};
3199
rgindaf0090c92012-02-10 14:58:52 -08003200hterm.Terminal.prototype.overlaySize = function() {
3201 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
3202};
3203
rginda87b86462011-12-14 13:48:03 -08003204/**
3205 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3206 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003207 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003208 */
3209hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08003210 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08003211 this.scrollPort_.scrollRowToBottom(this.getRowCount());
3212
Robert Ginda8cb7d902013-06-20 14:37:18 -07003213 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08003214};
3215
3216/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003217 * Open the selected url.
3218 */
3219hterm.Terminal.prototype.openSelectedUrl_ = function() {
3220 var str = this.getSelectionText();
3221
3222 // If there is no selection, try and expand wherever they clicked.
3223 if (str == null) {
3224 this.screen_.expandSelection(this.document_.getSelection());
3225 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003226
3227 // If clicking in empty space, return.
3228 if (str == null)
3229 return;
Mike Frysinger70b94692017-01-26 18:57:50 -10003230 }
3231
3232 // Make sure URL is valid before opening.
3233 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
3234 return;
Mike Frysinger43472622017-06-26 18:11:07 -04003235
3236 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003237 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003238 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3239 // We have to whitelist a few protocols that lack authorities and thus
3240 // never use the //. Like mailto.
3241 switch (str.split(':', 1)[0]) {
3242 case 'mailto':
3243 break;
3244 default:
3245 str = 'http://' + str;
3246 break;
3247 }
3248 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003249
Mike Frysinger720fa832017-10-23 01:15:52 -04003250 hterm.openUrl(str);
Mike Frysinger70b94692017-01-26 18:57:50 -10003251}
3252
3253
3254/**
rgindad5613292012-06-19 15:40:37 -07003255 * Add the terminalRow and terminalColumn properties to mouse events and
3256 * then forward on to onMouse().
3257 *
3258 * The terminalRow and terminalColumn properties contain the (row, column)
3259 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003260 *
3261 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003262 */
3263hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003264 if (e.processedByTerminalHandler_) {
3265 // We register our event handlers on the document, as well as the cursor
3266 // and the scroll blocker. Mouse events that occur on the cursor or
3267 // scroll blocker will also appear on the document, but we don't want to
3268 // process them twice.
3269 //
3270 // We can't just prevent bubbling because that has other side effects, so
3271 // we decorate the event object with this property instead.
3272 return;
3273 }
3274
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003275 var reportMouseEvents = (!this.defeatMouseReports_ &&
3276 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3277
rgindafaa74742012-08-21 13:34:03 -07003278 e.processedByTerminalHandler_ = true;
3279
Robert Gindaeda48db2014-07-17 09:25:30 -07003280 // One based row/column stored on the mouse event.
3281 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
3282 this.scrollPort_.characterSize.height) + 1;
3283 e.terminalColumn = parseInt(e.clientX /
3284 this.scrollPort_.characterSize.width) + 1;
3285
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003286 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3287 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003288 return;
3289 }
3290
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003291 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003292 // If the cursor is visible and we're not sending mouse events to the
3293 // host app, then we want to hide the terminal cursor when the mouse
3294 // cursor is over top. This keeps the terminal cursor from interfering
3295 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003296 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3297 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3298 this.cursorNode_.style.display = 'none';
3299 } else if (this.cursorNode_.style.display == 'none') {
3300 this.cursorNode_.style.display = '';
3301 }
3302 }
rgindad5613292012-06-19 15:40:37 -07003303
Robert Ginda928cf632014-03-05 15:07:41 -08003304 if (e.type == 'mousedown') {
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003305 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003306 // If VT mouse reporting is disabled, or has been defeated with
3307 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003308 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003309 this.setSelectionEnabled(true);
3310 } else {
3311 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003312 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003313 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003314 this.setSelectionEnabled(false);
3315 e.preventDefault();
3316 }
3317 }
3318
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003319 if (!reportMouseEvents) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003320 if (e.type == 'dblclick' && this.copyOnSelect) {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003321 this.screen_.expandSelection(this.document_.getSelection());
Robert Ginda15ed4902016-07-12 10:43:22 -07003322 this.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07003323 }
3324
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003325 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003326 // Debounce this event with the dblclick event. If you try to doubleclick
3327 // a URL to open it, Chrome will fire click then dblclick, but we won't
3328 // have expanded the selection text at the first click event.
3329 clearTimeout(this.timeouts_.openUrl);
3330 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3331 500);
3332 return;
3333 }
3334
Mike Frysinger847577f2017-05-23 23:25:57 -04003335 if (e.type == 'mousedown') {
3336 if ((this.mouseRightClickPaste && e.button == 2 /* right button */) ||
Mike Frysinger2edd3612017-05-24 00:54:39 -04003337 e.button == this.mousePasteButton) {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003338 if (!this.paste())
Mike Frysinger05a57f02017-08-27 17:48:55 -04003339 console.warn('Could not paste manually due to web restrictions');
Mike Frysinger847577f2017-05-23 23:25:57 -04003340 }
3341 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003342
Mike Frysinger2edd3612017-05-24 00:54:39 -04003343 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003344 !this.document_.getSelection().isCollapsed) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003345 this.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003346 }
3347
3348 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3349 this.scrollBlockerNode_.engaged) {
3350 // Disengage the scroll-blocker after one of these events.
3351 this.scrollBlockerNode_.engaged = false;
3352 this.scrollBlockerNode_.style.top = '-99px';
3353 }
3354
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003355 // Emulate arrow key presses via scroll wheel events.
3356 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3357 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003358 if (e.type == 'wheel') {
3359 var delta = this.scrollPort_.scrollWheelDelta(e);
3360 var lines = lib.f.smartFloorDivide(
3361 Math.abs(delta), this.scrollPort_.characterSize.height);
3362
3363 var data = '\x1bO' + (delta < 0 ? 'B' : 'A');
3364 this.io.sendString(data.repeat(lines));
3365
3366 e.preventDefault();
3367 }
3368 }
Robert Ginda928cf632014-03-05 15:07:41 -08003369 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003370 if (!this.scrollBlockerNode_.engaged) {
3371 if (e.type == 'mousedown') {
3372 // Move the scroll-blocker into place if we want to keep the scrollport
3373 // from scrolling.
3374 this.scrollBlockerNode_.engaged = true;
3375 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3376 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3377 } else if (e.type == 'mousemove') {
3378 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3379 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003380 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003381 e.preventDefault();
3382 }
3383 }
Robert Ginda928cf632014-03-05 15:07:41 -08003384
3385 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003386 }
3387
Robert Ginda928cf632014-03-05 15:07:41 -08003388 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3389 // Restore this on mouseup in case it was temporarily defeated with a
3390 // alt-mousedown. Only do this when the selection is empty so that
3391 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003392 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003393 }
rgindad5613292012-06-19 15:40:37 -07003394};
3395
3396/**
3397 * Clients should override this if they care to know about mouse events.
3398 *
3399 * The event parameter will be a normal DOM mouse click event with additional
3400 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003401 *
3402 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003403 */
3404hterm.Terminal.prototype.onMouse = function(e) { };
3405
3406/**
rginda8e92a692012-05-20 19:37:20 -07003407 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003408 *
3409 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003410 */
Rob Spies06533ba2014-04-24 11:20:37 -07003411hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3412 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003413 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003414
3415 if (this.reportFocus) {
3416 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O')
3417 }
3418
Michael Kelly485ecd12014-06-09 11:41:56 -04003419 if (focused === true)
3420 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003421};
3422
3423/**
rginda8ba33642011-12-14 12:31:31 -08003424 * React when the ScrollPort is scrolled.
3425 */
3426hterm.Terminal.prototype.onScroll_ = function() {
3427 this.scheduleSyncCursorPosition_();
3428};
3429
3430/**
rginda9846e2f2012-01-27 13:53:33 -08003431 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003432 *
3433 * @param {Event} e The DOM paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003434 */
3435hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07003436 var data = e.text.replace(/\n/mg, '\r');
Connor Hegartyf525ceb2014-09-03 13:36:03 -07003437 data = this.keyboard.encode(data);
Robert Gindaa063b202014-07-21 11:08:25 -07003438 if (this.options_.bracketedPaste)
3439 data = '\x1b[200~' + data + '\x1b[201~';
3440
3441 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003442};
3443
3444/**
rgindaa09e7332012-08-17 12:49:51 -07003445 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003446 *
3447 * @param {Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003448 */
3449hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003450 if (!this.useDefaultWindowCopy) {
3451 e.preventDefault();
3452 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3453 }
rgindaa09e7332012-08-17 12:49:51 -07003454};
3455
3456/**
rginda8ba33642011-12-14 12:31:31 -08003457 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003458 *
3459 * Note: This function should not directly contain code that alters the internal
3460 * state of the terminal. That kind of code belongs in realizeWidth or
3461 * realizeHeight, so that it can be executed synchronously in the case of a
3462 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003463 */
3464hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003465 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003466 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003467 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003468 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003469
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003470 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003471 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003472 // gets removed from the document or during the initial load, and we can't
3473 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003474 // This can also happen if called before the scrollPort calculates the
3475 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003476 return;
3477 }
3478
rgindaa8ba17d2012-08-15 14:41:10 -07003479 var isNewSize = (columnCount != this.screenSize.width ||
3480 rowCount != this.screenSize.height);
3481
3482 // We do this even if the size didn't change, just to be sure everything is
3483 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003484 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003485 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003486
3487 if (isNewSize)
3488 this.overlaySize();
3489
Robert Gindafb1be6a2013-12-11 11:56:22 -08003490 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003491 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08003492};
3493
3494/**
3495 * Service the cursor blink timeout.
3496 */
3497hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003498 if (!this.options_.cursorBlink) {
3499 delete this.timeouts_.cursorBlink;
3500 return;
3501 }
3502
Robert Ginda830583c2013-08-07 13:20:46 -07003503 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3504 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003505 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003506 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3507 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003508 } else {
rginda87b86462011-12-14 13:48:03 -08003509 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003510 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3511 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003512 }
3513};
David Reveman8f552492012-03-28 12:18:41 -04003514
3515/**
3516 * Set the scrollbar-visible mode bit.
3517 *
3518 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3519 * Otherwise it will not.
3520 *
3521 * Defaults to on.
3522 *
3523 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3524 */
3525hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3526 this.scrollPort_.setScrollbarVisible(state);
3527};
Michael Kelly485ecd12014-06-09 11:41:56 -04003528
3529/**
Rob Spies49039e52014-12-17 13:40:04 -08003530 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003531 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003532 *
3533 * Defaults to 1.
3534 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003535 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003536 */
3537hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3538 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3539};
3540
3541/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003542 * Close all web notifications created by terminal bells.
3543 */
3544hterm.Terminal.prototype.closeBellNotifications_ = function() {
3545 this.bellNotificationList_.forEach(function(n) {
3546 n.close();
3547 });
3548 this.bellNotificationList_.length = 0;
3549};