blob: 60e2b06568f331e8b0071cc27d011aa61ef1556d [file] [log] [blame]
rginda87b86462011-12-14 13:48:03 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rginda8ba33642011-12-14 12:31:31 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rgindacbbd7482012-06-13 15:06:16 -07005'use strict';
6
Masaya Suzuki273aa982014-05-31 07:25:55 +09007lib.rtdep('lib.colors', 'lib.PreferenceManager', 'lib.resource', 'lib.wc',
Rob Spiesf4e90e82015-01-28 12:10:13 -08008 'lib.f', 'hterm.Keyboard', 'hterm.Options', 'hterm.PreferenceManager',
Ricky Liang48f05cb2013-12-31 23:35:29 +08009 'hterm.Screen', 'hterm.ScrollPort', 'hterm.Size',
10 'hterm.TextAttributes', 'hterm.VT');
rgindacbbd7482012-06-13 15:06:16 -070011
rginda8ba33642011-12-14 12:31:31 -080012/**
13 * Constructor for the Terminal class.
14 *
15 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
16 * classes to provide the complete terminal functionality.
17 *
18 * There are a number of lower-level Terminal methods that can be called
19 * directly to manipulate the cursor, text, scroll region, and other terminal
20 * attributes. However, the primary method is interpret(), which parses VT
21 * escape sequences and invokes the appropriate Terminal methods.
22 *
23 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
24 *
25 * TODO(rginda): Eventually we're going to need to support characters which are
26 * displayed twice as wide as standard latin characters. This is to support
27 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080028 *
Robert Ginda57f03b42012-09-13 11:02:48 -070029 * @param {string} opt_profileId Optional preference profile name. If not
rginda9f5222b2012-03-05 11:53:28 -080030 * provided, defaults to 'default'.
rginda8ba33642011-12-14 12:31:31 -080031 */
Robert Ginda57f03b42012-09-13 11:02:48 -070032hterm.Terminal = function(opt_profileId) {
33 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080034
rginda8ba33642011-12-14 12:31:31 -080035 // Two screen instances.
36 this.primaryScreen_ = new hterm.Screen();
37 this.alternateScreen_ = new hterm.Screen();
38
39 // The "current" screen.
40 this.screen_ = this.primaryScreen_;
41
rginda8ba33642011-12-14 12:31:31 -080042 // The local notion of the screen size. ScreenBuffers also have a size which
43 // indicates their present size. During size changes, the two may disagree.
44 // Also, the inactive screen's size is not altered until it is made the active
45 // screen.
46 this.screenSize = new hterm.Size(0, 0);
47
rginda8ba33642011-12-14 12:31:31 -080048 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080049 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080050 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
51 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080052 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070053 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080054
rginda87b86462011-12-14 13:48:03 -080055 // The div that contains this terminal.
56 this.div_ = null;
57
rgindac9bc5502012-01-18 11:48:44 -080058 // The document that contains the scrollPort. Defaulted to the global
59 // document here so that the terminal is functional even if it hasn't been
60 // inserted into a document yet, but re-set in decorate().
61 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080062
rginda8ba33642011-12-14 12:31:31 -080063 // The rows that have scrolled off screen and are no longer addressable.
64 this.scrollbackRows_ = [];
65
rgindac9bc5502012-01-18 11:48:44 -080066 // Saved tab stops.
67 this.tabStops_ = [];
68
David Benjamin66e954d2012-05-05 21:08:12 -040069 // Keep track of whether default tab stops have been erased; after a TBC
70 // clears all tab stops, defaults aren't restored on resize until a reset.
71 this.defaultTabStops = true;
72
rginda8ba33642011-12-14 12:31:31 -080073 // The VT's notion of the top and bottom rows. Used during some VT
74 // cursor positioning and scrolling commands.
75 this.vtScrollTop_ = null;
76 this.vtScrollBottom_ = null;
77
78 // The DIV element for the visible cursor.
79 this.cursorNode_ = null;
80
Robert Ginda830583c2013-08-07 13:20:46 -070081 // The current cursor shape of the terminal.
82 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
83
84 // The current color of the cursor.
85 this.cursorColor_ = null;
86
Robert Gindaea2183e2014-07-17 09:51:51 -070087 // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded.
88 this.cursorBlinkCycle_ = [100, 100];
89
90 // Pre-bound onCursorBlink_ handler, so we don't have to do this for each
91 // cursor on/off servicing.
92 this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
93
rginda9f5222b2012-03-05 11:53:28 -080094 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070095 // each output and keystroke. They are initialized by the preference manager.
Robert Ginda8cb7d902013-06-20 14:37:18 -070096 this.backgroundColor_ = null;
97 this.foregroundColor_ = null;
Robert Ginda57f03b42012-09-13 11:02:48 -070098 this.scrollOnOutput_ = null;
99 this.scrollOnKeystroke_ = null;
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400100 this.scrollWheelArrowKeys_ = null;
rginda9f5222b2012-03-05 11:53:28 -0800101
Robert Ginda6aec7eb2015-06-16 10:31:30 -0700102 // True if we should override mouse event reporting to allow local selection.
103 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -0800104
rgindaf0090c92012-02-10 14:58:52 -0800105 // Terminal bell sound.
106 this.bellAudio_ = this.document_.createElement('audio');
Mike Frysingerd826f1a2017-07-06 16:20:06 -0400107 this.bellAudio_.id = 'hterm:bell-audio';
rgindaf0090c92012-02-10 14:58:52 -0800108 this.bellAudio_.setAttribute('preload', 'auto');
109
Michael Kelly485ecd12014-06-09 11:41:56 -0400110 // All terminal bell notifications that have been generated (not necessarily
111 // shown).
112 this.bellNotificationList_ = [];
113
114 // Whether we have permission to display notifications.
115 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400116
rginda6d397402012-01-17 10:58:29 -0800117 // Cursor position and attributes saved with DECSC.
118 this.savedOptions_ = {};
119
rginda8ba33642011-12-14 12:31:31 -0800120 // The current mode bits for the terminal.
121 this.options_ = new hterm.Options();
122
123 // Timeouts we might need to clear.
124 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800125
126 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800127 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800128
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800129 this.saveCursorAndState(true);
130
Zhu Qunying30d40712017-03-14 16:27:00 -0700131 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800132 this.keyboard = new hterm.Keyboard(this);
133
rginda87b86462011-12-14 13:48:03 -0800134 // General IO interface that can be given to third parties without exposing
135 // the entire terminal object.
136 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800137
rgindad5613292012-06-19 15:40:37 -0700138 // True if mouse-click-drag should scroll the terminal.
139 this.enableMouseDragScroll = true;
140
Robert Ginda57f03b42012-09-13 11:02:48 -0700141 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400142 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700143 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700144
Zhu Qunying30d40712017-03-14 16:27:00 -0700145 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700146 this.useDefaultWindowCopy = false;
147
148 this.clearSelectionAfterCopy = true;
149
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400150 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800151 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700152
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400153 // Whether we allow images to be shown.
154 this.allowImagesInline = null;
155
Gabriel Holodake8a09be2017-10-10 01:07:11 -0400156 this.reportFocus = false;
157
Robert Ginda57f03b42012-09-13 11:02:48 -0700158 this.setProfile(opt_profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500159 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800160};
161
162/**
Robert Ginda830583c2013-08-07 13:20:46 -0700163 * Possible cursor shapes.
164 */
165hterm.Terminal.cursorShape = {
166 BLOCK: 'BLOCK',
167 BEAM: 'BEAM',
168 UNDERLINE: 'UNDERLINE'
169};
170
171/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700172 * Clients should override this to be notified when the terminal is ready
173 * for use.
174 *
175 * The terminal initialization is asynchronous, and shouldn't be used before
176 * this method is called.
177 */
178hterm.Terminal.prototype.onTerminalReady = function() { };
179
180/**
rginda35c456b2012-02-09 17:29:05 -0800181 * Default tab with of 8 to match xterm.
182 */
183hterm.Terminal.prototype.tabWidth = 8;
184
185/**
rginda9f5222b2012-03-05 11:53:28 -0800186 * Select a preference profile.
187 *
188 * This will load the terminal preferences for the given profile name and
189 * associate subsequent preference changes with the new preference profile.
190 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500191 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800192 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700193 * @param {function} opt_callback Optional callback to invoke when the profile
194 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800195 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700196hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
197 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800198
Robert Ginda57f03b42012-09-13 11:02:48 -0700199 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800200
Robert Ginda57f03b42012-09-13 11:02:48 -0700201 if (this.prefs_)
202 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800203
Robert Ginda57f03b42012-09-13 11:02:48 -0700204 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
205 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800206 'alt-gr-mode': function(v) {
207 if (v == null) {
208 if (navigator.language.toLowerCase() == 'en-us') {
209 v = 'none';
210 } else {
211 v = 'right-alt';
212 }
213 } else if (typeof v == 'string') {
214 v = v.toLowerCase();
215 } else {
216 v = 'none';
217 }
218
219 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v))
220 v = 'none';
221
222 terminal.keyboard.altGrMode = v;
223 },
224
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700225 'alt-backspace-is-meta-backspace': function(v) {
226 terminal.keyboard.altBackspaceIsMetaBackspace = v;
227 },
228
Robert Ginda57f03b42012-09-13 11:02:48 -0700229 'alt-is-meta': function(v) {
230 terminal.keyboard.altIsMeta = v;
231 },
232
233 'alt-sends-what': function(v) {
234 if (!/^(escape|8-bit|browser-key)$/.test(v))
235 v = 'escape';
236
237 terminal.keyboard.altSendsWhat = v;
238 },
239
240 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800241 var ary = v.match(/^lib-resource:(\S+)/);
242 if (ary) {
243 terminal.bellAudio_.setAttribute('src',
244 lib.resource.getDataUrl(ary[1]));
245 } else {
246 terminal.bellAudio_.setAttribute('src', v);
247 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700248 },
249
Michael Kelly485ecd12014-06-09 11:41:56 -0400250 'desktop-notification-bell': function(v) {
251 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700252 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400253 Notification.permission === 'granted';
254 if (!terminal.desktopNotificationBell_) {
255 // Note: We don't call Notification.requestPermission here because
256 // Chrome requires the call be the result of a user action (such as an
257 // onclick handler), and pref listeners are run asynchronously.
258 //
259 // A way of working around this would be to display a dialog in the
260 // terminal with a "click-to-request-permission" button.
261 console.warn('desktop-notification-bell is true but we do not have ' +
262 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400263 }
264 } else {
265 terminal.desktopNotificationBell_ = false;
266 }
267 },
268
Robert Ginda57f03b42012-09-13 11:02:48 -0700269 'background-color': function(v) {
270 terminal.setBackgroundColor(v);
271 },
272
273 'background-image': function(v) {
274 terminal.scrollPort_.setBackgroundImage(v);
275 },
276
277 'background-size': function(v) {
278 terminal.scrollPort_.setBackgroundSize(v);
279 },
280
281 'background-position': function(v) {
282 terminal.scrollPort_.setBackgroundPosition(v);
283 },
284
285 'backspace-sends-backspace': function(v) {
286 terminal.keyboard.backspaceSendsBackspace = v;
287 },
288
Brad Town18654b62015-03-12 00:27:45 -0700289 'character-map-overrides': function(v) {
290 if (!(v == null || v instanceof Object)) {
291 console.warn('Preference character-map-modifications is not an ' +
292 'object: ' + v);
293 return;
294 }
295
Mike Frysinger095d4062017-06-14 00:29:48 -0700296 terminal.vt.characterMaps.reset();
297 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700298 },
299
Robert Ginda57f03b42012-09-13 11:02:48 -0700300 'cursor-blink': function(v) {
301 terminal.setCursorBlink(!!v);
302 },
303
Robert Gindaea2183e2014-07-17 09:51:51 -0700304 'cursor-blink-cycle': function(v) {
305 if (v instanceof Array &&
306 typeof v[0] == 'number' &&
307 typeof v[1] == 'number') {
308 terminal.cursorBlinkCycle_ = v;
309 } else if (typeof v == 'number') {
310 terminal.cursorBlinkCycle_ = [v, v];
311 } else {
312 // Fast blink indicates an error.
313 terminal.cursorBlinkCycle_ = [100, 100];
314 }
315 },
316
Robert Ginda57f03b42012-09-13 11:02:48 -0700317 'cursor-color': function(v) {
318 terminal.setCursorColor(v);
319 },
320
321 'color-palette-overrides': function(v) {
322 if (!(v == null || v instanceof Object || v instanceof Array)) {
323 console.warn('Preference color-palette-overrides is not an array or ' +
324 'object: ' + v);
325 return;
rginda9f5222b2012-03-05 11:53:28 -0800326 }
rginda9f5222b2012-03-05 11:53:28 -0800327
Robert Ginda57f03b42012-09-13 11:02:48 -0700328 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700329
Robert Ginda57f03b42012-09-13 11:02:48 -0700330 if (v) {
331 for (var key in v) {
332 var i = parseInt(key);
333 if (isNaN(i) || i < 0 || i > 255) {
334 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
335 continue;
336 }
337
338 if (v[i]) {
339 var rgb = lib.colors.normalizeCSS(v[i]);
340 if (rgb)
341 lib.colors.colorPalette[i] = rgb;
342 }
343 }
rginda30f20f62012-04-05 16:36:19 -0700344 }
rginda30f20f62012-04-05 16:36:19 -0700345
Evan Jones5f9df812016-12-06 09:38:58 -0500346 terminal.primaryScreen_.textAttributes.resetColorPalette();
Robert Ginda57f03b42012-09-13 11:02:48 -0700347 terminal.alternateScreen_.textAttributes.resetColorPalette();
348 },
rginda30f20f62012-04-05 16:36:19 -0700349
Robert Ginda57f03b42012-09-13 11:02:48 -0700350 'copy-on-select': function(v) {
351 terminal.copyOnSelect = !!v;
352 },
rginda9f5222b2012-03-05 11:53:28 -0800353
Rob Spies0bec09b2014-06-06 15:58:09 -0700354 'use-default-window-copy': function(v) {
355 terminal.useDefaultWindowCopy = !!v;
356 },
357
358 'clear-selection-after-copy': function(v) {
359 terminal.clearSelectionAfterCopy = !!v;
360 },
361
Robert Ginda7e5e9522014-03-14 12:23:58 -0700362 'ctrl-plus-minus-zero-zoom': function(v) {
363 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
364 },
365
Robert Gindafb5a3f92014-05-13 14:12:00 -0700366 'ctrl-c-copy': function(v) {
367 terminal.keyboard.ctrlCCopy = v;
368 },
369
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100370 'ctrl-v-paste': function(v) {
371 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700372 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100373 },
374
Masaya Suzuki273aa982014-05-31 07:25:55 +0900375 'east-asian-ambiguous-as-two-column': function(v) {
376 lib.wc.regardCjkAmbiguous = v;
377 },
378
Robert Ginda57f03b42012-09-13 11:02:48 -0700379 'enable-8-bit-control': function(v) {
380 terminal.vt.enable8BitControl = !!v;
381 },
rginda30f20f62012-04-05 16:36:19 -0700382
Robert Ginda57f03b42012-09-13 11:02:48 -0700383 'enable-bold': function(v) {
384 terminal.syncBoldSafeState();
385 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400386
Robert Ginda3e278d72014-03-25 13:18:51 -0700387 'enable-bold-as-bright': function(v) {
388 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
389 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
390 },
391
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400392 'enable-blink': function(v) {
Mike Frysinger261597c2017-12-28 01:14:21 -0500393 terminal.setTextBlink(!!v);
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400394 },
395
Robert Ginda57f03b42012-09-13 11:02:48 -0700396 'enable-clipboard-write': function(v) {
397 terminal.vt.enableClipboardWrite = !!v;
398 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400399
Robert Ginda3755e752013-05-31 13:34:09 -0700400 'enable-dec12': function(v) {
401 terminal.vt.enableDec12 = !!v;
402 },
403
Robert Ginda57f03b42012-09-13 11:02:48 -0700404 'font-family': function(v) {
405 terminal.syncFontFamily();
406 },
rginda30f20f62012-04-05 16:36:19 -0700407
Robert Ginda57f03b42012-09-13 11:02:48 -0700408 'font-size': function(v) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500409 v = parseInt(v);
410 if (v <= 0) {
411 console.error(`Invalid font size: ${v}`);
412 return;
413 }
414
Robert Ginda57f03b42012-09-13 11:02:48 -0700415 terminal.setFontSize(v);
416 },
rginda9875d902012-08-20 16:21:57 -0700417
Robert Ginda57f03b42012-09-13 11:02:48 -0700418 'font-smoothing': function(v) {
419 terminal.syncFontFamily();
420 },
rgindade84e382012-04-20 15:39:31 -0700421
Robert Ginda57f03b42012-09-13 11:02:48 -0700422 'foreground-color': function(v) {
423 terminal.setForegroundColor(v);
424 },
rginda30f20f62012-04-05 16:36:19 -0700425
Robert Ginda57f03b42012-09-13 11:02:48 -0700426 'home-keys-scroll': function(v) {
427 terminal.keyboard.homeKeysScroll = v;
428 },
rginda4bba5e12012-06-20 16:15:30 -0700429
Robert Gindaa8165692015-06-15 14:46:31 -0700430 'keybindings': function(v) {
431 terminal.keyboard.bindings.clear();
432
433 if (!v)
434 return;
435
436 if (!(v instanceof Object)) {
437 console.error('Error in keybindings preference: Expected object');
438 return;
439 }
440
441 try {
442 terminal.keyboard.bindings.addBindings(v);
443 } catch (ex) {
444 console.error('Error in keybindings preference: ' + ex);
445 }
446 },
447
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700448 'media-keys-are-fkeys': function(v) {
449 terminal.keyboard.mediaKeysAreFKeys = v;
450 },
451
Robert Ginda57f03b42012-09-13 11:02:48 -0700452 'meta-sends-escape': function(v) {
453 terminal.keyboard.metaSendsEscape = v;
454 },
rginda30f20f62012-04-05 16:36:19 -0700455
Mike Frysinger847577f2017-05-23 23:25:57 -0400456 'mouse-right-click-paste': function(v) {
457 terminal.mouseRightClickPaste = v;
458 },
459
Robert Ginda57f03b42012-09-13 11:02:48 -0700460 'mouse-paste-button': function(v) {
461 terminal.syncMousePasteButton();
462 },
rgindaa8ba17d2012-08-15 14:41:10 -0700463
Robert Gindae76aa9f2014-03-14 12:29:12 -0700464 'page-keys-scroll': function(v) {
465 terminal.keyboard.pageKeysScroll = v;
466 },
467
Robert Ginda40932892012-12-10 17:26:40 -0800468 'pass-alt-number': function(v) {
469 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800470 // Let Alt-1..9 pass to the browser (to control tab switching) on
471 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500472 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800473 }
474
475 terminal.passAltNumber = v;
476 },
477
478 'pass-ctrl-number': function(v) {
479 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800480 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
481 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500482 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800483 }
484
485 terminal.passCtrlNumber = v;
486 },
487
488 'pass-meta-number': function(v) {
489 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800490 // Let Meta-1..9 pass to the browser (to control tab switching) on
491 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500492 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800493 }
494
495 terminal.passMetaNumber = v;
496 },
497
Marius Schilder77857b32014-05-14 16:21:26 -0700498 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700499 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700500 },
501
Robert Ginda8cb7d902013-06-20 14:37:18 -0700502 'receive-encoding': function(v) {
503 if (!(/^(utf-8|raw)$/).test(v)) {
504 console.warn('Invalid value for "receive-encoding": ' + v);
505 v = 'utf-8';
506 }
507
508 terminal.vt.characterEncoding = v;
509 },
510
Robert Ginda57f03b42012-09-13 11:02:48 -0700511 'scroll-on-keystroke': function(v) {
512 terminal.scrollOnKeystroke_ = v;
513 },
rginda9f5222b2012-03-05 11:53:28 -0800514
Robert Ginda57f03b42012-09-13 11:02:48 -0700515 'scroll-on-output': function(v) {
516 terminal.scrollOnOutput_ = v;
517 },
rginda30f20f62012-04-05 16:36:19 -0700518
Robert Ginda57f03b42012-09-13 11:02:48 -0700519 'scrollbar-visible': function(v) {
520 terminal.setScrollbarVisible(v);
521 },
rginda9f5222b2012-03-05 11:53:28 -0800522
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400523 'scroll-wheel-may-send-arrow-keys': function(v) {
524 terminal.scrollWheelArrowKeys_ = v;
525 },
526
Rob Spies49039e52014-12-17 13:40:04 -0800527 'scroll-wheel-move-multiplier': function(v) {
528 terminal.setScrollWheelMoveMultipler(v);
529 },
530
Robert Ginda8cb7d902013-06-20 14:37:18 -0700531 'send-encoding': function(v) {
532 if (!(/^(utf-8|raw)$/).test(v)) {
533 console.warn('Invalid value for "send-encoding": ' + v);
534 v = 'utf-8';
535 }
536
537 terminal.keyboard.characterEncoding = v;
538 },
539
Robert Ginda57f03b42012-09-13 11:02:48 -0700540 'shift-insert-paste': function(v) {
541 terminal.keyboard.shiftInsertPaste = v;
542 },
rginda9f5222b2012-03-05 11:53:28 -0800543
Mike Frysingera7768922017-07-28 15:00:12 -0400544 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400545 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400546 },
547
Robert Gindae76aa9f2014-03-14 12:29:12 -0700548 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400549 terminal.scrollPort_.setUserCssUrl(v);
550 },
551
552 'user-css-text': function(v) {
553 terminal.scrollPort_.setUserCssText(v);
554 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400555
556 'word-break-match-left': function(v) {
557 terminal.primaryScreen_.wordBreakMatchLeft = v;
558 terminal.alternateScreen_.wordBreakMatchLeft = v;
559 },
560
561 'word-break-match-right': function(v) {
562 terminal.primaryScreen_.wordBreakMatchRight = v;
563 terminal.alternateScreen_.wordBreakMatchRight = v;
564 },
565
566 'word-break-match-middle': function(v) {
567 terminal.primaryScreen_.wordBreakMatchMiddle = v;
568 terminal.alternateScreen_.wordBreakMatchMiddle = v;
569 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400570
571 'allow-images-inline': function(v) {
572 terminal.allowImagesInline = v;
573 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700574 });
rginda30f20f62012-04-05 16:36:19 -0700575
Robert Ginda57f03b42012-09-13 11:02:48 -0700576 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800577 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700578
579 if (opt_callback)
580 opt_callback();
581 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800582};
583
Rob Spies56953412014-04-28 14:09:47 -0700584
585/**
586 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500587 *
588 * @return {hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700589 */
590hterm.Terminal.prototype.getPrefs = function() {
591 return this.prefs_;
592};
593
Robert Gindaa063b202014-07-21 11:08:25 -0700594/**
595 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500596 *
597 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700598 */
599hterm.Terminal.prototype.setBracketedPaste = function(state) {
600 this.options_.bracketedPaste = state;
601};
Rob Spies56953412014-04-28 14:09:47 -0700602
rginda8e92a692012-05-20 19:37:20 -0700603/**
604 * Set the color for the cursor.
605 *
606 * If you want this setting to persist, set it through prefs_, rather than
607 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500608 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500609 * @param {string=} color The color to set. If not defined, we reset to the
610 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700611 */
612hterm.Terminal.prototype.setCursorColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500613 if (color === undefined)
614 color = this.prefs_.get('cursor-color');
615
Robert Ginda830583c2013-08-07 13:20:46 -0700616 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700617 this.cursorNode_.style.backgroundColor = color;
618 this.cursorNode_.style.borderColor = color;
619};
620
621/**
622 * Return the current cursor color as a string.
Evan Jones2600d4f2016-12-06 09:29:36 -0500623 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700624 */
625hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700626 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700627};
628
629/**
rgindad5613292012-06-19 15:40:37 -0700630 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500631 *
632 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700633 */
634hterm.Terminal.prototype.setSelectionEnabled = function(state) {
635 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700636};
637
638/**
rginda8e92a692012-05-20 19:37:20 -0700639 * Set the background color.
640 *
641 * If you want this setting to persist, set it through prefs_, rather than
642 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500643 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500644 * @param {string=} color The color to set. If not defined, we reset to the
645 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700646 */
647hterm.Terminal.prototype.setBackgroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500648 if (color === undefined)
649 color = this.prefs_.get('background-color');
650
rgindacbbd7482012-06-13 15:06:16 -0700651 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700652 this.primaryScreen_.textAttributes.setDefaults(
653 this.foregroundColor_, this.backgroundColor_);
654 this.alternateScreen_.textAttributes.setDefaults(
655 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700656 this.scrollPort_.setBackgroundColor(color);
657};
658
rginda9f5222b2012-03-05 11:53:28 -0800659/**
660 * Return the current terminal background color.
661 *
662 * Intended for use by other classes, so we don't have to expose the entire
663 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500664 *
665 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800666 */
667hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700668 return this.backgroundColor_;
669};
670
671/**
672 * Set the foreground color.
673 *
674 * If you want this setting to persist, set it through prefs_, rather than
675 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500676 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500677 * @param {string=} color The color to set. If not defined, we reset to the
678 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700679 */
680hterm.Terminal.prototype.setForegroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500681 if (color === undefined)
682 color = this.prefs_.get('foreground-color');
683
rgindacbbd7482012-06-13 15:06:16 -0700684 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700685 this.primaryScreen_.textAttributes.setDefaults(
686 this.foregroundColor_, this.backgroundColor_);
687 this.alternateScreen_.textAttributes.setDefaults(
688 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700689 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800690};
691
692/**
693 * Return the current terminal foreground color.
694 *
695 * Intended for use by other classes, so we don't have to expose the entire
696 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500697 *
698 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800699 */
700hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700701 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800702};
703
704/**
rginda87b86462011-12-14 13:48:03 -0800705 * Create a new instance of a terminal command and run it with a given
706 * argument string.
707 *
708 * @param {function} commandClass The constructor for a terminal command.
709 * @param {string} argString The argument string to pass to the command.
710 */
711hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700712 var environment = this.prefs_.get('environment');
713 if (typeof environment != 'object' || environment == null)
714 environment = {};
715
rginda87b86462011-12-14 13:48:03 -0800716 var self = this;
717 this.command = new commandClass(
718 { argString: argString || '',
719 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700720 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800721 onExit: function(code) {
722 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800723 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700724 if (self.prefs_.get('close-on-exit'))
725 window.close();
rginda87b86462011-12-14 13:48:03 -0800726 }
727 });
728
rgindafeaf3142012-01-31 15:14:20 -0800729 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800730 this.command.run();
731};
732
733/**
rgindafeaf3142012-01-31 15:14:20 -0800734 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500735 *
736 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800737 */
738hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700739 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800740};
741
742/**
743 * Install the keyboard handler for this terminal.
744 *
745 * This will prevent the browser from seeing any keystrokes sent to the
746 * terminal.
747 */
748hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700749 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
rgindafeaf3142012-01-31 15:14:20 -0800750}
751
752/**
753 * Uninstall the keyboard handler for this terminal.
754 */
755hterm.Terminal.prototype.uninstallKeyboard = function() {
756 this.keyboard.installKeyboard(null);
757}
758
759/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400760 * Set a CSS variable.
761 *
762 * Normally this is used to set variables in the hterm namespace.
763 *
764 * @param {string} name The variable to set.
765 * @param {string} value The value to assign to the variable.
766 * @param {string?} opt_prefix The variable namespace/prefix to use.
767 */
768hterm.Terminal.prototype.setCssVar = function(name, value,
769 opt_prefix='--hterm-') {
770 this.document_.documentElement.style.setProperty(
771 `${opt_prefix}${name}`, value);
772};
773
774/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500775 * Get a CSS variable.
776 *
777 * Normally this is used to get variables in the hterm namespace.
778 *
779 * @param {string} name The variable to read.
780 * @param {string?} opt_prefix The variable namespace/prefix to use.
781 * @return {string} The current setting for this variable.
782 */
783hterm.Terminal.prototype.getCssVar = function(name, opt_prefix='--hterm-') {
784 return this.document_.documentElement.style.getPropertyValue(
785 `${opt_prefix}${name}`);
786};
787
788/**
rginda35c456b2012-02-09 17:29:05 -0800789 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800790 *
791 * Call setFontSize(0) to reset to the default font size.
792 *
793 * This function does not modify the font-size preference.
794 *
795 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800796 */
797hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500798 if (px <= 0)
rginda9f5222b2012-03-05 11:53:28 -0800799 px = this.prefs_.get('font-size');
800
rginda35c456b2012-02-09 17:29:05 -0800801 this.scrollPort_.setFontSize(px);
Mike Frysingercce97c42017-08-05 01:11:22 -0400802 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
803 this.setCssVar('charsize-height',
804 this.scrollPort_.characterSize.height + 'px');
rginda35c456b2012-02-09 17:29:05 -0800805};
806
807/**
808 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500809 *
810 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800811 */
812hterm.Terminal.prototype.getFontSize = function() {
813 return this.scrollPort_.getFontSize();
814};
815
816/**
rginda8e92a692012-05-20 19:37:20 -0700817 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500818 *
819 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700820 */
821hterm.Terminal.prototype.getFontFamily = function() {
822 return this.scrollPort_.getFontFamily();
823};
824
825/**
rginda35c456b2012-02-09 17:29:05 -0800826 * Set the CSS "font-family" for this terminal.
827 */
rginda9f5222b2012-03-05 11:53:28 -0800828hterm.Terminal.prototype.syncFontFamily = function() {
829 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
830 this.prefs_.get('font-smoothing'));
831 this.syncBoldSafeState();
832};
833
rginda4bba5e12012-06-20 16:15:30 -0700834/**
835 * Set this.mousePasteButton based on the mouse-paste-button pref,
836 * autodetecting if necessary.
837 */
838hterm.Terminal.prototype.syncMousePasteButton = function() {
839 var button = this.prefs_.get('mouse-paste-button');
840 if (typeof button == 'number') {
841 this.mousePasteButton = button;
842 return;
843 }
844
Mike Frysingeree81a002017-12-12 16:14:53 -0500845 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400846 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700847 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400848 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700849 }
850};
851
852/**
853 * Enable or disable bold based on the enable-bold pref, autodetecting if
854 * necessary.
855 */
rginda9f5222b2012-03-05 11:53:28 -0800856hterm.Terminal.prototype.syncBoldSafeState = function() {
857 var enableBold = this.prefs_.get('enable-bold');
858 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700859 this.primaryScreen_.textAttributes.enableBold = enableBold;
860 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800861 return;
862 }
863
rgindaf7521392012-02-28 17:20:34 -0800864 var normalSize = this.scrollPort_.measureCharacterSize();
865 var boldSize = this.scrollPort_.measureCharacterSize('bold');
866
867 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800868 if (!isBoldSafe) {
869 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700870 'from normal. Font family is: ' +
871 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800872 }
rginda9f5222b2012-03-05 11:53:28 -0800873
Robert Gindaed016262012-10-26 16:27:09 -0700874 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
875 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800876};
877
878/**
Mike Frysinger261597c2017-12-28 01:14:21 -0500879 * Control text blinking behavior.
880 *
881 * @param {boolean=} state Whether to enable support for blinking text.
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400882 */
Mike Frysinger261597c2017-12-28 01:14:21 -0500883hterm.Terminal.prototype.setTextBlink = function(state) {
884 if (state === undefined)
885 state = this.prefs_.get('enable-blink');
886 this.setCssVar('blink-node-duration', state ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400887};
888
889/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400890 * Set the mouse cursor style based on the current terminal mode.
891 */
892hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400893 this.setCssVar('mouse-cursor-style',
894 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
895 'var(--hterm-mouse-cursor-text)' :
896 'var(--hterm-mouse-cursor-pointer)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400897};
898
899/**
rginda87b86462011-12-14 13:48:03 -0800900 * Return a copy of the current cursor position.
901 *
902 * @return {hterm.RowCol} The RowCol object representing the current position.
903 */
904hterm.Terminal.prototype.saveCursor = function() {
905 return this.screen_.cursorPosition.clone();
906};
907
Evan Jones2600d4f2016-12-06 09:29:36 -0500908/**
909 * Return the current text attributes.
910 *
911 * @return {string}
912 */
rgindaa19afe22012-01-25 15:40:22 -0800913hterm.Terminal.prototype.getTextAttributes = function() {
914 return this.screen_.textAttributes;
915};
916
Evan Jones2600d4f2016-12-06 09:29:36 -0500917/**
918 * Set the text attributes.
919 *
920 * @param {string} textAttributes The attributes to set.
921 */
rginda1a09aa02012-06-18 21:11:25 -0700922hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
923 this.screen_.textAttributes = textAttributes;
924};
925
rginda87b86462011-12-14 13:48:03 -0800926/**
rgindaf522ce02012-04-17 17:49:17 -0700927 * Return the current browser zoom factor applied to the terminal.
928 *
929 * @return {number} The current browser zoom factor.
930 */
931hterm.Terminal.prototype.getZoomFactor = function() {
932 return this.scrollPort_.characterSize.zoomFactor;
933};
934
935/**
rginda9846e2f2012-01-27 13:53:33 -0800936 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500937 *
938 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800939 */
940hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800941 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800942};
943
944/**
rginda87b86462011-12-14 13:48:03 -0800945 * Restore a previously saved cursor position.
946 *
947 * @param {hterm.RowCol} cursor The position to restore.
948 */
949hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700950 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
951 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800952 this.screen_.setCursorPosition(row, column);
953 if (cursor.column > column ||
954 cursor.column == column && cursor.overflow) {
955 this.screen_.cursorPosition.overflow = true;
956 }
rginda87b86462011-12-14 13:48:03 -0800957};
958
959/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400960 * Clear the cursor's overflow flag.
961 */
962hterm.Terminal.prototype.clearCursorOverflow = function() {
963 this.screen_.cursorPosition.overflow = false;
964};
965
966/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800967 * Save the current cursor state to the corresponding screens.
968 *
969 * See the hterm.Screen.CursorState class for more details.
970 *
971 * @param {boolean=} both If true, update both screens, else only update the
972 * current screen.
973 */
974hterm.Terminal.prototype.saveCursorAndState = function(both) {
975 if (both) {
976 this.primaryScreen_.saveCursorAndState(this.vt);
977 this.alternateScreen_.saveCursorAndState(this.vt);
978 } else
979 this.screen_.saveCursorAndState(this.vt);
980};
981
982/**
983 * Restore the saved cursor state in the corresponding screens.
984 *
985 * See the hterm.Screen.CursorState class for more details.
986 *
987 * @param {boolean=} both If true, update both screens, else only update the
988 * current screen.
989 */
990hterm.Terminal.prototype.restoreCursorAndState = function(both) {
991 if (both) {
992 this.primaryScreen_.restoreCursorAndState(this.vt);
993 this.alternateScreen_.restoreCursorAndState(this.vt);
994 } else
995 this.screen_.restoreCursorAndState(this.vt);
996};
997
998/**
Robert Ginda830583c2013-08-07 13:20:46 -0700999 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001000 *
1001 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -07001002 */
1003hterm.Terminal.prototype.setCursorShape = function(shape) {
1004 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -08001005 this.restyleCursor_();
Robert Ginda830583c2013-08-07 13:20:46 -07001006}
1007
1008/**
1009 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -05001010 *
1011 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -07001012 */
1013hterm.Terminal.prototype.getCursorShape = function() {
1014 return this.cursorShape_;
1015}
1016
1017/**
rginda87b86462011-12-14 13:48:03 -08001018 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001019 *
1020 * @param {number} columnCount
rginda87b86462011-12-14 13:48:03 -08001021 */
1022hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001023 if (columnCount == null) {
1024 this.div_.style.width = '100%';
1025 return;
1026 }
1027
Robert Ginda26806d12014-07-24 13:44:07 -07001028 this.div_.style.width = Math.ceil(
1029 this.scrollPort_.characterSize.width *
1030 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001031 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001032 this.scheduleSyncCursorPosition_();
1033};
rginda87b86462011-12-14 13:48:03 -08001034
rgindac9bc5502012-01-18 11:48:44 -08001035/**
rginda35c456b2012-02-09 17:29:05 -08001036 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001037 *
1038 * @param {number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001039 */
1040hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001041 if (rowCount == null) {
1042 this.div_.style.height = '100%';
1043 return;
1044 }
1045
rginda35c456b2012-02-09 17:29:05 -08001046 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -07001047 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -08001048 this.realizeSize_(this.screenSize.width, rowCount);
1049 this.scheduleSyncCursorPosition_();
1050};
1051
1052/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001053 * Deal with terminal size changes.
1054 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001055 * @param {number} columnCount The number of columns.
1056 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001057 */
1058hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
1059 if (columnCount != this.screenSize.width)
1060 this.realizeWidth_(columnCount);
1061
1062 if (rowCount != this.screenSize.height)
1063 this.realizeHeight_(rowCount);
1064
1065 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -07001066 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001067};
1068
1069/**
rgindac9bc5502012-01-18 11:48:44 -08001070 * Deal with terminal width changes.
1071 *
1072 * This function does what needs to be done when the terminal width changes
1073 * out from under us. It happens here rather than in onResize_() because this
1074 * code may need to run synchronously to handle programmatic changes of
1075 * terminal width.
1076 *
1077 * Relying on the browser to send us an async resize event means we may not be
1078 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001079 *
1080 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001081 */
1082hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001083 if (columnCount <= 0)
1084 throw new Error('Attempt to realize bad width: ' + columnCount);
1085
rgindac9bc5502012-01-18 11:48:44 -08001086 var deltaColumns = columnCount - this.screen_.getWidth();
1087
rginda87b86462011-12-14 13:48:03 -08001088 this.screenSize.width = columnCount;
1089 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001090
1091 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -04001092 if (this.defaultTabStops)
1093 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -08001094 } else {
1095 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -04001096 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -08001097 break;
1098
1099 this.tabStops_.pop();
1100 }
1101 }
1102
1103 this.screen_.setColumnCount(this.screenSize.width);
1104};
1105
1106/**
1107 * Deal with terminal height changes.
1108 *
1109 * This function does what needs to be done when the terminal height changes
1110 * out from under us. It happens here rather than in onResize_() because this
1111 * code may need to run synchronously to handle programmatic changes of
1112 * terminal height.
1113 *
1114 * Relying on the browser to send us an async resize event means we may not be
1115 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001116 *
1117 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001118 */
1119hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001120 if (rowCount <= 0)
1121 throw new Error('Attempt to realize bad height: ' + rowCount);
1122
rgindac9bc5502012-01-18 11:48:44 -08001123 var deltaRows = rowCount - this.screen_.getHeight();
1124
1125 this.screenSize.height = rowCount;
1126
1127 var cursor = this.saveCursor();
1128
1129 if (deltaRows < 0) {
1130 // Screen got smaller.
1131 deltaRows *= -1;
1132 while (deltaRows) {
1133 var lastRow = this.getRowCount() - 1;
1134 if (lastRow - this.scrollbackRows_.length == cursor.row)
1135 break;
1136
1137 if (this.getRowText(lastRow))
1138 break;
1139
1140 this.screen_.popRow();
1141 deltaRows--;
1142 }
1143
1144 var ary = this.screen_.shiftRows(deltaRows);
1145 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1146
1147 // We just removed rows from the top of the screen, we need to update
1148 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001149 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001150 } else if (deltaRows > 0) {
1151 // Screen got larger.
1152
1153 if (deltaRows <= this.scrollbackRows_.length) {
1154 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1155 var rows = this.scrollbackRows_.splice(
1156 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1157 this.screen_.unshiftRows(rows);
1158 deltaRows -= scrollbackCount;
1159 cursor.row += scrollbackCount;
1160 }
1161
1162 if (deltaRows)
1163 this.appendRows_(deltaRows);
1164 }
1165
rginda35c456b2012-02-09 17:29:05 -08001166 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001167 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001168};
1169
1170/**
1171 * Scroll the terminal to the top of the scrollback buffer.
1172 */
1173hterm.Terminal.prototype.scrollHome = function() {
1174 this.scrollPort_.scrollRowToTop(0);
1175};
1176
1177/**
1178 * Scroll the terminal to the end.
1179 */
1180hterm.Terminal.prototype.scrollEnd = function() {
1181 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1182};
1183
1184/**
1185 * Scroll the terminal one page up (minus one line) relative to the current
1186 * position.
1187 */
1188hterm.Terminal.prototype.scrollPageUp = function() {
1189 var i = this.scrollPort_.getTopRowIndex();
1190 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
1191};
1192
1193/**
1194 * Scroll the terminal one page down (minus one line) relative to the current
1195 * position.
1196 */
1197hterm.Terminal.prototype.scrollPageDown = function() {
1198 var i = this.scrollPort_.getTopRowIndex();
1199 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -08001200};
1201
rgindac9bc5502012-01-18 11:48:44 -08001202/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001203 * Scroll the terminal one line up relative to the current position.
1204 */
1205hterm.Terminal.prototype.scrollLineUp = function() {
1206 var i = this.scrollPort_.getTopRowIndex();
1207 this.scrollPort_.scrollRowToTop(i - 1);
1208};
1209
1210/**
1211 * Scroll the terminal one line down relative to the current position.
1212 */
1213hterm.Terminal.prototype.scrollLineDown = function() {
1214 var i = this.scrollPort_.getTopRowIndex();
1215 this.scrollPort_.scrollRowToTop(i + 1);
1216};
1217
1218/**
Robert Ginda40932892012-12-10 17:26:40 -08001219 * Clear primary screen, secondary screen, and the scrollback buffer.
1220 */
1221hterm.Terminal.prototype.wipeContents = function() {
1222 this.scrollbackRows_.length = 0;
1223 this.scrollPort_.resetCache();
1224
1225 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
1226 var bottom = screen.getHeight();
1227 if (bottom > 0) {
1228 this.renumberRows_(0, bottom);
1229 this.clearHome(screen);
1230 }
1231 }.bind(this));
1232
1233 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001234 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001235};
1236
1237/**
rgindac9bc5502012-01-18 11:48:44 -08001238 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001239 *
1240 * Perform a full reset to the default values listed in
1241 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001242 */
rginda87b86462011-12-14 13:48:03 -08001243hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001244 this.vt.reset();
1245
rgindac9bc5502012-01-18 11:48:44 -08001246 this.clearAllTabStops();
1247 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001248
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001249 const resetScreen = (screen) => {
1250 // We want to make sure to reset the attributes before we clear the screen.
1251 // The attributes might be used to initialize default/empty rows.
1252 screen.textAttributes.reset();
1253 screen.textAttributes.resetColorPalette();
1254 this.clearHome(screen);
1255 screen.saveCursorAndState(this.vt);
1256 };
1257 resetScreen(this.primaryScreen_);
1258 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001259
Mike Frysinger84301d02017-11-29 13:28:46 -08001260 // Reset terminal options to their default values.
1261 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001262 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1263
Mike Frysinger84301d02017-11-29 13:28:46 -08001264 this.setVTScrollRegion(null, null);
1265
1266 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001267};
1268
rgindac9bc5502012-01-18 11:48:44 -08001269/**
1270 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001271 *
1272 * Perform a soft reset to the default values listed in
1273 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001274 */
rginda0f5c0292012-01-13 11:00:13 -08001275hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001276 this.vt.reset();
1277
rgindab8bc8932012-04-27 12:45:03 -07001278 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001279 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001280
Brad Townb62dfdc2015-03-16 19:07:15 -07001281 // We show the cursor on soft reset but do not alter the blink state.
1282 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1283
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001284 const resetScreen = (screen) => {
1285 // Xterm also resets the color palette on soft reset, even though it doesn't
1286 // seem to be documented anywhere.
1287 screen.textAttributes.reset();
1288 screen.textAttributes.resetColorPalette();
1289 screen.saveCursorAndState(this.vt);
1290 };
1291 resetScreen(this.primaryScreen_);
1292 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001293
rgindab8bc8932012-04-27 12:45:03 -07001294 // The xterm man page explicitly says this will happen on soft reset.
1295 this.setVTScrollRegion(null, null);
1296
1297 // Xterm also shows the cursor on soft reset, but does not alter the blink
1298 // state.
rgindaa19afe22012-01-25 15:40:22 -08001299 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001300};
1301
rgindac9bc5502012-01-18 11:48:44 -08001302/**
1303 * Move the cursor forward to the next tab stop, or to the last column
1304 * if no more tab stops are set.
1305 */
1306hterm.Terminal.prototype.forwardTabStop = function() {
1307 var column = this.screen_.cursorPosition.column;
1308
1309 for (var i = 0; i < this.tabStops_.length; i++) {
1310 if (this.tabStops_[i] > column) {
1311 this.setCursorColumn(this.tabStops_[i]);
1312 return;
1313 }
1314 }
1315
David Benjamin66e954d2012-05-05 21:08:12 -04001316 // xterm does not clear the overflow flag on HT or CHT.
1317 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001318 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001319 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001320};
1321
rgindac9bc5502012-01-18 11:48:44 -08001322/**
1323 * Move the cursor backward to the previous tab stop, or to the first column
1324 * if no previous tab stops are set.
1325 */
1326hterm.Terminal.prototype.backwardTabStop = function() {
1327 var column = this.screen_.cursorPosition.column;
1328
1329 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1330 if (this.tabStops_[i] < column) {
1331 this.setCursorColumn(this.tabStops_[i]);
1332 return;
1333 }
1334 }
1335
1336 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001337};
1338
rgindac9bc5502012-01-18 11:48:44 -08001339/**
1340 * Set a tab stop at the given column.
1341 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001342 * @param {integer} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001343 */
1344hterm.Terminal.prototype.setTabStop = function(column) {
1345 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1346 if (this.tabStops_[i] == column)
1347 return;
1348
1349 if (this.tabStops_[i] < column) {
1350 this.tabStops_.splice(i + 1, 0, column);
1351 return;
1352 }
1353 }
1354
1355 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001356};
1357
rgindac9bc5502012-01-18 11:48:44 -08001358/**
1359 * Clear the tab stop at the current cursor position.
1360 *
1361 * No effect if there is no tab stop at the current cursor position.
1362 */
1363hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1364 var column = this.screen_.cursorPosition.column;
1365
1366 var i = this.tabStops_.indexOf(column);
1367 if (i == -1)
1368 return;
1369
1370 this.tabStops_.splice(i, 1);
1371};
1372
1373/**
1374 * Clear all tab stops.
1375 */
1376hterm.Terminal.prototype.clearAllTabStops = function() {
1377 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001378 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001379};
1380
1381/**
1382 * Set up the default tab stops, starting from a given column.
1383 *
1384 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001385 * from the specified column, or 0 if no column is provided. It also flags
1386 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001387 *
1388 * This does not clear the existing tab stops first, use clearAllTabStops
1389 * for that.
1390 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001391 * @param {integer} opt_start Optional starting zero based starting column, useful
rgindac9bc5502012-01-18 11:48:44 -08001392 * for filling out missing tab stops when the terminal is resized.
1393 */
1394hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1395 var start = opt_start || 0;
1396 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001397 // Round start up to a default tab stop.
1398 start = start - 1 - ((start - 1) % w) + w;
1399 for (var i = start; i < this.screenSize.width; i += w) {
1400 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001401 }
David Benjamin66e954d2012-05-05 21:08:12 -04001402
1403 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001404};
1405
rginda6d397402012-01-17 10:58:29 -08001406/**
rginda8ba33642011-12-14 12:31:31 -08001407 * Interpret a sequence of characters.
1408 *
1409 * Incomplete escape sequences are buffered until the next call.
1410 *
1411 * @param {string} str Sequence of characters to interpret or pass through.
1412 */
1413hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001414 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001415 this.scheduleSyncCursorPosition_();
1416};
1417
1418/**
1419 * Take over the given DIV for use as the terminal display.
1420 *
1421 * @param {HTMLDivElement} div The div to use as the terminal display.
1422 */
1423hterm.Terminal.prototype.decorate = function(div) {
Mike Frysinger5768a9d2017-12-26 12:57:44 -05001424 const charset = div.ownerDocument.characterSet.toLowerCase();
1425 if (charset != 'utf-8') {
1426 console.warn(`Document encoding should be set to utf-8, not "${charset}";` +
1427 ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`);
1428 }
1429
rginda87b86462011-12-14 13:48:03 -08001430 this.div_ = div;
1431
rginda8ba33642011-12-14 12:31:31 -08001432 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001433 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001434 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1435 this.scrollPort_.setBackgroundPosition(
1436 this.prefs_.get('background-position'));
Mike Frysinger08bad432017-04-24 00:50:54 -04001437 this.scrollPort_.setUserCssUrl(this.prefs_.get('user-css'));
1438 this.scrollPort_.setUserCssText(this.prefs_.get('user-css-text'));
rginda30f20f62012-04-05 16:36:19 -07001439
rginda0918b652012-04-04 11:26:24 -07001440 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001441
rginda9f5222b2012-03-05 11:53:28 -08001442 this.setFontSize(this.prefs_.get('font-size'));
1443 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001444
David Reveman8f552492012-03-28 12:18:41 -04001445 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001446 this.setScrollWheelMoveMultipler(
1447 this.prefs_.get('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001448
rginda8ba33642011-12-14 12:31:31 -08001449 this.document_ = this.scrollPort_.getDocument();
1450
Evan Jones5f9df812016-12-06 09:38:58 -05001451 this.document_.body.oncontextmenu = function() { return false; };
rginda4bba5e12012-06-20 16:15:30 -07001452
1453 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001454 var screenNode = this.scrollPort_.getScreenNode();
1455 screenNode.addEventListener('mousedown', onMouse);
1456 screenNode.addEventListener('mouseup', onMouse);
1457 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001458 this.scrollPort_.onScrollWheel = onMouse;
1459
Toni Barzic0bfa8922013-11-22 11:18:35 -08001460 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001461 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001462 // Listen for mousedown events on the screenNode as in FF the focus
1463 // events don't bubble.
1464 screenNode.addEventListener('mousedown', function() {
1465 setTimeout(this.onFocusChange_.bind(this, true));
1466 }.bind(this));
1467
Toni Barzic0bfa8922013-11-22 11:18:35 -08001468 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001469 'blur', this.onFocusChange_.bind(this, false));
1470
1471 var style = this.document_.createElement('style');
1472 style.textContent =
1473 ('.cursor-node[focus="false"] {' +
1474 ' box-sizing: border-box;' +
1475 ' background-color: transparent !important;' +
1476 ' border-width: 2px;' +
1477 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001478 '}' +
1479 '.wc-node {' +
1480 ' display: inline-block;' +
1481 ' text-align: center;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001482 ' width: calc(var(--hterm-charsize-width) * 2);' +
Mike Frysinger4036f6e2017-05-31 14:02:55 -04001483 ' line-height: var(--hterm-charsize-height);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001484 '}' +
1485 ':root {' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001486 ' --hterm-charsize-width: ' + this.scrollPort_.characterSize.width + 'px;' +
1487 ' --hterm-charsize-height: ' + this.scrollPort_.characterSize.height + 'px;' +
Mike Frysingera27c0502017-08-23 22:37:10 -04001488 // Default position hides the cursor for when the window is initializing.
1489 ' --hterm-cursor-offset-col: -1;' +
1490 ' --hterm-cursor-offset-row: -1;' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001491 ' --hterm-blink-node-duration: 0.7s;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001492 ' --hterm-mouse-cursor-text: text;' +
1493 ' --hterm-mouse-cursor-pointer: default;' +
1494 ' --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001495 '}' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001496 '.uri-node:hover {' +
1497 ' text-decoration: underline;' +
1498 ' cursor: pointer;' +
1499 '}' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001500 '@keyframes blink {' +
1501 ' from { opacity: 1.0; }' +
1502 ' to { opacity: 0.0; }' +
1503 '}' +
1504 '.blink-node {' +
1505 ' animation-name: blink;' +
1506 ' animation-duration: var(--hterm-blink-node-duration);' +
1507 ' animation-iteration-count: infinite;' +
1508 ' animation-timing-function: ease-in-out;' +
1509 ' animation-direction: alternate;' +
rginda8e92a692012-05-20 19:37:20 -07001510 '}');
1511 this.document_.head.appendChild(style);
1512
rginda8ba33642011-12-14 12:31:31 -08001513 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001514 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001515 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001516 this.cursorNode_.style.cssText =
1517 ('position: absolute;' +
Mike Frysinger44c32202017-08-05 01:13:09 -04001518 'left: calc(var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));' +
1519 'top: calc(var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));' +
rginda87b86462011-12-14 13:48:03 -08001520 'display: block;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001521 'width: var(--hterm-charsize-width);' +
1522 'height: var(--hterm-charsize-height);' +
Rob Spies06533ba2014-04-24 11:20:37 -07001523 '-webkit-transition: opacity, background-color 100ms linear;' +
1524 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001525
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001526 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001527 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1528 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001529
rginda8ba33642011-12-14 12:31:31 -08001530 this.document_.body.appendChild(this.cursorNode_);
1531
rgindad5613292012-06-19 15:40:37 -07001532 // When 'enableMouseDragScroll' is off we reposition this element directly
1533 // under the mouse cursor after a click. This makes Chrome associate
1534 // subsequent mousemove events with the scroll-blocker. Since the
1535 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1536 // events do not cause the scrollport to scroll.
1537 //
1538 // It's a hack, but it's the cleanest way I could find.
1539 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001540 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
rgindad5613292012-06-19 15:40:37 -07001541 this.scrollBlockerNode_.style.cssText =
1542 ('position: absolute;' +
1543 'top: -99px;' +
1544 'display: block;' +
1545 'width: 10px;' +
1546 'height: 10px;');
1547 this.document_.body.appendChild(this.scrollBlockerNode_);
1548
rgindad5613292012-06-19 15:40:37 -07001549 this.scrollPort_.onScrollWheel = onMouse;
1550 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1551 ].forEach(function(event) {
1552 this.scrollBlockerNode_.addEventListener(event, onMouse);
1553 this.cursorNode_.addEventListener(event, onMouse);
1554 this.document_.addEventListener(event, onMouse);
1555 }.bind(this));
1556
1557 this.cursorNode_.addEventListener('mousedown', function() {
1558 setTimeout(this.focus.bind(this));
1559 }.bind(this));
1560
rginda8ba33642011-12-14 12:31:31 -08001561 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001562
rginda87b86462011-12-14 13:48:03 -08001563 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001564 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001565};
1566
rginda0918b652012-04-04 11:26:24 -07001567/**
1568 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001569 *
1570 * @return {HTMLDocument}
rginda0918b652012-04-04 11:26:24 -07001571 */
rginda87b86462011-12-14 13:48:03 -08001572hterm.Terminal.prototype.getDocument = function() {
1573 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001574};
1575
1576/**
rginda0918b652012-04-04 11:26:24 -07001577 * Focus the terminal.
1578 */
1579hterm.Terminal.prototype.focus = function() {
1580 this.scrollPort_.focus();
1581};
1582
1583/**
rginda8ba33642011-12-14 12:31:31 -08001584 * Return the HTML Element for a given row index.
1585 *
1586 * This is a method from the RowProvider interface. The ScrollPort uses
1587 * it to fetch rows on demand as they are scrolled into view.
1588 *
1589 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1590 * pairs to conserve memory.
1591 *
1592 * @param {integer} index The zero-based row index, measured relative to the
1593 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001594 * largest indices.
rginda8ba33642011-12-14 12:31:31 -08001595 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1596 */
1597hterm.Terminal.prototype.getRowNode = function(index) {
1598 if (index < this.scrollbackRows_.length)
1599 return this.scrollbackRows_[index];
1600
1601 var screenIndex = index - this.scrollbackRows_.length;
1602 return this.screen_.rowsArray[screenIndex];
1603};
1604
1605/**
1606 * Return the text content for a given range of rows.
1607 *
1608 * This is a method from the RowProvider interface. The ScrollPort uses
1609 * it to fetch text content on demand when the user attempts to copy their
1610 * selection to the clipboard.
1611 *
1612 * @param {integer} start The zero-based row index to start from, measured
1613 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001614 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001615 * @param {integer} end The zero-based row index to end on, measured
1616 * relative to the start of the scrollback buffer.
1617 * @return {string} A single string containing the text value of the range of
1618 * rows. Lines will be newline delimited, with no trailing newline.
1619 */
1620hterm.Terminal.prototype.getRowsText = function(start, end) {
1621 var ary = [];
1622 for (var i = start; i < end; i++) {
1623 var node = this.getRowNode(i);
1624 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001625 if (i < end - 1 && !node.getAttribute('line-overflow'))
1626 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001627 }
1628
rgindaa09e7332012-08-17 12:49:51 -07001629 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001630};
1631
1632/**
1633 * Return the text content for a given row.
1634 *
1635 * This is a method from the RowProvider interface. The ScrollPort uses
1636 * it to fetch text content on demand when the user attempts to copy their
1637 * selection to the clipboard.
1638 *
1639 * @param {integer} index The zero-based row index to return, measured
1640 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001641 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001642 * @return {string} A string containing the text value of the selected row.
1643 */
1644hterm.Terminal.prototype.getRowText = function(index) {
1645 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001646 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001647};
1648
1649/**
1650 * Return the total number of rows in the addressable screen and in the
1651 * scrollback buffer of this terminal.
1652 *
1653 * This is a method from the RowProvider interface. The ScrollPort uses
1654 * it to compute the size of the scrollbar.
1655 *
1656 * @return {integer} The number of rows in this terminal.
1657 */
1658hterm.Terminal.prototype.getRowCount = function() {
1659 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1660};
1661
1662/**
1663 * Create DOM nodes for new rows and append them to the end of the terminal.
1664 *
1665 * This is the only correct way to add a new DOM node for a row. Notice that
1666 * the new row is appended to the bottom of the list of rows, and does not
1667 * require renumbering (of the rowIndex property) of previous rows.
1668 *
1669 * If you think you want a new blank row somewhere in the middle of the
1670 * terminal, look into moveRows_().
1671 *
1672 * This method does not pay attention to vtScrollTop/Bottom, since you should
1673 * be using moveRows() in cases where they would matter.
1674 *
1675 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001676 *
1677 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001678 */
1679hterm.Terminal.prototype.appendRows_ = function(count) {
1680 var cursorRow = this.screen_.rowsArray.length;
1681 var offset = this.scrollbackRows_.length + cursorRow;
1682 for (var i = 0; i < count; i++) {
1683 var row = this.document_.createElement('x-row');
1684 row.appendChild(this.document_.createTextNode(''));
1685 row.rowIndex = offset + i;
1686 this.screen_.pushRow(row);
1687 }
1688
1689 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1690 if (extraRows > 0) {
1691 var ary = this.screen_.shiftRows(extraRows);
1692 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001693 if (this.scrollPort_.isScrolledEnd)
1694 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001695 }
1696
1697 if (cursorRow >= this.screen_.rowsArray.length)
1698 cursorRow = this.screen_.rowsArray.length - 1;
1699
rginda87b86462011-12-14 13:48:03 -08001700 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001701};
1702
1703/**
1704 * Relocate rows from one part of the addressable screen to another.
1705 *
1706 * This is used to recycle rows during VT scrolls (those which are driven
1707 * by VT commands, rather than by the user manipulating the scrollbar.)
1708 *
1709 * In this case, the blank lines scrolled into the scroll region are made of
1710 * the nodes we scrolled off. These have their rowIndex properties carefully
1711 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001712 *
1713 * @param {number} fromIndex The start index.
1714 * @param {number} count The number of rows to move.
1715 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001716 */
1717hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1718 var ary = this.screen_.removeRows(fromIndex, count);
1719 this.screen_.insertRows(toIndex, ary);
1720
1721 var start, end;
1722 if (fromIndex < toIndex) {
1723 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001724 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001725 } else {
1726 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001727 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001728 }
1729
1730 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001731 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001732};
1733
1734/**
1735 * Renumber the rowIndex property of the given range of rows.
1736 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001737 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001738 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001739 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001740 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001741 *
1742 * @param {number} start The start index.
1743 * @param {number} end The end index.
1744 * @param {hterm.Screen} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001745 */
Robert Ginda40932892012-12-10 17:26:40 -08001746hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1747 var screen = opt_screen || this.screen_;
1748
rginda8ba33642011-12-14 12:31:31 -08001749 var offset = this.scrollbackRows_.length;
1750 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001751 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001752 }
1753};
1754
1755/**
1756 * Print a string to the terminal.
1757 *
1758 * This respects the current insert and wraparound modes. It will add new lines
1759 * to the end of the terminal, scrolling off the top into the scrollback buffer
1760 * if necessary.
1761 *
1762 * The string is *not* parsed for escape codes. Use the interpret() method if
1763 * that's what you're after.
1764 *
1765 * @param{string} str The string to print.
1766 */
1767hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001768 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001769
Ricky Liang48f05cb2013-12-31 23:35:29 +08001770 var strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04001771 // Fun edge case: If the string only contains zero width codepoints (like
1772 // combining characters), we make sure to iterate at least once below.
1773 if (strWidth == 0 && str)
1774 strWidth = 1;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001775
1776 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001777 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1778 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001779 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001780 }
rgindaa19afe22012-01-25 15:40:22 -08001781
Ricky Liang48f05cb2013-12-31 23:35:29 +08001782 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001783 var didOverflow = false;
1784 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001785
rgindaa9abdd82012-08-06 18:05:09 -07001786 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1787 didOverflow = true;
1788 count = this.screenSize.width - this.screen_.cursorPosition.column;
1789 }
rgindaa19afe22012-01-25 15:40:22 -08001790
rgindaa9abdd82012-08-06 18:05:09 -07001791 if (didOverflow && !this.options_.wraparound) {
1792 // If the string overflowed the line but wraparound is off, then the
1793 // last printed character should be the last of the string.
1794 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001795 substr = lib.wc.substr(str, startOffset, count - 1) +
1796 lib.wc.substr(str, strWidth - 1);
1797 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001798 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001799 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001800 }
rgindaa19afe22012-01-25 15:40:22 -08001801
Ricky Liang48f05cb2013-12-31 23:35:29 +08001802 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1803 for (var i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001804 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
1805 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001806
1807 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001808 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001809 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001810 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001811 }
1812 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001813 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07001814 }
1815
1816 this.screen_.maybeClipCurrentRow();
1817 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001818 }
rginda8ba33642011-12-14 12:31:31 -08001819
1820 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001821
rginda9f5222b2012-03-05 11:53:28 -08001822 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001823 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001824};
1825
1826/**
rginda87b86462011-12-14 13:48:03 -08001827 * Set the VT scroll region.
1828 *
rginda87b86462011-12-14 13:48:03 -08001829 * This also resets the cursor position to the absolute (0, 0) position, since
1830 * that's what xterm appears to do.
1831 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001832 * Setting the scroll region to the full height of the terminal will clear
1833 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1834 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1835 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1836 * continue to work as most users would expect.
1837 *
rginda87b86462011-12-14 13:48:03 -08001838 * @param {integer} scrollTop The zero-based top of the scroll region.
1839 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1840 * inclusive.
1841 */
1842hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001843 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001844 this.vtScrollTop_ = null;
1845 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001846 } else {
1847 this.vtScrollTop_ = scrollTop;
1848 this.vtScrollBottom_ = scrollBottom;
1849 }
rginda87b86462011-12-14 13:48:03 -08001850};
1851
1852/**
rginda8ba33642011-12-14 12:31:31 -08001853 * Return the top row index according to the VT.
1854 *
1855 * This will return 0 unless the terminal has been told to restrict scrolling
1856 * to some lower row. It is used for some VT cursor positioning and scrolling
1857 * commands.
1858 *
1859 * @return {integer} The topmost row in the terminal's scroll region.
1860 */
1861hterm.Terminal.prototype.getVTScrollTop = function() {
1862 if (this.vtScrollTop_ != null)
1863 return this.vtScrollTop_;
1864
1865 return 0;
rginda87b86462011-12-14 13:48:03 -08001866};
rginda8ba33642011-12-14 12:31:31 -08001867
1868/**
1869 * Return the bottom row index according to the VT.
1870 *
1871 * This will return the height of the terminal unless the it has been told to
1872 * restrict scrolling to some higher row. It is used for some VT cursor
1873 * positioning and scrolling commands.
1874 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001875 * @return {integer} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001876 */
1877hterm.Terminal.prototype.getVTScrollBottom = function() {
1878 if (this.vtScrollBottom_ != null)
1879 return this.vtScrollBottom_;
1880
rginda87b86462011-12-14 13:48:03 -08001881 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001882}
1883
1884/**
1885 * Process a '\n' character.
1886 *
1887 * If the cursor is on the final row of the terminal this will append a new
1888 * blank row to the screen and scroll the topmost row into the scrollback
1889 * buffer.
1890 *
1891 * Otherwise, this moves the cursor to column zero of the next row.
1892 */
1893hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001894 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1895 this.screen_.rowsArray.length - 1);
1896
1897 if (this.vtScrollBottom_ != null) {
1898 // A VT Scroll region is active, we never append new rows.
1899 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1900 // We're at the end of the VT Scroll Region, perform a VT scroll.
1901 this.vtScrollUp(1);
1902 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1903 } else if (cursorAtEndOfScreen) {
1904 // We're at the end of the screen, the only thing to do is put the
1905 // cursor to column 0.
1906 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1907 } else {
1908 // Anywhere else, advance the cursor row, and reset the column.
1909 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1910 }
1911 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001912 // We're at the end of the screen. Append a new row to the terminal,
1913 // shifting the top row into the scrollback.
1914 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001915 } else {
rginda87b86462011-12-14 13:48:03 -08001916 // Anywhere else in the screen just moves the cursor.
1917 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001918 }
1919};
1920
1921/**
1922 * Like newLine(), except maintain the cursor column.
1923 */
1924hterm.Terminal.prototype.lineFeed = function() {
1925 var column = this.screen_.cursorPosition.column;
1926 this.newLine();
1927 this.setCursorColumn(column);
1928};
1929
1930/**
rginda87b86462011-12-14 13:48:03 -08001931 * If autoCarriageReturn is set then newLine(), else lineFeed().
1932 */
1933hterm.Terminal.prototype.formFeed = function() {
1934 if (this.options_.autoCarriageReturn) {
1935 this.newLine();
1936 } else {
1937 this.lineFeed();
1938 }
1939};
1940
1941/**
1942 * Move the cursor up one row, possibly inserting a blank line.
1943 *
1944 * The cursor column is not changed.
1945 */
1946hterm.Terminal.prototype.reverseLineFeed = function() {
1947 var scrollTop = this.getVTScrollTop();
1948 var currentRow = this.screen_.cursorPosition.row;
1949
1950 if (currentRow == scrollTop) {
1951 this.insertLines(1);
1952 } else {
1953 this.setAbsoluteCursorRow(currentRow - 1);
1954 }
1955};
1956
1957/**
rginda8ba33642011-12-14 12:31:31 -08001958 * Replace all characters to the left of the current cursor with the space
1959 * character.
1960 *
1961 * TODO(rginda): This should probably *remove* the characters (not just replace
1962 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001963 * position.
rginda8ba33642011-12-14 12:31:31 -08001964 */
1965hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001966 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001967 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04001968 const count = cursor.column + 1;
1969 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08001970 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001971};
1972
1973/**
David Benjamin684a9b72012-05-01 17:19:58 -04001974 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001975 *
1976 * The cursor position is unchanged.
1977 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001978 * If the current background color is not the default background color this
1979 * will insert spaces rather than delete. This is unfortunate because the
1980 * trailing space will affect text selection, but it's difficult to come up
1981 * with a way to style empty space that wouldn't trip up the hterm.Screen
1982 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001983 *
1984 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1985 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1986 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05001987 *
1988 * @param {number} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08001989 */
1990hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001991 if (this.screen_.cursorPosition.overflow)
1992 return;
1993
Robert Ginda7fd57082012-09-25 14:41:47 -07001994 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1995 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001996
1997 if (this.screen_.textAttributes.background ===
1998 this.screen_.textAttributes.DEFAULT_COLOR) {
1999 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08002000 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07002001 this.screen_.cursorPosition.column + count) {
2002 this.screen_.deleteChars(count);
2003 this.clearCursorOverflow();
2004 return;
2005 }
2006 }
2007
rginda87b86462011-12-14 13:48:03 -08002008 var cursor = this.saveCursor();
Mike Frysinger6380bed2017-08-24 18:46:39 -04002009 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08002010 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002011 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002012};
2013
2014/**
2015 * Erase the current line.
2016 *
2017 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002018 */
2019hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08002020 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002021 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002022 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002023 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002024};
2025
2026/**
David Benjamina08d78f2012-05-05 00:28:49 -04002027 * Erase all characters from the start of the screen to the current cursor
2028 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002029 *
2030 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002031 */
2032hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08002033 var cursor = this.saveCursor();
2034
2035 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002036
David Benjamina08d78f2012-05-05 00:28:49 -04002037 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002038 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002039 this.screen_.clearCursorRow();
2040 }
2041
rginda87b86462011-12-14 13:48:03 -08002042 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002043 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002044};
2045
2046/**
2047 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002048 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002049 *
2050 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002051 */
2052hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08002053 var cursor = this.saveCursor();
2054
2055 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002056
David Benjamina08d78f2012-05-05 00:28:49 -04002057 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08002058 for (var i = cursor.row + 1; i <= bottom; i++) {
2059 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002060 this.screen_.clearCursorRow();
2061 }
2062
rginda87b86462011-12-14 13:48:03 -08002063 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002064 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002065};
2066
2067/**
2068 * Fill the terminal with a given character.
2069 *
2070 * This methods does not respect the VT scroll region.
2071 *
2072 * @param {string} ch The character to use for the fill.
2073 */
2074hterm.Terminal.prototype.fill = function(ch) {
2075 var cursor = this.saveCursor();
2076
2077 this.setAbsoluteCursorPosition(0, 0);
2078 for (var row = 0; row < this.screenSize.height; row++) {
2079 for (var col = 0; col < this.screenSize.width; col++) {
2080 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002081 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002082 }
2083 }
2084
2085 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002086};
2087
2088/**
rginda9ea433c2012-03-16 11:57:00 -07002089 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002090 *
rginda9ea433c2012-03-16 11:57:00 -07002091 * This does not respect the scroll region.
2092 *
2093 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2094 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002095 */
rginda9ea433c2012-03-16 11:57:00 -07002096hterm.Terminal.prototype.clearHome = function(opt_screen) {
2097 var screen = opt_screen || this.screen_;
2098 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002099
rginda11057d52012-04-25 12:29:56 -07002100 if (bottom == 0) {
2101 // Empty screen, nothing to do.
2102 return;
2103 }
2104
rgindae4d29232012-01-19 10:47:13 -08002105 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002106 screen.setCursorPosition(i, 0);
2107 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002108 }
2109
rginda9ea433c2012-03-16 11:57:00 -07002110 screen.setCursorPosition(0, 0);
2111};
2112
2113/**
2114 * Erase the entire display without changing the cursor position.
2115 *
2116 * The cursor position is unchanged. This does not respect the scroll
2117 * region.
2118 *
2119 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2120 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002121 */
2122hterm.Terminal.prototype.clear = function(opt_screen) {
2123 var screen = opt_screen || this.screen_;
2124 var cursor = screen.cursorPosition.clone();
2125 this.clearHome(screen);
2126 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002127};
2128
2129/**
2130 * VT command to insert lines at the current cursor row.
2131 *
2132 * This respects the current scroll region. Rows pushed off the bottom are
2133 * lost (they won't show up in the scrollback buffer).
2134 *
rginda8ba33642011-12-14 12:31:31 -08002135 * @param {integer} count The number of lines to insert.
2136 */
2137hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002138 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002139
2140 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002141 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002142
Robert Ginda579186b2012-09-26 11:40:04 -07002143 // The moveCount is the number of rows we need to relocate to make room for
2144 // the new row(s). The count is the distance to move them.
2145 var moveCount = bottom - cursorRow - count + 1;
2146 if (moveCount)
2147 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002148
Robert Ginda579186b2012-09-26 11:40:04 -07002149 for (var i = count - 1; i >= 0; i--) {
2150 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002151 this.screen_.clearCursorRow();
2152 }
rginda8ba33642011-12-14 12:31:31 -08002153};
2154
2155/**
2156 * VT command to delete lines at the current cursor row.
2157 *
2158 * New rows are added to the bottom of scroll region to take their place. New
2159 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002160 *
2161 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002162 */
2163hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002164 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002165
rginda87b86462011-12-14 13:48:03 -08002166 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002167 var bottom = this.getVTScrollBottom();
2168
rginda87b86462011-12-14 13:48:03 -08002169 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002170 count = Math.min(count, maxCount);
2171
rginda87b86462011-12-14 13:48:03 -08002172 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002173 if (count != maxCount)
2174 this.moveRows_(top, count, moveStart);
2175
2176 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002177 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002178 this.screen_.clearCursorRow();
2179 }
2180
rginda87b86462011-12-14 13:48:03 -08002181 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002182 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002183};
2184
2185/**
2186 * Inserts the given number of spaces at the current cursor position.
2187 *
rginda87b86462011-12-14 13:48:03 -08002188 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002189 *
2190 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002191 */
2192hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002193 var cursor = this.saveCursor();
2194
rgindacbbd7482012-06-13 15:06:16 -07002195 var ws = lib.f.getWhitespace(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002196 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002197 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002198
2199 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002200 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002201};
2202
2203/**
2204 * Forward-delete the specified number of characters starting at the cursor
2205 * position.
2206 *
2207 * @param {integer} count The number of characters to delete.
2208 */
2209hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002210 var deleted = this.screen_.deleteChars(count);
2211 if (deleted && !this.screen_.textAttributes.isDefault()) {
2212 var cursor = this.saveCursor();
2213 this.setCursorColumn(this.screenSize.width - deleted);
2214 this.screen_.insertString(lib.f.getWhitespace(deleted));
2215 this.restoreCursor(cursor);
2216 }
2217
David Benjamin54e8bf62012-06-01 22:31:40 -04002218 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002219};
2220
2221/**
2222 * Shift rows in the scroll region upwards by a given number of lines.
2223 *
2224 * New rows are inserted at the bottom of the scroll region to fill the
2225 * vacated rows. The new rows not filled out with the current text attributes.
2226 *
2227 * This function does not affect the scrollback rows at all. Rows shifted
2228 * off the top are lost.
2229 *
rginda87b86462011-12-14 13:48:03 -08002230 * The cursor position is not altered.
2231 *
rginda8ba33642011-12-14 12:31:31 -08002232 * @param {integer} count The number of rows to scroll.
2233 */
2234hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002235 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002236
rginda87b86462011-12-14 13:48:03 -08002237 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002238 this.deleteLines(count);
2239
rginda87b86462011-12-14 13:48:03 -08002240 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002241};
2242
2243/**
2244 * Shift rows below the cursor down by a given number of lines.
2245 *
2246 * This function respects the current scroll region.
2247 *
2248 * New rows are inserted at the top of the scroll region to fill the
2249 * vacated rows. The new rows not filled out with the current text attributes.
2250 *
2251 * This function does not affect the scrollback rows at all. Rows shifted
2252 * off the bottom are lost.
2253 *
2254 * @param {integer} count The number of rows to scroll.
2255 */
2256hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08002257 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002258
rginda87b86462011-12-14 13:48:03 -08002259 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08002260 this.insertLines(opt_count);
2261
rginda87b86462011-12-14 13:48:03 -08002262 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002263};
2264
rginda87b86462011-12-14 13:48:03 -08002265
rginda8ba33642011-12-14 12:31:31 -08002266/**
2267 * Set the cursor position.
2268 *
2269 * The cursor row is relative to the scroll region if the terminal has
2270 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2271 *
2272 * @param {integer} row The new zero-based cursor row.
2273 * @param {integer} row The new zero-based cursor column.
2274 */
2275hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2276 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002277 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002278 } else {
rginda87b86462011-12-14 13:48:03 -08002279 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002280 }
rginda87b86462011-12-14 13:48:03 -08002281};
rginda8ba33642011-12-14 12:31:31 -08002282
Evan Jones2600d4f2016-12-06 09:29:36 -05002283/**
2284 * Move the cursor relative to its current position.
2285 *
2286 * @param {number} row
2287 * @param {number} column
2288 */
rginda87b86462011-12-14 13:48:03 -08002289hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2290 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002291 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2292 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002293 this.screen_.setCursorPosition(row, column);
2294};
2295
Evan Jones2600d4f2016-12-06 09:29:36 -05002296/**
2297 * Move the cursor to the specified position.
2298 *
2299 * @param {number} row
2300 * @param {number} column
2301 */
rginda87b86462011-12-14 13:48:03 -08002302hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002303 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2304 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002305 this.screen_.setCursorPosition(row, column);
2306};
2307
2308/**
2309 * Set the cursor column.
2310 *
2311 * @param {integer} column The new zero-based cursor column.
2312 */
2313hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002314 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002315};
2316
2317/**
2318 * Return the cursor column.
2319 *
2320 * @return {integer} The zero-based cursor column.
2321 */
2322hterm.Terminal.prototype.getCursorColumn = function() {
2323 return this.screen_.cursorPosition.column;
2324};
2325
2326/**
2327 * Set the cursor row.
2328 *
2329 * The cursor row is relative to the scroll region if the terminal has
2330 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2331 *
2332 * @param {integer} row The new cursor row.
2333 */
rginda87b86462011-12-14 13:48:03 -08002334hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2335 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002336};
2337
2338/**
2339 * Return the cursor row.
2340 *
2341 * @return {integer} The zero-based cursor row.
2342 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002343hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002344 return this.screen_.cursorPosition.row;
2345};
2346
2347/**
2348 * Request that the ScrollPort redraw itself soon.
2349 *
2350 * The redraw will happen asynchronously, soon after the call stack winds down.
2351 * Multiple calls will be coalesced into a single redraw.
2352 */
2353hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002354 if (this.timeouts_.redraw)
2355 return;
rginda8ba33642011-12-14 12:31:31 -08002356
2357 var self = this;
rginda87b86462011-12-14 13:48:03 -08002358 this.timeouts_.redraw = setTimeout(function() {
2359 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002360 self.scrollPort_.redraw_();
2361 }, 0);
2362};
2363
2364/**
2365 * Request that the ScrollPort be scrolled to the bottom.
2366 *
2367 * The scroll will happen asynchronously, soon after the call stack winds down.
2368 * Multiple calls will be coalesced into a single scroll.
2369 *
2370 * This affects the scrollbar position of the ScrollPort, and has nothing to
2371 * do with the VT scroll commands.
2372 */
2373hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2374 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002375 return;
rginda8ba33642011-12-14 12:31:31 -08002376
2377 var self = this;
2378 this.timeouts_.scrollDown = setTimeout(function() {
2379 delete self.timeouts_.scrollDown;
2380 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2381 }, 10);
2382};
2383
2384/**
2385 * Move the cursor up a specified number of rows.
2386 *
2387 * @param {integer} count The number of rows to move the cursor.
2388 */
2389hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002390 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002391};
2392
2393/**
2394 * Move the cursor down a specified number of rows.
2395 *
2396 * @param {integer} count The number of rows to move the cursor.
2397 */
2398hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002399 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002400 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2401 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2402 this.screenSize.height - 1);
2403
rgindacbbd7482012-06-13 15:06:16 -07002404 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002405 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002406 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002407};
2408
2409/**
2410 * Move the cursor left a specified number of columns.
2411 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002412 * If reverse wraparound mode is enabled and the previous row wrapped into
2413 * the current row then we back up through the wraparound as well.
2414 *
rginda8ba33642011-12-14 12:31:31 -08002415 * @param {integer} count The number of columns to move the cursor.
2416 */
2417hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002418 count = count || 1;
2419
2420 if (count < 1)
2421 return;
2422
2423 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002424 if (this.options_.reverseWraparound) {
2425 if (this.screen_.cursorPosition.overflow) {
2426 // If this cursor is in the right margin, consume one count to get it
2427 // back to the last column. This only applies when we're in reverse
2428 // wraparound mode.
2429 count--;
2430 this.clearCursorOverflow();
2431
2432 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002433 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002434 }
2435
Robert Gindabfb32622014-07-17 13:20:27 -07002436 var newRow = this.screen_.cursorPosition.row;
2437 var newColumn = currentColumn - count;
2438 if (newColumn < 0) {
2439 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2440 if (newRow < 0) {
2441 // xterm also wraps from row 0 to the last row.
2442 newRow = this.screenSize.height + newRow % this.screenSize.height;
2443 }
2444 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2445 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002446
Robert Gindabfb32622014-07-17 13:20:27 -07002447 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2448
2449 } else {
2450 var newColumn = Math.max(currentColumn - count, 0);
2451 this.setCursorColumn(newColumn);
2452 }
rginda8ba33642011-12-14 12:31:31 -08002453};
2454
2455/**
2456 * Move the cursor right a specified number of columns.
2457 *
2458 * @param {integer} count The number of columns to move the cursor.
2459 */
2460hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002461 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002462
2463 if (count < 1)
2464 return;
2465
rgindacbbd7482012-06-13 15:06:16 -07002466 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002467 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002468 this.setCursorColumn(column);
2469};
2470
2471/**
2472 * Reverse the foreground and background colors of the terminal.
2473 *
2474 * This only affects text that was drawn with no attributes.
2475 *
2476 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2477 * been drawn with attributes that happen to coincide with the default
2478 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002479 *
2480 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002481 */
2482hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002483 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002484 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002485 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2486 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002487 } else {
rginda9f5222b2012-03-05 11:53:28 -08002488 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2489 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002490 }
2491};
2492
2493/**
rginda87b86462011-12-14 13:48:03 -08002494 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002495 *
2496 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002497 */
2498hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002499 this.cursorNode_.style.backgroundColor =
2500 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002501
2502 var self = this;
2503 setTimeout(function() {
Matheus Fernandes2d733082017-09-11 06:43:01 -04002504 self.restyleCursor_();
rginda6d397402012-01-17 10:58:29 -08002505 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002506
Michael Kelly485ecd12014-06-09 11:41:56 -04002507 // bellSquelchTimeout_ affects both audio and notification bells.
2508 if (this.bellSquelchTimeout_)
2509 return;
2510
Robert Ginda92e18102013-03-14 13:56:37 -07002511 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002512 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002513 this.bellSequelchTimeout_ = setTimeout(function() {
2514 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002515 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002516 } else {
2517 delete this.bellSquelchTimeout_;
2518 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002519
2520 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002521 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002522 this.bellNotificationList_.push(n);
2523 // TODO: Should we try to raise the window here?
2524 n.onclick = function() { self.closeBellNotifications_(); };
2525 }
rginda87b86462011-12-14 13:48:03 -08002526};
2527
2528/**
rginda8ba33642011-12-14 12:31:31 -08002529 * Set the origin mode bit.
2530 *
2531 * If origin mode is on, certain VT cursor and scrolling commands measure their
2532 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2533 * to the top of the addressable screen.
2534 *
2535 * Defaults to off.
2536 *
2537 * @param {boolean} state True to set origin mode, false to unset.
2538 */
2539hterm.Terminal.prototype.setOriginMode = function(state) {
2540 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002541 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002542};
2543
2544/**
2545 * Set the insert mode bit.
2546 *
2547 * If insert mode is on, existing text beyond the cursor position will be
2548 * shifted right to make room for new text. Otherwise, new text overwrites
2549 * any existing text.
2550 *
2551 * Defaults to off.
2552 *
2553 * @param {boolean} state True to set insert mode, false to unset.
2554 */
2555hterm.Terminal.prototype.setInsertMode = function(state) {
2556 this.options_.insertMode = state;
2557};
2558
2559/**
rginda87b86462011-12-14 13:48:03 -08002560 * Set the auto carriage return bit.
2561 *
2562 * If auto carriage return is on then a formfeed character is interpreted
2563 * as a newline, otherwise it's the same as a linefeed. The difference boils
2564 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002565 *
2566 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002567 */
2568hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2569 this.options_.autoCarriageReturn = state;
2570};
2571
2572/**
rginda8ba33642011-12-14 12:31:31 -08002573 * Set the wraparound mode bit.
2574 *
2575 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2576 * to the start of the following row. Otherwise, the cursor is clamped to the
2577 * end of the screen and attempts to write past it are ignored.
2578 *
2579 * Defaults to on.
2580 *
2581 * @param {boolean} state True to set wraparound mode, false to unset.
2582 */
2583hterm.Terminal.prototype.setWraparound = function(state) {
2584 this.options_.wraparound = state;
2585};
2586
2587/**
2588 * Set the reverse-wraparound mode bit.
2589 *
2590 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2591 * to the end of the previous row. Otherwise, the cursor is clamped to column
2592 * 0.
2593 *
2594 * Defaults to off.
2595 *
2596 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2597 */
2598hterm.Terminal.prototype.setReverseWraparound = function(state) {
2599 this.options_.reverseWraparound = state;
2600};
2601
2602/**
2603 * Selects between the primary and alternate screens.
2604 *
2605 * If alternate mode is on, the alternate screen is active. Otherwise the
2606 * primary screen is active.
2607 *
2608 * Swapping screens has no effect on the scrollback buffer.
2609 *
2610 * Each screen maintains its own cursor position.
2611 *
2612 * Defaults to off.
2613 *
2614 * @param {boolean} state True to set alternate mode, false to unset.
2615 */
2616hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002617 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002618 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2619
rginda35c456b2012-02-09 17:29:05 -08002620 if (this.screen_.rowsArray.length &&
2621 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2622 // If the screen changed sizes while we were away, our rowIndexes may
2623 // be incorrect.
2624 var offset = this.scrollbackRows_.length;
2625 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002626 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002627 ary[i].rowIndex = offset + i;
2628 }
2629 }
rginda8ba33642011-12-14 12:31:31 -08002630
rginda35c456b2012-02-09 17:29:05 -08002631 this.realizeWidth_(this.screenSize.width);
2632 this.realizeHeight_(this.screenSize.height);
2633 this.scrollPort_.syncScrollHeight();
2634 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002635
rginda6d397402012-01-17 10:58:29 -08002636 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002637 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002638};
2639
2640/**
2641 * Set the cursor-blink mode bit.
2642 *
2643 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2644 * a visible cursor does not blink.
2645 *
2646 * You should make sure to turn blinking off if you're going to dispose of a
2647 * terminal, otherwise you'll leak a timeout.
2648 *
2649 * Defaults to on.
2650 *
2651 * @param {boolean} state True to set cursor-blink mode, false to unset.
2652 */
2653hterm.Terminal.prototype.setCursorBlink = function(state) {
2654 this.options_.cursorBlink = state;
2655
2656 if (!state && this.timeouts_.cursorBlink) {
2657 clearTimeout(this.timeouts_.cursorBlink);
2658 delete this.timeouts_.cursorBlink;
2659 }
2660
2661 if (this.options_.cursorVisible)
2662 this.setCursorVisible(true);
2663};
2664
2665/**
2666 * Set the cursor-visible mode bit.
2667 *
2668 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2669 *
2670 * Defaults to on.
2671 *
2672 * @param {boolean} state True to set cursor-visible mode, false to unset.
2673 */
2674hterm.Terminal.prototype.setCursorVisible = function(state) {
2675 this.options_.cursorVisible = state;
2676
2677 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002678 if (this.timeouts_.cursorBlink) {
2679 clearTimeout(this.timeouts_.cursorBlink);
2680 delete this.timeouts_.cursorBlink;
2681 }
rginda87b86462011-12-14 13:48:03 -08002682 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002683 return;
2684 }
2685
rginda87b86462011-12-14 13:48:03 -08002686 this.syncCursorPosition_();
2687
2688 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002689
2690 if (this.options_.cursorBlink) {
2691 if (this.timeouts_.cursorBlink)
2692 return;
2693
Robert Gindaea2183e2014-07-17 09:51:51 -07002694 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002695 } else {
2696 if (this.timeouts_.cursorBlink) {
2697 clearTimeout(this.timeouts_.cursorBlink);
2698 delete this.timeouts_.cursorBlink;
2699 }
2700 }
2701};
2702
2703/**
rginda87b86462011-12-14 13:48:03 -08002704 * Synchronizes the visible cursor and document selection with the current
2705 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002706 */
2707hterm.Terminal.prototype.syncCursorPosition_ = function() {
2708 var topRowIndex = this.scrollPort_.getTopRowIndex();
2709 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2710 var cursorRowIndex = this.scrollbackRows_.length +
2711 this.screen_.cursorPosition.row;
2712
2713 if (cursorRowIndex > bottomRowIndex) {
2714 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04002715 this.setCssVar('cursor-offset-row', '-1');
rginda8ba33642011-12-14 12:31:31 -08002716 return;
2717 }
2718
Robert Gindab837c052014-08-11 11:17:51 -07002719 if (this.options_.cursorVisible &&
2720 this.cursorNode_.style.display == 'none') {
2721 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2722 this.cursorNode_.style.display = '';
2723 }
2724
Mike Frysinger44c32202017-08-05 01:13:09 -04002725 // Position the cursor using CSS variable math. If we do the math in JS,
2726 // the float math will end up being more precise than the CSS which will
2727 // cause the cursor tracking to be off.
2728 this.setCssVar(
2729 'cursor-offset-row',
2730 `${cursorRowIndex - topRowIndex} + ` +
2731 `${this.scrollPort_.visibleRowTopMargin}px`);
2732 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08002733
2734 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04002735 '(' + this.screen_.cursorPosition.column +
2736 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08002737 ')');
2738
2739 // Update the caret for a11y purposes.
2740 var selection = this.document_.getSelection();
2741 if (selection && selection.isCollapsed)
2742 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002743};
2744
Robert Gindafb1be6a2013-12-11 11:56:22 -08002745/**
2746 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2747 * and character cell dimensions.
2748 */
Robert Ginda830583c2013-08-07 13:20:46 -07002749hterm.Terminal.prototype.restyleCursor_ = function() {
2750 var shape = this.cursorShape_;
2751
2752 if (this.cursorNode_.getAttribute('focus') == 'false') {
2753 // Always show a block cursor when unfocused.
2754 shape = hterm.Terminal.cursorShape.BLOCK;
2755 }
2756
2757 var style = this.cursorNode_.style;
2758
2759 switch (shape) {
2760 case hterm.Terminal.cursorShape.BEAM:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002761 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002762 style.backgroundColor = 'transparent';
2763 style.borderBottomStyle = null;
2764 style.borderLeftStyle = 'solid';
2765 break;
2766
2767 case hterm.Terminal.cursorShape.UNDERLINE:
2768 style.height = this.scrollPort_.characterSize.baseline + 'px';
2769 style.backgroundColor = 'transparent';
2770 style.borderBottomStyle = 'solid';
2771 // correct the size to put it exactly at the baseline
2772 style.borderLeftStyle = null;
2773 break;
2774
2775 default:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002776 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002777 style.backgroundColor = this.cursorColor_;
2778 style.borderBottomStyle = null;
2779 style.borderLeftStyle = null;
2780 break;
2781 }
2782};
2783
rginda8ba33642011-12-14 12:31:31 -08002784/**
2785 * Synchronizes the visible cursor with the current cursor coordinates.
2786 *
2787 * The sync will happen asynchronously, soon after the call stack winds down.
2788 * Multiple calls will be coalesced into a single sync.
2789 */
2790hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2791 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002792 return;
rginda8ba33642011-12-14 12:31:31 -08002793
2794 var self = this;
2795 this.timeouts_.syncCursor = setTimeout(function() {
2796 self.syncCursorPosition_();
2797 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002798 }, 0);
2799};
2800
rgindacc2996c2012-02-24 14:59:31 -08002801/**
rgindaf522ce02012-04-17 17:49:17 -07002802 * Show or hide the zoom warning.
2803 *
2804 * The zoom warning is a message warning the user that their browser zoom must
2805 * be set to 100% in order for hterm to function properly.
2806 *
2807 * @param {boolean} state True to show the message, false to hide it.
2808 */
2809hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2810 if (!this.zoomWarningNode_) {
2811 if (!state)
2812 return;
2813
2814 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002815 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07002816 this.zoomWarningNode_.style.cssText = (
2817 'color: black;' +
2818 'background-color: #ff2222;' +
2819 'font-size: large;' +
2820 'border-radius: 8px;' +
2821 'opacity: 0.75;' +
2822 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2823 'top: 0.5em;' +
2824 'right: 1.2em;' +
2825 'position: absolute;' +
2826 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002827 '-webkit-user-select: none;' +
2828 '-moz-text-size-adjust: none;' +
2829 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002830
2831 this.zoomWarningNode_.addEventListener('click', function(e) {
2832 this.parentNode.removeChild(this);
2833 });
rgindaf522ce02012-04-17 17:49:17 -07002834 }
2835
Robert Gindab4839c22013-02-28 16:52:10 -08002836 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2837 hterm.zoomWarningMessage,
2838 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2839
rgindaf522ce02012-04-17 17:49:17 -07002840 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2841
2842 if (state) {
2843 if (!this.zoomWarningNode_.parentNode)
2844 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2845 } else if (this.zoomWarningNode_.parentNode) {
2846 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2847 }
2848};
2849
2850/**
rgindacc2996c2012-02-24 14:59:31 -08002851 * Show the terminal overlay for a given amount of time.
2852 *
2853 * The terminal overlay appears in inverse video in a large font, centered
2854 * over the terminal. You should probably keep the overlay message brief,
2855 * since it's in a large font and you probably aren't going to check the size
2856 * of the terminal first.
2857 *
2858 * @param {string} msg The text (not HTML) message to display in the overlay.
2859 * @param {number} opt_timeout The amount of time to wait before fading out
2860 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2861 * stay up forever (or until the next overlay).
2862 */
2863hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002864 if (!this.overlayNode_) {
2865 if (!this.div_)
2866 return;
2867
2868 this.overlayNode_ = this.document_.createElement('div');
2869 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002870 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002871 'font-size: xx-large;' +
2872 'opacity: 0.75;' +
2873 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2874 'position: absolute;' +
2875 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002876 '-webkit-transition: opacity 180ms ease-in;' +
2877 '-moz-user-select: none;' +
2878 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002879
2880 this.overlayNode_.addEventListener('mousedown', function(e) {
2881 e.preventDefault();
2882 e.stopPropagation();
2883 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002884 }
2885
rginda9f5222b2012-03-05 11:53:28 -08002886 this.overlayNode_.style.color = this.prefs_.get('background-color');
2887 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2888 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2889
rgindaf0090c92012-02-10 14:58:52 -08002890 this.overlayNode_.textContent = msg;
2891 this.overlayNode_.style.opacity = '0.75';
2892
2893 if (!this.overlayNode_.parentNode)
2894 this.div_.appendChild(this.overlayNode_);
2895
Robert Ginda97769282013-02-01 15:30:30 -08002896 var divSize = hterm.getClientSize(this.div_);
2897 var overlaySize = hterm.getClientSize(this.overlayNode_);
2898
Robert Ginda8a59f762014-07-23 11:29:55 -07002899 this.overlayNode_.style.top =
2900 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08002901 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07002902 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08002903
rgindaf0090c92012-02-10 14:58:52 -08002904 if (this.overlayTimeout_)
2905 clearTimeout(this.overlayTimeout_);
2906
rgindacc2996c2012-02-24 14:59:31 -08002907 if (opt_timeout === null)
2908 return;
2909
Mike Frysingerb6cfded2017-09-18 00:39:31 -04002910 this.overlayTimeout_ = setTimeout(() => {
2911 this.overlayNode_.style.opacity = '0';
2912 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
2913 }, opt_timeout || 1500);
2914};
2915
2916/**
2917 * Hide the terminal overlay immediately.
2918 *
2919 * Useful when we show an overlay for an event with an unknown end time.
2920 */
2921hterm.Terminal.prototype.hideOverlay = function() {
2922 if (this.overlayTimeout_)
2923 clearTimeout(this.overlayTimeout_);
2924 this.overlayTimeout_ = null;
2925
2926 if (this.overlayNode_.parentNode)
2927 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
2928 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08002929};
2930
rginda4bba5e12012-06-20 16:15:30 -07002931/**
2932 * Paste from the system clipboard to the terminal.
2933 */
2934hterm.Terminal.prototype.paste = function() {
Mike Frysinger4628ad22017-07-20 02:44:20 -04002935 return hterm.pasteFromClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002936};
2937
2938/**
2939 * Copy a string to the system clipboard.
2940 *
2941 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05002942 *
2943 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07002944 */
2945hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002946 if (this.prefs_.get('enable-clipboard-notice'))
2947 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2948
rgindaa09e7332012-08-17 12:49:51 -07002949 var copySource = this.document_.createElement('pre');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002950 copySource.id = 'hterm:copy-to-clipboard-source';
rginda4bba5e12012-06-20 16:15:30 -07002951 copySource.textContent = str;
2952 copySource.style.cssText = (
2953 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002954 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07002955 'position: absolute;' +
2956 'top: -99px');
2957
2958 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002959
rginda4bba5e12012-06-20 16:15:30 -07002960 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002961 var anchorNode = selection.anchorNode;
2962 var anchorOffset = selection.anchorOffset;
2963 var focusNode = selection.focusNode;
2964 var focusOffset = selection.focusOffset;
2965
rginda4bba5e12012-06-20 16:15:30 -07002966 selection.selectAllChildren(copySource);
2967
rgindaa09e7332012-08-17 12:49:51 -07002968 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002969
Rob Spies56953412014-04-28 14:09:47 -07002970 // IE doesn't support selection.extend. This means that the selection
2971 // won't return on IE.
Robert Ginda6f4f0aa2014-07-28 10:21:39 -07002972 if (selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07002973 selection.collapse(anchorNode, anchorOffset);
2974 selection.extend(focusNode, focusOffset);
2975 }
rgindafaa74742012-08-21 13:34:03 -07002976
rginda4bba5e12012-06-20 16:15:30 -07002977 copySource.parentNode.removeChild(copySource);
2978};
2979
Evan Jones2600d4f2016-12-06 09:29:36 -05002980/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04002981 * Display an image.
2982 *
2983 * @param {Object} options The image to display.
2984 * @param {string=} options.name A human readable string for the image.
2985 * @param {string|number=} options.size The size (in bytes).
2986 * @param {boolean=} options.preserveAspectRatio Whether to preserve aspect.
2987 * @param {boolean=} options.inline Whether to display the image inline.
2988 * @param {string|number=} options.width The width of the image.
2989 * @param {string|number=} options.height The height of the image.
2990 * @param {string=} options.align Direction to align the image.
2991 * @param {string} options.uri The source URI for the image.
2992 */
2993hterm.Terminal.prototype.displayImage = function(options) {
2994 // Make sure we're actually given a resource to display.
2995 if (options.uri === undefined)
2996 return;
2997
2998 // Set up the defaults to simplify code below.
2999 if (!options.name)
3000 options.name = '';
3001
3002 // Has the user approved image display yet?
3003 if (this.allowImagesInline !== true) {
3004 this.newLine();
3005 const row = this.getRowNode(this.scrollbackRows_.length +
3006 this.getCursorRow() - 1);
3007
3008 if (this.allowImagesInline === false) {
3009 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
3010 'Inline Images Disabled');
3011 return;
3012 }
3013
3014 // Show a prompt.
3015 let button;
3016 const span = this.document_.createElement('span');
3017 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3018 span.style.fontWeight = 'bold';
3019 span.style.borderWidth = '1px';
3020 span.style.borderStyle = 'dashed';
3021 button = this.document_.createElement('span');
3022 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3023 button.style.marginLeft = '1em';
3024 button.style.borderWidth = '1px';
3025 button.style.borderStyle = 'solid';
3026 button.addEventListener('click', () => {
3027 this.prefs_.set('allow-images-inline', false);
3028 });
3029 span.appendChild(button);
3030 button = this.document_.createElement('span');
3031 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3032 'allow this session');
3033 button.style.marginLeft = '1em';
3034 button.style.borderWidth = '1px';
3035 button.style.borderStyle = 'solid';
3036 button.addEventListener('click', () => {
3037 this.allowImagesInline = true;
3038 });
3039 span.appendChild(button);
3040 button = this.document_.createElement('span');
3041 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3042 button.style.marginLeft = '1em';
3043 button.style.borderWidth = '1px';
3044 button.style.borderStyle = 'solid';
3045 button.addEventListener('click', () => {
3046 this.prefs_.set('allow-images-inline', true);
3047 });
3048 span.appendChild(button);
3049
3050 row.appendChild(span);
3051 return;
3052 }
3053
3054 // See if we should show this object directly, or download it.
3055 if (options.inline) {
3056 const io = this.io.push();
3057 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
3058 'Loading $1 ...'), null);
3059
3060 // While we're loading the image, eat all the user's input.
3061 io.onVTKeystroke = io.sendString = () => {};
3062
3063 // Initialize this new image.
3064 const img = this.document_.createElement('img');
3065 img.src = options.uri;
3066 img.title = img.alt = options.name;
3067
3068 // Attach the image to the page to let it load/render. It won't stay here.
3069 // This is needed so it's visible and the DOM can calculate the height. If
3070 // the image is hidden or not in the DOM, the height is always 0.
3071 this.document_.body.appendChild(img);
3072
3073 // Wait for the image to finish loading before we try moving it to the
3074 // right place in the terminal.
3075 img.onload = () => {
3076 // Now that we have the image dimensions, figure out how to show it.
3077 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
3078 img.style.maxWidth = `${this.document_.body.clientWidth}px`;
3079 img.style.maxHeight = `${this.document_.body.clientHeight}px`;
3080
3081 // Parse a width/height specification.
3082 const parseDim = (dim, maxDim, cssVar) => {
3083 if (!dim || dim == 'auto')
3084 return '';
3085
3086 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3087 if (ary) {
3088 if (ary[2] == '%')
3089 return maxDim * parseInt(ary[1]) / 100 + 'px';
3090 else if (ary[2] == 'px')
3091 return dim;
3092 else
3093 return `calc(${dim} * var(${cssVar}))`;
3094 }
3095
3096 return '';
3097 };
3098 img.style.width =
3099 parseDim(options.width, this.document_.body.clientWidth,
3100 '--hterm-charsize-width');
3101 img.style.height =
3102 parseDim(options.height, this.document_.body.clientHeight,
3103 '--hterm-charsize-height');
3104
3105 // Figure out how many rows the image occupies, then add that many.
3106 // XXX: This count will be inaccurate if the font size changes on us.
3107 const padRows = Math.ceil(img.clientHeight /
3108 this.scrollPort_.characterSize.height);
3109 for (let i = 0; i < padRows; ++i)
3110 this.newLine();
3111
3112 // Update the max height in case the user shrinks the character size.
3113 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3114
3115 // Move the image to the last row. This way when we scroll up, it doesn't
3116 // disappear when the first row gets clipped. It will disappear when we
3117 // scroll down and the last row is clipped ...
3118 this.document_.body.removeChild(img);
3119 // Create a wrapper node so we can do an absolute in a relative position.
3120 // This helps with rounding errors between JS & CSS counts.
3121 const div = this.document_.createElement('div');
3122 div.style.position = 'relative';
3123 div.style.textAlign = options.align;
3124 img.style.position = 'absolute';
3125 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3126 div.appendChild(img);
3127 const row = this.getRowNode(this.scrollbackRows_.length +
3128 this.getCursorRow() - 1);
3129 row.appendChild(div);
3130
3131 io.hideOverlay();
3132 io.pop();
3133 };
3134
3135 // If we got a malformed image, give up.
3136 img.onerror = (e) => {
3137 this.document_.body.removeChild(img);
3138 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
3139 'Loading $1 failed ...'));
3140 io.pop();
3141 };
3142 } else {
3143 // We can't use chrome.downloads.download as that requires "downloads"
3144 // permissions, and that works only in extensions, not apps.
3145 const a = this.document_.createElement('a');
3146 a.href = options.uri;
3147 a.download = options.name;
3148 this.document_.body.appendChild(a);
3149 a.click();
3150 a.remove();
3151 }
3152};
3153
3154/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003155 * Returns the selected text, or null if no text is selected.
3156 *
3157 * @return {string|null}
3158 */
rgindaa09e7332012-08-17 12:49:51 -07003159hterm.Terminal.prototype.getSelectionText = function() {
3160 var selection = this.scrollPort_.selection;
3161 selection.sync();
3162
3163 if (selection.isCollapsed)
3164 return null;
3165
3166
3167 // Start offset measures from the beginning of the line.
3168 var startOffset = selection.startOffset;
3169 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003170
Robert Gindafdbb3f22012-09-06 20:23:06 -07003171 if (node.nodeName != 'X-ROW') {
3172 // If the selection doesn't start on an x-row node, then it must be
3173 // somewhere inside the x-row. Add any characters from previous siblings
3174 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003175
3176 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3177 // If node is the text node in a styled span, move up to the span node.
3178 node = node.parentNode;
3179 }
3180
Robert Gindafdbb3f22012-09-06 20:23:06 -07003181 while (node.previousSibling) {
3182 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003183 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003184 }
rgindaa09e7332012-08-17 12:49:51 -07003185 }
3186
3187 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08003188 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
3189 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003190 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003191
Robert Gindafdbb3f22012-09-06 20:23:06 -07003192 if (node.nodeName != 'X-ROW') {
3193 // If the selection doesn't end on an x-row node, then it must be
3194 // somewhere inside the x-row. Add any characters from following siblings
3195 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003196
3197 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3198 // If node is the text node in a styled span, move up to the span node.
3199 node = node.parentNode;
3200 }
3201
Robert Gindafdbb3f22012-09-06 20:23:06 -07003202 while (node.nextSibling) {
3203 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003204 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003205 }
rgindaa09e7332012-08-17 12:49:51 -07003206 }
3207
3208 var rv = this.getRowsText(selection.startRow.rowIndex,
3209 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003210 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003211};
3212
rginda4bba5e12012-06-20 16:15:30 -07003213/**
3214 * Copy the current selection to the system clipboard, then clear it after a
3215 * short delay.
3216 */
3217hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07003218 var text = this.getSelectionText();
3219 if (text != null)
3220 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07003221};
3222
rgindaf0090c92012-02-10 14:58:52 -08003223hterm.Terminal.prototype.overlaySize = function() {
3224 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
3225};
3226
rginda87b86462011-12-14 13:48:03 -08003227/**
3228 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3229 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003230 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003231 */
3232hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08003233 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08003234 this.scrollPort_.scrollRowToBottom(this.getRowCount());
3235
Robert Ginda8cb7d902013-06-20 14:37:18 -07003236 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08003237};
3238
3239/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003240 * Open the selected url.
3241 */
3242hterm.Terminal.prototype.openSelectedUrl_ = function() {
3243 var str = this.getSelectionText();
3244
3245 // If there is no selection, try and expand wherever they clicked.
3246 if (str == null) {
John Lincae9b732018-03-08 13:56:35 +08003247 this.screen_.expandSelectionForUrl(this.document_.getSelection());
Mike Frysinger70b94692017-01-26 18:57:50 -10003248 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003249
3250 // If clicking in empty space, return.
3251 if (str == null)
3252 return;
Mike Frysinger70b94692017-01-26 18:57:50 -10003253 }
3254
3255 // Make sure URL is valid before opening.
3256 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
3257 return;
Mike Frysinger43472622017-06-26 18:11:07 -04003258
3259 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003260 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003261 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3262 // We have to whitelist a few protocols that lack authorities and thus
3263 // never use the //. Like mailto.
3264 switch (str.split(':', 1)[0]) {
3265 case 'mailto':
3266 break;
3267 default:
3268 str = 'http://' + str;
3269 break;
3270 }
3271 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003272
Mike Frysinger720fa832017-10-23 01:15:52 -04003273 hterm.openUrl(str);
Mike Frysinger70b94692017-01-26 18:57:50 -10003274}
3275
3276
3277/**
rgindad5613292012-06-19 15:40:37 -07003278 * Add the terminalRow and terminalColumn properties to mouse events and
3279 * then forward on to onMouse().
3280 *
3281 * The terminalRow and terminalColumn properties contain the (row, column)
3282 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003283 *
3284 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003285 */
3286hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003287 if (e.processedByTerminalHandler_) {
3288 // We register our event handlers on the document, as well as the cursor
3289 // and the scroll blocker. Mouse events that occur on the cursor or
3290 // scroll blocker will also appear on the document, but we don't want to
3291 // process them twice.
3292 //
3293 // We can't just prevent bubbling because that has other side effects, so
3294 // we decorate the event object with this property instead.
3295 return;
3296 }
3297
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003298 var reportMouseEvents = (!this.defeatMouseReports_ &&
3299 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3300
rgindafaa74742012-08-21 13:34:03 -07003301 e.processedByTerminalHandler_ = true;
3302
Robert Gindaeda48db2014-07-17 09:25:30 -07003303 // One based row/column stored on the mouse event.
3304 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
3305 this.scrollPort_.characterSize.height) + 1;
3306 e.terminalColumn = parseInt(e.clientX /
3307 this.scrollPort_.characterSize.width) + 1;
3308
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003309 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3310 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003311 return;
3312 }
3313
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003314 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003315 // If the cursor is visible and we're not sending mouse events to the
3316 // host app, then we want to hide the terminal cursor when the mouse
3317 // cursor is over top. This keeps the terminal cursor from interfering
3318 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003319 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3320 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3321 this.cursorNode_.style.display = 'none';
3322 } else if (this.cursorNode_.style.display == 'none') {
3323 this.cursorNode_.style.display = '';
3324 }
3325 }
rgindad5613292012-06-19 15:40:37 -07003326
Robert Ginda928cf632014-03-05 15:07:41 -08003327 if (e.type == 'mousedown') {
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003328 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003329 // If VT mouse reporting is disabled, or has been defeated with
3330 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003331 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003332 this.setSelectionEnabled(true);
3333 } else {
3334 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003335 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003336 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003337 this.setSelectionEnabled(false);
3338 e.preventDefault();
3339 }
3340 }
3341
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003342 if (!reportMouseEvents) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003343 if (e.type == 'dblclick' && this.copyOnSelect) {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003344 this.screen_.expandSelection(this.document_.getSelection());
Robert Ginda15ed4902016-07-12 10:43:22 -07003345 this.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07003346 }
3347
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003348 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003349 // Debounce this event with the dblclick event. If you try to doubleclick
3350 // a URL to open it, Chrome will fire click then dblclick, but we won't
3351 // have expanded the selection text at the first click event.
3352 clearTimeout(this.timeouts_.openUrl);
3353 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3354 500);
3355 return;
3356 }
3357
Mike Frysinger847577f2017-05-23 23:25:57 -04003358 if (e.type == 'mousedown') {
3359 if ((this.mouseRightClickPaste && e.button == 2 /* right button */) ||
Mike Frysinger2edd3612017-05-24 00:54:39 -04003360 e.button == this.mousePasteButton) {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003361 if (!this.paste())
Mike Frysinger05a57f02017-08-27 17:48:55 -04003362 console.warn('Could not paste manually due to web restrictions');
Mike Frysinger847577f2017-05-23 23:25:57 -04003363 }
3364 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003365
Mike Frysinger2edd3612017-05-24 00:54:39 -04003366 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003367 !this.document_.getSelection().isCollapsed) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003368 this.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003369 }
3370
3371 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3372 this.scrollBlockerNode_.engaged) {
3373 // Disengage the scroll-blocker after one of these events.
3374 this.scrollBlockerNode_.engaged = false;
3375 this.scrollBlockerNode_.style.top = '-99px';
3376 }
3377
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003378 // Emulate arrow key presses via scroll wheel events.
3379 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3380 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003381 if (e.type == 'wheel') {
3382 var delta = this.scrollPort_.scrollWheelDelta(e);
3383 var lines = lib.f.smartFloorDivide(
3384 Math.abs(delta), this.scrollPort_.characterSize.height);
3385
3386 var data = '\x1bO' + (delta < 0 ? 'B' : 'A');
3387 this.io.sendString(data.repeat(lines));
3388
3389 e.preventDefault();
3390 }
3391 }
Robert Ginda928cf632014-03-05 15:07:41 -08003392 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003393 if (!this.scrollBlockerNode_.engaged) {
3394 if (e.type == 'mousedown') {
3395 // Move the scroll-blocker into place if we want to keep the scrollport
3396 // from scrolling.
3397 this.scrollBlockerNode_.engaged = true;
3398 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3399 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3400 } else if (e.type == 'mousemove') {
3401 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3402 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003403 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003404 e.preventDefault();
3405 }
3406 }
Robert Ginda928cf632014-03-05 15:07:41 -08003407
3408 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003409 }
3410
Robert Ginda928cf632014-03-05 15:07:41 -08003411 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3412 // Restore this on mouseup in case it was temporarily defeated with a
3413 // alt-mousedown. Only do this when the selection is empty so that
3414 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003415 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003416 }
rgindad5613292012-06-19 15:40:37 -07003417};
3418
3419/**
3420 * Clients should override this if they care to know about mouse events.
3421 *
3422 * The event parameter will be a normal DOM mouse click event with additional
3423 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003424 *
3425 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003426 */
3427hterm.Terminal.prototype.onMouse = function(e) { };
3428
3429/**
rginda8e92a692012-05-20 19:37:20 -07003430 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003431 *
3432 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003433 */
Rob Spies06533ba2014-04-24 11:20:37 -07003434hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3435 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003436 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003437
3438 if (this.reportFocus) {
3439 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O')
3440 }
3441
Michael Kelly485ecd12014-06-09 11:41:56 -04003442 if (focused === true)
3443 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003444};
3445
3446/**
rginda8ba33642011-12-14 12:31:31 -08003447 * React when the ScrollPort is scrolled.
3448 */
3449hterm.Terminal.prototype.onScroll_ = function() {
3450 this.scheduleSyncCursorPosition_();
3451};
3452
3453/**
rginda9846e2f2012-01-27 13:53:33 -08003454 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003455 *
3456 * @param {Event} e The DOM paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003457 */
3458hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07003459 var data = e.text.replace(/\n/mg, '\r');
Connor Hegartyf525ceb2014-09-03 13:36:03 -07003460 data = this.keyboard.encode(data);
Mike Frysingere8c32c82018-03-11 14:57:28 -07003461 if (this.options_.bracketedPaste) {
3462 // We strip out most escape sequences as they can cause issues (like
3463 // inserting an \x1b[201~ midstream). We pass through whitespace
3464 // though: 0x08:\b 0x09:\t 0x0a:\n 0x0d:\r.
3465 // This matches xterm behavior.
3466 const filter = (data) => data.replace(/[\x00-\x07\x0b-\x0c\x0e-\x1f]/g, '');
3467 data = '\x1b[200~' + filter(data) + '\x1b[201~';
3468 }
Robert Gindaa063b202014-07-21 11:08:25 -07003469
3470 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003471};
3472
3473/**
rgindaa09e7332012-08-17 12:49:51 -07003474 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003475 *
3476 * @param {Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003477 */
3478hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003479 if (!this.useDefaultWindowCopy) {
3480 e.preventDefault();
3481 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3482 }
rgindaa09e7332012-08-17 12:49:51 -07003483};
3484
3485/**
rginda8ba33642011-12-14 12:31:31 -08003486 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003487 *
3488 * Note: This function should not directly contain code that alters the internal
3489 * state of the terminal. That kind of code belongs in realizeWidth or
3490 * realizeHeight, so that it can be executed synchronously in the case of a
3491 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003492 */
3493hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003494 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003495 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003496 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003497 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003498
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003499 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003500 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003501 // gets removed from the document or during the initial load, and we can't
3502 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003503 // This can also happen if called before the scrollPort calculates the
3504 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003505 return;
3506 }
3507
rgindaa8ba17d2012-08-15 14:41:10 -07003508 var isNewSize = (columnCount != this.screenSize.width ||
3509 rowCount != this.screenSize.height);
3510
3511 // We do this even if the size didn't change, just to be sure everything is
3512 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003513 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003514 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003515
3516 if (isNewSize)
3517 this.overlaySize();
3518
Robert Gindafb1be6a2013-12-11 11:56:22 -08003519 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003520 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08003521};
3522
3523/**
3524 * Service the cursor blink timeout.
3525 */
3526hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003527 if (!this.options_.cursorBlink) {
3528 delete this.timeouts_.cursorBlink;
3529 return;
3530 }
3531
Robert Ginda830583c2013-08-07 13:20:46 -07003532 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3533 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003534 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003535 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3536 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003537 } else {
rginda87b86462011-12-14 13:48:03 -08003538 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003539 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3540 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003541 }
3542};
David Reveman8f552492012-03-28 12:18:41 -04003543
3544/**
3545 * Set the scrollbar-visible mode bit.
3546 *
3547 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3548 * Otherwise it will not.
3549 *
3550 * Defaults to on.
3551 *
3552 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3553 */
3554hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3555 this.scrollPort_.setScrollbarVisible(state);
3556};
Michael Kelly485ecd12014-06-09 11:41:56 -04003557
3558/**
Rob Spies49039e52014-12-17 13:40:04 -08003559 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003560 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003561 *
3562 * Defaults to 1.
3563 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003564 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003565 */
3566hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3567 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3568};
3569
3570/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003571 * Close all web notifications created by terminal bells.
3572 */
3573hterm.Terminal.prototype.closeBellNotifications_ = function() {
3574 this.bellNotificationList_.forEach(function(n) {
3575 n.close();
3576 });
3577 this.bellNotificationList_.length = 0;
3578};