blob: af3a70bc24298ca1368514021e1cd7150d9ebdf8 [file] [log] [blame]
rginda87b86462011-12-14 13:48:03 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rginda8ba33642011-12-14 12:31:31 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rgindacbbd7482012-06-13 15:06:16 -07005'use strict';
6
Masaya Suzuki273aa982014-05-31 07:25:55 +09007lib.rtdep('lib.colors', 'lib.PreferenceManager', 'lib.resource', 'lib.wc',
Rob Spiesf4e90e82015-01-28 12:10:13 -08008 'lib.f', 'hterm.Keyboard', 'hterm.Options', 'hterm.PreferenceManager',
Ricky Liang48f05cb2013-12-31 23:35:29 +08009 'hterm.Screen', 'hterm.ScrollPort', 'hterm.Size',
10 'hterm.TextAttributes', 'hterm.VT');
rgindacbbd7482012-06-13 15:06:16 -070011
rginda8ba33642011-12-14 12:31:31 -080012/**
13 * Constructor for the Terminal class.
14 *
15 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
16 * classes to provide the complete terminal functionality.
17 *
18 * There are a number of lower-level Terminal methods that can be called
19 * directly to manipulate the cursor, text, scroll region, and other terminal
20 * attributes. However, the primary method is interpret(), which parses VT
21 * escape sequences and invokes the appropriate Terminal methods.
22 *
23 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
24 *
25 * TODO(rginda): Eventually we're going to need to support characters which are
26 * displayed twice as wide as standard latin characters. This is to support
27 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080028 *
Robert Ginda57f03b42012-09-13 11:02:48 -070029 * @param {string} opt_profileId Optional preference profile name. If not
rginda9f5222b2012-03-05 11:53:28 -080030 * provided, defaults to 'default'.
rginda8ba33642011-12-14 12:31:31 -080031 */
Robert Ginda57f03b42012-09-13 11:02:48 -070032hterm.Terminal = function(opt_profileId) {
33 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080034
rginda8ba33642011-12-14 12:31:31 -080035 // Two screen instances.
36 this.primaryScreen_ = new hterm.Screen();
37 this.alternateScreen_ = new hterm.Screen();
38
39 // The "current" screen.
40 this.screen_ = this.primaryScreen_;
41
rginda8ba33642011-12-14 12:31:31 -080042 // The local notion of the screen size. ScreenBuffers also have a size which
43 // indicates their present size. During size changes, the two may disagree.
44 // Also, the inactive screen's size is not altered until it is made the active
45 // screen.
46 this.screenSize = new hterm.Size(0, 0);
47
rginda8ba33642011-12-14 12:31:31 -080048 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080049 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080050 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
51 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080052 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070053 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080054
rginda87b86462011-12-14 13:48:03 -080055 // The div that contains this terminal.
56 this.div_ = null;
57
rgindac9bc5502012-01-18 11:48:44 -080058 // The document that contains the scrollPort. Defaulted to the global
59 // document here so that the terminal is functional even if it hasn't been
60 // inserted into a document yet, but re-set in decorate().
61 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080062
rginda8ba33642011-12-14 12:31:31 -080063 // The rows that have scrolled off screen and are no longer addressable.
64 this.scrollbackRows_ = [];
65
rgindac9bc5502012-01-18 11:48:44 -080066 // Saved tab stops.
67 this.tabStops_ = [];
68
David Benjamin66e954d2012-05-05 21:08:12 -040069 // Keep track of whether default tab stops have been erased; after a TBC
70 // clears all tab stops, defaults aren't restored on resize until a reset.
71 this.defaultTabStops = true;
72
rginda8ba33642011-12-14 12:31:31 -080073 // The VT's notion of the top and bottom rows. Used during some VT
74 // cursor positioning and scrolling commands.
75 this.vtScrollTop_ = null;
76 this.vtScrollBottom_ = null;
77
78 // The DIV element for the visible cursor.
79 this.cursorNode_ = null;
80
Robert Ginda830583c2013-08-07 13:20:46 -070081 // The current cursor shape of the terminal.
82 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
83
84 // The current color of the cursor.
85 this.cursorColor_ = null;
86
Robert Gindaea2183e2014-07-17 09:51:51 -070087 // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded.
88 this.cursorBlinkCycle_ = [100, 100];
89
90 // Pre-bound onCursorBlink_ handler, so we don't have to do this for each
91 // cursor on/off servicing.
92 this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
93
rginda9f5222b2012-03-05 11:53:28 -080094 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070095 // each output and keystroke. They are initialized by the preference manager.
Robert Ginda8cb7d902013-06-20 14:37:18 -070096 this.backgroundColor_ = null;
97 this.foregroundColor_ = null;
Robert Ginda57f03b42012-09-13 11:02:48 -070098 this.scrollOnOutput_ = null;
99 this.scrollOnKeystroke_ = null;
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400100 this.scrollWheelArrowKeys_ = null;
rginda9f5222b2012-03-05 11:53:28 -0800101
Robert Ginda6aec7eb2015-06-16 10:31:30 -0700102 // True if we should override mouse event reporting to allow local selection.
103 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -0800104
rgindaf0090c92012-02-10 14:58:52 -0800105 // Terminal bell sound.
106 this.bellAudio_ = this.document_.createElement('audio');
Mike Frysingerd826f1a2017-07-06 16:20:06 -0400107 this.bellAudio_.id = 'hterm:bell-audio';
rgindaf0090c92012-02-10 14:58:52 -0800108 this.bellAudio_.setAttribute('preload', 'auto');
109
Michael Kelly485ecd12014-06-09 11:41:56 -0400110 // All terminal bell notifications that have been generated (not necessarily
111 // shown).
112 this.bellNotificationList_ = [];
113
114 // Whether we have permission to display notifications.
115 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400116
rginda6d397402012-01-17 10:58:29 -0800117 // Cursor position and attributes saved with DECSC.
118 this.savedOptions_ = {};
119
rginda8ba33642011-12-14 12:31:31 -0800120 // The current mode bits for the terminal.
121 this.options_ = new hterm.Options();
122
123 // Timeouts we might need to clear.
124 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800125
126 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800127 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800128
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800129 this.saveCursorAndState(true);
130
Zhu Qunying30d40712017-03-14 16:27:00 -0700131 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800132 this.keyboard = new hterm.Keyboard(this);
133
rginda87b86462011-12-14 13:48:03 -0800134 // General IO interface that can be given to third parties without exposing
135 // the entire terminal object.
136 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800137
rgindad5613292012-06-19 15:40:37 -0700138 // True if mouse-click-drag should scroll the terminal.
139 this.enableMouseDragScroll = true;
140
Robert Ginda57f03b42012-09-13 11:02:48 -0700141 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400142 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700143 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700144
Zhu Qunying30d40712017-03-14 16:27:00 -0700145 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700146 this.useDefaultWindowCopy = false;
147
148 this.clearSelectionAfterCopy = true;
149
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400150 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800151 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700152
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400153 // Whether we allow images to be shown.
154 this.allowImagesInline = null;
155
Gabriel Holodake8a09be2017-10-10 01:07:11 -0400156 this.reportFocus = false;
157
Robert Ginda57f03b42012-09-13 11:02:48 -0700158 this.setProfile(opt_profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500159 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800160};
161
162/**
Robert Ginda830583c2013-08-07 13:20:46 -0700163 * Possible cursor shapes.
164 */
165hterm.Terminal.cursorShape = {
166 BLOCK: 'BLOCK',
167 BEAM: 'BEAM',
168 UNDERLINE: 'UNDERLINE'
169};
170
171/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700172 * Clients should override this to be notified when the terminal is ready
173 * for use.
174 *
175 * The terminal initialization is asynchronous, and shouldn't be used before
176 * this method is called.
177 */
178hterm.Terminal.prototype.onTerminalReady = function() { };
179
180/**
rginda35c456b2012-02-09 17:29:05 -0800181 * Default tab with of 8 to match xterm.
182 */
183hterm.Terminal.prototype.tabWidth = 8;
184
185/**
rginda9f5222b2012-03-05 11:53:28 -0800186 * Select a preference profile.
187 *
188 * This will load the terminal preferences for the given profile name and
189 * associate subsequent preference changes with the new preference profile.
190 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500191 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800192 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700193 * @param {function} opt_callback Optional callback to invoke when the profile
194 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800195 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700196hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
197 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800198
Robert Ginda57f03b42012-09-13 11:02:48 -0700199 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800200
Robert Ginda57f03b42012-09-13 11:02:48 -0700201 if (this.prefs_)
202 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800203
Robert Ginda57f03b42012-09-13 11:02:48 -0700204 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
205 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800206 'alt-gr-mode': function(v) {
207 if (v == null) {
208 if (navigator.language.toLowerCase() == 'en-us') {
209 v = 'none';
210 } else {
211 v = 'right-alt';
212 }
213 } else if (typeof v == 'string') {
214 v = v.toLowerCase();
215 } else {
216 v = 'none';
217 }
218
219 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v))
220 v = 'none';
221
222 terminal.keyboard.altGrMode = v;
223 },
224
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700225 'alt-backspace-is-meta-backspace': function(v) {
226 terminal.keyboard.altBackspaceIsMetaBackspace = v;
227 },
228
Robert Ginda57f03b42012-09-13 11:02:48 -0700229 'alt-is-meta': function(v) {
230 terminal.keyboard.altIsMeta = v;
231 },
232
233 'alt-sends-what': function(v) {
234 if (!/^(escape|8-bit|browser-key)$/.test(v))
235 v = 'escape';
236
237 terminal.keyboard.altSendsWhat = v;
238 },
239
240 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800241 var ary = v.match(/^lib-resource:(\S+)/);
242 if (ary) {
243 terminal.bellAudio_.setAttribute('src',
244 lib.resource.getDataUrl(ary[1]));
245 } else {
246 terminal.bellAudio_.setAttribute('src', v);
247 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700248 },
249
Michael Kelly485ecd12014-06-09 11:41:56 -0400250 'desktop-notification-bell': function(v) {
251 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700252 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400253 Notification.permission === 'granted';
254 if (!terminal.desktopNotificationBell_) {
255 // Note: We don't call Notification.requestPermission here because
256 // Chrome requires the call be the result of a user action (such as an
257 // onclick handler), and pref listeners are run asynchronously.
258 //
259 // A way of working around this would be to display a dialog in the
260 // terminal with a "click-to-request-permission" button.
261 console.warn('desktop-notification-bell is true but we do not have ' +
262 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400263 }
264 } else {
265 terminal.desktopNotificationBell_ = false;
266 }
267 },
268
Robert Ginda57f03b42012-09-13 11:02:48 -0700269 'background-color': function(v) {
270 terminal.setBackgroundColor(v);
271 },
272
273 'background-image': function(v) {
274 terminal.scrollPort_.setBackgroundImage(v);
275 },
276
277 'background-size': function(v) {
278 terminal.scrollPort_.setBackgroundSize(v);
279 },
280
281 'background-position': function(v) {
282 terminal.scrollPort_.setBackgroundPosition(v);
283 },
284
285 'backspace-sends-backspace': function(v) {
286 terminal.keyboard.backspaceSendsBackspace = v;
287 },
288
Brad Town18654b62015-03-12 00:27:45 -0700289 'character-map-overrides': function(v) {
290 if (!(v == null || v instanceof Object)) {
291 console.warn('Preference character-map-modifications is not an ' +
292 'object: ' + v);
293 return;
294 }
295
Mike Frysinger095d4062017-06-14 00:29:48 -0700296 terminal.vt.characterMaps.reset();
297 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700298 },
299
Robert Ginda57f03b42012-09-13 11:02:48 -0700300 'cursor-blink': function(v) {
301 terminal.setCursorBlink(!!v);
302 },
303
Robert Gindaea2183e2014-07-17 09:51:51 -0700304 'cursor-blink-cycle': function(v) {
305 if (v instanceof Array &&
306 typeof v[0] == 'number' &&
307 typeof v[1] == 'number') {
308 terminal.cursorBlinkCycle_ = v;
309 } else if (typeof v == 'number') {
310 terminal.cursorBlinkCycle_ = [v, v];
311 } else {
312 // Fast blink indicates an error.
313 terminal.cursorBlinkCycle_ = [100, 100];
314 }
315 },
316
Robert Ginda57f03b42012-09-13 11:02:48 -0700317 'cursor-color': function(v) {
318 terminal.setCursorColor(v);
319 },
320
321 'color-palette-overrides': function(v) {
322 if (!(v == null || v instanceof Object || v instanceof Array)) {
323 console.warn('Preference color-palette-overrides is not an array or ' +
324 'object: ' + v);
325 return;
rginda9f5222b2012-03-05 11:53:28 -0800326 }
rginda9f5222b2012-03-05 11:53:28 -0800327
Robert Ginda57f03b42012-09-13 11:02:48 -0700328 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700329
Robert Ginda57f03b42012-09-13 11:02:48 -0700330 if (v) {
331 for (var key in v) {
332 var i = parseInt(key);
333 if (isNaN(i) || i < 0 || i > 255) {
334 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
335 continue;
336 }
337
338 if (v[i]) {
339 var rgb = lib.colors.normalizeCSS(v[i]);
340 if (rgb)
341 lib.colors.colorPalette[i] = rgb;
342 }
343 }
rginda30f20f62012-04-05 16:36:19 -0700344 }
rginda30f20f62012-04-05 16:36:19 -0700345
Evan Jones5f9df812016-12-06 09:38:58 -0500346 terminal.primaryScreen_.textAttributes.resetColorPalette();
Robert Ginda57f03b42012-09-13 11:02:48 -0700347 terminal.alternateScreen_.textAttributes.resetColorPalette();
348 },
rginda30f20f62012-04-05 16:36:19 -0700349
Robert Ginda57f03b42012-09-13 11:02:48 -0700350 'copy-on-select': function(v) {
351 terminal.copyOnSelect = !!v;
352 },
rginda9f5222b2012-03-05 11:53:28 -0800353
Rob Spies0bec09b2014-06-06 15:58:09 -0700354 'use-default-window-copy': function(v) {
355 terminal.useDefaultWindowCopy = !!v;
356 },
357
358 'clear-selection-after-copy': function(v) {
359 terminal.clearSelectionAfterCopy = !!v;
360 },
361
Robert Ginda7e5e9522014-03-14 12:23:58 -0700362 'ctrl-plus-minus-zero-zoom': function(v) {
363 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
364 },
365
Robert Gindafb5a3f92014-05-13 14:12:00 -0700366 'ctrl-c-copy': function(v) {
367 terminal.keyboard.ctrlCCopy = v;
368 },
369
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100370 'ctrl-v-paste': function(v) {
371 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700372 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100373 },
374
Masaya Suzuki273aa982014-05-31 07:25:55 +0900375 'east-asian-ambiguous-as-two-column': function(v) {
376 lib.wc.regardCjkAmbiguous = v;
377 },
378
Robert Ginda57f03b42012-09-13 11:02:48 -0700379 'enable-8-bit-control': function(v) {
380 terminal.vt.enable8BitControl = !!v;
381 },
rginda30f20f62012-04-05 16:36:19 -0700382
Robert Ginda57f03b42012-09-13 11:02:48 -0700383 'enable-bold': function(v) {
384 terminal.syncBoldSafeState();
385 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400386
Robert Ginda3e278d72014-03-25 13:18:51 -0700387 'enable-bold-as-bright': function(v) {
388 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
389 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
390 },
391
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400392 'enable-blink': function(v) {
393 terminal.syncBlinkState();
394 },
395
Robert Ginda57f03b42012-09-13 11:02:48 -0700396 'enable-clipboard-write': function(v) {
397 terminal.vt.enableClipboardWrite = !!v;
398 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400399
Robert Ginda3755e752013-05-31 13:34:09 -0700400 'enable-dec12': function(v) {
401 terminal.vt.enableDec12 = !!v;
402 },
403
Robert Ginda57f03b42012-09-13 11:02:48 -0700404 'font-family': function(v) {
405 terminal.syncFontFamily();
406 },
rginda30f20f62012-04-05 16:36:19 -0700407
Robert Ginda57f03b42012-09-13 11:02:48 -0700408 'font-size': function(v) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500409 v = parseInt(v);
410 if (v <= 0) {
411 console.error(`Invalid font size: ${v}`);
412 return;
413 }
414
Robert Ginda57f03b42012-09-13 11:02:48 -0700415 terminal.setFontSize(v);
416 },
rginda9875d902012-08-20 16:21:57 -0700417
Robert Ginda57f03b42012-09-13 11:02:48 -0700418 'font-smoothing': function(v) {
419 terminal.syncFontFamily();
420 },
rgindade84e382012-04-20 15:39:31 -0700421
Robert Ginda57f03b42012-09-13 11:02:48 -0700422 'foreground-color': function(v) {
423 terminal.setForegroundColor(v);
424 },
rginda30f20f62012-04-05 16:36:19 -0700425
Robert Ginda57f03b42012-09-13 11:02:48 -0700426 'home-keys-scroll': function(v) {
427 terminal.keyboard.homeKeysScroll = v;
428 },
rginda4bba5e12012-06-20 16:15:30 -0700429
Robert Gindaa8165692015-06-15 14:46:31 -0700430 'keybindings': function(v) {
431 terminal.keyboard.bindings.clear();
432
433 if (!v)
434 return;
435
436 if (!(v instanceof Object)) {
437 console.error('Error in keybindings preference: Expected object');
438 return;
439 }
440
441 try {
442 terminal.keyboard.bindings.addBindings(v);
443 } catch (ex) {
444 console.error('Error in keybindings preference: ' + ex);
445 }
446 },
447
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700448 'media-keys-are-fkeys': function(v) {
449 terminal.keyboard.mediaKeysAreFKeys = v;
450 },
451
Robert Ginda57f03b42012-09-13 11:02:48 -0700452 'meta-sends-escape': function(v) {
453 terminal.keyboard.metaSendsEscape = v;
454 },
rginda30f20f62012-04-05 16:36:19 -0700455
Mike Frysinger847577f2017-05-23 23:25:57 -0400456 'mouse-right-click-paste': function(v) {
457 terminal.mouseRightClickPaste = v;
458 },
459
Robert Ginda57f03b42012-09-13 11:02:48 -0700460 'mouse-paste-button': function(v) {
461 terminal.syncMousePasteButton();
462 },
rgindaa8ba17d2012-08-15 14:41:10 -0700463
Robert Gindae76aa9f2014-03-14 12:29:12 -0700464 'page-keys-scroll': function(v) {
465 terminal.keyboard.pageKeysScroll = v;
466 },
467
Robert Ginda40932892012-12-10 17:26:40 -0800468 'pass-alt-number': function(v) {
469 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800470 // Let Alt-1..9 pass to the browser (to control tab switching) on
471 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500472 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800473 }
474
475 terminal.passAltNumber = v;
476 },
477
478 'pass-ctrl-number': function(v) {
479 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800480 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
481 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500482 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800483 }
484
485 terminal.passCtrlNumber = v;
486 },
487
488 'pass-meta-number': function(v) {
489 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800490 // Let Meta-1..9 pass to the browser (to control tab switching) on
491 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500492 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800493 }
494
495 terminal.passMetaNumber = v;
496 },
497
Marius Schilder77857b32014-05-14 16:21:26 -0700498 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700499 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700500 },
501
Robert Ginda8cb7d902013-06-20 14:37:18 -0700502 'receive-encoding': function(v) {
503 if (!(/^(utf-8|raw)$/).test(v)) {
504 console.warn('Invalid value for "receive-encoding": ' + v);
505 v = 'utf-8';
506 }
507
508 terminal.vt.characterEncoding = v;
509 },
510
Robert Ginda57f03b42012-09-13 11:02:48 -0700511 'scroll-on-keystroke': function(v) {
512 terminal.scrollOnKeystroke_ = v;
513 },
rginda9f5222b2012-03-05 11:53:28 -0800514
Robert Ginda57f03b42012-09-13 11:02:48 -0700515 'scroll-on-output': function(v) {
516 terminal.scrollOnOutput_ = v;
517 },
rginda30f20f62012-04-05 16:36:19 -0700518
Robert Ginda57f03b42012-09-13 11:02:48 -0700519 'scrollbar-visible': function(v) {
520 terminal.setScrollbarVisible(v);
521 },
rginda9f5222b2012-03-05 11:53:28 -0800522
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400523 'scroll-wheel-may-send-arrow-keys': function(v) {
524 terminal.scrollWheelArrowKeys_ = v;
525 },
526
Rob Spies49039e52014-12-17 13:40:04 -0800527 'scroll-wheel-move-multiplier': function(v) {
528 terminal.setScrollWheelMoveMultipler(v);
529 },
530
Robert Ginda8cb7d902013-06-20 14:37:18 -0700531 'send-encoding': function(v) {
532 if (!(/^(utf-8|raw)$/).test(v)) {
533 console.warn('Invalid value for "send-encoding": ' + v);
534 v = 'utf-8';
535 }
536
537 terminal.keyboard.characterEncoding = v;
538 },
539
Robert Ginda57f03b42012-09-13 11:02:48 -0700540 'shift-insert-paste': function(v) {
541 terminal.keyboard.shiftInsertPaste = v;
542 },
rginda9f5222b2012-03-05 11:53:28 -0800543
Mike Frysingera7768922017-07-28 15:00:12 -0400544 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400545 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400546 },
547
Robert Gindae76aa9f2014-03-14 12:29:12 -0700548 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400549 terminal.scrollPort_.setUserCssUrl(v);
550 },
551
552 'user-css-text': function(v) {
553 terminal.scrollPort_.setUserCssText(v);
554 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400555
556 'word-break-match-left': function(v) {
557 terminal.primaryScreen_.wordBreakMatchLeft = v;
558 terminal.alternateScreen_.wordBreakMatchLeft = v;
559 },
560
561 'word-break-match-right': function(v) {
562 terminal.primaryScreen_.wordBreakMatchRight = v;
563 terminal.alternateScreen_.wordBreakMatchRight = v;
564 },
565
566 'word-break-match-middle': function(v) {
567 terminal.primaryScreen_.wordBreakMatchMiddle = v;
568 terminal.alternateScreen_.wordBreakMatchMiddle = v;
569 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400570
571 'allow-images-inline': function(v) {
572 terminal.allowImagesInline = v;
573 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700574 });
rginda30f20f62012-04-05 16:36:19 -0700575
Robert Ginda57f03b42012-09-13 11:02:48 -0700576 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800577 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700578
579 if (opt_callback)
580 opt_callback();
581 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800582};
583
Rob Spies56953412014-04-28 14:09:47 -0700584
585/**
586 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500587 *
588 * @return {hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700589 */
590hterm.Terminal.prototype.getPrefs = function() {
591 return this.prefs_;
592};
593
Robert Gindaa063b202014-07-21 11:08:25 -0700594/**
595 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500596 *
597 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700598 */
599hterm.Terminal.prototype.setBracketedPaste = function(state) {
600 this.options_.bracketedPaste = state;
601};
Rob Spies56953412014-04-28 14:09:47 -0700602
rginda8e92a692012-05-20 19:37:20 -0700603/**
604 * Set the color for the cursor.
605 *
606 * If you want this setting to persist, set it through prefs_, rather than
607 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500608 *
609 * @param {string} color The color to set.
rginda8e92a692012-05-20 19:37:20 -0700610 */
611hterm.Terminal.prototype.setCursorColor = function(color) {
Robert Ginda830583c2013-08-07 13:20:46 -0700612 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700613 this.cursorNode_.style.backgroundColor = color;
614 this.cursorNode_.style.borderColor = color;
615};
616
617/**
618 * Return the current cursor color as a string.
Evan Jones2600d4f2016-12-06 09:29:36 -0500619 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700620 */
621hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700622 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700623};
624
625/**
rgindad5613292012-06-19 15:40:37 -0700626 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500627 *
628 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700629 */
630hterm.Terminal.prototype.setSelectionEnabled = function(state) {
631 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700632};
633
634/**
rginda8e92a692012-05-20 19:37:20 -0700635 * Set the background color.
636 *
637 * If you want this setting to persist, set it through prefs_, rather than
638 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500639 *
640 * @param {string} color The color to set.
rginda8e92a692012-05-20 19:37:20 -0700641 */
642hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700643 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700644 this.primaryScreen_.textAttributes.setDefaults(
645 this.foregroundColor_, this.backgroundColor_);
646 this.alternateScreen_.textAttributes.setDefaults(
647 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700648 this.scrollPort_.setBackgroundColor(color);
649};
650
rginda9f5222b2012-03-05 11:53:28 -0800651/**
652 * Return the current terminal background color.
653 *
654 * Intended for use by other classes, so we don't have to expose the entire
655 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500656 *
657 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800658 */
659hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700660 return this.backgroundColor_;
661};
662
663/**
664 * Set the foreground color.
665 *
666 * If you want this setting to persist, set it through prefs_, rather than
667 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500668 *
669 * @param {string} color The color to set.
rginda8e92a692012-05-20 19:37:20 -0700670 */
671hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700672 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700673 this.primaryScreen_.textAttributes.setDefaults(
674 this.foregroundColor_, this.backgroundColor_);
675 this.alternateScreen_.textAttributes.setDefaults(
676 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700677 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800678};
679
680/**
681 * Return the current terminal foreground color.
682 *
683 * Intended for use by other classes, so we don't have to expose the entire
684 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500685 *
686 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800687 */
688hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700689 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800690};
691
692/**
rginda87b86462011-12-14 13:48:03 -0800693 * Create a new instance of a terminal command and run it with a given
694 * argument string.
695 *
696 * @param {function} commandClass The constructor for a terminal command.
697 * @param {string} argString The argument string to pass to the command.
698 */
699hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700700 var environment = this.prefs_.get('environment');
701 if (typeof environment != 'object' || environment == null)
702 environment = {};
703
rginda87b86462011-12-14 13:48:03 -0800704 var self = this;
705 this.command = new commandClass(
706 { argString: argString || '',
707 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700708 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800709 onExit: function(code) {
710 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800711 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700712 if (self.prefs_.get('close-on-exit'))
713 window.close();
rginda87b86462011-12-14 13:48:03 -0800714 }
715 });
716
rgindafeaf3142012-01-31 15:14:20 -0800717 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800718 this.command.run();
719};
720
721/**
rgindafeaf3142012-01-31 15:14:20 -0800722 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500723 *
724 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800725 */
726hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700727 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800728};
729
730/**
731 * Install the keyboard handler for this terminal.
732 *
733 * This will prevent the browser from seeing any keystrokes sent to the
734 * terminal.
735 */
736hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700737 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
rgindafeaf3142012-01-31 15:14:20 -0800738}
739
740/**
741 * Uninstall the keyboard handler for this terminal.
742 */
743hterm.Terminal.prototype.uninstallKeyboard = function() {
744 this.keyboard.installKeyboard(null);
745}
746
747/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400748 * Set a CSS variable.
749 *
750 * Normally this is used to set variables in the hterm namespace.
751 *
752 * @param {string} name The variable to set.
753 * @param {string} value The value to assign to the variable.
754 * @param {string?} opt_prefix The variable namespace/prefix to use.
755 */
756hterm.Terminal.prototype.setCssVar = function(name, value,
757 opt_prefix='--hterm-') {
758 this.document_.documentElement.style.setProperty(
759 `${opt_prefix}${name}`, value);
760};
761
762/**
rginda35c456b2012-02-09 17:29:05 -0800763 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800764 *
765 * Call setFontSize(0) to reset to the default font size.
766 *
767 * This function does not modify the font-size preference.
768 *
769 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800770 */
771hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500772 if (px <= 0)
rginda9f5222b2012-03-05 11:53:28 -0800773 px = this.prefs_.get('font-size');
774
rginda35c456b2012-02-09 17:29:05 -0800775 this.scrollPort_.setFontSize(px);
Mike Frysingercce97c42017-08-05 01:11:22 -0400776 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
777 this.setCssVar('charsize-height',
778 this.scrollPort_.characterSize.height + 'px');
rginda35c456b2012-02-09 17:29:05 -0800779};
780
781/**
782 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500783 *
784 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800785 */
786hterm.Terminal.prototype.getFontSize = function() {
787 return this.scrollPort_.getFontSize();
788};
789
790/**
rginda8e92a692012-05-20 19:37:20 -0700791 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500792 *
793 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700794 */
795hterm.Terminal.prototype.getFontFamily = function() {
796 return this.scrollPort_.getFontFamily();
797};
798
799/**
rginda35c456b2012-02-09 17:29:05 -0800800 * Set the CSS "font-family" for this terminal.
801 */
rginda9f5222b2012-03-05 11:53:28 -0800802hterm.Terminal.prototype.syncFontFamily = function() {
803 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
804 this.prefs_.get('font-smoothing'));
805 this.syncBoldSafeState();
806};
807
rginda4bba5e12012-06-20 16:15:30 -0700808/**
809 * Set this.mousePasteButton based on the mouse-paste-button pref,
810 * autodetecting if necessary.
811 */
812hterm.Terminal.prototype.syncMousePasteButton = function() {
813 var button = this.prefs_.get('mouse-paste-button');
814 if (typeof button == 'number') {
815 this.mousePasteButton = button;
816 return;
817 }
818
Mike Frysingeree81a002017-12-12 16:14:53 -0500819 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400820 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700821 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400822 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700823 }
824};
825
826/**
827 * Enable or disable bold based on the enable-bold pref, autodetecting if
828 * necessary.
829 */
rginda9f5222b2012-03-05 11:53:28 -0800830hterm.Terminal.prototype.syncBoldSafeState = function() {
831 var enableBold = this.prefs_.get('enable-bold');
832 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700833 this.primaryScreen_.textAttributes.enableBold = enableBold;
834 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800835 return;
836 }
837
rgindaf7521392012-02-28 17:20:34 -0800838 var normalSize = this.scrollPort_.measureCharacterSize();
839 var boldSize = this.scrollPort_.measureCharacterSize('bold');
840
841 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800842 if (!isBoldSafe) {
843 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700844 'from normal. Font family is: ' +
845 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800846 }
rginda9f5222b2012-03-05 11:53:28 -0800847
Robert Gindaed016262012-10-26 16:27:09 -0700848 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
849 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800850};
851
852/**
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400853 * Enable or disable blink based on the enable-blink pref.
854 */
855hterm.Terminal.prototype.syncBlinkState = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400856 this.setCssVar('node-duration',
857 this.prefs_.get('enable-blink') ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400858};
859
860/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400861 * Set the mouse cursor style based on the current terminal mode.
862 */
863hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400864 this.setCssVar('mouse-cursor-style',
865 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
866 'var(--hterm-mouse-cursor-text)' :
867 'var(--hterm-mouse-cursor-pointer)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400868};
869
870/**
rginda87b86462011-12-14 13:48:03 -0800871 * Return a copy of the current cursor position.
872 *
873 * @return {hterm.RowCol} The RowCol object representing the current position.
874 */
875hterm.Terminal.prototype.saveCursor = function() {
876 return this.screen_.cursorPosition.clone();
877};
878
Evan Jones2600d4f2016-12-06 09:29:36 -0500879/**
880 * Return the current text attributes.
881 *
882 * @return {string}
883 */
rgindaa19afe22012-01-25 15:40:22 -0800884hterm.Terminal.prototype.getTextAttributes = function() {
885 return this.screen_.textAttributes;
886};
887
Evan Jones2600d4f2016-12-06 09:29:36 -0500888/**
889 * Set the text attributes.
890 *
891 * @param {string} textAttributes The attributes to set.
892 */
rginda1a09aa02012-06-18 21:11:25 -0700893hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
894 this.screen_.textAttributes = textAttributes;
895};
896
rginda87b86462011-12-14 13:48:03 -0800897/**
rgindaf522ce02012-04-17 17:49:17 -0700898 * Return the current browser zoom factor applied to the terminal.
899 *
900 * @return {number} The current browser zoom factor.
901 */
902hterm.Terminal.prototype.getZoomFactor = function() {
903 return this.scrollPort_.characterSize.zoomFactor;
904};
905
906/**
rginda9846e2f2012-01-27 13:53:33 -0800907 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500908 *
909 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800910 */
911hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800912 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800913};
914
915/**
rginda87b86462011-12-14 13:48:03 -0800916 * Restore a previously saved cursor position.
917 *
918 * @param {hterm.RowCol} cursor The position to restore.
919 */
920hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700921 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
922 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800923 this.screen_.setCursorPosition(row, column);
924 if (cursor.column > column ||
925 cursor.column == column && cursor.overflow) {
926 this.screen_.cursorPosition.overflow = true;
927 }
rginda87b86462011-12-14 13:48:03 -0800928};
929
930/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400931 * Clear the cursor's overflow flag.
932 */
933hterm.Terminal.prototype.clearCursorOverflow = function() {
934 this.screen_.cursorPosition.overflow = false;
935};
936
937/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800938 * Save the current cursor state to the corresponding screens.
939 *
940 * See the hterm.Screen.CursorState class for more details.
941 *
942 * @param {boolean=} both If true, update both screens, else only update the
943 * current screen.
944 */
945hterm.Terminal.prototype.saveCursorAndState = function(both) {
946 if (both) {
947 this.primaryScreen_.saveCursorAndState(this.vt);
948 this.alternateScreen_.saveCursorAndState(this.vt);
949 } else
950 this.screen_.saveCursorAndState(this.vt);
951};
952
953/**
954 * Restore the saved cursor state in the corresponding screens.
955 *
956 * See the hterm.Screen.CursorState class for more details.
957 *
958 * @param {boolean=} both If true, update both screens, else only update the
959 * current screen.
960 */
961hterm.Terminal.prototype.restoreCursorAndState = function(both) {
962 if (both) {
963 this.primaryScreen_.restoreCursorAndState(this.vt);
964 this.alternateScreen_.restoreCursorAndState(this.vt);
965 } else
966 this.screen_.restoreCursorAndState(this.vt);
967};
968
969/**
Robert Ginda830583c2013-08-07 13:20:46 -0700970 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -0500971 *
972 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -0700973 */
974hterm.Terminal.prototype.setCursorShape = function(shape) {
975 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -0800976 this.restyleCursor_();
Robert Ginda830583c2013-08-07 13:20:46 -0700977}
978
979/**
980 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -0500981 *
982 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -0700983 */
984hterm.Terminal.prototype.getCursorShape = function() {
985 return this.cursorShape_;
986}
987
988/**
rginda87b86462011-12-14 13:48:03 -0800989 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -0500990 *
991 * @param {number} columnCount
rginda87b86462011-12-14 13:48:03 -0800992 */
993hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800994 if (columnCount == null) {
995 this.div_.style.width = '100%';
996 return;
997 }
998
Robert Ginda26806d12014-07-24 13:44:07 -0700999 this.div_.style.width = Math.ceil(
1000 this.scrollPort_.characterSize.width *
1001 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001002 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001003 this.scheduleSyncCursorPosition_();
1004};
rginda87b86462011-12-14 13:48:03 -08001005
rgindac9bc5502012-01-18 11:48:44 -08001006/**
rginda35c456b2012-02-09 17:29:05 -08001007 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001008 *
1009 * @param {number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001010 */
1011hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001012 if (rowCount == null) {
1013 this.div_.style.height = '100%';
1014 return;
1015 }
1016
rginda35c456b2012-02-09 17:29:05 -08001017 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -07001018 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -08001019 this.realizeSize_(this.screenSize.width, rowCount);
1020 this.scheduleSyncCursorPosition_();
1021};
1022
1023/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001024 * Deal with terminal size changes.
1025 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001026 * @param {number} columnCount The number of columns.
1027 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001028 */
1029hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
1030 if (columnCount != this.screenSize.width)
1031 this.realizeWidth_(columnCount);
1032
1033 if (rowCount != this.screenSize.height)
1034 this.realizeHeight_(rowCount);
1035
1036 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -07001037 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001038};
1039
1040/**
rgindac9bc5502012-01-18 11:48:44 -08001041 * Deal with terminal width changes.
1042 *
1043 * This function does what needs to be done when the terminal width changes
1044 * out from under us. It happens here rather than in onResize_() because this
1045 * code may need to run synchronously to handle programmatic changes of
1046 * terminal width.
1047 *
1048 * Relying on the browser to send us an async resize event means we may not be
1049 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001050 *
1051 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001052 */
1053hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001054 if (columnCount <= 0)
1055 throw new Error('Attempt to realize bad width: ' + columnCount);
1056
rgindac9bc5502012-01-18 11:48:44 -08001057 var deltaColumns = columnCount - this.screen_.getWidth();
1058
rginda87b86462011-12-14 13:48:03 -08001059 this.screenSize.width = columnCount;
1060 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001061
1062 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -04001063 if (this.defaultTabStops)
1064 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -08001065 } else {
1066 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -04001067 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -08001068 break;
1069
1070 this.tabStops_.pop();
1071 }
1072 }
1073
1074 this.screen_.setColumnCount(this.screenSize.width);
1075};
1076
1077/**
1078 * Deal with terminal height changes.
1079 *
1080 * This function does what needs to be done when the terminal height changes
1081 * out from under us. It happens here rather than in onResize_() because this
1082 * code may need to run synchronously to handle programmatic changes of
1083 * terminal height.
1084 *
1085 * Relying on the browser to send us an async resize event means we may not be
1086 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001087 *
1088 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001089 */
1090hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001091 if (rowCount <= 0)
1092 throw new Error('Attempt to realize bad height: ' + rowCount);
1093
rgindac9bc5502012-01-18 11:48:44 -08001094 var deltaRows = rowCount - this.screen_.getHeight();
1095
1096 this.screenSize.height = rowCount;
1097
1098 var cursor = this.saveCursor();
1099
1100 if (deltaRows < 0) {
1101 // Screen got smaller.
1102 deltaRows *= -1;
1103 while (deltaRows) {
1104 var lastRow = this.getRowCount() - 1;
1105 if (lastRow - this.scrollbackRows_.length == cursor.row)
1106 break;
1107
1108 if (this.getRowText(lastRow))
1109 break;
1110
1111 this.screen_.popRow();
1112 deltaRows--;
1113 }
1114
1115 var ary = this.screen_.shiftRows(deltaRows);
1116 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1117
1118 // We just removed rows from the top of the screen, we need to update
1119 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001120 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001121 } else if (deltaRows > 0) {
1122 // Screen got larger.
1123
1124 if (deltaRows <= this.scrollbackRows_.length) {
1125 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1126 var rows = this.scrollbackRows_.splice(
1127 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1128 this.screen_.unshiftRows(rows);
1129 deltaRows -= scrollbackCount;
1130 cursor.row += scrollbackCount;
1131 }
1132
1133 if (deltaRows)
1134 this.appendRows_(deltaRows);
1135 }
1136
rginda35c456b2012-02-09 17:29:05 -08001137 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001138 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001139};
1140
1141/**
1142 * Scroll the terminal to the top of the scrollback buffer.
1143 */
1144hterm.Terminal.prototype.scrollHome = function() {
1145 this.scrollPort_.scrollRowToTop(0);
1146};
1147
1148/**
1149 * Scroll the terminal to the end.
1150 */
1151hterm.Terminal.prototype.scrollEnd = function() {
1152 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1153};
1154
1155/**
1156 * Scroll the terminal one page up (minus one line) relative to the current
1157 * position.
1158 */
1159hterm.Terminal.prototype.scrollPageUp = function() {
1160 var i = this.scrollPort_.getTopRowIndex();
1161 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
1162};
1163
1164/**
1165 * Scroll the terminal one page down (minus one line) relative to the current
1166 * position.
1167 */
1168hterm.Terminal.prototype.scrollPageDown = function() {
1169 var i = this.scrollPort_.getTopRowIndex();
1170 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -08001171};
1172
rgindac9bc5502012-01-18 11:48:44 -08001173/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001174 * Scroll the terminal one line up relative to the current position.
1175 */
1176hterm.Terminal.prototype.scrollLineUp = function() {
1177 var i = this.scrollPort_.getTopRowIndex();
1178 this.scrollPort_.scrollRowToTop(i - 1);
1179};
1180
1181/**
1182 * Scroll the terminal one line down relative to the current position.
1183 */
1184hterm.Terminal.prototype.scrollLineDown = function() {
1185 var i = this.scrollPort_.getTopRowIndex();
1186 this.scrollPort_.scrollRowToTop(i + 1);
1187};
1188
1189/**
Robert Ginda40932892012-12-10 17:26:40 -08001190 * Clear primary screen, secondary screen, and the scrollback buffer.
1191 */
1192hterm.Terminal.prototype.wipeContents = function() {
1193 this.scrollbackRows_.length = 0;
1194 this.scrollPort_.resetCache();
1195
1196 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
1197 var bottom = screen.getHeight();
1198 if (bottom > 0) {
1199 this.renumberRows_(0, bottom);
1200 this.clearHome(screen);
1201 }
1202 }.bind(this));
1203
1204 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001205 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001206};
1207
1208/**
rgindac9bc5502012-01-18 11:48:44 -08001209 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001210 *
1211 * Perform a full reset to the default values listed in
1212 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001213 */
rginda87b86462011-12-14 13:48:03 -08001214hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001215 this.vt.reset();
1216
rgindac9bc5502012-01-18 11:48:44 -08001217 this.clearAllTabStops();
1218 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001219
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001220 const resetScreen = (screen) => {
1221 // We want to make sure to reset the attributes before we clear the screen.
1222 // The attributes might be used to initialize default/empty rows.
1223 screen.textAttributes.reset();
1224 screen.textAttributes.resetColorPalette();
1225 this.clearHome(screen);
1226 screen.saveCursorAndState(this.vt);
1227 };
1228 resetScreen(this.primaryScreen_);
1229 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001230
Mike Frysinger84301d02017-11-29 13:28:46 -08001231 // Reset terminal options to their default values.
1232 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001233 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1234
Mike Frysinger84301d02017-11-29 13:28:46 -08001235 this.setVTScrollRegion(null, null);
1236
1237 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001238};
1239
rgindac9bc5502012-01-18 11:48:44 -08001240/**
1241 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001242 *
1243 * Perform a soft reset to the default values listed in
1244 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001245 */
rginda0f5c0292012-01-13 11:00:13 -08001246hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001247 this.vt.reset();
1248
rgindab8bc8932012-04-27 12:45:03 -07001249 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001250 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001251
Brad Townb62dfdc2015-03-16 19:07:15 -07001252 // We show the cursor on soft reset but do not alter the blink state.
1253 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1254
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001255 const resetScreen = (screen) => {
1256 // Xterm also resets the color palette on soft reset, even though it doesn't
1257 // seem to be documented anywhere.
1258 screen.textAttributes.reset();
1259 screen.textAttributes.resetColorPalette();
1260 screen.saveCursorAndState(this.vt);
1261 };
1262 resetScreen(this.primaryScreen_);
1263 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001264
rgindab8bc8932012-04-27 12:45:03 -07001265 // The xterm man page explicitly says this will happen on soft reset.
1266 this.setVTScrollRegion(null, null);
1267
1268 // Xterm also shows the cursor on soft reset, but does not alter the blink
1269 // state.
rgindaa19afe22012-01-25 15:40:22 -08001270 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001271};
1272
rgindac9bc5502012-01-18 11:48:44 -08001273/**
1274 * Move the cursor forward to the next tab stop, or to the last column
1275 * if no more tab stops are set.
1276 */
1277hterm.Terminal.prototype.forwardTabStop = function() {
1278 var column = this.screen_.cursorPosition.column;
1279
1280 for (var i = 0; i < this.tabStops_.length; i++) {
1281 if (this.tabStops_[i] > column) {
1282 this.setCursorColumn(this.tabStops_[i]);
1283 return;
1284 }
1285 }
1286
David Benjamin66e954d2012-05-05 21:08:12 -04001287 // xterm does not clear the overflow flag on HT or CHT.
1288 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001289 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001290 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001291};
1292
rgindac9bc5502012-01-18 11:48:44 -08001293/**
1294 * Move the cursor backward to the previous tab stop, or to the first column
1295 * if no previous tab stops are set.
1296 */
1297hterm.Terminal.prototype.backwardTabStop = function() {
1298 var column = this.screen_.cursorPosition.column;
1299
1300 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1301 if (this.tabStops_[i] < column) {
1302 this.setCursorColumn(this.tabStops_[i]);
1303 return;
1304 }
1305 }
1306
1307 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001308};
1309
rgindac9bc5502012-01-18 11:48:44 -08001310/**
1311 * Set a tab stop at the given column.
1312 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001313 * @param {integer} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001314 */
1315hterm.Terminal.prototype.setTabStop = function(column) {
1316 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1317 if (this.tabStops_[i] == column)
1318 return;
1319
1320 if (this.tabStops_[i] < column) {
1321 this.tabStops_.splice(i + 1, 0, column);
1322 return;
1323 }
1324 }
1325
1326 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001327};
1328
rgindac9bc5502012-01-18 11:48:44 -08001329/**
1330 * Clear the tab stop at the current cursor position.
1331 *
1332 * No effect if there is no tab stop at the current cursor position.
1333 */
1334hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1335 var column = this.screen_.cursorPosition.column;
1336
1337 var i = this.tabStops_.indexOf(column);
1338 if (i == -1)
1339 return;
1340
1341 this.tabStops_.splice(i, 1);
1342};
1343
1344/**
1345 * Clear all tab stops.
1346 */
1347hterm.Terminal.prototype.clearAllTabStops = function() {
1348 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001349 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001350};
1351
1352/**
1353 * Set up the default tab stops, starting from a given column.
1354 *
1355 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001356 * from the specified column, or 0 if no column is provided. It also flags
1357 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001358 *
1359 * This does not clear the existing tab stops first, use clearAllTabStops
1360 * for that.
1361 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001362 * @param {integer} opt_start Optional starting zero based starting column, useful
rgindac9bc5502012-01-18 11:48:44 -08001363 * for filling out missing tab stops when the terminal is resized.
1364 */
1365hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1366 var start = opt_start || 0;
1367 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001368 // Round start up to a default tab stop.
1369 start = start - 1 - ((start - 1) % w) + w;
1370 for (var i = start; i < this.screenSize.width; i += w) {
1371 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001372 }
David Benjamin66e954d2012-05-05 21:08:12 -04001373
1374 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001375};
1376
rginda6d397402012-01-17 10:58:29 -08001377/**
rginda8ba33642011-12-14 12:31:31 -08001378 * Interpret a sequence of characters.
1379 *
1380 * Incomplete escape sequences are buffered until the next call.
1381 *
1382 * @param {string} str Sequence of characters to interpret or pass through.
1383 */
1384hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001385 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001386 this.scheduleSyncCursorPosition_();
1387};
1388
1389/**
1390 * Take over the given DIV for use as the terminal display.
1391 *
1392 * @param {HTMLDivElement} div The div to use as the terminal display.
1393 */
1394hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001395 this.div_ = div;
1396
rginda8ba33642011-12-14 12:31:31 -08001397 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001398 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001399 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1400 this.scrollPort_.setBackgroundPosition(
1401 this.prefs_.get('background-position'));
Mike Frysinger08bad432017-04-24 00:50:54 -04001402 this.scrollPort_.setUserCssUrl(this.prefs_.get('user-css'));
1403 this.scrollPort_.setUserCssText(this.prefs_.get('user-css-text'));
rginda30f20f62012-04-05 16:36:19 -07001404
rginda0918b652012-04-04 11:26:24 -07001405 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001406
rginda9f5222b2012-03-05 11:53:28 -08001407 this.setFontSize(this.prefs_.get('font-size'));
1408 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001409
David Reveman8f552492012-03-28 12:18:41 -04001410 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001411 this.setScrollWheelMoveMultipler(
1412 this.prefs_.get('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001413
rginda8ba33642011-12-14 12:31:31 -08001414 this.document_ = this.scrollPort_.getDocument();
1415
Evan Jones5f9df812016-12-06 09:38:58 -05001416 this.document_.body.oncontextmenu = function() { return false; };
rginda4bba5e12012-06-20 16:15:30 -07001417
1418 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001419 var screenNode = this.scrollPort_.getScreenNode();
1420 screenNode.addEventListener('mousedown', onMouse);
1421 screenNode.addEventListener('mouseup', onMouse);
1422 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001423 this.scrollPort_.onScrollWheel = onMouse;
1424
Toni Barzic0bfa8922013-11-22 11:18:35 -08001425 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001426 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001427 // Listen for mousedown events on the screenNode as in FF the focus
1428 // events don't bubble.
1429 screenNode.addEventListener('mousedown', function() {
1430 setTimeout(this.onFocusChange_.bind(this, true));
1431 }.bind(this));
1432
Toni Barzic0bfa8922013-11-22 11:18:35 -08001433 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001434 'blur', this.onFocusChange_.bind(this, false));
1435
1436 var style = this.document_.createElement('style');
1437 style.textContent =
1438 ('.cursor-node[focus="false"] {' +
1439 ' box-sizing: border-box;' +
1440 ' background-color: transparent !important;' +
1441 ' border-width: 2px;' +
1442 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001443 '}' +
1444 '.wc-node {' +
1445 ' display: inline-block;' +
1446 ' text-align: center;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001447 ' width: calc(var(--hterm-charsize-width) * 2);' +
Mike Frysinger4036f6e2017-05-31 14:02:55 -04001448 ' line-height: var(--hterm-charsize-height);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001449 '}' +
1450 ':root {' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001451 ' --hterm-charsize-width: ' + this.scrollPort_.characterSize.width + 'px;' +
1452 ' --hterm-charsize-height: ' + this.scrollPort_.characterSize.height + 'px;' +
Mike Frysingera27c0502017-08-23 22:37:10 -04001453 // Default position hides the cursor for when the window is initializing.
1454 ' --hterm-cursor-offset-col: -1;' +
1455 ' --hterm-cursor-offset-row: -1;' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001456 ' --hterm-blink-node-duration: 0.7s;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001457 ' --hterm-mouse-cursor-text: text;' +
1458 ' --hterm-mouse-cursor-pointer: default;' +
1459 ' --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001460 '}' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001461 '.uri-node:hover {' +
1462 ' text-decoration: underline;' +
1463 ' cursor: pointer;' +
1464 '}' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001465 '@keyframes blink {' +
1466 ' from { opacity: 1.0; }' +
1467 ' to { opacity: 0.0; }' +
1468 '}' +
1469 '.blink-node {' +
1470 ' animation-name: blink;' +
1471 ' animation-duration: var(--hterm-blink-node-duration);' +
1472 ' animation-iteration-count: infinite;' +
1473 ' animation-timing-function: ease-in-out;' +
1474 ' animation-direction: alternate;' +
rginda8e92a692012-05-20 19:37:20 -07001475 '}');
1476 this.document_.head.appendChild(style);
1477
rginda8ba33642011-12-14 12:31:31 -08001478 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001479 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001480 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001481 this.cursorNode_.style.cssText =
1482 ('position: absolute;' +
Mike Frysinger44c32202017-08-05 01:13:09 -04001483 'left: calc(var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));' +
1484 'top: calc(var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));' +
rginda87b86462011-12-14 13:48:03 -08001485 'display: block;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001486 'width: var(--hterm-charsize-width);' +
1487 'height: var(--hterm-charsize-height);' +
Rob Spies06533ba2014-04-24 11:20:37 -07001488 '-webkit-transition: opacity, background-color 100ms linear;' +
1489 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001490
rginda8e92a692012-05-20 19:37:20 -07001491 this.setCursorColor(this.prefs_.get('cursor-color'));
Robert Gindafb1be6a2013-12-11 11:56:22 -08001492 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1493 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001494
rginda8ba33642011-12-14 12:31:31 -08001495 this.document_.body.appendChild(this.cursorNode_);
1496
rgindad5613292012-06-19 15:40:37 -07001497 // When 'enableMouseDragScroll' is off we reposition this element directly
1498 // under the mouse cursor after a click. This makes Chrome associate
1499 // subsequent mousemove events with the scroll-blocker. Since the
1500 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1501 // events do not cause the scrollport to scroll.
1502 //
1503 // It's a hack, but it's the cleanest way I could find.
1504 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001505 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
rgindad5613292012-06-19 15:40:37 -07001506 this.scrollBlockerNode_.style.cssText =
1507 ('position: absolute;' +
1508 'top: -99px;' +
1509 'display: block;' +
1510 'width: 10px;' +
1511 'height: 10px;');
1512 this.document_.body.appendChild(this.scrollBlockerNode_);
1513
rgindad5613292012-06-19 15:40:37 -07001514 this.scrollPort_.onScrollWheel = onMouse;
1515 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1516 ].forEach(function(event) {
1517 this.scrollBlockerNode_.addEventListener(event, onMouse);
1518 this.cursorNode_.addEventListener(event, onMouse);
1519 this.document_.addEventListener(event, onMouse);
1520 }.bind(this));
1521
1522 this.cursorNode_.addEventListener('mousedown', function() {
1523 setTimeout(this.focus.bind(this));
1524 }.bind(this));
1525
rginda8ba33642011-12-14 12:31:31 -08001526 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001527
rginda87b86462011-12-14 13:48:03 -08001528 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001529 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001530};
1531
rginda0918b652012-04-04 11:26:24 -07001532/**
1533 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001534 *
1535 * @return {HTMLDocument}
rginda0918b652012-04-04 11:26:24 -07001536 */
rginda87b86462011-12-14 13:48:03 -08001537hterm.Terminal.prototype.getDocument = function() {
1538 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001539};
1540
1541/**
rginda0918b652012-04-04 11:26:24 -07001542 * Focus the terminal.
1543 */
1544hterm.Terminal.prototype.focus = function() {
1545 this.scrollPort_.focus();
1546};
1547
1548/**
rginda8ba33642011-12-14 12:31:31 -08001549 * Return the HTML Element for a given row index.
1550 *
1551 * This is a method from the RowProvider interface. The ScrollPort uses
1552 * it to fetch rows on demand as they are scrolled into view.
1553 *
1554 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1555 * pairs to conserve memory.
1556 *
1557 * @param {integer} index The zero-based row index, measured relative to the
1558 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001559 * largest indices.
rginda8ba33642011-12-14 12:31:31 -08001560 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1561 */
1562hterm.Terminal.prototype.getRowNode = function(index) {
1563 if (index < this.scrollbackRows_.length)
1564 return this.scrollbackRows_[index];
1565
1566 var screenIndex = index - this.scrollbackRows_.length;
1567 return this.screen_.rowsArray[screenIndex];
1568};
1569
1570/**
1571 * Return the text content for a given range of rows.
1572 *
1573 * This is a method from the RowProvider interface. The ScrollPort uses
1574 * it to fetch text content on demand when the user attempts to copy their
1575 * selection to the clipboard.
1576 *
1577 * @param {integer} start The zero-based row index to start from, measured
1578 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001579 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001580 * @param {integer} end The zero-based row index to end on, measured
1581 * relative to the start of the scrollback buffer.
1582 * @return {string} A single string containing the text value of the range of
1583 * rows. Lines will be newline delimited, with no trailing newline.
1584 */
1585hterm.Terminal.prototype.getRowsText = function(start, end) {
1586 var ary = [];
1587 for (var i = start; i < end; i++) {
1588 var node = this.getRowNode(i);
1589 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001590 if (i < end - 1 && !node.getAttribute('line-overflow'))
1591 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001592 }
1593
rgindaa09e7332012-08-17 12:49:51 -07001594 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001595};
1596
1597/**
1598 * Return the text content for a given row.
1599 *
1600 * This is a method from the RowProvider interface. The ScrollPort uses
1601 * it to fetch text content on demand when the user attempts to copy their
1602 * selection to the clipboard.
1603 *
1604 * @param {integer} index The zero-based row index to return, measured
1605 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001606 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001607 * @return {string} A string containing the text value of the selected row.
1608 */
1609hterm.Terminal.prototype.getRowText = function(index) {
1610 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001611 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001612};
1613
1614/**
1615 * Return the total number of rows in the addressable screen and in the
1616 * scrollback buffer of this terminal.
1617 *
1618 * This is a method from the RowProvider interface. The ScrollPort uses
1619 * it to compute the size of the scrollbar.
1620 *
1621 * @return {integer} The number of rows in this terminal.
1622 */
1623hterm.Terminal.prototype.getRowCount = function() {
1624 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1625};
1626
1627/**
1628 * Create DOM nodes for new rows and append them to the end of the terminal.
1629 *
1630 * This is the only correct way to add a new DOM node for a row. Notice that
1631 * the new row is appended to the bottom of the list of rows, and does not
1632 * require renumbering (of the rowIndex property) of previous rows.
1633 *
1634 * If you think you want a new blank row somewhere in the middle of the
1635 * terminal, look into moveRows_().
1636 *
1637 * This method does not pay attention to vtScrollTop/Bottom, since you should
1638 * be using moveRows() in cases where they would matter.
1639 *
1640 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001641 *
1642 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001643 */
1644hterm.Terminal.prototype.appendRows_ = function(count) {
1645 var cursorRow = this.screen_.rowsArray.length;
1646 var offset = this.scrollbackRows_.length + cursorRow;
1647 for (var i = 0; i < count; i++) {
1648 var row = this.document_.createElement('x-row');
1649 row.appendChild(this.document_.createTextNode(''));
1650 row.rowIndex = offset + i;
1651 this.screen_.pushRow(row);
1652 }
1653
1654 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1655 if (extraRows > 0) {
1656 var ary = this.screen_.shiftRows(extraRows);
1657 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001658 if (this.scrollPort_.isScrolledEnd)
1659 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001660 }
1661
1662 if (cursorRow >= this.screen_.rowsArray.length)
1663 cursorRow = this.screen_.rowsArray.length - 1;
1664
rginda87b86462011-12-14 13:48:03 -08001665 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001666};
1667
1668/**
1669 * Relocate rows from one part of the addressable screen to another.
1670 *
1671 * This is used to recycle rows during VT scrolls (those which are driven
1672 * by VT commands, rather than by the user manipulating the scrollbar.)
1673 *
1674 * In this case, the blank lines scrolled into the scroll region are made of
1675 * the nodes we scrolled off. These have their rowIndex properties carefully
1676 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001677 *
1678 * @param {number} fromIndex The start index.
1679 * @param {number} count The number of rows to move.
1680 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001681 */
1682hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1683 var ary = this.screen_.removeRows(fromIndex, count);
1684 this.screen_.insertRows(toIndex, ary);
1685
1686 var start, end;
1687 if (fromIndex < toIndex) {
1688 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001689 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001690 } else {
1691 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001692 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001693 }
1694
1695 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001696 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001697};
1698
1699/**
1700 * Renumber the rowIndex property of the given range of rows.
1701 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001702 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001703 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001704 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001705 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001706 *
1707 * @param {number} start The start index.
1708 * @param {number} end The end index.
1709 * @param {hterm.Screen} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001710 */
Robert Ginda40932892012-12-10 17:26:40 -08001711hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1712 var screen = opt_screen || this.screen_;
1713
rginda8ba33642011-12-14 12:31:31 -08001714 var offset = this.scrollbackRows_.length;
1715 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001716 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001717 }
1718};
1719
1720/**
1721 * Print a string to the terminal.
1722 *
1723 * This respects the current insert and wraparound modes. It will add new lines
1724 * to the end of the terminal, scrolling off the top into the scrollback buffer
1725 * if necessary.
1726 *
1727 * The string is *not* parsed for escape codes. Use the interpret() method if
1728 * that's what you're after.
1729 *
1730 * @param{string} str The string to print.
1731 */
1732hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001733 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001734
Ricky Liang48f05cb2013-12-31 23:35:29 +08001735 var strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04001736 // Fun edge case: If the string only contains zero width codepoints (like
1737 // combining characters), we make sure to iterate at least once below.
1738 if (strWidth == 0 && str)
1739 strWidth = 1;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001740
1741 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001742 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1743 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001744 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001745 }
rgindaa19afe22012-01-25 15:40:22 -08001746
Ricky Liang48f05cb2013-12-31 23:35:29 +08001747 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001748 var didOverflow = false;
1749 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001750
rgindaa9abdd82012-08-06 18:05:09 -07001751 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1752 didOverflow = true;
1753 count = this.screenSize.width - this.screen_.cursorPosition.column;
1754 }
rgindaa19afe22012-01-25 15:40:22 -08001755
rgindaa9abdd82012-08-06 18:05:09 -07001756 if (didOverflow && !this.options_.wraparound) {
1757 // If the string overflowed the line but wraparound is off, then the
1758 // last printed character should be the last of the string.
1759 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001760 substr = lib.wc.substr(str, startOffset, count - 1) +
1761 lib.wc.substr(str, strWidth - 1);
1762 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001763 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001764 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001765 }
rgindaa19afe22012-01-25 15:40:22 -08001766
Ricky Liang48f05cb2013-12-31 23:35:29 +08001767 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1768 for (var i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001769 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
1770 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001771
1772 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001773 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001774 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001775 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001776 }
1777 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001778 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07001779 }
1780
1781 this.screen_.maybeClipCurrentRow();
1782 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001783 }
rginda8ba33642011-12-14 12:31:31 -08001784
1785 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001786
rginda9f5222b2012-03-05 11:53:28 -08001787 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001788 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001789};
1790
1791/**
rginda87b86462011-12-14 13:48:03 -08001792 * Set the VT scroll region.
1793 *
rginda87b86462011-12-14 13:48:03 -08001794 * This also resets the cursor position to the absolute (0, 0) position, since
1795 * that's what xterm appears to do.
1796 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001797 * Setting the scroll region to the full height of the terminal will clear
1798 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1799 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1800 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1801 * continue to work as most users would expect.
1802 *
rginda87b86462011-12-14 13:48:03 -08001803 * @param {integer} scrollTop The zero-based top of the scroll region.
1804 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1805 * inclusive.
1806 */
1807hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001808 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001809 this.vtScrollTop_ = null;
1810 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001811 } else {
1812 this.vtScrollTop_ = scrollTop;
1813 this.vtScrollBottom_ = scrollBottom;
1814 }
rginda87b86462011-12-14 13:48:03 -08001815};
1816
1817/**
rginda8ba33642011-12-14 12:31:31 -08001818 * Return the top row index according to the VT.
1819 *
1820 * This will return 0 unless the terminal has been told to restrict scrolling
1821 * to some lower row. It is used for some VT cursor positioning and scrolling
1822 * commands.
1823 *
1824 * @return {integer} The topmost row in the terminal's scroll region.
1825 */
1826hterm.Terminal.prototype.getVTScrollTop = function() {
1827 if (this.vtScrollTop_ != null)
1828 return this.vtScrollTop_;
1829
1830 return 0;
rginda87b86462011-12-14 13:48:03 -08001831};
rginda8ba33642011-12-14 12:31:31 -08001832
1833/**
1834 * Return the bottom row index according to the VT.
1835 *
1836 * This will return the height of the terminal unless the it has been told to
1837 * restrict scrolling to some higher row. It is used for some VT cursor
1838 * positioning and scrolling commands.
1839 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001840 * @return {integer} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001841 */
1842hterm.Terminal.prototype.getVTScrollBottom = function() {
1843 if (this.vtScrollBottom_ != null)
1844 return this.vtScrollBottom_;
1845
rginda87b86462011-12-14 13:48:03 -08001846 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001847}
1848
1849/**
1850 * Process a '\n' character.
1851 *
1852 * If the cursor is on the final row of the terminal this will append a new
1853 * blank row to the screen and scroll the topmost row into the scrollback
1854 * buffer.
1855 *
1856 * Otherwise, this moves the cursor to column zero of the next row.
1857 */
1858hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001859 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1860 this.screen_.rowsArray.length - 1);
1861
1862 if (this.vtScrollBottom_ != null) {
1863 // A VT Scroll region is active, we never append new rows.
1864 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1865 // We're at the end of the VT Scroll Region, perform a VT scroll.
1866 this.vtScrollUp(1);
1867 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1868 } else if (cursorAtEndOfScreen) {
1869 // We're at the end of the screen, the only thing to do is put the
1870 // cursor to column 0.
1871 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1872 } else {
1873 // Anywhere else, advance the cursor row, and reset the column.
1874 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1875 }
1876 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001877 // We're at the end of the screen. Append a new row to the terminal,
1878 // shifting the top row into the scrollback.
1879 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001880 } else {
rginda87b86462011-12-14 13:48:03 -08001881 // Anywhere else in the screen just moves the cursor.
1882 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001883 }
1884};
1885
1886/**
1887 * Like newLine(), except maintain the cursor column.
1888 */
1889hterm.Terminal.prototype.lineFeed = function() {
1890 var column = this.screen_.cursorPosition.column;
1891 this.newLine();
1892 this.setCursorColumn(column);
1893};
1894
1895/**
rginda87b86462011-12-14 13:48:03 -08001896 * If autoCarriageReturn is set then newLine(), else lineFeed().
1897 */
1898hterm.Terminal.prototype.formFeed = function() {
1899 if (this.options_.autoCarriageReturn) {
1900 this.newLine();
1901 } else {
1902 this.lineFeed();
1903 }
1904};
1905
1906/**
1907 * Move the cursor up one row, possibly inserting a blank line.
1908 *
1909 * The cursor column is not changed.
1910 */
1911hterm.Terminal.prototype.reverseLineFeed = function() {
1912 var scrollTop = this.getVTScrollTop();
1913 var currentRow = this.screen_.cursorPosition.row;
1914
1915 if (currentRow == scrollTop) {
1916 this.insertLines(1);
1917 } else {
1918 this.setAbsoluteCursorRow(currentRow - 1);
1919 }
1920};
1921
1922/**
rginda8ba33642011-12-14 12:31:31 -08001923 * Replace all characters to the left of the current cursor with the space
1924 * character.
1925 *
1926 * TODO(rginda): This should probably *remove* the characters (not just replace
1927 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001928 * position.
rginda8ba33642011-12-14 12:31:31 -08001929 */
1930hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001931 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001932 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04001933 const count = cursor.column + 1;
1934 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08001935 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001936};
1937
1938/**
David Benjamin684a9b72012-05-01 17:19:58 -04001939 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001940 *
1941 * The cursor position is unchanged.
1942 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001943 * If the current background color is not the default background color this
1944 * will insert spaces rather than delete. This is unfortunate because the
1945 * trailing space will affect text selection, but it's difficult to come up
1946 * with a way to style empty space that wouldn't trip up the hterm.Screen
1947 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001948 *
1949 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1950 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1951 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05001952 *
1953 * @param {number} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08001954 */
1955hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001956 if (this.screen_.cursorPosition.overflow)
1957 return;
1958
Robert Ginda7fd57082012-09-25 14:41:47 -07001959 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1960 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001961
1962 if (this.screen_.textAttributes.background ===
1963 this.screen_.textAttributes.DEFAULT_COLOR) {
1964 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08001965 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07001966 this.screen_.cursorPosition.column + count) {
1967 this.screen_.deleteChars(count);
1968 this.clearCursorOverflow();
1969 return;
1970 }
1971 }
1972
rginda87b86462011-12-14 13:48:03 -08001973 var cursor = this.saveCursor();
Mike Frysinger6380bed2017-08-24 18:46:39 -04001974 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08001975 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001976 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001977};
1978
1979/**
1980 * Erase the current line.
1981 *
1982 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001983 */
1984hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001985 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001986 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001987 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001988 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001989};
1990
1991/**
David Benjamina08d78f2012-05-05 00:28:49 -04001992 * Erase all characters from the start of the screen to the current cursor
1993 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001994 *
1995 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001996 */
1997hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001998 var cursor = this.saveCursor();
1999
2000 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002001
David Benjamina08d78f2012-05-05 00:28:49 -04002002 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002003 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002004 this.screen_.clearCursorRow();
2005 }
2006
rginda87b86462011-12-14 13:48:03 -08002007 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002008 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002009};
2010
2011/**
2012 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002013 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002014 *
2015 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002016 */
2017hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08002018 var cursor = this.saveCursor();
2019
2020 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002021
David Benjamina08d78f2012-05-05 00:28:49 -04002022 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08002023 for (var i = cursor.row + 1; i <= bottom; i++) {
2024 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002025 this.screen_.clearCursorRow();
2026 }
2027
rginda87b86462011-12-14 13:48:03 -08002028 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002029 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002030};
2031
2032/**
2033 * Fill the terminal with a given character.
2034 *
2035 * This methods does not respect the VT scroll region.
2036 *
2037 * @param {string} ch The character to use for the fill.
2038 */
2039hterm.Terminal.prototype.fill = function(ch) {
2040 var cursor = this.saveCursor();
2041
2042 this.setAbsoluteCursorPosition(0, 0);
2043 for (var row = 0; row < this.screenSize.height; row++) {
2044 for (var col = 0; col < this.screenSize.width; col++) {
2045 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002046 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002047 }
2048 }
2049
2050 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002051};
2052
2053/**
rginda9ea433c2012-03-16 11:57:00 -07002054 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002055 *
rginda9ea433c2012-03-16 11:57:00 -07002056 * This does not respect the scroll region.
2057 *
2058 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2059 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002060 */
rginda9ea433c2012-03-16 11:57:00 -07002061hterm.Terminal.prototype.clearHome = function(opt_screen) {
2062 var screen = opt_screen || this.screen_;
2063 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002064
rginda11057d52012-04-25 12:29:56 -07002065 if (bottom == 0) {
2066 // Empty screen, nothing to do.
2067 return;
2068 }
2069
rgindae4d29232012-01-19 10:47:13 -08002070 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002071 screen.setCursorPosition(i, 0);
2072 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002073 }
2074
rginda9ea433c2012-03-16 11:57:00 -07002075 screen.setCursorPosition(0, 0);
2076};
2077
2078/**
2079 * Erase the entire display without changing the cursor position.
2080 *
2081 * The cursor position is unchanged. This does not respect the scroll
2082 * region.
2083 *
2084 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2085 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002086 */
2087hterm.Terminal.prototype.clear = function(opt_screen) {
2088 var screen = opt_screen || this.screen_;
2089 var cursor = screen.cursorPosition.clone();
2090 this.clearHome(screen);
2091 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002092};
2093
2094/**
2095 * VT command to insert lines at the current cursor row.
2096 *
2097 * This respects the current scroll region. Rows pushed off the bottom are
2098 * lost (they won't show up in the scrollback buffer).
2099 *
rginda8ba33642011-12-14 12:31:31 -08002100 * @param {integer} count The number of lines to insert.
2101 */
2102hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002103 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002104
2105 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002106 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002107
Robert Ginda579186b2012-09-26 11:40:04 -07002108 // The moveCount is the number of rows we need to relocate to make room for
2109 // the new row(s). The count is the distance to move them.
2110 var moveCount = bottom - cursorRow - count + 1;
2111 if (moveCount)
2112 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002113
Robert Ginda579186b2012-09-26 11:40:04 -07002114 for (var i = count - 1; i >= 0; i--) {
2115 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002116 this.screen_.clearCursorRow();
2117 }
rginda8ba33642011-12-14 12:31:31 -08002118};
2119
2120/**
2121 * VT command to delete lines at the current cursor row.
2122 *
2123 * New rows are added to the bottom of scroll region to take their place. New
2124 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002125 *
2126 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002127 */
2128hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002129 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002130
rginda87b86462011-12-14 13:48:03 -08002131 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002132 var bottom = this.getVTScrollBottom();
2133
rginda87b86462011-12-14 13:48:03 -08002134 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002135 count = Math.min(count, maxCount);
2136
rginda87b86462011-12-14 13:48:03 -08002137 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002138 if (count != maxCount)
2139 this.moveRows_(top, count, moveStart);
2140
2141 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002142 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002143 this.screen_.clearCursorRow();
2144 }
2145
rginda87b86462011-12-14 13:48:03 -08002146 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002147 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002148};
2149
2150/**
2151 * Inserts the given number of spaces at the current cursor position.
2152 *
rginda87b86462011-12-14 13:48:03 -08002153 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002154 *
2155 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002156 */
2157hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002158 var cursor = this.saveCursor();
2159
rgindacbbd7482012-06-13 15:06:16 -07002160 var ws = lib.f.getWhitespace(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002161 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002162 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002163
2164 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002165 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002166};
2167
2168/**
2169 * Forward-delete the specified number of characters starting at the cursor
2170 * position.
2171 *
2172 * @param {integer} count The number of characters to delete.
2173 */
2174hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002175 var deleted = this.screen_.deleteChars(count);
2176 if (deleted && !this.screen_.textAttributes.isDefault()) {
2177 var cursor = this.saveCursor();
2178 this.setCursorColumn(this.screenSize.width - deleted);
2179 this.screen_.insertString(lib.f.getWhitespace(deleted));
2180 this.restoreCursor(cursor);
2181 }
2182
David Benjamin54e8bf62012-06-01 22:31:40 -04002183 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002184};
2185
2186/**
2187 * Shift rows in the scroll region upwards by a given number of lines.
2188 *
2189 * New rows are inserted at the bottom of the scroll region to fill the
2190 * vacated rows. The new rows not filled out with the current text attributes.
2191 *
2192 * This function does not affect the scrollback rows at all. Rows shifted
2193 * off the top are lost.
2194 *
rginda87b86462011-12-14 13:48:03 -08002195 * The cursor position is not altered.
2196 *
rginda8ba33642011-12-14 12:31:31 -08002197 * @param {integer} count The number of rows to scroll.
2198 */
2199hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002200 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002201
rginda87b86462011-12-14 13:48:03 -08002202 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002203 this.deleteLines(count);
2204
rginda87b86462011-12-14 13:48:03 -08002205 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002206};
2207
2208/**
2209 * Shift rows below the cursor down by a given number of lines.
2210 *
2211 * This function respects the current scroll region.
2212 *
2213 * New rows are inserted at the top of the scroll region to fill the
2214 * vacated rows. The new rows not filled out with the current text attributes.
2215 *
2216 * This function does not affect the scrollback rows at all. Rows shifted
2217 * off the bottom are lost.
2218 *
2219 * @param {integer} count The number of rows to scroll.
2220 */
2221hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08002222 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002223
rginda87b86462011-12-14 13:48:03 -08002224 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08002225 this.insertLines(opt_count);
2226
rginda87b86462011-12-14 13:48:03 -08002227 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002228};
2229
rginda87b86462011-12-14 13:48:03 -08002230
rginda8ba33642011-12-14 12:31:31 -08002231/**
2232 * Set the cursor position.
2233 *
2234 * The cursor row is relative to the scroll region if the terminal has
2235 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2236 *
2237 * @param {integer} row The new zero-based cursor row.
2238 * @param {integer} row The new zero-based cursor column.
2239 */
2240hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2241 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002242 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002243 } else {
rginda87b86462011-12-14 13:48:03 -08002244 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002245 }
rginda87b86462011-12-14 13:48:03 -08002246};
rginda8ba33642011-12-14 12:31:31 -08002247
Evan Jones2600d4f2016-12-06 09:29:36 -05002248/**
2249 * Move the cursor relative to its current position.
2250 *
2251 * @param {number} row
2252 * @param {number} column
2253 */
rginda87b86462011-12-14 13:48:03 -08002254hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2255 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002256 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2257 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002258 this.screen_.setCursorPosition(row, column);
2259};
2260
Evan Jones2600d4f2016-12-06 09:29:36 -05002261/**
2262 * Move the cursor to the specified position.
2263 *
2264 * @param {number} row
2265 * @param {number} column
2266 */
rginda87b86462011-12-14 13:48:03 -08002267hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002268 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2269 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002270 this.screen_.setCursorPosition(row, column);
2271};
2272
2273/**
2274 * Set the cursor column.
2275 *
2276 * @param {integer} column The new zero-based cursor column.
2277 */
2278hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002279 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002280};
2281
2282/**
2283 * Return the cursor column.
2284 *
2285 * @return {integer} The zero-based cursor column.
2286 */
2287hterm.Terminal.prototype.getCursorColumn = function() {
2288 return this.screen_.cursorPosition.column;
2289};
2290
2291/**
2292 * Set the cursor row.
2293 *
2294 * The cursor row is relative to the scroll region if the terminal has
2295 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2296 *
2297 * @param {integer} row The new cursor row.
2298 */
rginda87b86462011-12-14 13:48:03 -08002299hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2300 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002301};
2302
2303/**
2304 * Return the cursor row.
2305 *
2306 * @return {integer} The zero-based cursor row.
2307 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002308hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002309 return this.screen_.cursorPosition.row;
2310};
2311
2312/**
2313 * Request that the ScrollPort redraw itself soon.
2314 *
2315 * The redraw will happen asynchronously, soon after the call stack winds down.
2316 * Multiple calls will be coalesced into a single redraw.
2317 */
2318hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002319 if (this.timeouts_.redraw)
2320 return;
rginda8ba33642011-12-14 12:31:31 -08002321
2322 var self = this;
rginda87b86462011-12-14 13:48:03 -08002323 this.timeouts_.redraw = setTimeout(function() {
2324 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002325 self.scrollPort_.redraw_();
2326 }, 0);
2327};
2328
2329/**
2330 * Request that the ScrollPort be scrolled to the bottom.
2331 *
2332 * The scroll will happen asynchronously, soon after the call stack winds down.
2333 * Multiple calls will be coalesced into a single scroll.
2334 *
2335 * This affects the scrollbar position of the ScrollPort, and has nothing to
2336 * do with the VT scroll commands.
2337 */
2338hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2339 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002340 return;
rginda8ba33642011-12-14 12:31:31 -08002341
2342 var self = this;
2343 this.timeouts_.scrollDown = setTimeout(function() {
2344 delete self.timeouts_.scrollDown;
2345 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2346 }, 10);
2347};
2348
2349/**
2350 * Move the cursor up a specified number of rows.
2351 *
2352 * @param {integer} count The number of rows to move the cursor.
2353 */
2354hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002355 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002356};
2357
2358/**
2359 * Move the cursor down a specified number of rows.
2360 *
2361 * @param {integer} count The number of rows to move the cursor.
2362 */
2363hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002364 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002365 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2366 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2367 this.screenSize.height - 1);
2368
rgindacbbd7482012-06-13 15:06:16 -07002369 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002370 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002371 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002372};
2373
2374/**
2375 * Move the cursor left a specified number of columns.
2376 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002377 * If reverse wraparound mode is enabled and the previous row wrapped into
2378 * the current row then we back up through the wraparound as well.
2379 *
rginda8ba33642011-12-14 12:31:31 -08002380 * @param {integer} count The number of columns to move the cursor.
2381 */
2382hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002383 count = count || 1;
2384
2385 if (count < 1)
2386 return;
2387
2388 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002389 if (this.options_.reverseWraparound) {
2390 if (this.screen_.cursorPosition.overflow) {
2391 // If this cursor is in the right margin, consume one count to get it
2392 // back to the last column. This only applies when we're in reverse
2393 // wraparound mode.
2394 count--;
2395 this.clearCursorOverflow();
2396
2397 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002398 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002399 }
2400
Robert Gindabfb32622014-07-17 13:20:27 -07002401 var newRow = this.screen_.cursorPosition.row;
2402 var newColumn = currentColumn - count;
2403 if (newColumn < 0) {
2404 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2405 if (newRow < 0) {
2406 // xterm also wraps from row 0 to the last row.
2407 newRow = this.screenSize.height + newRow % this.screenSize.height;
2408 }
2409 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2410 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002411
Robert Gindabfb32622014-07-17 13:20:27 -07002412 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2413
2414 } else {
2415 var newColumn = Math.max(currentColumn - count, 0);
2416 this.setCursorColumn(newColumn);
2417 }
rginda8ba33642011-12-14 12:31:31 -08002418};
2419
2420/**
2421 * Move the cursor right a specified number of columns.
2422 *
2423 * @param {integer} count The number of columns to move the cursor.
2424 */
2425hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002426 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002427
2428 if (count < 1)
2429 return;
2430
rgindacbbd7482012-06-13 15:06:16 -07002431 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002432 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002433 this.setCursorColumn(column);
2434};
2435
2436/**
2437 * Reverse the foreground and background colors of the terminal.
2438 *
2439 * This only affects text that was drawn with no attributes.
2440 *
2441 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2442 * been drawn with attributes that happen to coincide with the default
2443 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002444 *
2445 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002446 */
2447hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002448 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002449 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002450 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2451 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002452 } else {
rginda9f5222b2012-03-05 11:53:28 -08002453 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2454 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002455 }
2456};
2457
2458/**
rginda87b86462011-12-14 13:48:03 -08002459 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002460 *
2461 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002462 */
2463hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002464 this.cursorNode_.style.backgroundColor =
2465 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002466
2467 var self = this;
2468 setTimeout(function() {
Matheus Fernandes2d733082017-09-11 06:43:01 -04002469 self.restyleCursor_();
rginda6d397402012-01-17 10:58:29 -08002470 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002471
Michael Kelly485ecd12014-06-09 11:41:56 -04002472 // bellSquelchTimeout_ affects both audio and notification bells.
2473 if (this.bellSquelchTimeout_)
2474 return;
2475
Robert Ginda92e18102013-03-14 13:56:37 -07002476 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002477 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002478 this.bellSequelchTimeout_ = setTimeout(function() {
2479 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002480 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002481 } else {
2482 delete this.bellSquelchTimeout_;
2483 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002484
2485 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002486 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002487 this.bellNotificationList_.push(n);
2488 // TODO: Should we try to raise the window here?
2489 n.onclick = function() { self.closeBellNotifications_(); };
2490 }
rginda87b86462011-12-14 13:48:03 -08002491};
2492
2493/**
rginda8ba33642011-12-14 12:31:31 -08002494 * Set the origin mode bit.
2495 *
2496 * If origin mode is on, certain VT cursor and scrolling commands measure their
2497 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2498 * to the top of the addressable screen.
2499 *
2500 * Defaults to off.
2501 *
2502 * @param {boolean} state True to set origin mode, false to unset.
2503 */
2504hterm.Terminal.prototype.setOriginMode = function(state) {
2505 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002506 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002507};
2508
2509/**
2510 * Set the insert mode bit.
2511 *
2512 * If insert mode is on, existing text beyond the cursor position will be
2513 * shifted right to make room for new text. Otherwise, new text overwrites
2514 * any existing text.
2515 *
2516 * Defaults to off.
2517 *
2518 * @param {boolean} state True to set insert mode, false to unset.
2519 */
2520hterm.Terminal.prototype.setInsertMode = function(state) {
2521 this.options_.insertMode = state;
2522};
2523
2524/**
rginda87b86462011-12-14 13:48:03 -08002525 * Set the auto carriage return bit.
2526 *
2527 * If auto carriage return is on then a formfeed character is interpreted
2528 * as a newline, otherwise it's the same as a linefeed. The difference boils
2529 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002530 *
2531 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002532 */
2533hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2534 this.options_.autoCarriageReturn = state;
2535};
2536
2537/**
rginda8ba33642011-12-14 12:31:31 -08002538 * Set the wraparound mode bit.
2539 *
2540 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2541 * to the start of the following row. Otherwise, the cursor is clamped to the
2542 * end of the screen and attempts to write past it are ignored.
2543 *
2544 * Defaults to on.
2545 *
2546 * @param {boolean} state True to set wraparound mode, false to unset.
2547 */
2548hterm.Terminal.prototype.setWraparound = function(state) {
2549 this.options_.wraparound = state;
2550};
2551
2552/**
2553 * Set the reverse-wraparound mode bit.
2554 *
2555 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2556 * to the end of the previous row. Otherwise, the cursor is clamped to column
2557 * 0.
2558 *
2559 * Defaults to off.
2560 *
2561 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2562 */
2563hterm.Terminal.prototype.setReverseWraparound = function(state) {
2564 this.options_.reverseWraparound = state;
2565};
2566
2567/**
2568 * Selects between the primary and alternate screens.
2569 *
2570 * If alternate mode is on, the alternate screen is active. Otherwise the
2571 * primary screen is active.
2572 *
2573 * Swapping screens has no effect on the scrollback buffer.
2574 *
2575 * Each screen maintains its own cursor position.
2576 *
2577 * Defaults to off.
2578 *
2579 * @param {boolean} state True to set alternate mode, false to unset.
2580 */
2581hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002582 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002583 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2584
rginda35c456b2012-02-09 17:29:05 -08002585 if (this.screen_.rowsArray.length &&
2586 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2587 // If the screen changed sizes while we were away, our rowIndexes may
2588 // be incorrect.
2589 var offset = this.scrollbackRows_.length;
2590 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002591 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002592 ary[i].rowIndex = offset + i;
2593 }
2594 }
rginda8ba33642011-12-14 12:31:31 -08002595
rginda35c456b2012-02-09 17:29:05 -08002596 this.realizeWidth_(this.screenSize.width);
2597 this.realizeHeight_(this.screenSize.height);
2598 this.scrollPort_.syncScrollHeight();
2599 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002600
rginda6d397402012-01-17 10:58:29 -08002601 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002602 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002603};
2604
2605/**
2606 * Set the cursor-blink mode bit.
2607 *
2608 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2609 * a visible cursor does not blink.
2610 *
2611 * You should make sure to turn blinking off if you're going to dispose of a
2612 * terminal, otherwise you'll leak a timeout.
2613 *
2614 * Defaults to on.
2615 *
2616 * @param {boolean} state True to set cursor-blink mode, false to unset.
2617 */
2618hterm.Terminal.prototype.setCursorBlink = function(state) {
2619 this.options_.cursorBlink = state;
2620
2621 if (!state && this.timeouts_.cursorBlink) {
2622 clearTimeout(this.timeouts_.cursorBlink);
2623 delete this.timeouts_.cursorBlink;
2624 }
2625
2626 if (this.options_.cursorVisible)
2627 this.setCursorVisible(true);
2628};
2629
2630/**
2631 * Set the cursor-visible mode bit.
2632 *
2633 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2634 *
2635 * Defaults to on.
2636 *
2637 * @param {boolean} state True to set cursor-visible mode, false to unset.
2638 */
2639hterm.Terminal.prototype.setCursorVisible = function(state) {
2640 this.options_.cursorVisible = state;
2641
2642 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002643 if (this.timeouts_.cursorBlink) {
2644 clearTimeout(this.timeouts_.cursorBlink);
2645 delete this.timeouts_.cursorBlink;
2646 }
rginda87b86462011-12-14 13:48:03 -08002647 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002648 return;
2649 }
2650
rginda87b86462011-12-14 13:48:03 -08002651 this.syncCursorPosition_();
2652
2653 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002654
2655 if (this.options_.cursorBlink) {
2656 if (this.timeouts_.cursorBlink)
2657 return;
2658
Robert Gindaea2183e2014-07-17 09:51:51 -07002659 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002660 } else {
2661 if (this.timeouts_.cursorBlink) {
2662 clearTimeout(this.timeouts_.cursorBlink);
2663 delete this.timeouts_.cursorBlink;
2664 }
2665 }
2666};
2667
2668/**
rginda87b86462011-12-14 13:48:03 -08002669 * Synchronizes the visible cursor and document selection with the current
2670 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002671 */
2672hterm.Terminal.prototype.syncCursorPosition_ = function() {
2673 var topRowIndex = this.scrollPort_.getTopRowIndex();
2674 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2675 var cursorRowIndex = this.scrollbackRows_.length +
2676 this.screen_.cursorPosition.row;
2677
2678 if (cursorRowIndex > bottomRowIndex) {
2679 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04002680 this.setCssVar('cursor-offset-row', '-1');
rginda8ba33642011-12-14 12:31:31 -08002681 return;
2682 }
2683
Robert Gindab837c052014-08-11 11:17:51 -07002684 if (this.options_.cursorVisible &&
2685 this.cursorNode_.style.display == 'none') {
2686 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2687 this.cursorNode_.style.display = '';
2688 }
2689
Mike Frysinger44c32202017-08-05 01:13:09 -04002690 // Position the cursor using CSS variable math. If we do the math in JS,
2691 // the float math will end up being more precise than the CSS which will
2692 // cause the cursor tracking to be off.
2693 this.setCssVar(
2694 'cursor-offset-row',
2695 `${cursorRowIndex - topRowIndex} + ` +
2696 `${this.scrollPort_.visibleRowTopMargin}px`);
2697 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08002698
2699 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04002700 '(' + this.screen_.cursorPosition.column +
2701 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08002702 ')');
2703
2704 // Update the caret for a11y purposes.
2705 var selection = this.document_.getSelection();
2706 if (selection && selection.isCollapsed)
2707 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002708};
2709
Robert Gindafb1be6a2013-12-11 11:56:22 -08002710/**
2711 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2712 * and character cell dimensions.
2713 */
Robert Ginda830583c2013-08-07 13:20:46 -07002714hterm.Terminal.prototype.restyleCursor_ = function() {
2715 var shape = this.cursorShape_;
2716
2717 if (this.cursorNode_.getAttribute('focus') == 'false') {
2718 // Always show a block cursor when unfocused.
2719 shape = hterm.Terminal.cursorShape.BLOCK;
2720 }
2721
2722 var style = this.cursorNode_.style;
2723
2724 switch (shape) {
2725 case hterm.Terminal.cursorShape.BEAM:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002726 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002727 style.backgroundColor = 'transparent';
2728 style.borderBottomStyle = null;
2729 style.borderLeftStyle = 'solid';
2730 break;
2731
2732 case hterm.Terminal.cursorShape.UNDERLINE:
2733 style.height = this.scrollPort_.characterSize.baseline + 'px';
2734 style.backgroundColor = 'transparent';
2735 style.borderBottomStyle = 'solid';
2736 // correct the size to put it exactly at the baseline
2737 style.borderLeftStyle = null;
2738 break;
2739
2740 default:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002741 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002742 style.backgroundColor = this.cursorColor_;
2743 style.borderBottomStyle = null;
2744 style.borderLeftStyle = null;
2745 break;
2746 }
2747};
2748
rginda8ba33642011-12-14 12:31:31 -08002749/**
2750 * Synchronizes the visible cursor with the current cursor coordinates.
2751 *
2752 * The sync will happen asynchronously, soon after the call stack winds down.
2753 * Multiple calls will be coalesced into a single sync.
2754 */
2755hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2756 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002757 return;
rginda8ba33642011-12-14 12:31:31 -08002758
2759 var self = this;
2760 this.timeouts_.syncCursor = setTimeout(function() {
2761 self.syncCursorPosition_();
2762 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002763 }, 0);
2764};
2765
rgindacc2996c2012-02-24 14:59:31 -08002766/**
rgindaf522ce02012-04-17 17:49:17 -07002767 * Show or hide the zoom warning.
2768 *
2769 * The zoom warning is a message warning the user that their browser zoom must
2770 * be set to 100% in order for hterm to function properly.
2771 *
2772 * @param {boolean} state True to show the message, false to hide it.
2773 */
2774hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2775 if (!this.zoomWarningNode_) {
2776 if (!state)
2777 return;
2778
2779 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002780 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07002781 this.zoomWarningNode_.style.cssText = (
2782 'color: black;' +
2783 'background-color: #ff2222;' +
2784 'font-size: large;' +
2785 'border-radius: 8px;' +
2786 'opacity: 0.75;' +
2787 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2788 'top: 0.5em;' +
2789 'right: 1.2em;' +
2790 'position: absolute;' +
2791 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002792 '-webkit-user-select: none;' +
2793 '-moz-text-size-adjust: none;' +
2794 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002795
2796 this.zoomWarningNode_.addEventListener('click', function(e) {
2797 this.parentNode.removeChild(this);
2798 });
rgindaf522ce02012-04-17 17:49:17 -07002799 }
2800
Robert Gindab4839c22013-02-28 16:52:10 -08002801 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2802 hterm.zoomWarningMessage,
2803 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2804
rgindaf522ce02012-04-17 17:49:17 -07002805 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2806
2807 if (state) {
2808 if (!this.zoomWarningNode_.parentNode)
2809 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2810 } else if (this.zoomWarningNode_.parentNode) {
2811 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2812 }
2813};
2814
2815/**
rgindacc2996c2012-02-24 14:59:31 -08002816 * Show the terminal overlay for a given amount of time.
2817 *
2818 * The terminal overlay appears in inverse video in a large font, centered
2819 * over the terminal. You should probably keep the overlay message brief,
2820 * since it's in a large font and you probably aren't going to check the size
2821 * of the terminal first.
2822 *
2823 * @param {string} msg The text (not HTML) message to display in the overlay.
2824 * @param {number} opt_timeout The amount of time to wait before fading out
2825 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2826 * stay up forever (or until the next overlay).
2827 */
2828hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002829 if (!this.overlayNode_) {
2830 if (!this.div_)
2831 return;
2832
2833 this.overlayNode_ = this.document_.createElement('div');
2834 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002835 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002836 'font-size: xx-large;' +
2837 'opacity: 0.75;' +
2838 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2839 'position: absolute;' +
2840 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002841 '-webkit-transition: opacity 180ms ease-in;' +
2842 '-moz-user-select: none;' +
2843 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002844
2845 this.overlayNode_.addEventListener('mousedown', function(e) {
2846 e.preventDefault();
2847 e.stopPropagation();
2848 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002849 }
2850
rginda9f5222b2012-03-05 11:53:28 -08002851 this.overlayNode_.style.color = this.prefs_.get('background-color');
2852 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2853 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2854
rgindaf0090c92012-02-10 14:58:52 -08002855 this.overlayNode_.textContent = msg;
2856 this.overlayNode_.style.opacity = '0.75';
2857
2858 if (!this.overlayNode_.parentNode)
2859 this.div_.appendChild(this.overlayNode_);
2860
Robert Ginda97769282013-02-01 15:30:30 -08002861 var divSize = hterm.getClientSize(this.div_);
2862 var overlaySize = hterm.getClientSize(this.overlayNode_);
2863
Robert Ginda8a59f762014-07-23 11:29:55 -07002864 this.overlayNode_.style.top =
2865 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08002866 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07002867 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08002868
rgindaf0090c92012-02-10 14:58:52 -08002869 if (this.overlayTimeout_)
2870 clearTimeout(this.overlayTimeout_);
2871
rgindacc2996c2012-02-24 14:59:31 -08002872 if (opt_timeout === null)
2873 return;
2874
Mike Frysingerb6cfded2017-09-18 00:39:31 -04002875 this.overlayTimeout_ = setTimeout(() => {
2876 this.overlayNode_.style.opacity = '0';
2877 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
2878 }, opt_timeout || 1500);
2879};
2880
2881/**
2882 * Hide the terminal overlay immediately.
2883 *
2884 * Useful when we show an overlay for an event with an unknown end time.
2885 */
2886hterm.Terminal.prototype.hideOverlay = function() {
2887 if (this.overlayTimeout_)
2888 clearTimeout(this.overlayTimeout_);
2889 this.overlayTimeout_ = null;
2890
2891 if (this.overlayNode_.parentNode)
2892 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
2893 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08002894};
2895
rginda4bba5e12012-06-20 16:15:30 -07002896/**
2897 * Paste from the system clipboard to the terminal.
2898 */
2899hterm.Terminal.prototype.paste = function() {
Mike Frysinger4628ad22017-07-20 02:44:20 -04002900 return hterm.pasteFromClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002901};
2902
2903/**
2904 * Copy a string to the system clipboard.
2905 *
2906 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05002907 *
2908 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07002909 */
2910hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002911 if (this.prefs_.get('enable-clipboard-notice'))
2912 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2913
rgindaa09e7332012-08-17 12:49:51 -07002914 var copySource = this.document_.createElement('pre');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002915 copySource.id = 'hterm:copy-to-clipboard-source';
rginda4bba5e12012-06-20 16:15:30 -07002916 copySource.textContent = str;
2917 copySource.style.cssText = (
2918 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002919 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07002920 'position: absolute;' +
2921 'top: -99px');
2922
2923 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002924
rginda4bba5e12012-06-20 16:15:30 -07002925 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002926 var anchorNode = selection.anchorNode;
2927 var anchorOffset = selection.anchorOffset;
2928 var focusNode = selection.focusNode;
2929 var focusOffset = selection.focusOffset;
2930
rginda4bba5e12012-06-20 16:15:30 -07002931 selection.selectAllChildren(copySource);
2932
rgindaa09e7332012-08-17 12:49:51 -07002933 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002934
Rob Spies56953412014-04-28 14:09:47 -07002935 // IE doesn't support selection.extend. This means that the selection
2936 // won't return on IE.
Robert Ginda6f4f0aa2014-07-28 10:21:39 -07002937 if (selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07002938 selection.collapse(anchorNode, anchorOffset);
2939 selection.extend(focusNode, focusOffset);
2940 }
rgindafaa74742012-08-21 13:34:03 -07002941
rginda4bba5e12012-06-20 16:15:30 -07002942 copySource.parentNode.removeChild(copySource);
2943};
2944
Evan Jones2600d4f2016-12-06 09:29:36 -05002945/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04002946 * Display an image.
2947 *
2948 * @param {Object} options The image to display.
2949 * @param {string=} options.name A human readable string for the image.
2950 * @param {string|number=} options.size The size (in bytes).
2951 * @param {boolean=} options.preserveAspectRatio Whether to preserve aspect.
2952 * @param {boolean=} options.inline Whether to display the image inline.
2953 * @param {string|number=} options.width The width of the image.
2954 * @param {string|number=} options.height The height of the image.
2955 * @param {string=} options.align Direction to align the image.
2956 * @param {string} options.uri The source URI for the image.
2957 */
2958hterm.Terminal.prototype.displayImage = function(options) {
2959 // Make sure we're actually given a resource to display.
2960 if (options.uri === undefined)
2961 return;
2962
2963 // Set up the defaults to simplify code below.
2964 if (!options.name)
2965 options.name = '';
2966
2967 // Has the user approved image display yet?
2968 if (this.allowImagesInline !== true) {
2969 this.newLine();
2970 const row = this.getRowNode(this.scrollbackRows_.length +
2971 this.getCursorRow() - 1);
2972
2973 if (this.allowImagesInline === false) {
2974 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
2975 'Inline Images Disabled');
2976 return;
2977 }
2978
2979 // Show a prompt.
2980 let button;
2981 const span = this.document_.createElement('span');
2982 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
2983 span.style.fontWeight = 'bold';
2984 span.style.borderWidth = '1px';
2985 span.style.borderStyle = 'dashed';
2986 button = this.document_.createElement('span');
2987 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
2988 button.style.marginLeft = '1em';
2989 button.style.borderWidth = '1px';
2990 button.style.borderStyle = 'solid';
2991 button.addEventListener('click', () => {
2992 this.prefs_.set('allow-images-inline', false);
2993 });
2994 span.appendChild(button);
2995 button = this.document_.createElement('span');
2996 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
2997 'allow this session');
2998 button.style.marginLeft = '1em';
2999 button.style.borderWidth = '1px';
3000 button.style.borderStyle = 'solid';
3001 button.addEventListener('click', () => {
3002 this.allowImagesInline = true;
3003 });
3004 span.appendChild(button);
3005 button = this.document_.createElement('span');
3006 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3007 button.style.marginLeft = '1em';
3008 button.style.borderWidth = '1px';
3009 button.style.borderStyle = 'solid';
3010 button.addEventListener('click', () => {
3011 this.prefs_.set('allow-images-inline', true);
3012 });
3013 span.appendChild(button);
3014
3015 row.appendChild(span);
3016 return;
3017 }
3018
3019 // See if we should show this object directly, or download it.
3020 if (options.inline) {
3021 const io = this.io.push();
3022 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
3023 'Loading $1 ...'), null);
3024
3025 // While we're loading the image, eat all the user's input.
3026 io.onVTKeystroke = io.sendString = () => {};
3027
3028 // Initialize this new image.
3029 const img = this.document_.createElement('img');
3030 img.src = options.uri;
3031 img.title = img.alt = options.name;
3032
3033 // Attach the image to the page to let it load/render. It won't stay here.
3034 // This is needed so it's visible and the DOM can calculate the height. If
3035 // the image is hidden or not in the DOM, the height is always 0.
3036 this.document_.body.appendChild(img);
3037
3038 // Wait for the image to finish loading before we try moving it to the
3039 // right place in the terminal.
3040 img.onload = () => {
3041 // Now that we have the image dimensions, figure out how to show it.
3042 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
3043 img.style.maxWidth = `${this.document_.body.clientWidth}px`;
3044 img.style.maxHeight = `${this.document_.body.clientHeight}px`;
3045
3046 // Parse a width/height specification.
3047 const parseDim = (dim, maxDim, cssVar) => {
3048 if (!dim || dim == 'auto')
3049 return '';
3050
3051 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3052 if (ary) {
3053 if (ary[2] == '%')
3054 return maxDim * parseInt(ary[1]) / 100 + 'px';
3055 else if (ary[2] == 'px')
3056 return dim;
3057 else
3058 return `calc(${dim} * var(${cssVar}))`;
3059 }
3060
3061 return '';
3062 };
3063 img.style.width =
3064 parseDim(options.width, this.document_.body.clientWidth,
3065 '--hterm-charsize-width');
3066 img.style.height =
3067 parseDim(options.height, this.document_.body.clientHeight,
3068 '--hterm-charsize-height');
3069
3070 // Figure out how many rows the image occupies, then add that many.
3071 // XXX: This count will be inaccurate if the font size changes on us.
3072 const padRows = Math.ceil(img.clientHeight /
3073 this.scrollPort_.characterSize.height);
3074 for (let i = 0; i < padRows; ++i)
3075 this.newLine();
3076
3077 // Update the max height in case the user shrinks the character size.
3078 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3079
3080 // Move the image to the last row. This way when we scroll up, it doesn't
3081 // disappear when the first row gets clipped. It will disappear when we
3082 // scroll down and the last row is clipped ...
3083 this.document_.body.removeChild(img);
3084 // Create a wrapper node so we can do an absolute in a relative position.
3085 // This helps with rounding errors between JS & CSS counts.
3086 const div = this.document_.createElement('div');
3087 div.style.position = 'relative';
3088 div.style.textAlign = options.align;
3089 img.style.position = 'absolute';
3090 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3091 div.appendChild(img);
3092 const row = this.getRowNode(this.scrollbackRows_.length +
3093 this.getCursorRow() - 1);
3094 row.appendChild(div);
3095
3096 io.hideOverlay();
3097 io.pop();
3098 };
3099
3100 // If we got a malformed image, give up.
3101 img.onerror = (e) => {
3102 this.document_.body.removeChild(img);
3103 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
3104 'Loading $1 failed ...'));
3105 io.pop();
3106 };
3107 } else {
3108 // We can't use chrome.downloads.download as that requires "downloads"
3109 // permissions, and that works only in extensions, not apps.
3110 const a = this.document_.createElement('a');
3111 a.href = options.uri;
3112 a.download = options.name;
3113 this.document_.body.appendChild(a);
3114 a.click();
3115 a.remove();
3116 }
3117};
3118
3119/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003120 * Returns the selected text, or null if no text is selected.
3121 *
3122 * @return {string|null}
3123 */
rgindaa09e7332012-08-17 12:49:51 -07003124hterm.Terminal.prototype.getSelectionText = function() {
3125 var selection = this.scrollPort_.selection;
3126 selection.sync();
3127
3128 if (selection.isCollapsed)
3129 return null;
3130
3131
3132 // Start offset measures from the beginning of the line.
3133 var startOffset = selection.startOffset;
3134 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003135
Robert Gindafdbb3f22012-09-06 20:23:06 -07003136 if (node.nodeName != 'X-ROW') {
3137 // If the selection doesn't start on an x-row node, then it must be
3138 // somewhere inside the x-row. Add any characters from previous siblings
3139 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003140
3141 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3142 // If node is the text node in a styled span, move up to the span node.
3143 node = node.parentNode;
3144 }
3145
Robert Gindafdbb3f22012-09-06 20:23:06 -07003146 while (node.previousSibling) {
3147 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003148 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003149 }
rgindaa09e7332012-08-17 12:49:51 -07003150 }
3151
3152 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08003153 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
3154 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003155 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003156
Robert Gindafdbb3f22012-09-06 20:23:06 -07003157 if (node.nodeName != 'X-ROW') {
3158 // If the selection doesn't end on an x-row node, then it must be
3159 // somewhere inside the x-row. Add any characters from following siblings
3160 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003161
3162 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3163 // If node is the text node in a styled span, move up to the span node.
3164 node = node.parentNode;
3165 }
3166
Robert Gindafdbb3f22012-09-06 20:23:06 -07003167 while (node.nextSibling) {
3168 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003169 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003170 }
rgindaa09e7332012-08-17 12:49:51 -07003171 }
3172
3173 var rv = this.getRowsText(selection.startRow.rowIndex,
3174 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003175 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003176};
3177
rginda4bba5e12012-06-20 16:15:30 -07003178/**
3179 * Copy the current selection to the system clipboard, then clear it after a
3180 * short delay.
3181 */
3182hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07003183 var text = this.getSelectionText();
3184 if (text != null)
3185 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07003186};
3187
rgindaf0090c92012-02-10 14:58:52 -08003188hterm.Terminal.prototype.overlaySize = function() {
3189 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
3190};
3191
rginda87b86462011-12-14 13:48:03 -08003192/**
3193 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3194 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003195 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003196 */
3197hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08003198 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08003199 this.scrollPort_.scrollRowToBottom(this.getRowCount());
3200
Robert Ginda8cb7d902013-06-20 14:37:18 -07003201 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08003202};
3203
3204/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003205 * Open the selected url.
3206 */
3207hterm.Terminal.prototype.openSelectedUrl_ = function() {
3208 var str = this.getSelectionText();
3209
3210 // If there is no selection, try and expand wherever they clicked.
3211 if (str == null) {
3212 this.screen_.expandSelection(this.document_.getSelection());
3213 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003214
3215 // If clicking in empty space, return.
3216 if (str == null)
3217 return;
Mike Frysinger70b94692017-01-26 18:57:50 -10003218 }
3219
3220 // Make sure URL is valid before opening.
3221 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
3222 return;
Mike Frysinger43472622017-06-26 18:11:07 -04003223
3224 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003225 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003226 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3227 // We have to whitelist a few protocols that lack authorities and thus
3228 // never use the //. Like mailto.
3229 switch (str.split(':', 1)[0]) {
3230 case 'mailto':
3231 break;
3232 default:
3233 str = 'http://' + str;
3234 break;
3235 }
3236 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003237
Mike Frysinger720fa832017-10-23 01:15:52 -04003238 hterm.openUrl(str);
Mike Frysinger70b94692017-01-26 18:57:50 -10003239}
3240
3241
3242/**
rgindad5613292012-06-19 15:40:37 -07003243 * Add the terminalRow and terminalColumn properties to mouse events and
3244 * then forward on to onMouse().
3245 *
3246 * The terminalRow and terminalColumn properties contain the (row, column)
3247 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003248 *
3249 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003250 */
3251hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003252 if (e.processedByTerminalHandler_) {
3253 // We register our event handlers on the document, as well as the cursor
3254 // and the scroll blocker. Mouse events that occur on the cursor or
3255 // scroll blocker will also appear on the document, but we don't want to
3256 // process them twice.
3257 //
3258 // We can't just prevent bubbling because that has other side effects, so
3259 // we decorate the event object with this property instead.
3260 return;
3261 }
3262
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003263 var reportMouseEvents = (!this.defeatMouseReports_ &&
3264 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3265
rgindafaa74742012-08-21 13:34:03 -07003266 e.processedByTerminalHandler_ = true;
3267
Robert Gindaeda48db2014-07-17 09:25:30 -07003268 // One based row/column stored on the mouse event.
3269 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
3270 this.scrollPort_.characterSize.height) + 1;
3271 e.terminalColumn = parseInt(e.clientX /
3272 this.scrollPort_.characterSize.width) + 1;
3273
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003274 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3275 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003276 return;
3277 }
3278
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003279 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003280 // If the cursor is visible and we're not sending mouse events to the
3281 // host app, then we want to hide the terminal cursor when the mouse
3282 // cursor is over top. This keeps the terminal cursor from interfering
3283 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003284 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3285 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3286 this.cursorNode_.style.display = 'none';
3287 } else if (this.cursorNode_.style.display == 'none') {
3288 this.cursorNode_.style.display = '';
3289 }
3290 }
rgindad5613292012-06-19 15:40:37 -07003291
Robert Ginda928cf632014-03-05 15:07:41 -08003292 if (e.type == 'mousedown') {
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003293 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003294 // If VT mouse reporting is disabled, or has been defeated with
3295 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003296 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003297 this.setSelectionEnabled(true);
3298 } else {
3299 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003300 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003301 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003302 this.setSelectionEnabled(false);
3303 e.preventDefault();
3304 }
3305 }
3306
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003307 if (!reportMouseEvents) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003308 if (e.type == 'dblclick' && this.copyOnSelect) {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003309 this.screen_.expandSelection(this.document_.getSelection());
Robert Ginda15ed4902016-07-12 10:43:22 -07003310 this.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07003311 }
3312
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003313 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003314 // Debounce this event with the dblclick event. If you try to doubleclick
3315 // a URL to open it, Chrome will fire click then dblclick, but we won't
3316 // have expanded the selection text at the first click event.
3317 clearTimeout(this.timeouts_.openUrl);
3318 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3319 500);
3320 return;
3321 }
3322
Mike Frysinger847577f2017-05-23 23:25:57 -04003323 if (e.type == 'mousedown') {
3324 if ((this.mouseRightClickPaste && e.button == 2 /* right button */) ||
Mike Frysinger2edd3612017-05-24 00:54:39 -04003325 e.button == this.mousePasteButton) {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003326 if (!this.paste())
Mike Frysinger05a57f02017-08-27 17:48:55 -04003327 console.warn('Could not paste manually due to web restrictions');
Mike Frysinger847577f2017-05-23 23:25:57 -04003328 }
3329 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003330
Mike Frysinger2edd3612017-05-24 00:54:39 -04003331 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003332 !this.document_.getSelection().isCollapsed) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003333 this.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003334 }
3335
3336 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3337 this.scrollBlockerNode_.engaged) {
3338 // Disengage the scroll-blocker after one of these events.
3339 this.scrollBlockerNode_.engaged = false;
3340 this.scrollBlockerNode_.style.top = '-99px';
3341 }
3342
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003343 // Emulate arrow key presses via scroll wheel events.
3344 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3345 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003346 if (e.type == 'wheel') {
3347 var delta = this.scrollPort_.scrollWheelDelta(e);
3348 var lines = lib.f.smartFloorDivide(
3349 Math.abs(delta), this.scrollPort_.characterSize.height);
3350
3351 var data = '\x1bO' + (delta < 0 ? 'B' : 'A');
3352 this.io.sendString(data.repeat(lines));
3353
3354 e.preventDefault();
3355 }
3356 }
Robert Ginda928cf632014-03-05 15:07:41 -08003357 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003358 if (!this.scrollBlockerNode_.engaged) {
3359 if (e.type == 'mousedown') {
3360 // Move the scroll-blocker into place if we want to keep the scrollport
3361 // from scrolling.
3362 this.scrollBlockerNode_.engaged = true;
3363 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3364 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3365 } else if (e.type == 'mousemove') {
3366 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3367 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003368 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003369 e.preventDefault();
3370 }
3371 }
Robert Ginda928cf632014-03-05 15:07:41 -08003372
3373 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003374 }
3375
Robert Ginda928cf632014-03-05 15:07:41 -08003376 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3377 // Restore this on mouseup in case it was temporarily defeated with a
3378 // alt-mousedown. Only do this when the selection is empty so that
3379 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003380 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003381 }
rgindad5613292012-06-19 15:40:37 -07003382};
3383
3384/**
3385 * Clients should override this if they care to know about mouse events.
3386 *
3387 * The event parameter will be a normal DOM mouse click event with additional
3388 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003389 *
3390 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003391 */
3392hterm.Terminal.prototype.onMouse = function(e) { };
3393
3394/**
rginda8e92a692012-05-20 19:37:20 -07003395 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003396 *
3397 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003398 */
Rob Spies06533ba2014-04-24 11:20:37 -07003399hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3400 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003401 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003402
3403 if (this.reportFocus) {
3404 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O')
3405 }
3406
Michael Kelly485ecd12014-06-09 11:41:56 -04003407 if (focused === true)
3408 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003409};
3410
3411/**
rginda8ba33642011-12-14 12:31:31 -08003412 * React when the ScrollPort is scrolled.
3413 */
3414hterm.Terminal.prototype.onScroll_ = function() {
3415 this.scheduleSyncCursorPosition_();
3416};
3417
3418/**
rginda9846e2f2012-01-27 13:53:33 -08003419 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003420 *
3421 * @param {Event} e The DOM paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003422 */
3423hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07003424 var data = e.text.replace(/\n/mg, '\r');
Connor Hegartyf525ceb2014-09-03 13:36:03 -07003425 data = this.keyboard.encode(data);
Robert Gindaa063b202014-07-21 11:08:25 -07003426 if (this.options_.bracketedPaste)
3427 data = '\x1b[200~' + data + '\x1b[201~';
3428
3429 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003430};
3431
3432/**
rgindaa09e7332012-08-17 12:49:51 -07003433 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003434 *
3435 * @param {Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003436 */
3437hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003438 if (!this.useDefaultWindowCopy) {
3439 e.preventDefault();
3440 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3441 }
rgindaa09e7332012-08-17 12:49:51 -07003442};
3443
3444/**
rginda8ba33642011-12-14 12:31:31 -08003445 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003446 *
3447 * Note: This function should not directly contain code that alters the internal
3448 * state of the terminal. That kind of code belongs in realizeWidth or
3449 * realizeHeight, so that it can be executed synchronously in the case of a
3450 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003451 */
3452hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003453 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003454 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003455 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003456 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003457
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003458 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003459 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003460 // gets removed from the document or during the initial load, and we can't
3461 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003462 // This can also happen if called before the scrollPort calculates the
3463 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003464 return;
3465 }
3466
rgindaa8ba17d2012-08-15 14:41:10 -07003467 var isNewSize = (columnCount != this.screenSize.width ||
3468 rowCount != this.screenSize.height);
3469
3470 // We do this even if the size didn't change, just to be sure everything is
3471 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003472 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003473 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003474
3475 if (isNewSize)
3476 this.overlaySize();
3477
Robert Gindafb1be6a2013-12-11 11:56:22 -08003478 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003479 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08003480};
3481
3482/**
3483 * Service the cursor blink timeout.
3484 */
3485hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003486 if (!this.options_.cursorBlink) {
3487 delete this.timeouts_.cursorBlink;
3488 return;
3489 }
3490
Robert Ginda830583c2013-08-07 13:20:46 -07003491 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3492 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003493 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003494 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3495 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003496 } else {
rginda87b86462011-12-14 13:48:03 -08003497 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003498 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3499 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003500 }
3501};
David Reveman8f552492012-03-28 12:18:41 -04003502
3503/**
3504 * Set the scrollbar-visible mode bit.
3505 *
3506 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3507 * Otherwise it will not.
3508 *
3509 * Defaults to on.
3510 *
3511 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3512 */
3513hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3514 this.scrollPort_.setScrollbarVisible(state);
3515};
Michael Kelly485ecd12014-06-09 11:41:56 -04003516
3517/**
Rob Spies49039e52014-12-17 13:40:04 -08003518 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003519 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003520 *
3521 * Defaults to 1.
3522 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003523 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003524 */
3525hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3526 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3527};
3528
3529/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003530 * Close all web notifications created by terminal bells.
3531 */
3532hterm.Terminal.prototype.closeBellNotifications_ = function() {
3533 this.bellNotificationList_.forEach(function(n) {
3534 n.close();
3535 });
3536 this.bellNotificationList_.length = 0;
3537};