blob: e43739d29c06ad0f805a06755b23cda484c6a715 [file] [log] [blame]
rginda87b86462011-12-14 13:48:03 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rginda8ba33642011-12-14 12:31:31 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rgindacbbd7482012-06-13 15:06:16 -07005'use strict';
6
Masaya Suzuki273aa982014-05-31 07:25:55 +09007lib.rtdep('lib.colors', 'lib.PreferenceManager', 'lib.resource', 'lib.wc',
Rob Spiesf4e90e82015-01-28 12:10:13 -08008 'lib.f', 'hterm.Keyboard', 'hterm.Options', 'hterm.PreferenceManager',
Ricky Liang48f05cb2013-12-31 23:35:29 +08009 'hterm.Screen', 'hterm.ScrollPort', 'hterm.Size',
10 'hterm.TextAttributes', 'hterm.VT');
rgindacbbd7482012-06-13 15:06:16 -070011
rginda8ba33642011-12-14 12:31:31 -080012/**
13 * Constructor for the Terminal class.
14 *
15 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
16 * classes to provide the complete terminal functionality.
17 *
18 * There are a number of lower-level Terminal methods that can be called
19 * directly to manipulate the cursor, text, scroll region, and other terminal
20 * attributes. However, the primary method is interpret(), which parses VT
21 * escape sequences and invokes the appropriate Terminal methods.
22 *
23 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
24 *
25 * TODO(rginda): Eventually we're going to need to support characters which are
26 * displayed twice as wide as standard latin characters. This is to support
27 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080028 *
Robert Ginda57f03b42012-09-13 11:02:48 -070029 * @param {string} opt_profileId Optional preference profile name. If not
rginda9f5222b2012-03-05 11:53:28 -080030 * provided, defaults to 'default'.
rginda8ba33642011-12-14 12:31:31 -080031 */
Robert Ginda57f03b42012-09-13 11:02:48 -070032hterm.Terminal = function(opt_profileId) {
33 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080034
rginda8ba33642011-12-14 12:31:31 -080035 // Two screen instances.
36 this.primaryScreen_ = new hterm.Screen();
37 this.alternateScreen_ = new hterm.Screen();
38
39 // The "current" screen.
40 this.screen_ = this.primaryScreen_;
41
rginda8ba33642011-12-14 12:31:31 -080042 // The local notion of the screen size. ScreenBuffers also have a size which
43 // indicates their present size. During size changes, the two may disagree.
44 // Also, the inactive screen's size is not altered until it is made the active
45 // screen.
46 this.screenSize = new hterm.Size(0, 0);
47
rginda8ba33642011-12-14 12:31:31 -080048 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080049 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080050 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
51 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080052 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070053 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080054
rginda87b86462011-12-14 13:48:03 -080055 // The div that contains this terminal.
56 this.div_ = null;
57
rgindac9bc5502012-01-18 11:48:44 -080058 // The document that contains the scrollPort. Defaulted to the global
59 // document here so that the terminal is functional even if it hasn't been
60 // inserted into a document yet, but re-set in decorate().
61 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080062
rginda8ba33642011-12-14 12:31:31 -080063 // The rows that have scrolled off screen and are no longer addressable.
64 this.scrollbackRows_ = [];
65
rgindac9bc5502012-01-18 11:48:44 -080066 // Saved tab stops.
67 this.tabStops_ = [];
68
David Benjamin66e954d2012-05-05 21:08:12 -040069 // Keep track of whether default tab stops have been erased; after a TBC
70 // clears all tab stops, defaults aren't restored on resize until a reset.
71 this.defaultTabStops = true;
72
rginda8ba33642011-12-14 12:31:31 -080073 // The VT's notion of the top and bottom rows. Used during some VT
74 // cursor positioning and scrolling commands.
75 this.vtScrollTop_ = null;
76 this.vtScrollBottom_ = null;
77
78 // The DIV element for the visible cursor.
79 this.cursorNode_ = null;
80
Robert Ginda830583c2013-08-07 13:20:46 -070081 // The current cursor shape of the terminal.
82 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
83
84 // The current color of the cursor.
85 this.cursorColor_ = null;
86
Robert Gindaea2183e2014-07-17 09:51:51 -070087 // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded.
88 this.cursorBlinkCycle_ = [100, 100];
89
90 // Pre-bound onCursorBlink_ handler, so we don't have to do this for each
91 // cursor on/off servicing.
92 this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
93
rginda9f5222b2012-03-05 11:53:28 -080094 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070095 // each output and keystroke. They are initialized by the preference manager.
Robert Ginda8cb7d902013-06-20 14:37:18 -070096 this.backgroundColor_ = null;
97 this.foregroundColor_ = null;
Robert Ginda57f03b42012-09-13 11:02:48 -070098 this.scrollOnOutput_ = null;
99 this.scrollOnKeystroke_ = null;
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400100 this.scrollWheelArrowKeys_ = null;
rginda9f5222b2012-03-05 11:53:28 -0800101
Robert Ginda6aec7eb2015-06-16 10:31:30 -0700102 // True if we should override mouse event reporting to allow local selection.
103 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -0800104
rgindaf0090c92012-02-10 14:58:52 -0800105 // Terminal bell sound.
106 this.bellAudio_ = this.document_.createElement('audio');
Mike Frysingerd826f1a2017-07-06 16:20:06 -0400107 this.bellAudio_.id = 'hterm:bell-audio';
rgindaf0090c92012-02-10 14:58:52 -0800108 this.bellAudio_.setAttribute('preload', 'auto');
109
Michael Kelly485ecd12014-06-09 11:41:56 -0400110 // All terminal bell notifications that have been generated (not necessarily
111 // shown).
112 this.bellNotificationList_ = [];
113
114 // Whether we have permission to display notifications.
115 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400116
rginda6d397402012-01-17 10:58:29 -0800117 // Cursor position and attributes saved with DECSC.
118 this.savedOptions_ = {};
119
rginda8ba33642011-12-14 12:31:31 -0800120 // The current mode bits for the terminal.
121 this.options_ = new hterm.Options();
122
123 // Timeouts we might need to clear.
124 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800125
126 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800127 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800128
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800129 this.saveCursorAndState(true);
130
Zhu Qunying30d40712017-03-14 16:27:00 -0700131 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800132 this.keyboard = new hterm.Keyboard(this);
133
rginda87b86462011-12-14 13:48:03 -0800134 // General IO interface that can be given to third parties without exposing
135 // the entire terminal object.
136 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800137
rgindad5613292012-06-19 15:40:37 -0700138 // True if mouse-click-drag should scroll the terminal.
139 this.enableMouseDragScroll = true;
140
Robert Ginda57f03b42012-09-13 11:02:48 -0700141 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400142 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700143 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700144
Zhu Qunying30d40712017-03-14 16:27:00 -0700145 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700146 this.useDefaultWindowCopy = false;
147
148 this.clearSelectionAfterCopy = true;
149
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400150 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800151 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700152
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400153 // Whether we allow images to be shown.
154 this.allowImagesInline = null;
155
Gabriel Holodake8a09be2017-10-10 01:07:11 -0400156 this.reportFocus = false;
157
Robert Ginda57f03b42012-09-13 11:02:48 -0700158 this.setProfile(opt_profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500159 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800160};
161
162/**
Robert Ginda830583c2013-08-07 13:20:46 -0700163 * Possible cursor shapes.
164 */
165hterm.Terminal.cursorShape = {
166 BLOCK: 'BLOCK',
167 BEAM: 'BEAM',
168 UNDERLINE: 'UNDERLINE'
169};
170
171/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700172 * Clients should override this to be notified when the terminal is ready
173 * for use.
174 *
175 * The terminal initialization is asynchronous, and shouldn't be used before
176 * this method is called.
177 */
178hterm.Terminal.prototype.onTerminalReady = function() { };
179
180/**
rginda35c456b2012-02-09 17:29:05 -0800181 * Default tab with of 8 to match xterm.
182 */
183hterm.Terminal.prototype.tabWidth = 8;
184
185/**
rginda9f5222b2012-03-05 11:53:28 -0800186 * Select a preference profile.
187 *
188 * This will load the terminal preferences for the given profile name and
189 * associate subsequent preference changes with the new preference profile.
190 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500191 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800192 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700193 * @param {function} opt_callback Optional callback to invoke when the profile
194 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800195 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700196hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
197 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800198
Robert Ginda57f03b42012-09-13 11:02:48 -0700199 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800200
Robert Ginda57f03b42012-09-13 11:02:48 -0700201 if (this.prefs_)
202 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800203
Robert Ginda57f03b42012-09-13 11:02:48 -0700204 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
205 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800206 'alt-gr-mode': function(v) {
207 if (v == null) {
208 if (navigator.language.toLowerCase() == 'en-us') {
209 v = 'none';
210 } else {
211 v = 'right-alt';
212 }
213 } else if (typeof v == 'string') {
214 v = v.toLowerCase();
215 } else {
216 v = 'none';
217 }
218
219 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v))
220 v = 'none';
221
222 terminal.keyboard.altGrMode = v;
223 },
224
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700225 'alt-backspace-is-meta-backspace': function(v) {
226 terminal.keyboard.altBackspaceIsMetaBackspace = v;
227 },
228
Robert Ginda57f03b42012-09-13 11:02:48 -0700229 'alt-is-meta': function(v) {
230 terminal.keyboard.altIsMeta = v;
231 },
232
233 'alt-sends-what': function(v) {
234 if (!/^(escape|8-bit|browser-key)$/.test(v))
235 v = 'escape';
236
237 terminal.keyboard.altSendsWhat = v;
238 },
239
240 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800241 var ary = v.match(/^lib-resource:(\S+)/);
242 if (ary) {
243 terminal.bellAudio_.setAttribute('src',
244 lib.resource.getDataUrl(ary[1]));
245 } else {
246 terminal.bellAudio_.setAttribute('src', v);
247 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700248 },
249
Michael Kelly485ecd12014-06-09 11:41:56 -0400250 'desktop-notification-bell': function(v) {
251 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700252 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400253 Notification.permission === 'granted';
254 if (!terminal.desktopNotificationBell_) {
255 // Note: We don't call Notification.requestPermission here because
256 // Chrome requires the call be the result of a user action (such as an
257 // onclick handler), and pref listeners are run asynchronously.
258 //
259 // A way of working around this would be to display a dialog in the
260 // terminal with a "click-to-request-permission" button.
261 console.warn('desktop-notification-bell is true but we do not have ' +
262 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400263 }
264 } else {
265 terminal.desktopNotificationBell_ = false;
266 }
267 },
268
Robert Ginda57f03b42012-09-13 11:02:48 -0700269 'background-color': function(v) {
270 terminal.setBackgroundColor(v);
271 },
272
273 'background-image': function(v) {
274 terminal.scrollPort_.setBackgroundImage(v);
275 },
276
277 'background-size': function(v) {
278 terminal.scrollPort_.setBackgroundSize(v);
279 },
280
281 'background-position': function(v) {
282 terminal.scrollPort_.setBackgroundPosition(v);
283 },
284
285 'backspace-sends-backspace': function(v) {
286 terminal.keyboard.backspaceSendsBackspace = v;
287 },
288
Brad Town18654b62015-03-12 00:27:45 -0700289 'character-map-overrides': function(v) {
290 if (!(v == null || v instanceof Object)) {
291 console.warn('Preference character-map-modifications is not an ' +
292 'object: ' + v);
293 return;
294 }
295
Mike Frysinger095d4062017-06-14 00:29:48 -0700296 terminal.vt.characterMaps.reset();
297 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700298 },
299
Robert Ginda57f03b42012-09-13 11:02:48 -0700300 'cursor-blink': function(v) {
301 terminal.setCursorBlink(!!v);
302 },
303
Robert Gindaea2183e2014-07-17 09:51:51 -0700304 'cursor-blink-cycle': function(v) {
305 if (v instanceof Array &&
306 typeof v[0] == 'number' &&
307 typeof v[1] == 'number') {
308 terminal.cursorBlinkCycle_ = v;
309 } else if (typeof v == 'number') {
310 terminal.cursorBlinkCycle_ = [v, v];
311 } else {
312 // Fast blink indicates an error.
313 terminal.cursorBlinkCycle_ = [100, 100];
314 }
315 },
316
Robert Ginda57f03b42012-09-13 11:02:48 -0700317 'cursor-color': function(v) {
318 terminal.setCursorColor(v);
319 },
320
321 'color-palette-overrides': function(v) {
322 if (!(v == null || v instanceof Object || v instanceof Array)) {
323 console.warn('Preference color-palette-overrides is not an array or ' +
324 'object: ' + v);
325 return;
rginda9f5222b2012-03-05 11:53:28 -0800326 }
rginda9f5222b2012-03-05 11:53:28 -0800327
Robert Ginda57f03b42012-09-13 11:02:48 -0700328 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700329
Robert Ginda57f03b42012-09-13 11:02:48 -0700330 if (v) {
331 for (var key in v) {
332 var i = parseInt(key);
333 if (isNaN(i) || i < 0 || i > 255) {
334 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
335 continue;
336 }
337
338 if (v[i]) {
339 var rgb = lib.colors.normalizeCSS(v[i]);
340 if (rgb)
341 lib.colors.colorPalette[i] = rgb;
342 }
343 }
rginda30f20f62012-04-05 16:36:19 -0700344 }
rginda30f20f62012-04-05 16:36:19 -0700345
Evan Jones5f9df812016-12-06 09:38:58 -0500346 terminal.primaryScreen_.textAttributes.resetColorPalette();
Robert Ginda57f03b42012-09-13 11:02:48 -0700347 terminal.alternateScreen_.textAttributes.resetColorPalette();
348 },
rginda30f20f62012-04-05 16:36:19 -0700349
Robert Ginda57f03b42012-09-13 11:02:48 -0700350 'copy-on-select': function(v) {
351 terminal.copyOnSelect = !!v;
352 },
rginda9f5222b2012-03-05 11:53:28 -0800353
Rob Spies0bec09b2014-06-06 15:58:09 -0700354 'use-default-window-copy': function(v) {
355 terminal.useDefaultWindowCopy = !!v;
356 },
357
358 'clear-selection-after-copy': function(v) {
359 terminal.clearSelectionAfterCopy = !!v;
360 },
361
Robert Ginda7e5e9522014-03-14 12:23:58 -0700362 'ctrl-plus-minus-zero-zoom': function(v) {
363 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
364 },
365
Robert Gindafb5a3f92014-05-13 14:12:00 -0700366 'ctrl-c-copy': function(v) {
367 terminal.keyboard.ctrlCCopy = v;
368 },
369
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100370 'ctrl-v-paste': function(v) {
371 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700372 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100373 },
374
Masaya Suzuki273aa982014-05-31 07:25:55 +0900375 'east-asian-ambiguous-as-two-column': function(v) {
376 lib.wc.regardCjkAmbiguous = v;
377 },
378
Robert Ginda57f03b42012-09-13 11:02:48 -0700379 'enable-8-bit-control': function(v) {
380 terminal.vt.enable8BitControl = !!v;
381 },
rginda30f20f62012-04-05 16:36:19 -0700382
Robert Ginda57f03b42012-09-13 11:02:48 -0700383 'enable-bold': function(v) {
384 terminal.syncBoldSafeState();
385 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400386
Robert Ginda3e278d72014-03-25 13:18:51 -0700387 'enable-bold-as-bright': function(v) {
388 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
389 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
390 },
391
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400392 'enable-blink': function(v) {
393 terminal.syncBlinkState();
394 },
395
Robert Ginda57f03b42012-09-13 11:02:48 -0700396 'enable-clipboard-write': function(v) {
397 terminal.vt.enableClipboardWrite = !!v;
398 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400399
Robert Ginda3755e752013-05-31 13:34:09 -0700400 'enable-dec12': function(v) {
401 terminal.vt.enableDec12 = !!v;
402 },
403
Robert Ginda57f03b42012-09-13 11:02:48 -0700404 'font-family': function(v) {
405 terminal.syncFontFamily();
406 },
rginda30f20f62012-04-05 16:36:19 -0700407
Robert Ginda57f03b42012-09-13 11:02:48 -0700408 'font-size': function(v) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500409 v = parseInt(v);
410 if (v <= 0) {
411 console.error(`Invalid font size: ${v}`);
412 return;
413 }
414
Robert Ginda57f03b42012-09-13 11:02:48 -0700415 terminal.setFontSize(v);
416 },
rginda9875d902012-08-20 16:21:57 -0700417
Robert Ginda57f03b42012-09-13 11:02:48 -0700418 'font-smoothing': function(v) {
419 terminal.syncFontFamily();
420 },
rgindade84e382012-04-20 15:39:31 -0700421
Robert Ginda57f03b42012-09-13 11:02:48 -0700422 'foreground-color': function(v) {
423 terminal.setForegroundColor(v);
424 },
rginda30f20f62012-04-05 16:36:19 -0700425
Robert Ginda57f03b42012-09-13 11:02:48 -0700426 'home-keys-scroll': function(v) {
427 terminal.keyboard.homeKeysScroll = v;
428 },
rginda4bba5e12012-06-20 16:15:30 -0700429
Robert Gindaa8165692015-06-15 14:46:31 -0700430 'keybindings': function(v) {
431 terminal.keyboard.bindings.clear();
432
433 if (!v)
434 return;
435
436 if (!(v instanceof Object)) {
437 console.error('Error in keybindings preference: Expected object');
438 return;
439 }
440
441 try {
442 terminal.keyboard.bindings.addBindings(v);
443 } catch (ex) {
444 console.error('Error in keybindings preference: ' + ex);
445 }
446 },
447
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700448 'media-keys-are-fkeys': function(v) {
449 terminal.keyboard.mediaKeysAreFKeys = v;
450 },
451
Robert Ginda57f03b42012-09-13 11:02:48 -0700452 'meta-sends-escape': function(v) {
453 terminal.keyboard.metaSendsEscape = v;
454 },
rginda30f20f62012-04-05 16:36:19 -0700455
Mike Frysinger847577f2017-05-23 23:25:57 -0400456 'mouse-right-click-paste': function(v) {
457 terminal.mouseRightClickPaste = v;
458 },
459
Robert Ginda57f03b42012-09-13 11:02:48 -0700460 'mouse-paste-button': function(v) {
461 terminal.syncMousePasteButton();
462 },
rgindaa8ba17d2012-08-15 14:41:10 -0700463
Robert Gindae76aa9f2014-03-14 12:29:12 -0700464 'page-keys-scroll': function(v) {
465 terminal.keyboard.pageKeysScroll = v;
466 },
467
Robert Ginda40932892012-12-10 17:26:40 -0800468 'pass-alt-number': function(v) {
469 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800470 // Let Alt-1..9 pass to the browser (to control tab switching) on
471 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500472 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800473 }
474
475 terminal.passAltNumber = v;
476 },
477
478 'pass-ctrl-number': function(v) {
479 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800480 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
481 // non-OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500482 v = (hterm.os != 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800483 }
484
485 terminal.passCtrlNumber = v;
486 },
487
488 'pass-meta-number': function(v) {
489 if (v == null) {
Robert Ginda40932892012-12-10 17:26:40 -0800490 // Let Meta-1..9 pass to the browser (to control tab switching) on
491 // OS X systems, or if hterm is not opened in an app window.
Mike Frysingeree81a002017-12-12 16:14:53 -0500492 v = (hterm.os == 'mac' && hterm.windowType != 'popup');
Robert Ginda40932892012-12-10 17:26:40 -0800493 }
494
495 terminal.passMetaNumber = v;
496 },
497
Marius Schilder77857b32014-05-14 16:21:26 -0700498 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700499 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700500 },
501
Robert Ginda8cb7d902013-06-20 14:37:18 -0700502 'receive-encoding': function(v) {
503 if (!(/^(utf-8|raw)$/).test(v)) {
504 console.warn('Invalid value for "receive-encoding": ' + v);
505 v = 'utf-8';
506 }
507
508 terminal.vt.characterEncoding = v;
509 },
510
Robert Ginda57f03b42012-09-13 11:02:48 -0700511 'scroll-on-keystroke': function(v) {
512 terminal.scrollOnKeystroke_ = v;
513 },
rginda9f5222b2012-03-05 11:53:28 -0800514
Robert Ginda57f03b42012-09-13 11:02:48 -0700515 'scroll-on-output': function(v) {
516 terminal.scrollOnOutput_ = v;
517 },
rginda30f20f62012-04-05 16:36:19 -0700518
Robert Ginda57f03b42012-09-13 11:02:48 -0700519 'scrollbar-visible': function(v) {
520 terminal.setScrollbarVisible(v);
521 },
rginda9f5222b2012-03-05 11:53:28 -0800522
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400523 'scroll-wheel-may-send-arrow-keys': function(v) {
524 terminal.scrollWheelArrowKeys_ = v;
525 },
526
Rob Spies49039e52014-12-17 13:40:04 -0800527 'scroll-wheel-move-multiplier': function(v) {
528 terminal.setScrollWheelMoveMultipler(v);
529 },
530
Robert Ginda8cb7d902013-06-20 14:37:18 -0700531 'send-encoding': function(v) {
532 if (!(/^(utf-8|raw)$/).test(v)) {
533 console.warn('Invalid value for "send-encoding": ' + v);
534 v = 'utf-8';
535 }
536
537 terminal.keyboard.characterEncoding = v;
538 },
539
Robert Ginda57f03b42012-09-13 11:02:48 -0700540 'shift-insert-paste': function(v) {
541 terminal.keyboard.shiftInsertPaste = v;
542 },
rginda9f5222b2012-03-05 11:53:28 -0800543
Mike Frysingera7768922017-07-28 15:00:12 -0400544 'terminal-encoding': function(v) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400545 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400546 },
547
Robert Gindae76aa9f2014-03-14 12:29:12 -0700548 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400549 terminal.scrollPort_.setUserCssUrl(v);
550 },
551
552 'user-css-text': function(v) {
553 terminal.scrollPort_.setUserCssText(v);
554 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400555
556 'word-break-match-left': function(v) {
557 terminal.primaryScreen_.wordBreakMatchLeft = v;
558 terminal.alternateScreen_.wordBreakMatchLeft = v;
559 },
560
561 'word-break-match-right': function(v) {
562 terminal.primaryScreen_.wordBreakMatchRight = v;
563 terminal.alternateScreen_.wordBreakMatchRight = v;
564 },
565
566 'word-break-match-middle': function(v) {
567 terminal.primaryScreen_.wordBreakMatchMiddle = v;
568 terminal.alternateScreen_.wordBreakMatchMiddle = v;
569 },
Mike Frysinger8c5a0a42017-04-21 11:38:27 -0400570
571 'allow-images-inline': function(v) {
572 terminal.allowImagesInline = v;
573 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700574 });
rginda30f20f62012-04-05 16:36:19 -0700575
Robert Ginda57f03b42012-09-13 11:02:48 -0700576 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800577 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700578
579 if (opt_callback)
580 opt_callback();
581 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800582};
583
Rob Spies56953412014-04-28 14:09:47 -0700584
585/**
586 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500587 *
588 * @return {hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700589 */
590hterm.Terminal.prototype.getPrefs = function() {
591 return this.prefs_;
592};
593
Robert Gindaa063b202014-07-21 11:08:25 -0700594/**
595 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500596 *
597 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700598 */
599hterm.Terminal.prototype.setBracketedPaste = function(state) {
600 this.options_.bracketedPaste = state;
601};
Rob Spies56953412014-04-28 14:09:47 -0700602
rginda8e92a692012-05-20 19:37:20 -0700603/**
604 * Set the color for the cursor.
605 *
606 * If you want this setting to persist, set it through prefs_, rather than
607 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500608 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500609 * @param {string=} color The color to set. If not defined, we reset to the
610 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700611 */
612hterm.Terminal.prototype.setCursorColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500613 if (color === undefined)
614 color = this.prefs_.get('cursor-color');
615
Robert Ginda830583c2013-08-07 13:20:46 -0700616 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700617 this.cursorNode_.style.backgroundColor = color;
618 this.cursorNode_.style.borderColor = color;
619};
620
621/**
622 * Return the current cursor color as a string.
Evan Jones2600d4f2016-12-06 09:29:36 -0500623 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700624 */
625hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700626 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700627};
628
629/**
rgindad5613292012-06-19 15:40:37 -0700630 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500631 *
632 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700633 */
634hterm.Terminal.prototype.setSelectionEnabled = function(state) {
635 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700636};
637
638/**
rginda8e92a692012-05-20 19:37:20 -0700639 * Set the background color.
640 *
641 * If you want this setting to persist, set it through prefs_, rather than
642 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500643 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500644 * @param {string=} color The color to set. If not defined, we reset to the
645 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700646 */
647hterm.Terminal.prototype.setBackgroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500648 if (color === undefined)
649 color = this.prefs_.get('background-color');
650
rgindacbbd7482012-06-13 15:06:16 -0700651 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700652 this.primaryScreen_.textAttributes.setDefaults(
653 this.foregroundColor_, this.backgroundColor_);
654 this.alternateScreen_.textAttributes.setDefaults(
655 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700656 this.scrollPort_.setBackgroundColor(color);
657};
658
rginda9f5222b2012-03-05 11:53:28 -0800659/**
660 * Return the current terminal background color.
661 *
662 * Intended for use by other classes, so we don't have to expose the entire
663 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500664 *
665 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800666 */
667hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700668 return this.backgroundColor_;
669};
670
671/**
672 * Set the foreground color.
673 *
674 * If you want this setting to persist, set it through prefs_, rather than
675 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500676 *
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500677 * @param {string=} color The color to set. If not defined, we reset to the
678 * saved user preference.
rginda8e92a692012-05-20 19:37:20 -0700679 */
680hterm.Terminal.prototype.setForegroundColor = function(color) {
Mike Frysingerf02a2cb2017-12-21 00:34:03 -0500681 if (color === undefined)
682 color = this.prefs_.get('foreground-color');
683
rgindacbbd7482012-06-13 15:06:16 -0700684 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700685 this.primaryScreen_.textAttributes.setDefaults(
686 this.foregroundColor_, this.backgroundColor_);
687 this.alternateScreen_.textAttributes.setDefaults(
688 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700689 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800690};
691
692/**
693 * Return the current terminal foreground color.
694 *
695 * Intended for use by other classes, so we don't have to expose the entire
696 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500697 *
698 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800699 */
700hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700701 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800702};
703
704/**
rginda87b86462011-12-14 13:48:03 -0800705 * Create a new instance of a terminal command and run it with a given
706 * argument string.
707 *
708 * @param {function} commandClass The constructor for a terminal command.
709 * @param {string} argString The argument string to pass to the command.
710 */
711hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700712 var environment = this.prefs_.get('environment');
713 if (typeof environment != 'object' || environment == null)
714 environment = {};
715
rginda87b86462011-12-14 13:48:03 -0800716 var self = this;
717 this.command = new commandClass(
718 { argString: argString || '',
719 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700720 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800721 onExit: function(code) {
722 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800723 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700724 if (self.prefs_.get('close-on-exit'))
725 window.close();
rginda87b86462011-12-14 13:48:03 -0800726 }
727 });
728
rgindafeaf3142012-01-31 15:14:20 -0800729 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800730 this.command.run();
731};
732
733/**
rgindafeaf3142012-01-31 15:14:20 -0800734 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500735 *
736 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800737 */
738hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700739 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800740};
741
742/**
743 * Install the keyboard handler for this terminal.
744 *
745 * This will prevent the browser from seeing any keystrokes sent to the
746 * terminal.
747 */
748hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700749 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
rgindafeaf3142012-01-31 15:14:20 -0800750}
751
752/**
753 * Uninstall the keyboard handler for this terminal.
754 */
755hterm.Terminal.prototype.uninstallKeyboard = function() {
756 this.keyboard.installKeyboard(null);
757}
758
759/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400760 * Set a CSS variable.
761 *
762 * Normally this is used to set variables in the hterm namespace.
763 *
764 * @param {string} name The variable to set.
765 * @param {string} value The value to assign to the variable.
766 * @param {string?} opt_prefix The variable namespace/prefix to use.
767 */
768hterm.Terminal.prototype.setCssVar = function(name, value,
769 opt_prefix='--hterm-') {
770 this.document_.documentElement.style.setProperty(
771 `${opt_prefix}${name}`, value);
772};
773
774/**
rginda35c456b2012-02-09 17:29:05 -0800775 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800776 *
777 * Call setFontSize(0) to reset to the default font size.
778 *
779 * This function does not modify the font-size preference.
780 *
781 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800782 */
783hterm.Terminal.prototype.setFontSize = function(px) {
Mike Frysinger47853ac2017-12-14 00:44:10 -0500784 if (px <= 0)
rginda9f5222b2012-03-05 11:53:28 -0800785 px = this.prefs_.get('font-size');
786
rginda35c456b2012-02-09 17:29:05 -0800787 this.scrollPort_.setFontSize(px);
Mike Frysingercce97c42017-08-05 01:11:22 -0400788 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
789 this.setCssVar('charsize-height',
790 this.scrollPort_.characterSize.height + 'px');
rginda35c456b2012-02-09 17:29:05 -0800791};
792
793/**
794 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500795 *
796 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800797 */
798hterm.Terminal.prototype.getFontSize = function() {
799 return this.scrollPort_.getFontSize();
800};
801
802/**
rginda8e92a692012-05-20 19:37:20 -0700803 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500804 *
805 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700806 */
807hterm.Terminal.prototype.getFontFamily = function() {
808 return this.scrollPort_.getFontFamily();
809};
810
811/**
rginda35c456b2012-02-09 17:29:05 -0800812 * Set the CSS "font-family" for this terminal.
813 */
rginda9f5222b2012-03-05 11:53:28 -0800814hterm.Terminal.prototype.syncFontFamily = function() {
815 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
816 this.prefs_.get('font-smoothing'));
817 this.syncBoldSafeState();
818};
819
rginda4bba5e12012-06-20 16:15:30 -0700820/**
821 * Set this.mousePasteButton based on the mouse-paste-button pref,
822 * autodetecting if necessary.
823 */
824hterm.Terminal.prototype.syncMousePasteButton = function() {
825 var button = this.prefs_.get('mouse-paste-button');
826 if (typeof button == 'number') {
827 this.mousePasteButton = button;
828 return;
829 }
830
Mike Frysingeree81a002017-12-12 16:14:53 -0500831 if (hterm.os != 'linux') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400832 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700833 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400834 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700835 }
836};
837
838/**
839 * Enable or disable bold based on the enable-bold pref, autodetecting if
840 * necessary.
841 */
rginda9f5222b2012-03-05 11:53:28 -0800842hterm.Terminal.prototype.syncBoldSafeState = function() {
843 var enableBold = this.prefs_.get('enable-bold');
844 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700845 this.primaryScreen_.textAttributes.enableBold = enableBold;
846 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800847 return;
848 }
849
rgindaf7521392012-02-28 17:20:34 -0800850 var normalSize = this.scrollPort_.measureCharacterSize();
851 var boldSize = this.scrollPort_.measureCharacterSize('bold');
852
853 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800854 if (!isBoldSafe) {
855 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700856 'from normal. Font family is: ' +
857 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800858 }
rginda9f5222b2012-03-05 11:53:28 -0800859
Robert Gindaed016262012-10-26 16:27:09 -0700860 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
861 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800862};
863
864/**
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400865 * Enable or disable blink based on the enable-blink pref.
866 */
867hterm.Terminal.prototype.syncBlinkState = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400868 this.setCssVar('node-duration',
869 this.prefs_.get('enable-blink') ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400870};
871
872/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400873 * Set the mouse cursor style based on the current terminal mode.
874 */
875hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400876 this.setCssVar('mouse-cursor-style',
877 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
878 'var(--hterm-mouse-cursor-text)' :
879 'var(--hterm-mouse-cursor-pointer)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400880};
881
882/**
rginda87b86462011-12-14 13:48:03 -0800883 * Return a copy of the current cursor position.
884 *
885 * @return {hterm.RowCol} The RowCol object representing the current position.
886 */
887hterm.Terminal.prototype.saveCursor = function() {
888 return this.screen_.cursorPosition.clone();
889};
890
Evan Jones2600d4f2016-12-06 09:29:36 -0500891/**
892 * Return the current text attributes.
893 *
894 * @return {string}
895 */
rgindaa19afe22012-01-25 15:40:22 -0800896hterm.Terminal.prototype.getTextAttributes = function() {
897 return this.screen_.textAttributes;
898};
899
Evan Jones2600d4f2016-12-06 09:29:36 -0500900/**
901 * Set the text attributes.
902 *
903 * @param {string} textAttributes The attributes to set.
904 */
rginda1a09aa02012-06-18 21:11:25 -0700905hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
906 this.screen_.textAttributes = textAttributes;
907};
908
rginda87b86462011-12-14 13:48:03 -0800909/**
rgindaf522ce02012-04-17 17:49:17 -0700910 * Return the current browser zoom factor applied to the terminal.
911 *
912 * @return {number} The current browser zoom factor.
913 */
914hterm.Terminal.prototype.getZoomFactor = function() {
915 return this.scrollPort_.characterSize.zoomFactor;
916};
917
918/**
rginda9846e2f2012-01-27 13:53:33 -0800919 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500920 *
921 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800922 */
923hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800924 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800925};
926
927/**
rginda87b86462011-12-14 13:48:03 -0800928 * Restore a previously saved cursor position.
929 *
930 * @param {hterm.RowCol} cursor The position to restore.
931 */
932hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700933 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
934 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800935 this.screen_.setCursorPosition(row, column);
936 if (cursor.column > column ||
937 cursor.column == column && cursor.overflow) {
938 this.screen_.cursorPosition.overflow = true;
939 }
rginda87b86462011-12-14 13:48:03 -0800940};
941
942/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400943 * Clear the cursor's overflow flag.
944 */
945hterm.Terminal.prototype.clearCursorOverflow = function() {
946 this.screen_.cursorPosition.overflow = false;
947};
948
949/**
Mike Frysingera2cacaa2017-11-29 13:51:09 -0800950 * Save the current cursor state to the corresponding screens.
951 *
952 * See the hterm.Screen.CursorState class for more details.
953 *
954 * @param {boolean=} both If true, update both screens, else only update the
955 * current screen.
956 */
957hterm.Terminal.prototype.saveCursorAndState = function(both) {
958 if (both) {
959 this.primaryScreen_.saveCursorAndState(this.vt);
960 this.alternateScreen_.saveCursorAndState(this.vt);
961 } else
962 this.screen_.saveCursorAndState(this.vt);
963};
964
965/**
966 * Restore the saved cursor state in the corresponding screens.
967 *
968 * See the hterm.Screen.CursorState class for more details.
969 *
970 * @param {boolean=} both If true, update both screens, else only update the
971 * current screen.
972 */
973hterm.Terminal.prototype.restoreCursorAndState = function(both) {
974 if (both) {
975 this.primaryScreen_.restoreCursorAndState(this.vt);
976 this.alternateScreen_.restoreCursorAndState(this.vt);
977 } else
978 this.screen_.restoreCursorAndState(this.vt);
979};
980
981/**
Robert Ginda830583c2013-08-07 13:20:46 -0700982 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -0500983 *
984 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -0700985 */
986hterm.Terminal.prototype.setCursorShape = function(shape) {
987 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -0800988 this.restyleCursor_();
Robert Ginda830583c2013-08-07 13:20:46 -0700989}
990
991/**
992 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -0500993 *
994 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -0700995 */
996hterm.Terminal.prototype.getCursorShape = function() {
997 return this.cursorShape_;
998}
999
1000/**
rginda87b86462011-12-14 13:48:03 -08001001 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001002 *
1003 * @param {number} columnCount
rginda87b86462011-12-14 13:48:03 -08001004 */
1005hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -08001006 if (columnCount == null) {
1007 this.div_.style.width = '100%';
1008 return;
1009 }
1010
Robert Ginda26806d12014-07-24 13:44:07 -07001011 this.div_.style.width = Math.ceil(
1012 this.scrollPort_.characterSize.width *
1013 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001014 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -08001015 this.scheduleSyncCursorPosition_();
1016};
rginda87b86462011-12-14 13:48:03 -08001017
rgindac9bc5502012-01-18 11:48:44 -08001018/**
rginda35c456b2012-02-09 17:29:05 -08001019 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -05001020 *
1021 * @param {number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -08001022 */
1023hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -08001024 if (rowCount == null) {
1025 this.div_.style.height = '100%';
1026 return;
1027 }
1028
rginda35c456b2012-02-09 17:29:05 -08001029 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -07001030 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -08001031 this.realizeSize_(this.screenSize.width, rowCount);
1032 this.scheduleSyncCursorPosition_();
1033};
1034
1035/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001036 * Deal with terminal size changes.
1037 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001038 * @param {number} columnCount The number of columns.
1039 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001040 */
1041hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
1042 if (columnCount != this.screenSize.width)
1043 this.realizeWidth_(columnCount);
1044
1045 if (rowCount != this.screenSize.height)
1046 this.realizeHeight_(rowCount);
1047
1048 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -07001049 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001050};
1051
1052/**
rgindac9bc5502012-01-18 11:48:44 -08001053 * Deal with terminal width changes.
1054 *
1055 * This function does what needs to be done when the terminal width changes
1056 * out from under us. It happens here rather than in onResize_() because this
1057 * code may need to run synchronously to handle programmatic changes of
1058 * terminal width.
1059 *
1060 * Relying on the browser to send us an async resize event means we may not be
1061 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001062 *
1063 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001064 */
1065hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001066 if (columnCount <= 0)
1067 throw new Error('Attempt to realize bad width: ' + columnCount);
1068
rgindac9bc5502012-01-18 11:48:44 -08001069 var deltaColumns = columnCount - this.screen_.getWidth();
1070
rginda87b86462011-12-14 13:48:03 -08001071 this.screenSize.width = columnCount;
1072 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001073
1074 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -04001075 if (this.defaultTabStops)
1076 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -08001077 } else {
1078 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -04001079 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -08001080 break;
1081
1082 this.tabStops_.pop();
1083 }
1084 }
1085
1086 this.screen_.setColumnCount(this.screenSize.width);
1087};
1088
1089/**
1090 * Deal with terminal height changes.
1091 *
1092 * This function does what needs to be done when the terminal height changes
1093 * out from under us. It happens here rather than in onResize_() because this
1094 * code may need to run synchronously to handle programmatic changes of
1095 * terminal height.
1096 *
1097 * Relying on the browser to send us an async resize event means we may not be
1098 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001099 *
1100 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001101 */
1102hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001103 if (rowCount <= 0)
1104 throw new Error('Attempt to realize bad height: ' + rowCount);
1105
rgindac9bc5502012-01-18 11:48:44 -08001106 var deltaRows = rowCount - this.screen_.getHeight();
1107
1108 this.screenSize.height = rowCount;
1109
1110 var cursor = this.saveCursor();
1111
1112 if (deltaRows < 0) {
1113 // Screen got smaller.
1114 deltaRows *= -1;
1115 while (deltaRows) {
1116 var lastRow = this.getRowCount() - 1;
1117 if (lastRow - this.scrollbackRows_.length == cursor.row)
1118 break;
1119
1120 if (this.getRowText(lastRow))
1121 break;
1122
1123 this.screen_.popRow();
1124 deltaRows--;
1125 }
1126
1127 var ary = this.screen_.shiftRows(deltaRows);
1128 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1129
1130 // We just removed rows from the top of the screen, we need to update
1131 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001132 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001133 } else if (deltaRows > 0) {
1134 // Screen got larger.
1135
1136 if (deltaRows <= this.scrollbackRows_.length) {
1137 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1138 var rows = this.scrollbackRows_.splice(
1139 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1140 this.screen_.unshiftRows(rows);
1141 deltaRows -= scrollbackCount;
1142 cursor.row += scrollbackCount;
1143 }
1144
1145 if (deltaRows)
1146 this.appendRows_(deltaRows);
1147 }
1148
rginda35c456b2012-02-09 17:29:05 -08001149 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001150 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001151};
1152
1153/**
1154 * Scroll the terminal to the top of the scrollback buffer.
1155 */
1156hterm.Terminal.prototype.scrollHome = function() {
1157 this.scrollPort_.scrollRowToTop(0);
1158};
1159
1160/**
1161 * Scroll the terminal to the end.
1162 */
1163hterm.Terminal.prototype.scrollEnd = function() {
1164 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1165};
1166
1167/**
1168 * Scroll the terminal one page up (minus one line) relative to the current
1169 * position.
1170 */
1171hterm.Terminal.prototype.scrollPageUp = function() {
1172 var i = this.scrollPort_.getTopRowIndex();
1173 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
1174};
1175
1176/**
1177 * Scroll the terminal one page down (minus one line) relative to the current
1178 * position.
1179 */
1180hterm.Terminal.prototype.scrollPageDown = function() {
1181 var i = this.scrollPort_.getTopRowIndex();
1182 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -08001183};
1184
rgindac9bc5502012-01-18 11:48:44 -08001185/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001186 * Scroll the terminal one line up relative to the current position.
1187 */
1188hterm.Terminal.prototype.scrollLineUp = function() {
1189 var i = this.scrollPort_.getTopRowIndex();
1190 this.scrollPort_.scrollRowToTop(i - 1);
1191};
1192
1193/**
1194 * Scroll the terminal one line down relative to the current position.
1195 */
1196hterm.Terminal.prototype.scrollLineDown = function() {
1197 var i = this.scrollPort_.getTopRowIndex();
1198 this.scrollPort_.scrollRowToTop(i + 1);
1199};
1200
1201/**
Robert Ginda40932892012-12-10 17:26:40 -08001202 * Clear primary screen, secondary screen, and the scrollback buffer.
1203 */
1204hterm.Terminal.prototype.wipeContents = function() {
1205 this.scrollbackRows_.length = 0;
1206 this.scrollPort_.resetCache();
1207
1208 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
1209 var bottom = screen.getHeight();
1210 if (bottom > 0) {
1211 this.renumberRows_(0, bottom);
1212 this.clearHome(screen);
1213 }
1214 }.bind(this));
1215
1216 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001217 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001218};
1219
1220/**
rgindac9bc5502012-01-18 11:48:44 -08001221 * Full terminal reset.
Mike Frysinger84301d02017-11-29 13:28:46 -08001222 *
1223 * Perform a full reset to the default values listed in
1224 * https://vt100.net/docs/vt510-rm/RIS.html
rgindac9bc5502012-01-18 11:48:44 -08001225 */
rginda87b86462011-12-14 13:48:03 -08001226hterm.Terminal.prototype.reset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001227 this.vt.reset();
1228
rgindac9bc5502012-01-18 11:48:44 -08001229 this.clearAllTabStops();
1230 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001231
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001232 const resetScreen = (screen) => {
1233 // We want to make sure to reset the attributes before we clear the screen.
1234 // The attributes might be used to initialize default/empty rows.
1235 screen.textAttributes.reset();
1236 screen.textAttributes.resetColorPalette();
1237 this.clearHome(screen);
1238 screen.saveCursorAndState(this.vt);
1239 };
1240 resetScreen(this.primaryScreen_);
1241 resetScreen(this.alternateScreen_);
rginda9ea433c2012-03-16 11:57:00 -07001242
Mike Frysinger84301d02017-11-29 13:28:46 -08001243 // Reset terminal options to their default values.
1244 this.options_ = new hterm.Options();
rgindab8bc8932012-04-27 12:45:03 -07001245 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1246
Mike Frysinger84301d02017-11-29 13:28:46 -08001247 this.setVTScrollRegion(null, null);
1248
1249 this.setCursorVisible(true);
rginda87b86462011-12-14 13:48:03 -08001250};
1251
rgindac9bc5502012-01-18 11:48:44 -08001252/**
1253 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001254 *
1255 * Perform a soft reset to the default values listed in
1256 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001257 */
rginda0f5c0292012-01-13 11:00:13 -08001258hterm.Terminal.prototype.softReset = function() {
Mike Frysinger7e42f632017-11-29 13:42:09 -08001259 this.vt.reset();
1260
rgindab8bc8932012-04-27 12:45:03 -07001261 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001262 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001263
Brad Townb62dfdc2015-03-16 19:07:15 -07001264 // We show the cursor on soft reset but do not alter the blink state.
1265 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1266
Mike Frysingera2cacaa2017-11-29 13:51:09 -08001267 const resetScreen = (screen) => {
1268 // Xterm also resets the color palette on soft reset, even though it doesn't
1269 // seem to be documented anywhere.
1270 screen.textAttributes.reset();
1271 screen.textAttributes.resetColorPalette();
1272 screen.saveCursorAndState(this.vt);
1273 };
1274 resetScreen(this.primaryScreen_);
1275 resetScreen(this.alternateScreen_);
rgindaf522ce02012-04-17 17:49:17 -07001276
rgindab8bc8932012-04-27 12:45:03 -07001277 // The xterm man page explicitly says this will happen on soft reset.
1278 this.setVTScrollRegion(null, null);
1279
1280 // Xterm also shows the cursor on soft reset, but does not alter the blink
1281 // state.
rgindaa19afe22012-01-25 15:40:22 -08001282 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001283};
1284
rgindac9bc5502012-01-18 11:48:44 -08001285/**
1286 * Move the cursor forward to the next tab stop, or to the last column
1287 * if no more tab stops are set.
1288 */
1289hterm.Terminal.prototype.forwardTabStop = function() {
1290 var column = this.screen_.cursorPosition.column;
1291
1292 for (var i = 0; i < this.tabStops_.length; i++) {
1293 if (this.tabStops_[i] > column) {
1294 this.setCursorColumn(this.tabStops_[i]);
1295 return;
1296 }
1297 }
1298
David Benjamin66e954d2012-05-05 21:08:12 -04001299 // xterm does not clear the overflow flag on HT or CHT.
1300 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001301 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001302 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001303};
1304
rgindac9bc5502012-01-18 11:48:44 -08001305/**
1306 * Move the cursor backward to the previous tab stop, or to the first column
1307 * if no previous tab stops are set.
1308 */
1309hterm.Terminal.prototype.backwardTabStop = function() {
1310 var column = this.screen_.cursorPosition.column;
1311
1312 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1313 if (this.tabStops_[i] < column) {
1314 this.setCursorColumn(this.tabStops_[i]);
1315 return;
1316 }
1317 }
1318
1319 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001320};
1321
rgindac9bc5502012-01-18 11:48:44 -08001322/**
1323 * Set a tab stop at the given column.
1324 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001325 * @param {integer} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001326 */
1327hterm.Terminal.prototype.setTabStop = function(column) {
1328 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1329 if (this.tabStops_[i] == column)
1330 return;
1331
1332 if (this.tabStops_[i] < column) {
1333 this.tabStops_.splice(i + 1, 0, column);
1334 return;
1335 }
1336 }
1337
1338 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001339};
1340
rgindac9bc5502012-01-18 11:48:44 -08001341/**
1342 * Clear the tab stop at the current cursor position.
1343 *
1344 * No effect if there is no tab stop at the current cursor position.
1345 */
1346hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1347 var column = this.screen_.cursorPosition.column;
1348
1349 var i = this.tabStops_.indexOf(column);
1350 if (i == -1)
1351 return;
1352
1353 this.tabStops_.splice(i, 1);
1354};
1355
1356/**
1357 * Clear all tab stops.
1358 */
1359hterm.Terminal.prototype.clearAllTabStops = function() {
1360 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001361 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001362};
1363
1364/**
1365 * Set up the default tab stops, starting from a given column.
1366 *
1367 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001368 * from the specified column, or 0 if no column is provided. It also flags
1369 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001370 *
1371 * This does not clear the existing tab stops first, use clearAllTabStops
1372 * for that.
1373 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001374 * @param {integer} opt_start Optional starting zero based starting column, useful
rgindac9bc5502012-01-18 11:48:44 -08001375 * for filling out missing tab stops when the terminal is resized.
1376 */
1377hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1378 var start = opt_start || 0;
1379 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001380 // Round start up to a default tab stop.
1381 start = start - 1 - ((start - 1) % w) + w;
1382 for (var i = start; i < this.screenSize.width; i += w) {
1383 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001384 }
David Benjamin66e954d2012-05-05 21:08:12 -04001385
1386 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001387};
1388
rginda6d397402012-01-17 10:58:29 -08001389/**
rginda8ba33642011-12-14 12:31:31 -08001390 * Interpret a sequence of characters.
1391 *
1392 * Incomplete escape sequences are buffered until the next call.
1393 *
1394 * @param {string} str Sequence of characters to interpret or pass through.
1395 */
1396hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001397 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001398 this.scheduleSyncCursorPosition_();
1399};
1400
1401/**
1402 * Take over the given DIV for use as the terminal display.
1403 *
1404 * @param {HTMLDivElement} div The div to use as the terminal display.
1405 */
1406hterm.Terminal.prototype.decorate = function(div) {
Mike Frysinger5768a9d2017-12-26 12:57:44 -05001407 const charset = div.ownerDocument.characterSet.toLowerCase();
1408 if (charset != 'utf-8') {
1409 console.warn(`Document encoding should be set to utf-8, not "${charset}";` +
1410 ` Add <meta charset='utf-8'/> to your HTML <head> to fix.`);
1411 }
1412
rginda87b86462011-12-14 13:48:03 -08001413 this.div_ = div;
1414
rginda8ba33642011-12-14 12:31:31 -08001415 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001416 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001417 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1418 this.scrollPort_.setBackgroundPosition(
1419 this.prefs_.get('background-position'));
Mike Frysinger08bad432017-04-24 00:50:54 -04001420 this.scrollPort_.setUserCssUrl(this.prefs_.get('user-css'));
1421 this.scrollPort_.setUserCssText(this.prefs_.get('user-css-text'));
rginda30f20f62012-04-05 16:36:19 -07001422
rginda0918b652012-04-04 11:26:24 -07001423 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001424
rginda9f5222b2012-03-05 11:53:28 -08001425 this.setFontSize(this.prefs_.get('font-size'));
1426 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001427
David Reveman8f552492012-03-28 12:18:41 -04001428 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001429 this.setScrollWheelMoveMultipler(
1430 this.prefs_.get('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001431
rginda8ba33642011-12-14 12:31:31 -08001432 this.document_ = this.scrollPort_.getDocument();
1433
Evan Jones5f9df812016-12-06 09:38:58 -05001434 this.document_.body.oncontextmenu = function() { return false; };
rginda4bba5e12012-06-20 16:15:30 -07001435
1436 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001437 var screenNode = this.scrollPort_.getScreenNode();
1438 screenNode.addEventListener('mousedown', onMouse);
1439 screenNode.addEventListener('mouseup', onMouse);
1440 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001441 this.scrollPort_.onScrollWheel = onMouse;
1442
Toni Barzic0bfa8922013-11-22 11:18:35 -08001443 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001444 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001445 // Listen for mousedown events on the screenNode as in FF the focus
1446 // events don't bubble.
1447 screenNode.addEventListener('mousedown', function() {
1448 setTimeout(this.onFocusChange_.bind(this, true));
1449 }.bind(this));
1450
Toni Barzic0bfa8922013-11-22 11:18:35 -08001451 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001452 'blur', this.onFocusChange_.bind(this, false));
1453
1454 var style = this.document_.createElement('style');
1455 style.textContent =
1456 ('.cursor-node[focus="false"] {' +
1457 ' box-sizing: border-box;' +
1458 ' background-color: transparent !important;' +
1459 ' border-width: 2px;' +
1460 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001461 '}' +
1462 '.wc-node {' +
1463 ' display: inline-block;' +
1464 ' text-align: center;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001465 ' width: calc(var(--hterm-charsize-width) * 2);' +
Mike Frysinger4036f6e2017-05-31 14:02:55 -04001466 ' line-height: var(--hterm-charsize-height);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001467 '}' +
1468 ':root {' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001469 ' --hterm-charsize-width: ' + this.scrollPort_.characterSize.width + 'px;' +
1470 ' --hterm-charsize-height: ' + this.scrollPort_.characterSize.height + 'px;' +
Mike Frysingera27c0502017-08-23 22:37:10 -04001471 // Default position hides the cursor for when the window is initializing.
1472 ' --hterm-cursor-offset-col: -1;' +
1473 ' --hterm-cursor-offset-row: -1;' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001474 ' --hterm-blink-node-duration: 0.7s;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001475 ' --hterm-mouse-cursor-text: text;' +
1476 ' --hterm-mouse-cursor-pointer: default;' +
1477 ' --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001478 '}' +
Mike Frysingerd3907202017-10-23 01:23:50 -04001479 '.uri-node:hover {' +
1480 ' text-decoration: underline;' +
1481 ' cursor: pointer;' +
1482 '}' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001483 '@keyframes blink {' +
1484 ' from { opacity: 1.0; }' +
1485 ' to { opacity: 0.0; }' +
1486 '}' +
1487 '.blink-node {' +
1488 ' animation-name: blink;' +
1489 ' animation-duration: var(--hterm-blink-node-duration);' +
1490 ' animation-iteration-count: infinite;' +
1491 ' animation-timing-function: ease-in-out;' +
1492 ' animation-direction: alternate;' +
rginda8e92a692012-05-20 19:37:20 -07001493 '}');
1494 this.document_.head.appendChild(style);
1495
rginda8ba33642011-12-14 12:31:31 -08001496 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001497 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001498 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001499 this.cursorNode_.style.cssText =
1500 ('position: absolute;' +
Mike Frysinger44c32202017-08-05 01:13:09 -04001501 'left: calc(var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));' +
1502 'top: calc(var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));' +
rginda87b86462011-12-14 13:48:03 -08001503 'display: block;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001504 'width: var(--hterm-charsize-width);' +
1505 'height: var(--hterm-charsize-height);' +
Rob Spies06533ba2014-04-24 11:20:37 -07001506 '-webkit-transition: opacity, background-color 100ms linear;' +
1507 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001508
Mike Frysingerf02a2cb2017-12-21 00:34:03 -05001509 this.setCursorColor();
Robert Gindafb1be6a2013-12-11 11:56:22 -08001510 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1511 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001512
rginda8ba33642011-12-14 12:31:31 -08001513 this.document_.body.appendChild(this.cursorNode_);
1514
rgindad5613292012-06-19 15:40:37 -07001515 // When 'enableMouseDragScroll' is off we reposition this element directly
1516 // under the mouse cursor after a click. This makes Chrome associate
1517 // subsequent mousemove events with the scroll-blocker. Since the
1518 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1519 // events do not cause the scrollport to scroll.
1520 //
1521 // It's a hack, but it's the cleanest way I could find.
1522 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001523 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
rgindad5613292012-06-19 15:40:37 -07001524 this.scrollBlockerNode_.style.cssText =
1525 ('position: absolute;' +
1526 'top: -99px;' +
1527 'display: block;' +
1528 'width: 10px;' +
1529 'height: 10px;');
1530 this.document_.body.appendChild(this.scrollBlockerNode_);
1531
rgindad5613292012-06-19 15:40:37 -07001532 this.scrollPort_.onScrollWheel = onMouse;
1533 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1534 ].forEach(function(event) {
1535 this.scrollBlockerNode_.addEventListener(event, onMouse);
1536 this.cursorNode_.addEventListener(event, onMouse);
1537 this.document_.addEventListener(event, onMouse);
1538 }.bind(this));
1539
1540 this.cursorNode_.addEventListener('mousedown', function() {
1541 setTimeout(this.focus.bind(this));
1542 }.bind(this));
1543
rginda8ba33642011-12-14 12:31:31 -08001544 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001545
rginda87b86462011-12-14 13:48:03 -08001546 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001547 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001548};
1549
rginda0918b652012-04-04 11:26:24 -07001550/**
1551 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001552 *
1553 * @return {HTMLDocument}
rginda0918b652012-04-04 11:26:24 -07001554 */
rginda87b86462011-12-14 13:48:03 -08001555hterm.Terminal.prototype.getDocument = function() {
1556 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001557};
1558
1559/**
rginda0918b652012-04-04 11:26:24 -07001560 * Focus the terminal.
1561 */
1562hterm.Terminal.prototype.focus = function() {
1563 this.scrollPort_.focus();
1564};
1565
1566/**
rginda8ba33642011-12-14 12:31:31 -08001567 * Return the HTML Element for a given row index.
1568 *
1569 * This is a method from the RowProvider interface. The ScrollPort uses
1570 * it to fetch rows on demand as they are scrolled into view.
1571 *
1572 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1573 * pairs to conserve memory.
1574 *
1575 * @param {integer} index The zero-based row index, measured relative to the
1576 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001577 * largest indices.
rginda8ba33642011-12-14 12:31:31 -08001578 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1579 */
1580hterm.Terminal.prototype.getRowNode = function(index) {
1581 if (index < this.scrollbackRows_.length)
1582 return this.scrollbackRows_[index];
1583
1584 var screenIndex = index - this.scrollbackRows_.length;
1585 return this.screen_.rowsArray[screenIndex];
1586};
1587
1588/**
1589 * Return the text content for a given range of rows.
1590 *
1591 * This is a method from the RowProvider interface. The ScrollPort uses
1592 * it to fetch text content on demand when the user attempts to copy their
1593 * selection to the clipboard.
1594 *
1595 * @param {integer} start The zero-based row index to start from, measured
1596 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001597 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001598 * @param {integer} end The zero-based row index to end on, measured
1599 * relative to the start of the scrollback buffer.
1600 * @return {string} A single string containing the text value of the range of
1601 * rows. Lines will be newline delimited, with no trailing newline.
1602 */
1603hterm.Terminal.prototype.getRowsText = function(start, end) {
1604 var ary = [];
1605 for (var i = start; i < end; i++) {
1606 var node = this.getRowNode(i);
1607 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001608 if (i < end - 1 && !node.getAttribute('line-overflow'))
1609 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001610 }
1611
rgindaa09e7332012-08-17 12:49:51 -07001612 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001613};
1614
1615/**
1616 * Return the text content for a given row.
1617 *
1618 * This is a method from the RowProvider interface. The ScrollPort uses
1619 * it to fetch text content on demand when the user attempts to copy their
1620 * selection to the clipboard.
1621 *
1622 * @param {integer} index The zero-based row index to return, measured
1623 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001624 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001625 * @return {string} A string containing the text value of the selected row.
1626 */
1627hterm.Terminal.prototype.getRowText = function(index) {
1628 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001629 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001630};
1631
1632/**
1633 * Return the total number of rows in the addressable screen and in the
1634 * scrollback buffer of this terminal.
1635 *
1636 * This is a method from the RowProvider interface. The ScrollPort uses
1637 * it to compute the size of the scrollbar.
1638 *
1639 * @return {integer} The number of rows in this terminal.
1640 */
1641hterm.Terminal.prototype.getRowCount = function() {
1642 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1643};
1644
1645/**
1646 * Create DOM nodes for new rows and append them to the end of the terminal.
1647 *
1648 * This is the only correct way to add a new DOM node for a row. Notice that
1649 * the new row is appended to the bottom of the list of rows, and does not
1650 * require renumbering (of the rowIndex property) of previous rows.
1651 *
1652 * If you think you want a new blank row somewhere in the middle of the
1653 * terminal, look into moveRows_().
1654 *
1655 * This method does not pay attention to vtScrollTop/Bottom, since you should
1656 * be using moveRows() in cases where they would matter.
1657 *
1658 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001659 *
1660 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001661 */
1662hterm.Terminal.prototype.appendRows_ = function(count) {
1663 var cursorRow = this.screen_.rowsArray.length;
1664 var offset = this.scrollbackRows_.length + cursorRow;
1665 for (var i = 0; i < count; i++) {
1666 var row = this.document_.createElement('x-row');
1667 row.appendChild(this.document_.createTextNode(''));
1668 row.rowIndex = offset + i;
1669 this.screen_.pushRow(row);
1670 }
1671
1672 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1673 if (extraRows > 0) {
1674 var ary = this.screen_.shiftRows(extraRows);
1675 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001676 if (this.scrollPort_.isScrolledEnd)
1677 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001678 }
1679
1680 if (cursorRow >= this.screen_.rowsArray.length)
1681 cursorRow = this.screen_.rowsArray.length - 1;
1682
rginda87b86462011-12-14 13:48:03 -08001683 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001684};
1685
1686/**
1687 * Relocate rows from one part of the addressable screen to another.
1688 *
1689 * This is used to recycle rows during VT scrolls (those which are driven
1690 * by VT commands, rather than by the user manipulating the scrollbar.)
1691 *
1692 * In this case, the blank lines scrolled into the scroll region are made of
1693 * the nodes we scrolled off. These have their rowIndex properties carefully
1694 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001695 *
1696 * @param {number} fromIndex The start index.
1697 * @param {number} count The number of rows to move.
1698 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001699 */
1700hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1701 var ary = this.screen_.removeRows(fromIndex, count);
1702 this.screen_.insertRows(toIndex, ary);
1703
1704 var start, end;
1705 if (fromIndex < toIndex) {
1706 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001707 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001708 } else {
1709 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001710 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001711 }
1712
1713 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001714 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001715};
1716
1717/**
1718 * Renumber the rowIndex property of the given range of rows.
1719 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001720 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001721 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001722 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001723 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001724 *
1725 * @param {number} start The start index.
1726 * @param {number} end The end index.
1727 * @param {hterm.Screen} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001728 */
Robert Ginda40932892012-12-10 17:26:40 -08001729hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1730 var screen = opt_screen || this.screen_;
1731
rginda8ba33642011-12-14 12:31:31 -08001732 var offset = this.scrollbackRows_.length;
1733 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001734 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001735 }
1736};
1737
1738/**
1739 * Print a string to the terminal.
1740 *
1741 * This respects the current insert and wraparound modes. It will add new lines
1742 * to the end of the terminal, scrolling off the top into the scrollback buffer
1743 * if necessary.
1744 *
1745 * The string is *not* parsed for escape codes. Use the interpret() method if
1746 * that's what you're after.
1747 *
1748 * @param{string} str The string to print.
1749 */
1750hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001751 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001752
Ricky Liang48f05cb2013-12-31 23:35:29 +08001753 var strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04001754 // Fun edge case: If the string only contains zero width codepoints (like
1755 // combining characters), we make sure to iterate at least once below.
1756 if (strWidth == 0 && str)
1757 strWidth = 1;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001758
1759 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001760 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1761 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001762 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001763 }
rgindaa19afe22012-01-25 15:40:22 -08001764
Ricky Liang48f05cb2013-12-31 23:35:29 +08001765 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001766 var didOverflow = false;
1767 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001768
rgindaa9abdd82012-08-06 18:05:09 -07001769 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1770 didOverflow = true;
1771 count = this.screenSize.width - this.screen_.cursorPosition.column;
1772 }
rgindaa19afe22012-01-25 15:40:22 -08001773
rgindaa9abdd82012-08-06 18:05:09 -07001774 if (didOverflow && !this.options_.wraparound) {
1775 // If the string overflowed the line but wraparound is off, then the
1776 // last printed character should be the last of the string.
1777 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001778 substr = lib.wc.substr(str, startOffset, count - 1) +
1779 lib.wc.substr(str, strWidth - 1);
1780 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001781 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001782 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001783 }
rgindaa19afe22012-01-25 15:40:22 -08001784
Ricky Liang48f05cb2013-12-31 23:35:29 +08001785 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1786 for (var i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001787 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
1788 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001789
1790 if (this.options_.insertMode) {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001791 this.screen_.insertString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001792 } else {
Mike Frysinger6380bed2017-08-24 18:46:39 -04001793 this.screen_.overwriteString(tokens[i].str, tokens[i].wcStrWidth);
Ricky Liang48f05cb2013-12-31 23:35:29 +08001794 }
1795 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001796 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07001797 }
1798
1799 this.screen_.maybeClipCurrentRow();
1800 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001801 }
rginda8ba33642011-12-14 12:31:31 -08001802
1803 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001804
rginda9f5222b2012-03-05 11:53:28 -08001805 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001806 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001807};
1808
1809/**
rginda87b86462011-12-14 13:48:03 -08001810 * Set the VT scroll region.
1811 *
rginda87b86462011-12-14 13:48:03 -08001812 * This also resets the cursor position to the absolute (0, 0) position, since
1813 * that's what xterm appears to do.
1814 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001815 * Setting the scroll region to the full height of the terminal will clear
1816 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1817 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1818 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1819 * continue to work as most users would expect.
1820 *
rginda87b86462011-12-14 13:48:03 -08001821 * @param {integer} scrollTop The zero-based top of the scroll region.
1822 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1823 * inclusive.
1824 */
1825hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001826 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001827 this.vtScrollTop_ = null;
1828 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001829 } else {
1830 this.vtScrollTop_ = scrollTop;
1831 this.vtScrollBottom_ = scrollBottom;
1832 }
rginda87b86462011-12-14 13:48:03 -08001833};
1834
1835/**
rginda8ba33642011-12-14 12:31:31 -08001836 * Return the top row index according to the VT.
1837 *
1838 * This will return 0 unless the terminal has been told to restrict scrolling
1839 * to some lower row. It is used for some VT cursor positioning and scrolling
1840 * commands.
1841 *
1842 * @return {integer} The topmost row in the terminal's scroll region.
1843 */
1844hterm.Terminal.prototype.getVTScrollTop = function() {
1845 if (this.vtScrollTop_ != null)
1846 return this.vtScrollTop_;
1847
1848 return 0;
rginda87b86462011-12-14 13:48:03 -08001849};
rginda8ba33642011-12-14 12:31:31 -08001850
1851/**
1852 * Return the bottom row index according to the VT.
1853 *
1854 * This will return the height of the terminal unless the it has been told to
1855 * restrict scrolling to some higher row. It is used for some VT cursor
1856 * positioning and scrolling commands.
1857 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001858 * @return {integer} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001859 */
1860hterm.Terminal.prototype.getVTScrollBottom = function() {
1861 if (this.vtScrollBottom_ != null)
1862 return this.vtScrollBottom_;
1863
rginda87b86462011-12-14 13:48:03 -08001864 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001865}
1866
1867/**
1868 * Process a '\n' character.
1869 *
1870 * If the cursor is on the final row of the terminal this will append a new
1871 * blank row to the screen and scroll the topmost row into the scrollback
1872 * buffer.
1873 *
1874 * Otherwise, this moves the cursor to column zero of the next row.
1875 */
1876hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001877 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1878 this.screen_.rowsArray.length - 1);
1879
1880 if (this.vtScrollBottom_ != null) {
1881 // A VT Scroll region is active, we never append new rows.
1882 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1883 // We're at the end of the VT Scroll Region, perform a VT scroll.
1884 this.vtScrollUp(1);
1885 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1886 } else if (cursorAtEndOfScreen) {
1887 // We're at the end of the screen, the only thing to do is put the
1888 // cursor to column 0.
1889 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1890 } else {
1891 // Anywhere else, advance the cursor row, and reset the column.
1892 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1893 }
1894 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001895 // We're at the end of the screen. Append a new row to the terminal,
1896 // shifting the top row into the scrollback.
1897 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001898 } else {
rginda87b86462011-12-14 13:48:03 -08001899 // Anywhere else in the screen just moves the cursor.
1900 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001901 }
1902};
1903
1904/**
1905 * Like newLine(), except maintain the cursor column.
1906 */
1907hterm.Terminal.prototype.lineFeed = function() {
1908 var column = this.screen_.cursorPosition.column;
1909 this.newLine();
1910 this.setCursorColumn(column);
1911};
1912
1913/**
rginda87b86462011-12-14 13:48:03 -08001914 * If autoCarriageReturn is set then newLine(), else lineFeed().
1915 */
1916hterm.Terminal.prototype.formFeed = function() {
1917 if (this.options_.autoCarriageReturn) {
1918 this.newLine();
1919 } else {
1920 this.lineFeed();
1921 }
1922};
1923
1924/**
1925 * Move the cursor up one row, possibly inserting a blank line.
1926 *
1927 * The cursor column is not changed.
1928 */
1929hterm.Terminal.prototype.reverseLineFeed = function() {
1930 var scrollTop = this.getVTScrollTop();
1931 var currentRow = this.screen_.cursorPosition.row;
1932
1933 if (currentRow == scrollTop) {
1934 this.insertLines(1);
1935 } else {
1936 this.setAbsoluteCursorRow(currentRow - 1);
1937 }
1938};
1939
1940/**
rginda8ba33642011-12-14 12:31:31 -08001941 * Replace all characters to the left of the current cursor with the space
1942 * character.
1943 *
1944 * TODO(rginda): This should probably *remove* the characters (not just replace
1945 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001946 * position.
rginda8ba33642011-12-14 12:31:31 -08001947 */
1948hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001949 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001950 this.setCursorColumn(0);
Mike Frysinger6380bed2017-08-24 18:46:39 -04001951 const count = cursor.column + 1;
1952 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08001953 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001954};
1955
1956/**
David Benjamin684a9b72012-05-01 17:19:58 -04001957 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001958 *
1959 * The cursor position is unchanged.
1960 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001961 * If the current background color is not the default background color this
1962 * will insert spaces rather than delete. This is unfortunate because the
1963 * trailing space will affect text selection, but it's difficult to come up
1964 * with a way to style empty space that wouldn't trip up the hterm.Screen
1965 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001966 *
1967 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1968 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1969 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05001970 *
1971 * @param {number} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08001972 */
1973hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001974 if (this.screen_.cursorPosition.overflow)
1975 return;
1976
Robert Ginda7fd57082012-09-25 14:41:47 -07001977 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1978 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001979
1980 if (this.screen_.textAttributes.background ===
1981 this.screen_.textAttributes.DEFAULT_COLOR) {
1982 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08001983 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07001984 this.screen_.cursorPosition.column + count) {
1985 this.screen_.deleteChars(count);
1986 this.clearCursorOverflow();
1987 return;
1988 }
1989 }
1990
rginda87b86462011-12-14 13:48:03 -08001991 var cursor = this.saveCursor();
Mike Frysinger6380bed2017-08-24 18:46:39 -04001992 this.screen_.overwriteString(lib.f.getWhitespace(count), count);
rginda87b86462011-12-14 13:48:03 -08001993 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001994 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001995};
1996
1997/**
1998 * Erase the current line.
1999 *
2000 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002001 */
2002hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08002003 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002004 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08002005 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002006 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002007};
2008
2009/**
David Benjamina08d78f2012-05-05 00:28:49 -04002010 * Erase all characters from the start of the screen to the current cursor
2011 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002012 *
2013 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002014 */
2015hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08002016 var cursor = this.saveCursor();
2017
2018 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08002019
David Benjamina08d78f2012-05-05 00:28:49 -04002020 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08002021 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002022 this.screen_.clearCursorRow();
2023 }
2024
rginda87b86462011-12-14 13:48:03 -08002025 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002026 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002027};
2028
2029/**
2030 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04002031 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08002032 *
2033 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08002034 */
2035hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08002036 var cursor = this.saveCursor();
2037
2038 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08002039
David Benjamina08d78f2012-05-05 00:28:49 -04002040 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08002041 for (var i = cursor.row + 1; i <= bottom; i++) {
2042 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08002043 this.screen_.clearCursorRow();
2044 }
2045
rginda87b86462011-12-14 13:48:03 -08002046 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002047 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08002048};
2049
2050/**
2051 * Fill the terminal with a given character.
2052 *
2053 * This methods does not respect the VT scroll region.
2054 *
2055 * @param {string} ch The character to use for the fill.
2056 */
2057hterm.Terminal.prototype.fill = function(ch) {
2058 var cursor = this.saveCursor();
2059
2060 this.setAbsoluteCursorPosition(0, 0);
2061 for (var row = 0; row < this.screenSize.height; row++) {
2062 for (var col = 0; col < this.screenSize.width; col++) {
2063 this.setAbsoluteCursorPosition(row, col);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002064 this.screen_.overwriteString(ch, 1);
rginda87b86462011-12-14 13:48:03 -08002065 }
2066 }
2067
2068 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002069};
2070
2071/**
rginda9ea433c2012-03-16 11:57:00 -07002072 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002073 *
rginda9ea433c2012-03-16 11:57:00 -07002074 * This does not respect the scroll region.
2075 *
2076 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2077 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002078 */
rginda9ea433c2012-03-16 11:57:00 -07002079hterm.Terminal.prototype.clearHome = function(opt_screen) {
2080 var screen = opt_screen || this.screen_;
2081 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002082
rginda11057d52012-04-25 12:29:56 -07002083 if (bottom == 0) {
2084 // Empty screen, nothing to do.
2085 return;
2086 }
2087
rgindae4d29232012-01-19 10:47:13 -08002088 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002089 screen.setCursorPosition(i, 0);
2090 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002091 }
2092
rginda9ea433c2012-03-16 11:57:00 -07002093 screen.setCursorPosition(0, 0);
2094};
2095
2096/**
2097 * Erase the entire display without changing the cursor position.
2098 *
2099 * The cursor position is unchanged. This does not respect the scroll
2100 * region.
2101 *
2102 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2103 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002104 */
2105hterm.Terminal.prototype.clear = function(opt_screen) {
2106 var screen = opt_screen || this.screen_;
2107 var cursor = screen.cursorPosition.clone();
2108 this.clearHome(screen);
2109 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002110};
2111
2112/**
2113 * VT command to insert lines at the current cursor row.
2114 *
2115 * This respects the current scroll region. Rows pushed off the bottom are
2116 * lost (they won't show up in the scrollback buffer).
2117 *
rginda8ba33642011-12-14 12:31:31 -08002118 * @param {integer} count The number of lines to insert.
2119 */
2120hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002121 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002122
2123 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002124 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002125
Robert Ginda579186b2012-09-26 11:40:04 -07002126 // The moveCount is the number of rows we need to relocate to make room for
2127 // the new row(s). The count is the distance to move them.
2128 var moveCount = bottom - cursorRow - count + 1;
2129 if (moveCount)
2130 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002131
Robert Ginda579186b2012-09-26 11:40:04 -07002132 for (var i = count - 1; i >= 0; i--) {
2133 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002134 this.screen_.clearCursorRow();
2135 }
rginda8ba33642011-12-14 12:31:31 -08002136};
2137
2138/**
2139 * VT command to delete lines at the current cursor row.
2140 *
2141 * New rows are added to the bottom of scroll region to take their place. New
2142 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002143 *
2144 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002145 */
2146hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002147 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002148
rginda87b86462011-12-14 13:48:03 -08002149 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002150 var bottom = this.getVTScrollBottom();
2151
rginda87b86462011-12-14 13:48:03 -08002152 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002153 count = Math.min(count, maxCount);
2154
rginda87b86462011-12-14 13:48:03 -08002155 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002156 if (count != maxCount)
2157 this.moveRows_(top, count, moveStart);
2158
2159 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002160 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002161 this.screen_.clearCursorRow();
2162 }
2163
rginda87b86462011-12-14 13:48:03 -08002164 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002165 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002166};
2167
2168/**
2169 * Inserts the given number of spaces at the current cursor position.
2170 *
rginda87b86462011-12-14 13:48:03 -08002171 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002172 *
2173 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002174 */
2175hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002176 var cursor = this.saveCursor();
2177
rgindacbbd7482012-06-13 15:06:16 -07002178 var ws = lib.f.getWhitespace(count || 1);
Mike Frysinger6380bed2017-08-24 18:46:39 -04002179 this.screen_.insertString(ws, ws.length);
rgindaa19afe22012-01-25 15:40:22 -08002180 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002181
2182 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002183 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002184};
2185
2186/**
2187 * Forward-delete the specified number of characters starting at the cursor
2188 * position.
2189 *
2190 * @param {integer} count The number of characters to delete.
2191 */
2192hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002193 var deleted = this.screen_.deleteChars(count);
2194 if (deleted && !this.screen_.textAttributes.isDefault()) {
2195 var cursor = this.saveCursor();
2196 this.setCursorColumn(this.screenSize.width - deleted);
2197 this.screen_.insertString(lib.f.getWhitespace(deleted));
2198 this.restoreCursor(cursor);
2199 }
2200
David Benjamin54e8bf62012-06-01 22:31:40 -04002201 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002202};
2203
2204/**
2205 * Shift rows in the scroll region upwards by a given number of lines.
2206 *
2207 * New rows are inserted at the bottom of the scroll region to fill the
2208 * vacated rows. The new rows not filled out with the current text attributes.
2209 *
2210 * This function does not affect the scrollback rows at all. Rows shifted
2211 * off the top are lost.
2212 *
rginda87b86462011-12-14 13:48:03 -08002213 * The cursor position is not altered.
2214 *
rginda8ba33642011-12-14 12:31:31 -08002215 * @param {integer} count The number of rows to scroll.
2216 */
2217hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002218 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002219
rginda87b86462011-12-14 13:48:03 -08002220 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002221 this.deleteLines(count);
2222
rginda87b86462011-12-14 13:48:03 -08002223 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002224};
2225
2226/**
2227 * Shift rows below the cursor down by a given number of lines.
2228 *
2229 * This function respects the current scroll region.
2230 *
2231 * New rows are inserted at the top of the scroll region to fill the
2232 * vacated rows. The new rows not filled out with the current text attributes.
2233 *
2234 * This function does not affect the scrollback rows at all. Rows shifted
2235 * off the bottom are lost.
2236 *
2237 * @param {integer} count The number of rows to scroll.
2238 */
2239hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08002240 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002241
rginda87b86462011-12-14 13:48:03 -08002242 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08002243 this.insertLines(opt_count);
2244
rginda87b86462011-12-14 13:48:03 -08002245 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002246};
2247
rginda87b86462011-12-14 13:48:03 -08002248
rginda8ba33642011-12-14 12:31:31 -08002249/**
2250 * Set the cursor position.
2251 *
2252 * The cursor row is relative to the scroll region if the terminal has
2253 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2254 *
2255 * @param {integer} row The new zero-based cursor row.
2256 * @param {integer} row The new zero-based cursor column.
2257 */
2258hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2259 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002260 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002261 } else {
rginda87b86462011-12-14 13:48:03 -08002262 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002263 }
rginda87b86462011-12-14 13:48:03 -08002264};
rginda8ba33642011-12-14 12:31:31 -08002265
Evan Jones2600d4f2016-12-06 09:29:36 -05002266/**
2267 * Move the cursor relative to its current position.
2268 *
2269 * @param {number} row
2270 * @param {number} column
2271 */
rginda87b86462011-12-14 13:48:03 -08002272hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2273 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002274 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2275 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002276 this.screen_.setCursorPosition(row, column);
2277};
2278
Evan Jones2600d4f2016-12-06 09:29:36 -05002279/**
2280 * Move the cursor to the specified position.
2281 *
2282 * @param {number} row
2283 * @param {number} column
2284 */
rginda87b86462011-12-14 13:48:03 -08002285hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002286 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2287 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002288 this.screen_.setCursorPosition(row, column);
2289};
2290
2291/**
2292 * Set the cursor column.
2293 *
2294 * @param {integer} column The new zero-based cursor column.
2295 */
2296hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002297 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002298};
2299
2300/**
2301 * Return the cursor column.
2302 *
2303 * @return {integer} The zero-based cursor column.
2304 */
2305hterm.Terminal.prototype.getCursorColumn = function() {
2306 return this.screen_.cursorPosition.column;
2307};
2308
2309/**
2310 * Set the cursor row.
2311 *
2312 * The cursor row is relative to the scroll region if the terminal has
2313 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2314 *
2315 * @param {integer} row The new cursor row.
2316 */
rginda87b86462011-12-14 13:48:03 -08002317hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2318 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002319};
2320
2321/**
2322 * Return the cursor row.
2323 *
2324 * @return {integer} The zero-based cursor row.
2325 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002326hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002327 return this.screen_.cursorPosition.row;
2328};
2329
2330/**
2331 * Request that the ScrollPort redraw itself soon.
2332 *
2333 * The redraw will happen asynchronously, soon after the call stack winds down.
2334 * Multiple calls will be coalesced into a single redraw.
2335 */
2336hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002337 if (this.timeouts_.redraw)
2338 return;
rginda8ba33642011-12-14 12:31:31 -08002339
2340 var self = this;
rginda87b86462011-12-14 13:48:03 -08002341 this.timeouts_.redraw = setTimeout(function() {
2342 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002343 self.scrollPort_.redraw_();
2344 }, 0);
2345};
2346
2347/**
2348 * Request that the ScrollPort be scrolled to the bottom.
2349 *
2350 * The scroll will happen asynchronously, soon after the call stack winds down.
2351 * Multiple calls will be coalesced into a single scroll.
2352 *
2353 * This affects the scrollbar position of the ScrollPort, and has nothing to
2354 * do with the VT scroll commands.
2355 */
2356hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2357 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002358 return;
rginda8ba33642011-12-14 12:31:31 -08002359
2360 var self = this;
2361 this.timeouts_.scrollDown = setTimeout(function() {
2362 delete self.timeouts_.scrollDown;
2363 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2364 }, 10);
2365};
2366
2367/**
2368 * Move the cursor up a specified number of rows.
2369 *
2370 * @param {integer} count The number of rows to move the cursor.
2371 */
2372hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002373 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002374};
2375
2376/**
2377 * Move the cursor down a specified number of rows.
2378 *
2379 * @param {integer} count The number of rows to move the cursor.
2380 */
2381hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002382 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002383 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2384 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2385 this.screenSize.height - 1);
2386
rgindacbbd7482012-06-13 15:06:16 -07002387 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002388 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002389 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002390};
2391
2392/**
2393 * Move the cursor left a specified number of columns.
2394 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002395 * If reverse wraparound mode is enabled and the previous row wrapped into
2396 * the current row then we back up through the wraparound as well.
2397 *
rginda8ba33642011-12-14 12:31:31 -08002398 * @param {integer} count The number of columns to move the cursor.
2399 */
2400hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002401 count = count || 1;
2402
2403 if (count < 1)
2404 return;
2405
2406 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002407 if (this.options_.reverseWraparound) {
2408 if (this.screen_.cursorPosition.overflow) {
2409 // If this cursor is in the right margin, consume one count to get it
2410 // back to the last column. This only applies when we're in reverse
2411 // wraparound mode.
2412 count--;
2413 this.clearCursorOverflow();
2414
2415 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002416 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002417 }
2418
Robert Gindabfb32622014-07-17 13:20:27 -07002419 var newRow = this.screen_.cursorPosition.row;
2420 var newColumn = currentColumn - count;
2421 if (newColumn < 0) {
2422 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2423 if (newRow < 0) {
2424 // xterm also wraps from row 0 to the last row.
2425 newRow = this.screenSize.height + newRow % this.screenSize.height;
2426 }
2427 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2428 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002429
Robert Gindabfb32622014-07-17 13:20:27 -07002430 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2431
2432 } else {
2433 var newColumn = Math.max(currentColumn - count, 0);
2434 this.setCursorColumn(newColumn);
2435 }
rginda8ba33642011-12-14 12:31:31 -08002436};
2437
2438/**
2439 * Move the cursor right a specified number of columns.
2440 *
2441 * @param {integer} count The number of columns to move the cursor.
2442 */
2443hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002444 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002445
2446 if (count < 1)
2447 return;
2448
rgindacbbd7482012-06-13 15:06:16 -07002449 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002450 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002451 this.setCursorColumn(column);
2452};
2453
2454/**
2455 * Reverse the foreground and background colors of the terminal.
2456 *
2457 * This only affects text that was drawn with no attributes.
2458 *
2459 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2460 * been drawn with attributes that happen to coincide with the default
2461 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002462 *
2463 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002464 */
2465hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002466 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002467 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002468 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2469 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002470 } else {
rginda9f5222b2012-03-05 11:53:28 -08002471 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2472 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002473 }
2474};
2475
2476/**
rginda87b86462011-12-14 13:48:03 -08002477 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002478 *
2479 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002480 */
2481hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002482 this.cursorNode_.style.backgroundColor =
2483 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002484
2485 var self = this;
2486 setTimeout(function() {
Matheus Fernandes2d733082017-09-11 06:43:01 -04002487 self.restyleCursor_();
rginda6d397402012-01-17 10:58:29 -08002488 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002489
Michael Kelly485ecd12014-06-09 11:41:56 -04002490 // bellSquelchTimeout_ affects both audio and notification bells.
2491 if (this.bellSquelchTimeout_)
2492 return;
2493
Robert Ginda92e18102013-03-14 13:56:37 -07002494 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002495 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002496 this.bellSequelchTimeout_ = setTimeout(function() {
2497 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002498 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002499 } else {
2500 delete this.bellSquelchTimeout_;
2501 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002502
2503 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002504 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002505 this.bellNotificationList_.push(n);
2506 // TODO: Should we try to raise the window here?
2507 n.onclick = function() { self.closeBellNotifications_(); };
2508 }
rginda87b86462011-12-14 13:48:03 -08002509};
2510
2511/**
rginda8ba33642011-12-14 12:31:31 -08002512 * Set the origin mode bit.
2513 *
2514 * If origin mode is on, certain VT cursor and scrolling commands measure their
2515 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2516 * to the top of the addressable screen.
2517 *
2518 * Defaults to off.
2519 *
2520 * @param {boolean} state True to set origin mode, false to unset.
2521 */
2522hterm.Terminal.prototype.setOriginMode = function(state) {
2523 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002524 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002525};
2526
2527/**
2528 * Set the insert mode bit.
2529 *
2530 * If insert mode is on, existing text beyond the cursor position will be
2531 * shifted right to make room for new text. Otherwise, new text overwrites
2532 * any existing text.
2533 *
2534 * Defaults to off.
2535 *
2536 * @param {boolean} state True to set insert mode, false to unset.
2537 */
2538hterm.Terminal.prototype.setInsertMode = function(state) {
2539 this.options_.insertMode = state;
2540};
2541
2542/**
rginda87b86462011-12-14 13:48:03 -08002543 * Set the auto carriage return bit.
2544 *
2545 * If auto carriage return is on then a formfeed character is interpreted
2546 * as a newline, otherwise it's the same as a linefeed. The difference boils
2547 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002548 *
2549 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002550 */
2551hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2552 this.options_.autoCarriageReturn = state;
2553};
2554
2555/**
rginda8ba33642011-12-14 12:31:31 -08002556 * Set the wraparound mode bit.
2557 *
2558 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2559 * to the start of the following row. Otherwise, the cursor is clamped to the
2560 * end of the screen and attempts to write past it are ignored.
2561 *
2562 * Defaults to on.
2563 *
2564 * @param {boolean} state True to set wraparound mode, false to unset.
2565 */
2566hterm.Terminal.prototype.setWraparound = function(state) {
2567 this.options_.wraparound = state;
2568};
2569
2570/**
2571 * Set the reverse-wraparound mode bit.
2572 *
2573 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2574 * to the end of the previous row. Otherwise, the cursor is clamped to column
2575 * 0.
2576 *
2577 * Defaults to off.
2578 *
2579 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2580 */
2581hterm.Terminal.prototype.setReverseWraparound = function(state) {
2582 this.options_.reverseWraparound = state;
2583};
2584
2585/**
2586 * Selects between the primary and alternate screens.
2587 *
2588 * If alternate mode is on, the alternate screen is active. Otherwise the
2589 * primary screen is active.
2590 *
2591 * Swapping screens has no effect on the scrollback buffer.
2592 *
2593 * Each screen maintains its own cursor position.
2594 *
2595 * Defaults to off.
2596 *
2597 * @param {boolean} state True to set alternate mode, false to unset.
2598 */
2599hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002600 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002601 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2602
rginda35c456b2012-02-09 17:29:05 -08002603 if (this.screen_.rowsArray.length &&
2604 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2605 // If the screen changed sizes while we were away, our rowIndexes may
2606 // be incorrect.
2607 var offset = this.scrollbackRows_.length;
2608 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002609 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002610 ary[i].rowIndex = offset + i;
2611 }
2612 }
rginda8ba33642011-12-14 12:31:31 -08002613
rginda35c456b2012-02-09 17:29:05 -08002614 this.realizeWidth_(this.screenSize.width);
2615 this.realizeHeight_(this.screenSize.height);
2616 this.scrollPort_.syncScrollHeight();
2617 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002618
rginda6d397402012-01-17 10:58:29 -08002619 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002620 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002621};
2622
2623/**
2624 * Set the cursor-blink mode bit.
2625 *
2626 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2627 * a visible cursor does not blink.
2628 *
2629 * You should make sure to turn blinking off if you're going to dispose of a
2630 * terminal, otherwise you'll leak a timeout.
2631 *
2632 * Defaults to on.
2633 *
2634 * @param {boolean} state True to set cursor-blink mode, false to unset.
2635 */
2636hterm.Terminal.prototype.setCursorBlink = function(state) {
2637 this.options_.cursorBlink = state;
2638
2639 if (!state && this.timeouts_.cursorBlink) {
2640 clearTimeout(this.timeouts_.cursorBlink);
2641 delete this.timeouts_.cursorBlink;
2642 }
2643
2644 if (this.options_.cursorVisible)
2645 this.setCursorVisible(true);
2646};
2647
2648/**
2649 * Set the cursor-visible mode bit.
2650 *
2651 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2652 *
2653 * Defaults to on.
2654 *
2655 * @param {boolean} state True to set cursor-visible mode, false to unset.
2656 */
2657hterm.Terminal.prototype.setCursorVisible = function(state) {
2658 this.options_.cursorVisible = state;
2659
2660 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002661 if (this.timeouts_.cursorBlink) {
2662 clearTimeout(this.timeouts_.cursorBlink);
2663 delete this.timeouts_.cursorBlink;
2664 }
rginda87b86462011-12-14 13:48:03 -08002665 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002666 return;
2667 }
2668
rginda87b86462011-12-14 13:48:03 -08002669 this.syncCursorPosition_();
2670
2671 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002672
2673 if (this.options_.cursorBlink) {
2674 if (this.timeouts_.cursorBlink)
2675 return;
2676
Robert Gindaea2183e2014-07-17 09:51:51 -07002677 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002678 } else {
2679 if (this.timeouts_.cursorBlink) {
2680 clearTimeout(this.timeouts_.cursorBlink);
2681 delete this.timeouts_.cursorBlink;
2682 }
2683 }
2684};
2685
2686/**
rginda87b86462011-12-14 13:48:03 -08002687 * Synchronizes the visible cursor and document selection with the current
2688 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002689 */
2690hterm.Terminal.prototype.syncCursorPosition_ = function() {
2691 var topRowIndex = this.scrollPort_.getTopRowIndex();
2692 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2693 var cursorRowIndex = this.scrollbackRows_.length +
2694 this.screen_.cursorPosition.row;
2695
2696 if (cursorRowIndex > bottomRowIndex) {
2697 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04002698 this.setCssVar('cursor-offset-row', '-1');
rginda8ba33642011-12-14 12:31:31 -08002699 return;
2700 }
2701
Robert Gindab837c052014-08-11 11:17:51 -07002702 if (this.options_.cursorVisible &&
2703 this.cursorNode_.style.display == 'none') {
2704 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2705 this.cursorNode_.style.display = '';
2706 }
2707
Mike Frysinger44c32202017-08-05 01:13:09 -04002708 // Position the cursor using CSS variable math. If we do the math in JS,
2709 // the float math will end up being more precise than the CSS which will
2710 // cause the cursor tracking to be off.
2711 this.setCssVar(
2712 'cursor-offset-row',
2713 `${cursorRowIndex - topRowIndex} + ` +
2714 `${this.scrollPort_.visibleRowTopMargin}px`);
2715 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08002716
2717 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04002718 '(' + this.screen_.cursorPosition.column +
2719 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08002720 ')');
2721
2722 // Update the caret for a11y purposes.
2723 var selection = this.document_.getSelection();
2724 if (selection && selection.isCollapsed)
2725 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002726};
2727
Robert Gindafb1be6a2013-12-11 11:56:22 -08002728/**
2729 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2730 * and character cell dimensions.
2731 */
Robert Ginda830583c2013-08-07 13:20:46 -07002732hterm.Terminal.prototype.restyleCursor_ = function() {
2733 var shape = this.cursorShape_;
2734
2735 if (this.cursorNode_.getAttribute('focus') == 'false') {
2736 // Always show a block cursor when unfocused.
2737 shape = hterm.Terminal.cursorShape.BLOCK;
2738 }
2739
2740 var style = this.cursorNode_.style;
2741
2742 switch (shape) {
2743 case hterm.Terminal.cursorShape.BEAM:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002744 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002745 style.backgroundColor = 'transparent';
2746 style.borderBottomStyle = null;
2747 style.borderLeftStyle = 'solid';
2748 break;
2749
2750 case hterm.Terminal.cursorShape.UNDERLINE:
2751 style.height = this.scrollPort_.characterSize.baseline + 'px';
2752 style.backgroundColor = 'transparent';
2753 style.borderBottomStyle = 'solid';
2754 // correct the size to put it exactly at the baseline
2755 style.borderLeftStyle = null;
2756 break;
2757
2758 default:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002759 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002760 style.backgroundColor = this.cursorColor_;
2761 style.borderBottomStyle = null;
2762 style.borderLeftStyle = null;
2763 break;
2764 }
2765};
2766
rginda8ba33642011-12-14 12:31:31 -08002767/**
2768 * Synchronizes the visible cursor with the current cursor coordinates.
2769 *
2770 * The sync will happen asynchronously, soon after the call stack winds down.
2771 * Multiple calls will be coalesced into a single sync.
2772 */
2773hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2774 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002775 return;
rginda8ba33642011-12-14 12:31:31 -08002776
2777 var self = this;
2778 this.timeouts_.syncCursor = setTimeout(function() {
2779 self.syncCursorPosition_();
2780 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002781 }, 0);
2782};
2783
rgindacc2996c2012-02-24 14:59:31 -08002784/**
rgindaf522ce02012-04-17 17:49:17 -07002785 * Show or hide the zoom warning.
2786 *
2787 * The zoom warning is a message warning the user that their browser zoom must
2788 * be set to 100% in order for hterm to function properly.
2789 *
2790 * @param {boolean} state True to show the message, false to hide it.
2791 */
2792hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2793 if (!this.zoomWarningNode_) {
2794 if (!state)
2795 return;
2796
2797 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002798 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07002799 this.zoomWarningNode_.style.cssText = (
2800 'color: black;' +
2801 'background-color: #ff2222;' +
2802 'font-size: large;' +
2803 'border-radius: 8px;' +
2804 'opacity: 0.75;' +
2805 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2806 'top: 0.5em;' +
2807 'right: 1.2em;' +
2808 'position: absolute;' +
2809 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002810 '-webkit-user-select: none;' +
2811 '-moz-text-size-adjust: none;' +
2812 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002813
2814 this.zoomWarningNode_.addEventListener('click', function(e) {
2815 this.parentNode.removeChild(this);
2816 });
rgindaf522ce02012-04-17 17:49:17 -07002817 }
2818
Robert Gindab4839c22013-02-28 16:52:10 -08002819 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2820 hterm.zoomWarningMessage,
2821 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2822
rgindaf522ce02012-04-17 17:49:17 -07002823 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2824
2825 if (state) {
2826 if (!this.zoomWarningNode_.parentNode)
2827 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2828 } else if (this.zoomWarningNode_.parentNode) {
2829 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2830 }
2831};
2832
2833/**
rgindacc2996c2012-02-24 14:59:31 -08002834 * Show the terminal overlay for a given amount of time.
2835 *
2836 * The terminal overlay appears in inverse video in a large font, centered
2837 * over the terminal. You should probably keep the overlay message brief,
2838 * since it's in a large font and you probably aren't going to check the size
2839 * of the terminal first.
2840 *
2841 * @param {string} msg The text (not HTML) message to display in the overlay.
2842 * @param {number} opt_timeout The amount of time to wait before fading out
2843 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2844 * stay up forever (or until the next overlay).
2845 */
2846hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002847 if (!this.overlayNode_) {
2848 if (!this.div_)
2849 return;
2850
2851 this.overlayNode_ = this.document_.createElement('div');
2852 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002853 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002854 'font-size: xx-large;' +
2855 'opacity: 0.75;' +
2856 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2857 'position: absolute;' +
2858 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002859 '-webkit-transition: opacity 180ms ease-in;' +
2860 '-moz-user-select: none;' +
2861 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002862
2863 this.overlayNode_.addEventListener('mousedown', function(e) {
2864 e.preventDefault();
2865 e.stopPropagation();
2866 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002867 }
2868
rginda9f5222b2012-03-05 11:53:28 -08002869 this.overlayNode_.style.color = this.prefs_.get('background-color');
2870 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2871 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2872
rgindaf0090c92012-02-10 14:58:52 -08002873 this.overlayNode_.textContent = msg;
2874 this.overlayNode_.style.opacity = '0.75';
2875
2876 if (!this.overlayNode_.parentNode)
2877 this.div_.appendChild(this.overlayNode_);
2878
Robert Ginda97769282013-02-01 15:30:30 -08002879 var divSize = hterm.getClientSize(this.div_);
2880 var overlaySize = hterm.getClientSize(this.overlayNode_);
2881
Robert Ginda8a59f762014-07-23 11:29:55 -07002882 this.overlayNode_.style.top =
2883 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08002884 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07002885 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08002886
rgindaf0090c92012-02-10 14:58:52 -08002887 if (this.overlayTimeout_)
2888 clearTimeout(this.overlayTimeout_);
2889
rgindacc2996c2012-02-24 14:59:31 -08002890 if (opt_timeout === null)
2891 return;
2892
Mike Frysingerb6cfded2017-09-18 00:39:31 -04002893 this.overlayTimeout_ = setTimeout(() => {
2894 this.overlayNode_.style.opacity = '0';
2895 this.overlayTimeout_ = setTimeout(() => this.hideOverlay(), 200);
2896 }, opt_timeout || 1500);
2897};
2898
2899/**
2900 * Hide the terminal overlay immediately.
2901 *
2902 * Useful when we show an overlay for an event with an unknown end time.
2903 */
2904hterm.Terminal.prototype.hideOverlay = function() {
2905 if (this.overlayTimeout_)
2906 clearTimeout(this.overlayTimeout_);
2907 this.overlayTimeout_ = null;
2908
2909 if (this.overlayNode_.parentNode)
2910 this.overlayNode_.parentNode.removeChild(this.overlayNode_);
2911 this.overlayNode_.style.opacity = '0.75';
rgindaf0090c92012-02-10 14:58:52 -08002912};
2913
rginda4bba5e12012-06-20 16:15:30 -07002914/**
2915 * Paste from the system clipboard to the terminal.
2916 */
2917hterm.Terminal.prototype.paste = function() {
Mike Frysinger4628ad22017-07-20 02:44:20 -04002918 return hterm.pasteFromClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002919};
2920
2921/**
2922 * Copy a string to the system clipboard.
2923 *
2924 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05002925 *
2926 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07002927 */
2928hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002929 if (this.prefs_.get('enable-clipboard-notice'))
2930 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2931
rgindaa09e7332012-08-17 12:49:51 -07002932 var copySource = this.document_.createElement('pre');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002933 copySource.id = 'hterm:copy-to-clipboard-source';
rginda4bba5e12012-06-20 16:15:30 -07002934 copySource.textContent = str;
2935 copySource.style.cssText = (
2936 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002937 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07002938 'position: absolute;' +
2939 'top: -99px');
2940
2941 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002942
rginda4bba5e12012-06-20 16:15:30 -07002943 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002944 var anchorNode = selection.anchorNode;
2945 var anchorOffset = selection.anchorOffset;
2946 var focusNode = selection.focusNode;
2947 var focusOffset = selection.focusOffset;
2948
rginda4bba5e12012-06-20 16:15:30 -07002949 selection.selectAllChildren(copySource);
2950
rgindaa09e7332012-08-17 12:49:51 -07002951 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002952
Rob Spies56953412014-04-28 14:09:47 -07002953 // IE doesn't support selection.extend. This means that the selection
2954 // won't return on IE.
Robert Ginda6f4f0aa2014-07-28 10:21:39 -07002955 if (selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07002956 selection.collapse(anchorNode, anchorOffset);
2957 selection.extend(focusNode, focusOffset);
2958 }
rgindafaa74742012-08-21 13:34:03 -07002959
rginda4bba5e12012-06-20 16:15:30 -07002960 copySource.parentNode.removeChild(copySource);
2961};
2962
Evan Jones2600d4f2016-12-06 09:29:36 -05002963/**
Mike Frysinger8c5a0a42017-04-21 11:38:27 -04002964 * Display an image.
2965 *
2966 * @param {Object} options The image to display.
2967 * @param {string=} options.name A human readable string for the image.
2968 * @param {string|number=} options.size The size (in bytes).
2969 * @param {boolean=} options.preserveAspectRatio Whether to preserve aspect.
2970 * @param {boolean=} options.inline Whether to display the image inline.
2971 * @param {string|number=} options.width The width of the image.
2972 * @param {string|number=} options.height The height of the image.
2973 * @param {string=} options.align Direction to align the image.
2974 * @param {string} options.uri The source URI for the image.
2975 */
2976hterm.Terminal.prototype.displayImage = function(options) {
2977 // Make sure we're actually given a resource to display.
2978 if (options.uri === undefined)
2979 return;
2980
2981 // Set up the defaults to simplify code below.
2982 if (!options.name)
2983 options.name = '';
2984
2985 // Has the user approved image display yet?
2986 if (this.allowImagesInline !== true) {
2987 this.newLine();
2988 const row = this.getRowNode(this.scrollbackRows_.length +
2989 this.getCursorRow() - 1);
2990
2991 if (this.allowImagesInline === false) {
2992 row.textContent = hterm.msg('POPUP_INLINE_IMAGE_DISABLED', [],
2993 'Inline Images Disabled');
2994 return;
2995 }
2996
2997 // Show a prompt.
2998 let button;
2999 const span = this.document_.createElement('span');
3000 span.innerText = hterm.msg('POPUP_INLINE_IMAGE', [], 'Inline Images');
3001 span.style.fontWeight = 'bold';
3002 span.style.borderWidth = '1px';
3003 span.style.borderStyle = 'dashed';
3004 button = this.document_.createElement('span');
3005 button.innerText = hterm.msg('BUTTON_BLOCK', [], 'block');
3006 button.style.marginLeft = '1em';
3007 button.style.borderWidth = '1px';
3008 button.style.borderStyle = 'solid';
3009 button.addEventListener('click', () => {
3010 this.prefs_.set('allow-images-inline', false);
3011 });
3012 span.appendChild(button);
3013 button = this.document_.createElement('span');
3014 button.innerText = hterm.msg('BUTTON_ALLOW_SESSION', [],
3015 'allow this session');
3016 button.style.marginLeft = '1em';
3017 button.style.borderWidth = '1px';
3018 button.style.borderStyle = 'solid';
3019 button.addEventListener('click', () => {
3020 this.allowImagesInline = true;
3021 });
3022 span.appendChild(button);
3023 button = this.document_.createElement('span');
3024 button.innerText = hterm.msg('BUTTON_ALLOW_ALWAYS', [], 'always allow');
3025 button.style.marginLeft = '1em';
3026 button.style.borderWidth = '1px';
3027 button.style.borderStyle = 'solid';
3028 button.addEventListener('click', () => {
3029 this.prefs_.set('allow-images-inline', true);
3030 });
3031 span.appendChild(button);
3032
3033 row.appendChild(span);
3034 return;
3035 }
3036
3037 // See if we should show this object directly, or download it.
3038 if (options.inline) {
3039 const io = this.io.push();
3040 io.showOverlay(hterm.msg('LOADING_RESOURCE_START', [options.name],
3041 'Loading $1 ...'), null);
3042
3043 // While we're loading the image, eat all the user's input.
3044 io.onVTKeystroke = io.sendString = () => {};
3045
3046 // Initialize this new image.
3047 const img = this.document_.createElement('img');
3048 img.src = options.uri;
3049 img.title = img.alt = options.name;
3050
3051 // Attach the image to the page to let it load/render. It won't stay here.
3052 // This is needed so it's visible and the DOM can calculate the height. If
3053 // the image is hidden or not in the DOM, the height is always 0.
3054 this.document_.body.appendChild(img);
3055
3056 // Wait for the image to finish loading before we try moving it to the
3057 // right place in the terminal.
3058 img.onload = () => {
3059 // Now that we have the image dimensions, figure out how to show it.
3060 img.style.objectFit = options.preserveAspectRatio ? 'scale-down' : 'fill';
3061 img.style.maxWidth = `${this.document_.body.clientWidth}px`;
3062 img.style.maxHeight = `${this.document_.body.clientHeight}px`;
3063
3064 // Parse a width/height specification.
3065 const parseDim = (dim, maxDim, cssVar) => {
3066 if (!dim || dim == 'auto')
3067 return '';
3068
3069 const ary = dim.match(/^([0-9]+)(px|%)?$/);
3070 if (ary) {
3071 if (ary[2] == '%')
3072 return maxDim * parseInt(ary[1]) / 100 + 'px';
3073 else if (ary[2] == 'px')
3074 return dim;
3075 else
3076 return `calc(${dim} * var(${cssVar}))`;
3077 }
3078
3079 return '';
3080 };
3081 img.style.width =
3082 parseDim(options.width, this.document_.body.clientWidth,
3083 '--hterm-charsize-width');
3084 img.style.height =
3085 parseDim(options.height, this.document_.body.clientHeight,
3086 '--hterm-charsize-height');
3087
3088 // Figure out how many rows the image occupies, then add that many.
3089 // XXX: This count will be inaccurate if the font size changes on us.
3090 const padRows = Math.ceil(img.clientHeight /
3091 this.scrollPort_.characterSize.height);
3092 for (let i = 0; i < padRows; ++i)
3093 this.newLine();
3094
3095 // Update the max height in case the user shrinks the character size.
3096 img.style.maxHeight = `calc(${padRows} * var(--hterm-charsize-height))`;
3097
3098 // Move the image to the last row. This way when we scroll up, it doesn't
3099 // disappear when the first row gets clipped. It will disappear when we
3100 // scroll down and the last row is clipped ...
3101 this.document_.body.removeChild(img);
3102 // Create a wrapper node so we can do an absolute in a relative position.
3103 // This helps with rounding errors between JS & CSS counts.
3104 const div = this.document_.createElement('div');
3105 div.style.position = 'relative';
3106 div.style.textAlign = options.align;
3107 img.style.position = 'absolute';
3108 img.style.bottom = 'calc(0px - var(--hterm-charsize-height))';
3109 div.appendChild(img);
3110 const row = this.getRowNode(this.scrollbackRows_.length +
3111 this.getCursorRow() - 1);
3112 row.appendChild(div);
3113
3114 io.hideOverlay();
3115 io.pop();
3116 };
3117
3118 // If we got a malformed image, give up.
3119 img.onerror = (e) => {
3120 this.document_.body.removeChild(img);
3121 io.showOverlay(hterm.msg('LOADING_RESOURCE_FAILED', [options.name],
3122 'Loading $1 failed ...'));
3123 io.pop();
3124 };
3125 } else {
3126 // We can't use chrome.downloads.download as that requires "downloads"
3127 // permissions, and that works only in extensions, not apps.
3128 const a = this.document_.createElement('a');
3129 a.href = options.uri;
3130 a.download = options.name;
3131 this.document_.body.appendChild(a);
3132 a.click();
3133 a.remove();
3134 }
3135};
3136
3137/**
Evan Jones2600d4f2016-12-06 09:29:36 -05003138 * Returns the selected text, or null if no text is selected.
3139 *
3140 * @return {string|null}
3141 */
rgindaa09e7332012-08-17 12:49:51 -07003142hterm.Terminal.prototype.getSelectionText = function() {
3143 var selection = this.scrollPort_.selection;
3144 selection.sync();
3145
3146 if (selection.isCollapsed)
3147 return null;
3148
3149
3150 // Start offset measures from the beginning of the line.
3151 var startOffset = selection.startOffset;
3152 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003153
Robert Gindafdbb3f22012-09-06 20:23:06 -07003154 if (node.nodeName != 'X-ROW') {
3155 // If the selection doesn't start on an x-row node, then it must be
3156 // somewhere inside the x-row. Add any characters from previous siblings
3157 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003158
3159 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3160 // If node is the text node in a styled span, move up to the span node.
3161 node = node.parentNode;
3162 }
3163
Robert Gindafdbb3f22012-09-06 20:23:06 -07003164 while (node.previousSibling) {
3165 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003166 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003167 }
rgindaa09e7332012-08-17 12:49:51 -07003168 }
3169
3170 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08003171 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
3172 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05003173 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07003174
Robert Gindafdbb3f22012-09-06 20:23:06 -07003175 if (node.nodeName != 'X-ROW') {
3176 // If the selection doesn't end on an x-row node, then it must be
3177 // somewhere inside the x-row. Add any characters from following siblings
3178 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07003179
3180 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
3181 // If node is the text node in a styled span, move up to the span node.
3182 node = node.parentNode;
3183 }
3184
Robert Gindafdbb3f22012-09-06 20:23:06 -07003185 while (node.nextSibling) {
3186 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08003187 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07003188 }
rgindaa09e7332012-08-17 12:49:51 -07003189 }
3190
3191 var rv = this.getRowsText(selection.startRow.rowIndex,
3192 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08003193 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07003194};
3195
rginda4bba5e12012-06-20 16:15:30 -07003196/**
3197 * Copy the current selection to the system clipboard, then clear it after a
3198 * short delay.
3199 */
3200hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07003201 var text = this.getSelectionText();
3202 if (text != null)
3203 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07003204};
3205
rgindaf0090c92012-02-10 14:58:52 -08003206hterm.Terminal.prototype.overlaySize = function() {
3207 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
3208};
3209
rginda87b86462011-12-14 13:48:03 -08003210/**
3211 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
3212 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07003213 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08003214 */
3215hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08003216 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08003217 this.scrollPort_.scrollRowToBottom(this.getRowCount());
3218
Robert Ginda8cb7d902013-06-20 14:37:18 -07003219 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08003220};
3221
3222/**
Mike Frysinger70b94692017-01-26 18:57:50 -10003223 * Open the selected url.
3224 */
3225hterm.Terminal.prototype.openSelectedUrl_ = function() {
3226 var str = this.getSelectionText();
3227
3228 // If there is no selection, try and expand wherever they clicked.
3229 if (str == null) {
3230 this.screen_.expandSelection(this.document_.getSelection());
3231 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04003232
3233 // If clicking in empty space, return.
3234 if (str == null)
3235 return;
Mike Frysinger70b94692017-01-26 18:57:50 -10003236 }
3237
3238 // Make sure URL is valid before opening.
3239 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
3240 return;
Mike Frysinger43472622017-06-26 18:11:07 -04003241
3242 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003243 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003244 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3245 // We have to whitelist a few protocols that lack authorities and thus
3246 // never use the //. Like mailto.
3247 switch (str.split(':', 1)[0]) {
3248 case 'mailto':
3249 break;
3250 default:
3251 str = 'http://' + str;
3252 break;
3253 }
3254 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003255
Mike Frysinger720fa832017-10-23 01:15:52 -04003256 hterm.openUrl(str);
Mike Frysinger70b94692017-01-26 18:57:50 -10003257}
3258
3259
3260/**
rgindad5613292012-06-19 15:40:37 -07003261 * Add the terminalRow and terminalColumn properties to mouse events and
3262 * then forward on to onMouse().
3263 *
3264 * The terminalRow and terminalColumn properties contain the (row, column)
3265 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003266 *
3267 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003268 */
3269hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003270 if (e.processedByTerminalHandler_) {
3271 // We register our event handlers on the document, as well as the cursor
3272 // and the scroll blocker. Mouse events that occur on the cursor or
3273 // scroll blocker will also appear on the document, but we don't want to
3274 // process them twice.
3275 //
3276 // We can't just prevent bubbling because that has other side effects, so
3277 // we decorate the event object with this property instead.
3278 return;
3279 }
3280
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003281 var reportMouseEvents = (!this.defeatMouseReports_ &&
3282 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3283
rgindafaa74742012-08-21 13:34:03 -07003284 e.processedByTerminalHandler_ = true;
3285
Robert Gindaeda48db2014-07-17 09:25:30 -07003286 // One based row/column stored on the mouse event.
3287 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
3288 this.scrollPort_.characterSize.height) + 1;
3289 e.terminalColumn = parseInt(e.clientX /
3290 this.scrollPort_.characterSize.width) + 1;
3291
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003292 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3293 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003294 return;
3295 }
3296
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003297 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003298 // If the cursor is visible and we're not sending mouse events to the
3299 // host app, then we want to hide the terminal cursor when the mouse
3300 // cursor is over top. This keeps the terminal cursor from interfering
3301 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003302 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3303 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3304 this.cursorNode_.style.display = 'none';
3305 } else if (this.cursorNode_.style.display == 'none') {
3306 this.cursorNode_.style.display = '';
3307 }
3308 }
rgindad5613292012-06-19 15:40:37 -07003309
Robert Ginda928cf632014-03-05 15:07:41 -08003310 if (e.type == 'mousedown') {
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003311 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003312 // If VT mouse reporting is disabled, or has been defeated with
3313 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003314 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003315 this.setSelectionEnabled(true);
3316 } else {
3317 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003318 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003319 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003320 this.setSelectionEnabled(false);
3321 e.preventDefault();
3322 }
3323 }
3324
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003325 if (!reportMouseEvents) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003326 if (e.type == 'dblclick' && this.copyOnSelect) {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003327 this.screen_.expandSelection(this.document_.getSelection());
Robert Ginda15ed4902016-07-12 10:43:22 -07003328 this.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07003329 }
3330
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003331 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003332 // Debounce this event with the dblclick event. If you try to doubleclick
3333 // a URL to open it, Chrome will fire click then dblclick, but we won't
3334 // have expanded the selection text at the first click event.
3335 clearTimeout(this.timeouts_.openUrl);
3336 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3337 500);
3338 return;
3339 }
3340
Mike Frysinger847577f2017-05-23 23:25:57 -04003341 if (e.type == 'mousedown') {
3342 if ((this.mouseRightClickPaste && e.button == 2 /* right button */) ||
Mike Frysinger2edd3612017-05-24 00:54:39 -04003343 e.button == this.mousePasteButton) {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003344 if (!this.paste())
Mike Frysinger05a57f02017-08-27 17:48:55 -04003345 console.warn('Could not paste manually due to web restrictions');
Mike Frysinger847577f2017-05-23 23:25:57 -04003346 }
3347 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003348
Mike Frysinger2edd3612017-05-24 00:54:39 -04003349 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003350 !this.document_.getSelection().isCollapsed) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003351 this.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003352 }
3353
3354 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3355 this.scrollBlockerNode_.engaged) {
3356 // Disengage the scroll-blocker after one of these events.
3357 this.scrollBlockerNode_.engaged = false;
3358 this.scrollBlockerNode_.style.top = '-99px';
3359 }
3360
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003361 // Emulate arrow key presses via scroll wheel events.
3362 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3363 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003364 if (e.type == 'wheel') {
3365 var delta = this.scrollPort_.scrollWheelDelta(e);
3366 var lines = lib.f.smartFloorDivide(
3367 Math.abs(delta), this.scrollPort_.characterSize.height);
3368
3369 var data = '\x1bO' + (delta < 0 ? 'B' : 'A');
3370 this.io.sendString(data.repeat(lines));
3371
3372 e.preventDefault();
3373 }
3374 }
Robert Ginda928cf632014-03-05 15:07:41 -08003375 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003376 if (!this.scrollBlockerNode_.engaged) {
3377 if (e.type == 'mousedown') {
3378 // Move the scroll-blocker into place if we want to keep the scrollport
3379 // from scrolling.
3380 this.scrollBlockerNode_.engaged = true;
3381 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3382 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3383 } else if (e.type == 'mousemove') {
3384 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3385 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003386 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003387 e.preventDefault();
3388 }
3389 }
Robert Ginda928cf632014-03-05 15:07:41 -08003390
3391 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003392 }
3393
Robert Ginda928cf632014-03-05 15:07:41 -08003394 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3395 // Restore this on mouseup in case it was temporarily defeated with a
3396 // alt-mousedown. Only do this when the selection is empty so that
3397 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003398 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003399 }
rgindad5613292012-06-19 15:40:37 -07003400};
3401
3402/**
3403 * Clients should override this if they care to know about mouse events.
3404 *
3405 * The event parameter will be a normal DOM mouse click event with additional
3406 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003407 *
3408 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003409 */
3410hterm.Terminal.prototype.onMouse = function(e) { };
3411
3412/**
rginda8e92a692012-05-20 19:37:20 -07003413 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003414 *
3415 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003416 */
Rob Spies06533ba2014-04-24 11:20:37 -07003417hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3418 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003419 this.restyleCursor_();
Gabriel Holodake8a09be2017-10-10 01:07:11 -04003420
3421 if (this.reportFocus) {
3422 this.io.sendString(focused === true ? '\x1b[I' : '\x1b[O')
3423 }
3424
Michael Kelly485ecd12014-06-09 11:41:56 -04003425 if (focused === true)
3426 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003427};
3428
3429/**
rginda8ba33642011-12-14 12:31:31 -08003430 * React when the ScrollPort is scrolled.
3431 */
3432hterm.Terminal.prototype.onScroll_ = function() {
3433 this.scheduleSyncCursorPosition_();
3434};
3435
3436/**
rginda9846e2f2012-01-27 13:53:33 -08003437 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003438 *
3439 * @param {Event} e The DOM paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003440 */
3441hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07003442 var data = e.text.replace(/\n/mg, '\r');
Connor Hegartyf525ceb2014-09-03 13:36:03 -07003443 data = this.keyboard.encode(data);
Robert Gindaa063b202014-07-21 11:08:25 -07003444 if (this.options_.bracketedPaste)
3445 data = '\x1b[200~' + data + '\x1b[201~';
3446
3447 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003448};
3449
3450/**
rgindaa09e7332012-08-17 12:49:51 -07003451 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003452 *
3453 * @param {Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003454 */
3455hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003456 if (!this.useDefaultWindowCopy) {
3457 e.preventDefault();
3458 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3459 }
rgindaa09e7332012-08-17 12:49:51 -07003460};
3461
3462/**
rginda8ba33642011-12-14 12:31:31 -08003463 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003464 *
3465 * Note: This function should not directly contain code that alters the internal
3466 * state of the terminal. That kind of code belongs in realizeWidth or
3467 * realizeHeight, so that it can be executed synchronously in the case of a
3468 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003469 */
3470hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003471 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003472 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003473 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003474 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003475
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003476 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003477 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003478 // gets removed from the document or during the initial load, and we can't
3479 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003480 // This can also happen if called before the scrollPort calculates the
3481 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003482 return;
3483 }
3484
rgindaa8ba17d2012-08-15 14:41:10 -07003485 var isNewSize = (columnCount != this.screenSize.width ||
3486 rowCount != this.screenSize.height);
3487
3488 // We do this even if the size didn't change, just to be sure everything is
3489 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003490 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003491 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003492
3493 if (isNewSize)
3494 this.overlaySize();
3495
Robert Gindafb1be6a2013-12-11 11:56:22 -08003496 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003497 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08003498};
3499
3500/**
3501 * Service the cursor blink timeout.
3502 */
3503hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003504 if (!this.options_.cursorBlink) {
3505 delete this.timeouts_.cursorBlink;
3506 return;
3507 }
3508
Robert Ginda830583c2013-08-07 13:20:46 -07003509 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3510 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003511 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003512 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3513 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003514 } else {
rginda87b86462011-12-14 13:48:03 -08003515 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003516 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3517 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003518 }
3519};
David Reveman8f552492012-03-28 12:18:41 -04003520
3521/**
3522 * Set the scrollbar-visible mode bit.
3523 *
3524 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3525 * Otherwise it will not.
3526 *
3527 * Defaults to on.
3528 *
3529 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3530 */
3531hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3532 this.scrollPort_.setScrollbarVisible(state);
3533};
Michael Kelly485ecd12014-06-09 11:41:56 -04003534
3535/**
Rob Spies49039e52014-12-17 13:40:04 -08003536 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003537 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003538 *
3539 * Defaults to 1.
3540 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003541 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003542 */
3543hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3544 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3545};
3546
3547/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003548 * Close all web notifications created by terminal bells.
3549 */
3550hterm.Terminal.prototype.closeBellNotifications_ = function() {
3551 this.bellNotificationList_.forEach(function(n) {
3552 n.close();
3553 });
3554 this.bellNotificationList_.length = 0;
3555};