blob: 374e71c0726b9bfb8709e86899b3c2310cb41c20 [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');
rgindaf0090c92012-02-10 14:58:52 -0800107 this.bellAudio_.setAttribute('preload', 'auto');
108
Michael Kelly485ecd12014-06-09 11:41:56 -0400109 // All terminal bell notifications that have been generated (not necessarily
110 // shown).
111 this.bellNotificationList_ = [];
112
113 // Whether we have permission to display notifications.
114 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400115
rginda6d397402012-01-17 10:58:29 -0800116 // Cursor position and attributes saved with DECSC.
117 this.savedOptions_ = {};
118
rginda8ba33642011-12-14 12:31:31 -0800119 // The current mode bits for the terminal.
120 this.options_ = new hterm.Options();
121
122 // Timeouts we might need to clear.
123 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800124
125 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800126 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800127
Zhu Qunying30d40712017-03-14 16:27:00 -0700128 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800129 this.keyboard = new hterm.Keyboard(this);
130
rginda87b86462011-12-14 13:48:03 -0800131 // General IO interface that can be given to third parties without exposing
132 // the entire terminal object.
133 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800134
rgindad5613292012-06-19 15:40:37 -0700135 // True if mouse-click-drag should scroll the terminal.
136 this.enableMouseDragScroll = true;
137
Robert Ginda57f03b42012-09-13 11:02:48 -0700138 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400139 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700140 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700141
Zhu Qunying30d40712017-03-14 16:27:00 -0700142 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700143 this.useDefaultWindowCopy = false;
144
145 this.clearSelectionAfterCopy = true;
146
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400147 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800148 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700149
150 this.setProfile(opt_profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500151 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800152};
153
154/**
Robert Ginda830583c2013-08-07 13:20:46 -0700155 * Possible cursor shapes.
156 */
157hterm.Terminal.cursorShape = {
158 BLOCK: 'BLOCK',
159 BEAM: 'BEAM',
160 UNDERLINE: 'UNDERLINE'
161};
162
163/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700164 * Clients should override this to be notified when the terminal is ready
165 * for use.
166 *
167 * The terminal initialization is asynchronous, and shouldn't be used before
168 * this method is called.
169 */
170hterm.Terminal.prototype.onTerminalReady = function() { };
171
172/**
rginda35c456b2012-02-09 17:29:05 -0800173 * Default tab with of 8 to match xterm.
174 */
175hterm.Terminal.prototype.tabWidth = 8;
176
177/**
rginda9f5222b2012-03-05 11:53:28 -0800178 * Select a preference profile.
179 *
180 * This will load the terminal preferences for the given profile name and
181 * associate subsequent preference changes with the new preference profile.
182 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500183 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800184 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700185 * @param {function} opt_callback Optional callback to invoke when the profile
186 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800187 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700188hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
189 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800190
Robert Ginda57f03b42012-09-13 11:02:48 -0700191 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800192
Robert Ginda57f03b42012-09-13 11:02:48 -0700193 if (this.prefs_)
194 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800195
Robert Ginda57f03b42012-09-13 11:02:48 -0700196 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
197 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800198 'alt-gr-mode': function(v) {
199 if (v == null) {
200 if (navigator.language.toLowerCase() == 'en-us') {
201 v = 'none';
202 } else {
203 v = 'right-alt';
204 }
205 } else if (typeof v == 'string') {
206 v = v.toLowerCase();
207 } else {
208 v = 'none';
209 }
210
211 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v))
212 v = 'none';
213
214 terminal.keyboard.altGrMode = v;
215 },
216
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700217 'alt-backspace-is-meta-backspace': function(v) {
218 terminal.keyboard.altBackspaceIsMetaBackspace = v;
219 },
220
Robert Ginda57f03b42012-09-13 11:02:48 -0700221 'alt-is-meta': function(v) {
222 terminal.keyboard.altIsMeta = v;
223 },
224
225 'alt-sends-what': function(v) {
226 if (!/^(escape|8-bit|browser-key)$/.test(v))
227 v = 'escape';
228
229 terminal.keyboard.altSendsWhat = v;
230 },
231
232 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800233 var ary = v.match(/^lib-resource:(\S+)/);
234 if (ary) {
235 terminal.bellAudio_.setAttribute('src',
236 lib.resource.getDataUrl(ary[1]));
237 } else {
238 terminal.bellAudio_.setAttribute('src', v);
239 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700240 },
241
Michael Kelly485ecd12014-06-09 11:41:56 -0400242 'desktop-notification-bell': function(v) {
243 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700244 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400245 Notification.permission === 'granted';
246 if (!terminal.desktopNotificationBell_) {
247 // Note: We don't call Notification.requestPermission here because
248 // Chrome requires the call be the result of a user action (such as an
249 // onclick handler), and pref listeners are run asynchronously.
250 //
251 // A way of working around this would be to display a dialog in the
252 // terminal with a "click-to-request-permission" button.
253 console.warn('desktop-notification-bell is true but we do not have ' +
254 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400255 }
256 } else {
257 terminal.desktopNotificationBell_ = false;
258 }
259 },
260
Robert Ginda57f03b42012-09-13 11:02:48 -0700261 'background-color': function(v) {
262 terminal.setBackgroundColor(v);
263 },
264
265 'background-image': function(v) {
266 terminal.scrollPort_.setBackgroundImage(v);
267 },
268
269 'background-size': function(v) {
270 terminal.scrollPort_.setBackgroundSize(v);
271 },
272
273 'background-position': function(v) {
274 terminal.scrollPort_.setBackgroundPosition(v);
275 },
276
277 'backspace-sends-backspace': function(v) {
278 terminal.keyboard.backspaceSendsBackspace = v;
279 },
280
Brad Town18654b62015-03-12 00:27:45 -0700281 'character-map-overrides': function(v) {
282 if (!(v == null || v instanceof Object)) {
283 console.warn('Preference character-map-modifications is not an ' +
284 'object: ' + v);
285 return;
286 }
287
Mike Frysinger095d4062017-06-14 00:29:48 -0700288 terminal.vt.characterMaps.reset();
289 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700290 },
291
Robert Ginda57f03b42012-09-13 11:02:48 -0700292 'cursor-blink': function(v) {
293 terminal.setCursorBlink(!!v);
294 },
295
Robert Gindaea2183e2014-07-17 09:51:51 -0700296 'cursor-blink-cycle': function(v) {
297 if (v instanceof Array &&
298 typeof v[0] == 'number' &&
299 typeof v[1] == 'number') {
300 terminal.cursorBlinkCycle_ = v;
301 } else if (typeof v == 'number') {
302 terminal.cursorBlinkCycle_ = [v, v];
303 } else {
304 // Fast blink indicates an error.
305 terminal.cursorBlinkCycle_ = [100, 100];
306 }
307 },
308
Robert Ginda57f03b42012-09-13 11:02:48 -0700309 'cursor-color': function(v) {
310 terminal.setCursorColor(v);
311 },
312
313 'color-palette-overrides': function(v) {
314 if (!(v == null || v instanceof Object || v instanceof Array)) {
315 console.warn('Preference color-palette-overrides is not an array or ' +
316 'object: ' + v);
317 return;
rginda9f5222b2012-03-05 11:53:28 -0800318 }
rginda9f5222b2012-03-05 11:53:28 -0800319
Robert Ginda57f03b42012-09-13 11:02:48 -0700320 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700321
Robert Ginda57f03b42012-09-13 11:02:48 -0700322 if (v) {
323 for (var key in v) {
324 var i = parseInt(key);
325 if (isNaN(i) || i < 0 || i > 255) {
326 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
327 continue;
328 }
329
330 if (v[i]) {
331 var rgb = lib.colors.normalizeCSS(v[i]);
332 if (rgb)
333 lib.colors.colorPalette[i] = rgb;
334 }
335 }
rginda30f20f62012-04-05 16:36:19 -0700336 }
rginda30f20f62012-04-05 16:36:19 -0700337
Evan Jones5f9df812016-12-06 09:38:58 -0500338 terminal.primaryScreen_.textAttributes.resetColorPalette();
Robert Ginda57f03b42012-09-13 11:02:48 -0700339 terminal.alternateScreen_.textAttributes.resetColorPalette();
340 },
rginda30f20f62012-04-05 16:36:19 -0700341
Robert Ginda57f03b42012-09-13 11:02:48 -0700342 'copy-on-select': function(v) {
343 terminal.copyOnSelect = !!v;
344 },
rginda9f5222b2012-03-05 11:53:28 -0800345
Rob Spies0bec09b2014-06-06 15:58:09 -0700346 'use-default-window-copy': function(v) {
347 terminal.useDefaultWindowCopy = !!v;
348 },
349
350 'clear-selection-after-copy': function(v) {
351 terminal.clearSelectionAfterCopy = !!v;
352 },
353
Robert Ginda7e5e9522014-03-14 12:23:58 -0700354 'ctrl-plus-minus-zero-zoom': function(v) {
355 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
356 },
357
Robert Gindafb5a3f92014-05-13 14:12:00 -0700358 'ctrl-c-copy': function(v) {
359 terminal.keyboard.ctrlCCopy = v;
360 },
361
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100362 'ctrl-v-paste': function(v) {
363 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700364 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100365 },
366
Masaya Suzuki273aa982014-05-31 07:25:55 +0900367 'east-asian-ambiguous-as-two-column': function(v) {
368 lib.wc.regardCjkAmbiguous = v;
369 },
370
Robert Ginda57f03b42012-09-13 11:02:48 -0700371 'enable-8-bit-control': function(v) {
372 terminal.vt.enable8BitControl = !!v;
373 },
rginda30f20f62012-04-05 16:36:19 -0700374
Robert Ginda57f03b42012-09-13 11:02:48 -0700375 'enable-bold': function(v) {
376 terminal.syncBoldSafeState();
377 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400378
Robert Ginda3e278d72014-03-25 13:18:51 -0700379 'enable-bold-as-bright': function(v) {
380 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
381 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
382 },
383
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400384 'enable-blink': function(v) {
385 terminal.syncBlinkState();
386 },
387
Robert Ginda57f03b42012-09-13 11:02:48 -0700388 'enable-clipboard-write': function(v) {
389 terminal.vt.enableClipboardWrite = !!v;
390 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400391
Robert Ginda3755e752013-05-31 13:34:09 -0700392 'enable-dec12': function(v) {
393 terminal.vt.enableDec12 = !!v;
394 },
395
Robert Ginda57f03b42012-09-13 11:02:48 -0700396 'font-family': function(v) {
397 terminal.syncFontFamily();
398 },
rginda30f20f62012-04-05 16:36:19 -0700399
Robert Ginda57f03b42012-09-13 11:02:48 -0700400 'font-size': function(v) {
401 terminal.setFontSize(v);
402 },
rginda9875d902012-08-20 16:21:57 -0700403
Robert Ginda57f03b42012-09-13 11:02:48 -0700404 'font-smoothing': function(v) {
405 terminal.syncFontFamily();
406 },
rgindade84e382012-04-20 15:39:31 -0700407
Robert Ginda57f03b42012-09-13 11:02:48 -0700408 'foreground-color': function(v) {
409 terminal.setForegroundColor(v);
410 },
rginda30f20f62012-04-05 16:36:19 -0700411
Robert Ginda57f03b42012-09-13 11:02:48 -0700412 'home-keys-scroll': function(v) {
413 terminal.keyboard.homeKeysScroll = v;
414 },
rginda4bba5e12012-06-20 16:15:30 -0700415
Robert Gindaa8165692015-06-15 14:46:31 -0700416 'keybindings': function(v) {
417 terminal.keyboard.bindings.clear();
418
419 if (!v)
420 return;
421
422 if (!(v instanceof Object)) {
423 console.error('Error in keybindings preference: Expected object');
424 return;
425 }
426
427 try {
428 terminal.keyboard.bindings.addBindings(v);
429 } catch (ex) {
430 console.error('Error in keybindings preference: ' + ex);
431 }
432 },
433
Robert Ginda57f03b42012-09-13 11:02:48 -0700434 'max-string-sequence': function(v) {
435 terminal.vt.maxStringSequence = v;
436 },
rginda11057d52012-04-25 12:29:56 -0700437
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700438 'media-keys-are-fkeys': function(v) {
439 terminal.keyboard.mediaKeysAreFKeys = v;
440 },
441
Robert Ginda57f03b42012-09-13 11:02:48 -0700442 'meta-sends-escape': function(v) {
443 terminal.keyboard.metaSendsEscape = v;
444 },
rginda30f20f62012-04-05 16:36:19 -0700445
Mike Frysinger847577f2017-05-23 23:25:57 -0400446 'mouse-right-click-paste': function(v) {
447 terminal.mouseRightClickPaste = v;
448 },
449
Robert Ginda57f03b42012-09-13 11:02:48 -0700450 'mouse-paste-button': function(v) {
451 terminal.syncMousePasteButton();
452 },
rgindaa8ba17d2012-08-15 14:41:10 -0700453
Robert Gindae76aa9f2014-03-14 12:29:12 -0700454 'page-keys-scroll': function(v) {
455 terminal.keyboard.pageKeysScroll = v;
456 },
457
Robert Ginda40932892012-12-10 17:26:40 -0800458 'pass-alt-number': function(v) {
459 if (v == null) {
460 var osx = window.navigator.userAgent.match(/Mac OS X/);
461
462 // Let Alt-1..9 pass to the browser (to control tab switching) on
463 // non-OS X systems, or if hterm is not opened in an app window.
464 v = (!osx && hterm.windowType != 'popup');
465 }
466
467 terminal.passAltNumber = v;
468 },
469
470 'pass-ctrl-number': function(v) {
471 if (v == null) {
472 var osx = window.navigator.userAgent.match(/Mac OS X/);
473
474 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
475 // non-OS X systems, or if hterm is not opened in an app window.
476 v = (!osx && hterm.windowType != 'popup');
477 }
478
479 terminal.passCtrlNumber = v;
480 },
481
482 'pass-meta-number': function(v) {
483 if (v == null) {
484 var osx = window.navigator.userAgent.match(/Mac OS X/);
485
486 // Let Meta-1..9 pass to the browser (to control tab switching) on
487 // OS X systems, or if hterm is not opened in an app window.
488 v = (osx && hterm.windowType != 'popup');
489 }
490
491 terminal.passMetaNumber = v;
492 },
493
Marius Schilder77857b32014-05-14 16:21:26 -0700494 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700495 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700496 },
497
Robert Ginda8cb7d902013-06-20 14:37:18 -0700498 'receive-encoding': function(v) {
499 if (!(/^(utf-8|raw)$/).test(v)) {
500 console.warn('Invalid value for "receive-encoding": ' + v);
501 v = 'utf-8';
502 }
503
504 terminal.vt.characterEncoding = v;
505 },
506
Robert Ginda57f03b42012-09-13 11:02:48 -0700507 'scroll-on-keystroke': function(v) {
508 terminal.scrollOnKeystroke_ = v;
509 },
rginda9f5222b2012-03-05 11:53:28 -0800510
Robert Ginda57f03b42012-09-13 11:02:48 -0700511 'scroll-on-output': function(v) {
512 terminal.scrollOnOutput_ = v;
513 },
rginda30f20f62012-04-05 16:36:19 -0700514
Robert Ginda57f03b42012-09-13 11:02:48 -0700515 'scrollbar-visible': function(v) {
516 terminal.setScrollbarVisible(v);
517 },
rginda9f5222b2012-03-05 11:53:28 -0800518
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400519 'scroll-wheel-may-send-arrow-keys': function(v) {
520 terminal.scrollWheelArrowKeys_ = v;
521 },
522
Rob Spies49039e52014-12-17 13:40:04 -0800523 'scroll-wheel-move-multiplier': function(v) {
524 terminal.setScrollWheelMoveMultipler(v);
525 },
526
Robert Ginda8cb7d902013-06-20 14:37:18 -0700527 'send-encoding': function(v) {
528 if (!(/^(utf-8|raw)$/).test(v)) {
529 console.warn('Invalid value for "send-encoding": ' + v);
530 v = 'utf-8';
531 }
532
533 terminal.keyboard.characterEncoding = v;
534 },
535
Robert Ginda57f03b42012-09-13 11:02:48 -0700536 'shift-insert-paste': function(v) {
537 terminal.keyboard.shiftInsertPaste = v;
538 },
rginda9f5222b2012-03-05 11:53:28 -0800539
Mike Frysingera7768922017-07-28 15:00:12 -0400540 'terminal-encoding': function(v) {
541 switch (v) {
542 default:
543 console.warn('Invalid value for "terminal-encoding": ' + v);
544 // Fall through.
545 case 'iso-2022':
546 terminal.vt.codingSystemUtf8 = false;
547 terminal.vt.codingSystemLocked = false;
548 break;
549 case 'utf-8-locked':
550 terminal.vt.codingSystemUtf8 = true;
551 terminal.vt.codingSystemLocked = true;
552 break;
553 case 'utf-8':
554 terminal.vt.codingSystemUtf8 = true;
555 terminal.vt.codingSystemLocked = false;
556 break;
557 }
558 },
559
Robert Gindae76aa9f2014-03-14 12:29:12 -0700560 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400561 terminal.scrollPort_.setUserCssUrl(v);
562 },
563
564 'user-css-text': function(v) {
565 terminal.scrollPort_.setUserCssText(v);
566 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400567
568 'word-break-match-left': function(v) {
569 terminal.primaryScreen_.wordBreakMatchLeft = v;
570 terminal.alternateScreen_.wordBreakMatchLeft = v;
571 },
572
573 'word-break-match-right': function(v) {
574 terminal.primaryScreen_.wordBreakMatchRight = v;
575 terminal.alternateScreen_.wordBreakMatchRight = v;
576 },
577
578 'word-break-match-middle': function(v) {
579 terminal.primaryScreen_.wordBreakMatchMiddle = v;
580 terminal.alternateScreen_.wordBreakMatchMiddle = v;
581 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700582 });
rginda30f20f62012-04-05 16:36:19 -0700583
Robert Ginda57f03b42012-09-13 11:02:48 -0700584 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800585 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700586
587 if (opt_callback)
588 opt_callback();
589 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800590};
591
Rob Spies56953412014-04-28 14:09:47 -0700592
593/**
594 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500595 *
596 * @return {hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700597 */
598hterm.Terminal.prototype.getPrefs = function() {
599 return this.prefs_;
600};
601
Robert Gindaa063b202014-07-21 11:08:25 -0700602/**
603 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500604 *
605 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700606 */
607hterm.Terminal.prototype.setBracketedPaste = function(state) {
608 this.options_.bracketedPaste = state;
609};
Rob Spies56953412014-04-28 14:09:47 -0700610
rginda8e92a692012-05-20 19:37:20 -0700611/**
612 * Set the color for the cursor.
613 *
614 * If you want this setting to persist, set it through prefs_, rather than
615 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500616 *
617 * @param {string} color The color to set.
rginda8e92a692012-05-20 19:37:20 -0700618 */
619hterm.Terminal.prototype.setCursorColor = function(color) {
Robert Ginda830583c2013-08-07 13:20:46 -0700620 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700621 this.cursorNode_.style.backgroundColor = color;
622 this.cursorNode_.style.borderColor = color;
623};
624
625/**
626 * Return the current cursor color as a string.
Evan Jones2600d4f2016-12-06 09:29:36 -0500627 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700628 */
629hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700630 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700631};
632
633/**
rgindad5613292012-06-19 15:40:37 -0700634 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500635 *
636 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700637 */
638hterm.Terminal.prototype.setSelectionEnabled = function(state) {
639 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700640};
641
642/**
rginda8e92a692012-05-20 19:37:20 -0700643 * Set the background color.
644 *
645 * If you want this setting to persist, set it through prefs_, rather than
646 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500647 *
648 * @param {string} color The color to set.
rginda8e92a692012-05-20 19:37:20 -0700649 */
650hterm.Terminal.prototype.setBackgroundColor = function(color) {
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 *
677 * @param {string} color The color to set.
rginda8e92a692012-05-20 19:37:20 -0700678 */
679hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700680 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700681 this.primaryScreen_.textAttributes.setDefaults(
682 this.foregroundColor_, this.backgroundColor_);
683 this.alternateScreen_.textAttributes.setDefaults(
684 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700685 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800686};
687
688/**
689 * Return the current terminal foreground color.
690 *
691 * Intended for use by other classes, so we don't have to expose the entire
692 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500693 *
694 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800695 */
696hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700697 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800698};
699
700/**
rginda87b86462011-12-14 13:48:03 -0800701 * Create a new instance of a terminal command and run it with a given
702 * argument string.
703 *
704 * @param {function} commandClass The constructor for a terminal command.
705 * @param {string} argString The argument string to pass to the command.
706 */
707hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700708 var environment = this.prefs_.get('environment');
709 if (typeof environment != 'object' || environment == null)
710 environment = {};
711
rginda87b86462011-12-14 13:48:03 -0800712 var self = this;
713 this.command = new commandClass(
714 { argString: argString || '',
715 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700716 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800717 onExit: function(code) {
718 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800719 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700720 if (self.prefs_.get('close-on-exit'))
721 window.close();
rginda87b86462011-12-14 13:48:03 -0800722 }
723 });
724
rgindafeaf3142012-01-31 15:14:20 -0800725 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800726 this.command.run();
727};
728
729/**
rgindafeaf3142012-01-31 15:14:20 -0800730 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500731 *
732 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800733 */
734hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700735 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800736};
737
738/**
739 * Install the keyboard handler for this terminal.
740 *
741 * This will prevent the browser from seeing any keystrokes sent to the
742 * terminal.
743 */
744hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700745 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
rgindafeaf3142012-01-31 15:14:20 -0800746}
747
748/**
749 * Uninstall the keyboard handler for this terminal.
750 */
751hterm.Terminal.prototype.uninstallKeyboard = function() {
752 this.keyboard.installKeyboard(null);
753}
754
755/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400756 * Set a CSS variable.
757 *
758 * Normally this is used to set variables in the hterm namespace.
759 *
760 * @param {string} name The variable to set.
761 * @param {string} value The value to assign to the variable.
762 * @param {string?} opt_prefix The variable namespace/prefix to use.
763 */
764hterm.Terminal.prototype.setCssVar = function(name, value,
765 opt_prefix='--hterm-') {
766 this.document_.documentElement.style.setProperty(
767 `${opt_prefix}${name}`, value);
768};
769
770/**
rginda35c456b2012-02-09 17:29:05 -0800771 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800772 *
773 * Call setFontSize(0) to reset to the default font size.
774 *
775 * This function does not modify the font-size preference.
776 *
777 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800778 */
779hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800780 if (px === 0)
781 px = this.prefs_.get('font-size');
782
rginda35c456b2012-02-09 17:29:05 -0800783 this.scrollPort_.setFontSize(px);
Mike Frysingercce97c42017-08-05 01:11:22 -0400784 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
785 this.setCssVar('charsize-height',
786 this.scrollPort_.characterSize.height + 'px');
rginda35c456b2012-02-09 17:29:05 -0800787};
788
789/**
790 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500791 *
792 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800793 */
794hterm.Terminal.prototype.getFontSize = function() {
795 return this.scrollPort_.getFontSize();
796};
797
798/**
rginda8e92a692012-05-20 19:37:20 -0700799 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500800 *
801 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700802 */
803hterm.Terminal.prototype.getFontFamily = function() {
804 return this.scrollPort_.getFontFamily();
805};
806
807/**
rginda35c456b2012-02-09 17:29:05 -0800808 * Set the CSS "font-family" for this terminal.
809 */
rginda9f5222b2012-03-05 11:53:28 -0800810hterm.Terminal.prototype.syncFontFamily = function() {
811 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
812 this.prefs_.get('font-smoothing'));
813 this.syncBoldSafeState();
814};
815
rginda4bba5e12012-06-20 16:15:30 -0700816/**
817 * Set this.mousePasteButton based on the mouse-paste-button pref,
818 * autodetecting if necessary.
819 */
820hterm.Terminal.prototype.syncMousePasteButton = function() {
821 var button = this.prefs_.get('mouse-paste-button');
822 if (typeof button == 'number') {
823 this.mousePasteButton = button;
824 return;
825 }
826
827 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
Mike Frysinger98dd15b2017-05-18 22:52:23 -0400828 if (!ary || ary[1] == 'CrOS') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400829 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700830 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400831 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700832 }
833};
834
835/**
836 * Enable or disable bold based on the enable-bold pref, autodetecting if
837 * necessary.
838 */
rginda9f5222b2012-03-05 11:53:28 -0800839hterm.Terminal.prototype.syncBoldSafeState = function() {
840 var enableBold = this.prefs_.get('enable-bold');
841 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700842 this.primaryScreen_.textAttributes.enableBold = enableBold;
843 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800844 return;
845 }
846
rgindaf7521392012-02-28 17:20:34 -0800847 var normalSize = this.scrollPort_.measureCharacterSize();
848 var boldSize = this.scrollPort_.measureCharacterSize('bold');
849
850 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800851 if (!isBoldSafe) {
852 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700853 'from normal. Font family is: ' +
854 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800855 }
rginda9f5222b2012-03-05 11:53:28 -0800856
Robert Gindaed016262012-10-26 16:27:09 -0700857 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
858 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800859};
860
861/**
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400862 * Enable or disable blink based on the enable-blink pref.
863 */
864hterm.Terminal.prototype.syncBlinkState = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400865 this.setCssVar('node-duration',
866 this.prefs_.get('enable-blink') ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400867};
868
869/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400870 * Set the mouse cursor style based on the current terminal mode.
871 */
872hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400873 this.setCssVar('mouse-cursor-style',
874 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
875 'var(--hterm-mouse-cursor-text)' :
876 'var(--hterm-mouse-cursor-pointer)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400877};
878
879/**
rginda87b86462011-12-14 13:48:03 -0800880 * Return a copy of the current cursor position.
881 *
882 * @return {hterm.RowCol} The RowCol object representing the current position.
883 */
884hterm.Terminal.prototype.saveCursor = function() {
885 return this.screen_.cursorPosition.clone();
886};
887
Evan Jones2600d4f2016-12-06 09:29:36 -0500888/**
889 * Return the current text attributes.
890 *
891 * @return {string}
892 */
rgindaa19afe22012-01-25 15:40:22 -0800893hterm.Terminal.prototype.getTextAttributes = function() {
894 return this.screen_.textAttributes;
895};
896
Evan Jones2600d4f2016-12-06 09:29:36 -0500897/**
898 * Set the text attributes.
899 *
900 * @param {string} textAttributes The attributes to set.
901 */
rginda1a09aa02012-06-18 21:11:25 -0700902hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
903 this.screen_.textAttributes = textAttributes;
904};
905
rginda87b86462011-12-14 13:48:03 -0800906/**
rgindaf522ce02012-04-17 17:49:17 -0700907 * Return the current browser zoom factor applied to the terminal.
908 *
909 * @return {number} The current browser zoom factor.
910 */
911hterm.Terminal.prototype.getZoomFactor = function() {
912 return this.scrollPort_.characterSize.zoomFactor;
913};
914
915/**
rginda9846e2f2012-01-27 13:53:33 -0800916 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500917 *
918 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800919 */
920hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800921 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800922};
923
924/**
rginda87b86462011-12-14 13:48:03 -0800925 * Restore a previously saved cursor position.
926 *
927 * @param {hterm.RowCol} cursor The position to restore.
928 */
929hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700930 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
931 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800932 this.screen_.setCursorPosition(row, column);
933 if (cursor.column > column ||
934 cursor.column == column && cursor.overflow) {
935 this.screen_.cursorPosition.overflow = true;
936 }
rginda87b86462011-12-14 13:48:03 -0800937};
938
939/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400940 * Clear the cursor's overflow flag.
941 */
942hterm.Terminal.prototype.clearCursorOverflow = function() {
943 this.screen_.cursorPosition.overflow = false;
944};
945
946/**
Robert Ginda830583c2013-08-07 13:20:46 -0700947 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -0500948 *
949 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -0700950 */
951hterm.Terminal.prototype.setCursorShape = function(shape) {
952 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -0800953 this.restyleCursor_();
Robert Ginda830583c2013-08-07 13:20:46 -0700954}
955
956/**
957 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -0500958 *
959 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -0700960 */
961hterm.Terminal.prototype.getCursorShape = function() {
962 return this.cursorShape_;
963}
964
965/**
rginda87b86462011-12-14 13:48:03 -0800966 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -0500967 *
968 * @param {number} columnCount
rginda87b86462011-12-14 13:48:03 -0800969 */
970hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800971 if (columnCount == null) {
972 this.div_.style.width = '100%';
973 return;
974 }
975
Robert Ginda26806d12014-07-24 13:44:07 -0700976 this.div_.style.width = Math.ceil(
977 this.scrollPort_.characterSize.width *
978 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400979 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800980 this.scheduleSyncCursorPosition_();
981};
rginda87b86462011-12-14 13:48:03 -0800982
rgindac9bc5502012-01-18 11:48:44 -0800983/**
rginda35c456b2012-02-09 17:29:05 -0800984 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -0500985 *
986 * @param {number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -0800987 */
988hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800989 if (rowCount == null) {
990 this.div_.style.height = '100%';
991 return;
992 }
993
rginda35c456b2012-02-09 17:29:05 -0800994 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700995 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800996 this.realizeSize_(this.screenSize.width, rowCount);
997 this.scheduleSyncCursorPosition_();
998};
999
1000/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001001 * Deal with terminal size changes.
1002 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001003 * @param {number} columnCount The number of columns.
1004 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001005 */
1006hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
1007 if (columnCount != this.screenSize.width)
1008 this.realizeWidth_(columnCount);
1009
1010 if (rowCount != this.screenSize.height)
1011 this.realizeHeight_(rowCount);
1012
1013 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -07001014 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001015};
1016
1017/**
rgindac9bc5502012-01-18 11:48:44 -08001018 * Deal with terminal width changes.
1019 *
1020 * This function does what needs to be done when the terminal width changes
1021 * out from under us. It happens here rather than in onResize_() because this
1022 * code may need to run synchronously to handle programmatic changes of
1023 * terminal width.
1024 *
1025 * Relying on the browser to send us an async resize event means we may not be
1026 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001027 *
1028 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001029 */
1030hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001031 if (columnCount <= 0)
1032 throw new Error('Attempt to realize bad width: ' + columnCount);
1033
rgindac9bc5502012-01-18 11:48:44 -08001034 var deltaColumns = columnCount - this.screen_.getWidth();
1035
rginda87b86462011-12-14 13:48:03 -08001036 this.screenSize.width = columnCount;
1037 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001038
1039 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -04001040 if (this.defaultTabStops)
1041 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -08001042 } else {
1043 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -04001044 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -08001045 break;
1046
1047 this.tabStops_.pop();
1048 }
1049 }
1050
1051 this.screen_.setColumnCount(this.screenSize.width);
1052};
1053
1054/**
1055 * Deal with terminal height changes.
1056 *
1057 * This function does what needs to be done when the terminal height changes
1058 * out from under us. It happens here rather than in onResize_() because this
1059 * code may need to run synchronously to handle programmatic changes of
1060 * terminal height.
1061 *
1062 * Relying on the browser to send us an async resize event means we may not be
1063 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001064 *
1065 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001066 */
1067hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001068 if (rowCount <= 0)
1069 throw new Error('Attempt to realize bad height: ' + rowCount);
1070
rgindac9bc5502012-01-18 11:48:44 -08001071 var deltaRows = rowCount - this.screen_.getHeight();
1072
1073 this.screenSize.height = rowCount;
1074
1075 var cursor = this.saveCursor();
1076
1077 if (deltaRows < 0) {
1078 // Screen got smaller.
1079 deltaRows *= -1;
1080 while (deltaRows) {
1081 var lastRow = this.getRowCount() - 1;
1082 if (lastRow - this.scrollbackRows_.length == cursor.row)
1083 break;
1084
1085 if (this.getRowText(lastRow))
1086 break;
1087
1088 this.screen_.popRow();
1089 deltaRows--;
1090 }
1091
1092 var ary = this.screen_.shiftRows(deltaRows);
1093 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1094
1095 // We just removed rows from the top of the screen, we need to update
1096 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001097 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001098 } else if (deltaRows > 0) {
1099 // Screen got larger.
1100
1101 if (deltaRows <= this.scrollbackRows_.length) {
1102 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1103 var rows = this.scrollbackRows_.splice(
1104 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1105 this.screen_.unshiftRows(rows);
1106 deltaRows -= scrollbackCount;
1107 cursor.row += scrollbackCount;
1108 }
1109
1110 if (deltaRows)
1111 this.appendRows_(deltaRows);
1112 }
1113
rginda35c456b2012-02-09 17:29:05 -08001114 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001115 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001116};
1117
1118/**
1119 * Scroll the terminal to the top of the scrollback buffer.
1120 */
1121hterm.Terminal.prototype.scrollHome = function() {
1122 this.scrollPort_.scrollRowToTop(0);
1123};
1124
1125/**
1126 * Scroll the terminal to the end.
1127 */
1128hterm.Terminal.prototype.scrollEnd = function() {
1129 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1130};
1131
1132/**
1133 * Scroll the terminal one page up (minus one line) relative to the current
1134 * position.
1135 */
1136hterm.Terminal.prototype.scrollPageUp = function() {
1137 var i = this.scrollPort_.getTopRowIndex();
1138 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
1139};
1140
1141/**
1142 * Scroll the terminal one page down (minus one line) relative to the current
1143 * position.
1144 */
1145hterm.Terminal.prototype.scrollPageDown = function() {
1146 var i = this.scrollPort_.getTopRowIndex();
1147 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -08001148};
1149
rgindac9bc5502012-01-18 11:48:44 -08001150/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001151 * Scroll the terminal one line up relative to the current position.
1152 */
1153hterm.Terminal.prototype.scrollLineUp = function() {
1154 var i = this.scrollPort_.getTopRowIndex();
1155 this.scrollPort_.scrollRowToTop(i - 1);
1156};
1157
1158/**
1159 * Scroll the terminal one line down relative to the current position.
1160 */
1161hterm.Terminal.prototype.scrollLineDown = function() {
1162 var i = this.scrollPort_.getTopRowIndex();
1163 this.scrollPort_.scrollRowToTop(i + 1);
1164};
1165
1166/**
Robert Ginda40932892012-12-10 17:26:40 -08001167 * Clear primary screen, secondary screen, and the scrollback buffer.
1168 */
1169hterm.Terminal.prototype.wipeContents = function() {
1170 this.scrollbackRows_.length = 0;
1171 this.scrollPort_.resetCache();
1172
1173 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
1174 var bottom = screen.getHeight();
1175 if (bottom > 0) {
1176 this.renumberRows_(0, bottom);
1177 this.clearHome(screen);
1178 }
1179 }.bind(this));
1180
1181 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001182 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001183};
1184
1185/**
rgindac9bc5502012-01-18 11:48:44 -08001186 * Full terminal reset.
1187 */
rginda87b86462011-12-14 13:48:03 -08001188hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -08001189 this.clearAllTabStops();
1190 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001191
1192 this.clearHome(this.primaryScreen_);
1193 this.primaryScreen_.textAttributes.reset();
1194
1195 this.clearHome(this.alternateScreen_);
1196 this.alternateScreen_.textAttributes.reset();
1197
rgindab8bc8932012-04-27 12:45:03 -07001198 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1199
Robert Ginda92e18102013-03-14 13:56:37 -07001200 this.vt.reset();
1201
rgindac9bc5502012-01-18 11:48:44 -08001202 this.softReset();
rginda87b86462011-12-14 13:48:03 -08001203};
1204
rgindac9bc5502012-01-18 11:48:44 -08001205/**
1206 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001207 *
1208 * Perform a soft reset to the default values listed in
1209 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001210 */
rginda0f5c0292012-01-13 11:00:13 -08001211hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -07001212 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001213 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001214
Brad Townb62dfdc2015-03-16 19:07:15 -07001215 // We show the cursor on soft reset but do not alter the blink state.
1216 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1217
rgindab8bc8932012-04-27 12:45:03 -07001218 // Xterm also resets the color palette on soft reset, even though it doesn't
1219 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -07001220 this.primaryScreen_.textAttributes.resetColorPalette();
1221 this.alternateScreen_.textAttributes.resetColorPalette();
1222
rgindab8bc8932012-04-27 12:45:03 -07001223 // The xterm man page explicitly says this will happen on soft reset.
1224 this.setVTScrollRegion(null, null);
1225
1226 // Xterm also shows the cursor on soft reset, but does not alter the blink
1227 // state.
rgindaa19afe22012-01-25 15:40:22 -08001228 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001229};
1230
rgindac9bc5502012-01-18 11:48:44 -08001231/**
1232 * Move the cursor forward to the next tab stop, or to the last column
1233 * if no more tab stops are set.
1234 */
1235hterm.Terminal.prototype.forwardTabStop = function() {
1236 var column = this.screen_.cursorPosition.column;
1237
1238 for (var i = 0; i < this.tabStops_.length; i++) {
1239 if (this.tabStops_[i] > column) {
1240 this.setCursorColumn(this.tabStops_[i]);
1241 return;
1242 }
1243 }
1244
David Benjamin66e954d2012-05-05 21:08:12 -04001245 // xterm does not clear the overflow flag on HT or CHT.
1246 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001247 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001248 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001249};
1250
rgindac9bc5502012-01-18 11:48:44 -08001251/**
1252 * Move the cursor backward to the previous tab stop, or to the first column
1253 * if no previous tab stops are set.
1254 */
1255hterm.Terminal.prototype.backwardTabStop = function() {
1256 var column = this.screen_.cursorPosition.column;
1257
1258 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1259 if (this.tabStops_[i] < column) {
1260 this.setCursorColumn(this.tabStops_[i]);
1261 return;
1262 }
1263 }
1264
1265 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001266};
1267
rgindac9bc5502012-01-18 11:48:44 -08001268/**
1269 * Set a tab stop at the given column.
1270 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001271 * @param {integer} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001272 */
1273hterm.Terminal.prototype.setTabStop = function(column) {
1274 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1275 if (this.tabStops_[i] == column)
1276 return;
1277
1278 if (this.tabStops_[i] < column) {
1279 this.tabStops_.splice(i + 1, 0, column);
1280 return;
1281 }
1282 }
1283
1284 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001285};
1286
rgindac9bc5502012-01-18 11:48:44 -08001287/**
1288 * Clear the tab stop at the current cursor position.
1289 *
1290 * No effect if there is no tab stop at the current cursor position.
1291 */
1292hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1293 var column = this.screen_.cursorPosition.column;
1294
1295 var i = this.tabStops_.indexOf(column);
1296 if (i == -1)
1297 return;
1298
1299 this.tabStops_.splice(i, 1);
1300};
1301
1302/**
1303 * Clear all tab stops.
1304 */
1305hterm.Terminal.prototype.clearAllTabStops = function() {
1306 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001307 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001308};
1309
1310/**
1311 * Set up the default tab stops, starting from a given column.
1312 *
1313 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001314 * from the specified column, or 0 if no column is provided. It also flags
1315 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001316 *
1317 * This does not clear the existing tab stops first, use clearAllTabStops
1318 * for that.
1319 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001320 * @param {integer} opt_start Optional starting zero based starting column, useful
rgindac9bc5502012-01-18 11:48:44 -08001321 * for filling out missing tab stops when the terminal is resized.
1322 */
1323hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1324 var start = opt_start || 0;
1325 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001326 // Round start up to a default tab stop.
1327 start = start - 1 - ((start - 1) % w) + w;
1328 for (var i = start; i < this.screenSize.width; i += w) {
1329 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001330 }
David Benjamin66e954d2012-05-05 21:08:12 -04001331
1332 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001333};
1334
rginda6d397402012-01-17 10:58:29 -08001335/**
rginda8ba33642011-12-14 12:31:31 -08001336 * Interpret a sequence of characters.
1337 *
1338 * Incomplete escape sequences are buffered until the next call.
1339 *
1340 * @param {string} str Sequence of characters to interpret or pass through.
1341 */
1342hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001343 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001344 this.scheduleSyncCursorPosition_();
1345};
1346
1347/**
1348 * Take over the given DIV for use as the terminal display.
1349 *
1350 * @param {HTMLDivElement} div The div to use as the terminal display.
1351 */
1352hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001353 this.div_ = div;
1354
rginda8ba33642011-12-14 12:31:31 -08001355 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001356 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001357 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1358 this.scrollPort_.setBackgroundPosition(
1359 this.prefs_.get('background-position'));
Mike Frysinger08bad432017-04-24 00:50:54 -04001360 this.scrollPort_.setUserCssUrl(this.prefs_.get('user-css'));
1361 this.scrollPort_.setUserCssText(this.prefs_.get('user-css-text'));
rginda30f20f62012-04-05 16:36:19 -07001362
rginda0918b652012-04-04 11:26:24 -07001363 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001364
rginda9f5222b2012-03-05 11:53:28 -08001365 this.setFontSize(this.prefs_.get('font-size'));
1366 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001367
David Reveman8f552492012-03-28 12:18:41 -04001368 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001369 this.setScrollWheelMoveMultipler(
1370 this.prefs_.get('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001371
rginda8ba33642011-12-14 12:31:31 -08001372 this.document_ = this.scrollPort_.getDocument();
1373
Evan Jones5f9df812016-12-06 09:38:58 -05001374 this.document_.body.oncontextmenu = function() { return false; };
rginda4bba5e12012-06-20 16:15:30 -07001375
1376 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001377 var screenNode = this.scrollPort_.getScreenNode();
1378 screenNode.addEventListener('mousedown', onMouse);
1379 screenNode.addEventListener('mouseup', onMouse);
1380 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001381 this.scrollPort_.onScrollWheel = onMouse;
1382
Toni Barzic0bfa8922013-11-22 11:18:35 -08001383 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001384 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001385 // Listen for mousedown events on the screenNode as in FF the focus
1386 // events don't bubble.
1387 screenNode.addEventListener('mousedown', function() {
1388 setTimeout(this.onFocusChange_.bind(this, true));
1389 }.bind(this));
1390
Toni Barzic0bfa8922013-11-22 11:18:35 -08001391 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001392 'blur', this.onFocusChange_.bind(this, false));
1393
1394 var style = this.document_.createElement('style');
1395 style.textContent =
1396 ('.cursor-node[focus="false"] {' +
1397 ' box-sizing: border-box;' +
1398 ' background-color: transparent !important;' +
1399 ' border-width: 2px;' +
1400 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001401 '}' +
1402 '.wc-node {' +
1403 ' display: inline-block;' +
1404 ' text-align: center;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001405 ' width: calc(var(--hterm-charsize-width) * 2);' +
Mike Frysinger4036f6e2017-05-31 14:02:55 -04001406 ' line-height: var(--hterm-charsize-height);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001407 '}' +
1408 ':root {' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001409 ' --hterm-charsize-width: ' + this.scrollPort_.characterSize.width + 'px;' +
1410 ' --hterm-charsize-height: ' + this.scrollPort_.characterSize.height + 'px;' +
Mike Frysinger44c32202017-08-05 01:13:09 -04001411 ' --hterm-cursor-offset-col: 0;' +
1412 ' --hterm-cursor-offset-row: 0;' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001413 ' --hterm-blink-node-duration: 0.7s;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001414 ' --hterm-mouse-cursor-text: text;' +
1415 ' --hterm-mouse-cursor-pointer: default;' +
1416 ' --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001417 '}' +
1418 '@keyframes blink {' +
1419 ' from { opacity: 1.0; }' +
1420 ' to { opacity: 0.0; }' +
1421 '}' +
1422 '.blink-node {' +
1423 ' animation-name: blink;' +
1424 ' animation-duration: var(--hterm-blink-node-duration);' +
1425 ' animation-iteration-count: infinite;' +
1426 ' animation-timing-function: ease-in-out;' +
1427 ' animation-direction: alternate;' +
rginda8e92a692012-05-20 19:37:20 -07001428 '}');
1429 this.document_.head.appendChild(style);
1430
rginda8ba33642011-12-14 12:31:31 -08001431 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001432 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001433 this.cursorNode_.style.cssText =
1434 ('position: absolute;' +
Mike Frysinger44c32202017-08-05 01:13:09 -04001435 'left: calc(var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));' +
1436 'top: calc(var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));' +
rginda87b86462011-12-14 13:48:03 -08001437 'display: block;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001438 'width: var(--hterm-charsize-width);' +
1439 'height: var(--hterm-charsize-height);' +
Rob Spies06533ba2014-04-24 11:20:37 -07001440 '-webkit-transition: opacity, background-color 100ms linear;' +
1441 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001442
rginda8e92a692012-05-20 19:37:20 -07001443 this.setCursorColor(this.prefs_.get('cursor-color'));
Robert Gindafb1be6a2013-12-11 11:56:22 -08001444 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1445 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001446
rginda8ba33642011-12-14 12:31:31 -08001447 this.document_.body.appendChild(this.cursorNode_);
1448
rgindad5613292012-06-19 15:40:37 -07001449 // When 'enableMouseDragScroll' is off we reposition this element directly
1450 // under the mouse cursor after a click. This makes Chrome associate
1451 // subsequent mousemove events with the scroll-blocker. Since the
1452 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1453 // events do not cause the scrollport to scroll.
1454 //
1455 // It's a hack, but it's the cleanest way I could find.
1456 this.scrollBlockerNode_ = this.document_.createElement('div');
1457 this.scrollBlockerNode_.style.cssText =
1458 ('position: absolute;' +
1459 'top: -99px;' +
1460 'display: block;' +
1461 'width: 10px;' +
1462 'height: 10px;');
1463 this.document_.body.appendChild(this.scrollBlockerNode_);
1464
rgindad5613292012-06-19 15:40:37 -07001465 this.scrollPort_.onScrollWheel = onMouse;
1466 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1467 ].forEach(function(event) {
1468 this.scrollBlockerNode_.addEventListener(event, onMouse);
1469 this.cursorNode_.addEventListener(event, onMouse);
1470 this.document_.addEventListener(event, onMouse);
1471 }.bind(this));
1472
1473 this.cursorNode_.addEventListener('mousedown', function() {
1474 setTimeout(this.focus.bind(this));
1475 }.bind(this));
1476
rginda8ba33642011-12-14 12:31:31 -08001477 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001478
rginda87b86462011-12-14 13:48:03 -08001479 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001480 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001481};
1482
rginda0918b652012-04-04 11:26:24 -07001483/**
1484 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001485 *
1486 * @return {HTMLDocument}
rginda0918b652012-04-04 11:26:24 -07001487 */
rginda87b86462011-12-14 13:48:03 -08001488hterm.Terminal.prototype.getDocument = function() {
1489 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001490};
1491
1492/**
rginda0918b652012-04-04 11:26:24 -07001493 * Focus the terminal.
1494 */
1495hterm.Terminal.prototype.focus = function() {
1496 this.scrollPort_.focus();
1497};
1498
1499/**
rginda8ba33642011-12-14 12:31:31 -08001500 * Return the HTML Element for a given row index.
1501 *
1502 * This is a method from the RowProvider interface. The ScrollPort uses
1503 * it to fetch rows on demand as they are scrolled into view.
1504 *
1505 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1506 * pairs to conserve memory.
1507 *
1508 * @param {integer} index The zero-based row index, measured relative to the
1509 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001510 * largest indices.
rginda8ba33642011-12-14 12:31:31 -08001511 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1512 */
1513hterm.Terminal.prototype.getRowNode = function(index) {
1514 if (index < this.scrollbackRows_.length)
1515 return this.scrollbackRows_[index];
1516
1517 var screenIndex = index - this.scrollbackRows_.length;
1518 return this.screen_.rowsArray[screenIndex];
1519};
1520
1521/**
1522 * Return the text content for a given range of rows.
1523 *
1524 * This is a method from the RowProvider interface. The ScrollPort uses
1525 * it to fetch text content on demand when the user attempts to copy their
1526 * selection to the clipboard.
1527 *
1528 * @param {integer} start The zero-based row index to start from, measured
1529 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001530 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001531 * @param {integer} end The zero-based row index to end on, measured
1532 * relative to the start of the scrollback buffer.
1533 * @return {string} A single string containing the text value of the range of
1534 * rows. Lines will be newline delimited, with no trailing newline.
1535 */
1536hterm.Terminal.prototype.getRowsText = function(start, end) {
1537 var ary = [];
1538 for (var i = start; i < end; i++) {
1539 var node = this.getRowNode(i);
1540 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001541 if (i < end - 1 && !node.getAttribute('line-overflow'))
1542 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001543 }
1544
rgindaa09e7332012-08-17 12:49:51 -07001545 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001546};
1547
1548/**
1549 * Return the text content for a given row.
1550 *
1551 * This is a method from the RowProvider interface. The ScrollPort uses
1552 * it to fetch text content on demand when the user attempts to copy their
1553 * selection to the clipboard.
1554 *
1555 * @param {integer} index The zero-based row index to return, measured
1556 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001557 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001558 * @return {string} A string containing the text value of the selected row.
1559 */
1560hterm.Terminal.prototype.getRowText = function(index) {
1561 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001562 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001563};
1564
1565/**
1566 * Return the total number of rows in the addressable screen and in the
1567 * scrollback buffer of this terminal.
1568 *
1569 * This is a method from the RowProvider interface. The ScrollPort uses
1570 * it to compute the size of the scrollbar.
1571 *
1572 * @return {integer} The number of rows in this terminal.
1573 */
1574hterm.Terminal.prototype.getRowCount = function() {
1575 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1576};
1577
1578/**
1579 * Create DOM nodes for new rows and append them to the end of the terminal.
1580 *
1581 * This is the only correct way to add a new DOM node for a row. Notice that
1582 * the new row is appended to the bottom of the list of rows, and does not
1583 * require renumbering (of the rowIndex property) of previous rows.
1584 *
1585 * If you think you want a new blank row somewhere in the middle of the
1586 * terminal, look into moveRows_().
1587 *
1588 * This method does not pay attention to vtScrollTop/Bottom, since you should
1589 * be using moveRows() in cases where they would matter.
1590 *
1591 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001592 *
1593 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001594 */
1595hterm.Terminal.prototype.appendRows_ = function(count) {
1596 var cursorRow = this.screen_.rowsArray.length;
1597 var offset = this.scrollbackRows_.length + cursorRow;
1598 for (var i = 0; i < count; i++) {
1599 var row = this.document_.createElement('x-row');
1600 row.appendChild(this.document_.createTextNode(''));
1601 row.rowIndex = offset + i;
1602 this.screen_.pushRow(row);
1603 }
1604
1605 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1606 if (extraRows > 0) {
1607 var ary = this.screen_.shiftRows(extraRows);
1608 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001609 if (this.scrollPort_.isScrolledEnd)
1610 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001611 }
1612
1613 if (cursorRow >= this.screen_.rowsArray.length)
1614 cursorRow = this.screen_.rowsArray.length - 1;
1615
rginda87b86462011-12-14 13:48:03 -08001616 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001617};
1618
1619/**
1620 * Relocate rows from one part of the addressable screen to another.
1621 *
1622 * This is used to recycle rows during VT scrolls (those which are driven
1623 * by VT commands, rather than by the user manipulating the scrollbar.)
1624 *
1625 * In this case, the blank lines scrolled into the scroll region are made of
1626 * the nodes we scrolled off. These have their rowIndex properties carefully
1627 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001628 *
1629 * @param {number} fromIndex The start index.
1630 * @param {number} count The number of rows to move.
1631 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001632 */
1633hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1634 var ary = this.screen_.removeRows(fromIndex, count);
1635 this.screen_.insertRows(toIndex, ary);
1636
1637 var start, end;
1638 if (fromIndex < toIndex) {
1639 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001640 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001641 } else {
1642 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001643 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001644 }
1645
1646 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001647 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001648};
1649
1650/**
1651 * Renumber the rowIndex property of the given range of rows.
1652 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001653 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001654 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001655 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001656 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001657 *
1658 * @param {number} start The start index.
1659 * @param {number} end The end index.
1660 * @param {hterm.Screen} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001661 */
Robert Ginda40932892012-12-10 17:26:40 -08001662hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1663 var screen = opt_screen || this.screen_;
1664
rginda8ba33642011-12-14 12:31:31 -08001665 var offset = this.scrollbackRows_.length;
1666 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001667 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001668 }
1669};
1670
1671/**
1672 * Print a string to the terminal.
1673 *
1674 * This respects the current insert and wraparound modes. It will add new lines
1675 * to the end of the terminal, scrolling off the top into the scrollback buffer
1676 * if necessary.
1677 *
1678 * The string is *not* parsed for escape codes. Use the interpret() method if
1679 * that's what you're after.
1680 *
1681 * @param{string} str The string to print.
1682 */
1683hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001684 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001685
Ricky Liang48f05cb2013-12-31 23:35:29 +08001686 var strWidth = lib.wc.strWidth(str);
1687
1688 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001689 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1690 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001691 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001692 }
rgindaa19afe22012-01-25 15:40:22 -08001693
Ricky Liang48f05cb2013-12-31 23:35:29 +08001694 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001695 var didOverflow = false;
1696 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001697
rgindaa9abdd82012-08-06 18:05:09 -07001698 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1699 didOverflow = true;
1700 count = this.screenSize.width - this.screen_.cursorPosition.column;
1701 }
rgindaa19afe22012-01-25 15:40:22 -08001702
rgindaa9abdd82012-08-06 18:05:09 -07001703 if (didOverflow && !this.options_.wraparound) {
1704 // If the string overflowed the line but wraparound is off, then the
1705 // last printed character should be the last of the string.
1706 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001707 substr = lib.wc.substr(str, startOffset, count - 1) +
1708 lib.wc.substr(str, strWidth - 1);
1709 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001710 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001711 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001712 }
rgindaa19afe22012-01-25 15:40:22 -08001713
Ricky Liang48f05cb2013-12-31 23:35:29 +08001714 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1715 for (var i = 0; i < tokens.length; i++) {
1716 if (tokens[i].wcNode)
1717 this.screen_.textAttributes.wcNode = true;
1718
1719 if (this.options_.insertMode) {
1720 this.screen_.insertString(tokens[i].str);
1721 } else {
1722 this.screen_.overwriteString(tokens[i].str);
1723 }
1724 this.screen_.textAttributes.wcNode = false;
rgindaa9abdd82012-08-06 18:05:09 -07001725 }
1726
1727 this.screen_.maybeClipCurrentRow();
1728 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001729 }
rginda8ba33642011-12-14 12:31:31 -08001730
1731 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001732
rginda9f5222b2012-03-05 11:53:28 -08001733 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001734 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001735};
1736
1737/**
rginda87b86462011-12-14 13:48:03 -08001738 * Set the VT scroll region.
1739 *
rginda87b86462011-12-14 13:48:03 -08001740 * This also resets the cursor position to the absolute (0, 0) position, since
1741 * that's what xterm appears to do.
1742 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001743 * Setting the scroll region to the full height of the terminal will clear
1744 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1745 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1746 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1747 * continue to work as most users would expect.
1748 *
rginda87b86462011-12-14 13:48:03 -08001749 * @param {integer} scrollTop The zero-based top of the scroll region.
1750 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1751 * inclusive.
1752 */
1753hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001754 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001755 this.vtScrollTop_ = null;
1756 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001757 } else {
1758 this.vtScrollTop_ = scrollTop;
1759 this.vtScrollBottom_ = scrollBottom;
1760 }
rginda87b86462011-12-14 13:48:03 -08001761};
1762
1763/**
rginda8ba33642011-12-14 12:31:31 -08001764 * Return the top row index according to the VT.
1765 *
1766 * This will return 0 unless the terminal has been told to restrict scrolling
1767 * to some lower row. It is used for some VT cursor positioning and scrolling
1768 * commands.
1769 *
1770 * @return {integer} The topmost row in the terminal's scroll region.
1771 */
1772hterm.Terminal.prototype.getVTScrollTop = function() {
1773 if (this.vtScrollTop_ != null)
1774 return this.vtScrollTop_;
1775
1776 return 0;
rginda87b86462011-12-14 13:48:03 -08001777};
rginda8ba33642011-12-14 12:31:31 -08001778
1779/**
1780 * Return the bottom row index according to the VT.
1781 *
1782 * This will return the height of the terminal unless the it has been told to
1783 * restrict scrolling to some higher row. It is used for some VT cursor
1784 * positioning and scrolling commands.
1785 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001786 * @return {integer} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001787 */
1788hterm.Terminal.prototype.getVTScrollBottom = function() {
1789 if (this.vtScrollBottom_ != null)
1790 return this.vtScrollBottom_;
1791
rginda87b86462011-12-14 13:48:03 -08001792 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001793}
1794
1795/**
1796 * Process a '\n' character.
1797 *
1798 * If the cursor is on the final row of the terminal this will append a new
1799 * blank row to the screen and scroll the topmost row into the scrollback
1800 * buffer.
1801 *
1802 * Otherwise, this moves the cursor to column zero of the next row.
1803 */
1804hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001805 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1806 this.screen_.rowsArray.length - 1);
1807
1808 if (this.vtScrollBottom_ != null) {
1809 // A VT Scroll region is active, we never append new rows.
1810 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1811 // We're at the end of the VT Scroll Region, perform a VT scroll.
1812 this.vtScrollUp(1);
1813 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1814 } else if (cursorAtEndOfScreen) {
1815 // We're at the end of the screen, the only thing to do is put the
1816 // cursor to column 0.
1817 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1818 } else {
1819 // Anywhere else, advance the cursor row, and reset the column.
1820 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1821 }
1822 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001823 // We're at the end of the screen. Append a new row to the terminal,
1824 // shifting the top row into the scrollback.
1825 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001826 } else {
rginda87b86462011-12-14 13:48:03 -08001827 // Anywhere else in the screen just moves the cursor.
1828 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001829 }
1830};
1831
1832/**
1833 * Like newLine(), except maintain the cursor column.
1834 */
1835hterm.Terminal.prototype.lineFeed = function() {
1836 var column = this.screen_.cursorPosition.column;
1837 this.newLine();
1838 this.setCursorColumn(column);
1839};
1840
1841/**
rginda87b86462011-12-14 13:48:03 -08001842 * If autoCarriageReturn is set then newLine(), else lineFeed().
1843 */
1844hterm.Terminal.prototype.formFeed = function() {
1845 if (this.options_.autoCarriageReturn) {
1846 this.newLine();
1847 } else {
1848 this.lineFeed();
1849 }
1850};
1851
1852/**
1853 * Move the cursor up one row, possibly inserting a blank line.
1854 *
1855 * The cursor column is not changed.
1856 */
1857hterm.Terminal.prototype.reverseLineFeed = function() {
1858 var scrollTop = this.getVTScrollTop();
1859 var currentRow = this.screen_.cursorPosition.row;
1860
1861 if (currentRow == scrollTop) {
1862 this.insertLines(1);
1863 } else {
1864 this.setAbsoluteCursorRow(currentRow - 1);
1865 }
1866};
1867
1868/**
rginda8ba33642011-12-14 12:31:31 -08001869 * Replace all characters to the left of the current cursor with the space
1870 * character.
1871 *
1872 * TODO(rginda): This should probably *remove* the characters (not just replace
1873 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001874 * position.
rginda8ba33642011-12-14 12:31:31 -08001875 */
1876hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001877 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001878 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001879 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001880 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001881};
1882
1883/**
David Benjamin684a9b72012-05-01 17:19:58 -04001884 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001885 *
1886 * The cursor position is unchanged.
1887 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001888 * If the current background color is not the default background color this
1889 * will insert spaces rather than delete. This is unfortunate because the
1890 * trailing space will affect text selection, but it's difficult to come up
1891 * with a way to style empty space that wouldn't trip up the hterm.Screen
1892 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001893 *
1894 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1895 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1896 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05001897 *
1898 * @param {number} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08001899 */
1900hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001901 if (this.screen_.cursorPosition.overflow)
1902 return;
1903
Robert Ginda7fd57082012-09-25 14:41:47 -07001904 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1905 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001906
1907 if (this.screen_.textAttributes.background ===
1908 this.screen_.textAttributes.DEFAULT_COLOR) {
1909 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08001910 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07001911 this.screen_.cursorPosition.column + count) {
1912 this.screen_.deleteChars(count);
1913 this.clearCursorOverflow();
1914 return;
1915 }
1916 }
1917
rginda87b86462011-12-14 13:48:03 -08001918 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001919 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001920 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001921 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001922};
1923
1924/**
1925 * Erase the current line.
1926 *
1927 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001928 */
1929hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001930 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001931 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001932 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001933 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001934};
1935
1936/**
David Benjamina08d78f2012-05-05 00:28:49 -04001937 * Erase all characters from the start of the screen to the current cursor
1938 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001939 *
1940 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001941 */
1942hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001943 var cursor = this.saveCursor();
1944
1945 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001946
David Benjamina08d78f2012-05-05 00:28:49 -04001947 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001948 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001949 this.screen_.clearCursorRow();
1950 }
1951
rginda87b86462011-12-14 13:48:03 -08001952 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001953 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001954};
1955
1956/**
1957 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001958 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001959 *
1960 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001961 */
1962hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001963 var cursor = this.saveCursor();
1964
1965 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001966
David Benjamina08d78f2012-05-05 00:28:49 -04001967 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001968 for (var i = cursor.row + 1; i <= bottom; i++) {
1969 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001970 this.screen_.clearCursorRow();
1971 }
1972
rginda87b86462011-12-14 13:48:03 -08001973 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001974 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001975};
1976
1977/**
1978 * Fill the terminal with a given character.
1979 *
1980 * This methods does not respect the VT scroll region.
1981 *
1982 * @param {string} ch The character to use for the fill.
1983 */
1984hterm.Terminal.prototype.fill = function(ch) {
1985 var cursor = this.saveCursor();
1986
1987 this.setAbsoluteCursorPosition(0, 0);
1988 for (var row = 0; row < this.screenSize.height; row++) {
1989 for (var col = 0; col < this.screenSize.width; col++) {
1990 this.setAbsoluteCursorPosition(row, col);
1991 this.screen_.overwriteString(ch);
1992 }
1993 }
1994
1995 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001996};
1997
1998/**
rginda9ea433c2012-03-16 11:57:00 -07001999 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002000 *
rginda9ea433c2012-03-16 11:57:00 -07002001 * This does not respect the scroll region.
2002 *
2003 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2004 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002005 */
rginda9ea433c2012-03-16 11:57:00 -07002006hterm.Terminal.prototype.clearHome = function(opt_screen) {
2007 var screen = opt_screen || this.screen_;
2008 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002009
rginda11057d52012-04-25 12:29:56 -07002010 if (bottom == 0) {
2011 // Empty screen, nothing to do.
2012 return;
2013 }
2014
rgindae4d29232012-01-19 10:47:13 -08002015 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002016 screen.setCursorPosition(i, 0);
2017 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002018 }
2019
rginda9ea433c2012-03-16 11:57:00 -07002020 screen.setCursorPosition(0, 0);
2021};
2022
2023/**
2024 * Erase the entire display without changing the cursor position.
2025 *
2026 * The cursor position is unchanged. This does not respect the scroll
2027 * region.
2028 *
2029 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2030 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002031 */
2032hterm.Terminal.prototype.clear = function(opt_screen) {
2033 var screen = opt_screen || this.screen_;
2034 var cursor = screen.cursorPosition.clone();
2035 this.clearHome(screen);
2036 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002037};
2038
2039/**
2040 * VT command to insert lines at the current cursor row.
2041 *
2042 * This respects the current scroll region. Rows pushed off the bottom are
2043 * lost (they won't show up in the scrollback buffer).
2044 *
rginda8ba33642011-12-14 12:31:31 -08002045 * @param {integer} count The number of lines to insert.
2046 */
2047hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002048 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002049
2050 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002051 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002052
Robert Ginda579186b2012-09-26 11:40:04 -07002053 // The moveCount is the number of rows we need to relocate to make room for
2054 // the new row(s). The count is the distance to move them.
2055 var moveCount = bottom - cursorRow - count + 1;
2056 if (moveCount)
2057 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002058
Robert Ginda579186b2012-09-26 11:40:04 -07002059 for (var i = count - 1; i >= 0; i--) {
2060 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002061 this.screen_.clearCursorRow();
2062 }
rginda8ba33642011-12-14 12:31:31 -08002063};
2064
2065/**
2066 * VT command to delete lines at the current cursor row.
2067 *
2068 * New rows are added to the bottom of scroll region to take their place. New
2069 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002070 *
2071 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002072 */
2073hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002074 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002075
rginda87b86462011-12-14 13:48:03 -08002076 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002077 var bottom = this.getVTScrollBottom();
2078
rginda87b86462011-12-14 13:48:03 -08002079 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002080 count = Math.min(count, maxCount);
2081
rginda87b86462011-12-14 13:48:03 -08002082 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002083 if (count != maxCount)
2084 this.moveRows_(top, count, moveStart);
2085
2086 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002087 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002088 this.screen_.clearCursorRow();
2089 }
2090
rginda87b86462011-12-14 13:48:03 -08002091 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002092 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002093};
2094
2095/**
2096 * Inserts the given number of spaces at the current cursor position.
2097 *
rginda87b86462011-12-14 13:48:03 -08002098 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002099 *
2100 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002101 */
2102hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002103 var cursor = this.saveCursor();
2104
rgindacbbd7482012-06-13 15:06:16 -07002105 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08002106 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08002107 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002108
2109 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002110 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002111};
2112
2113/**
2114 * Forward-delete the specified number of characters starting at the cursor
2115 * position.
2116 *
2117 * @param {integer} count The number of characters to delete.
2118 */
2119hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002120 var deleted = this.screen_.deleteChars(count);
2121 if (deleted && !this.screen_.textAttributes.isDefault()) {
2122 var cursor = this.saveCursor();
2123 this.setCursorColumn(this.screenSize.width - deleted);
2124 this.screen_.insertString(lib.f.getWhitespace(deleted));
2125 this.restoreCursor(cursor);
2126 }
2127
David Benjamin54e8bf62012-06-01 22:31:40 -04002128 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002129};
2130
2131/**
2132 * Shift rows in the scroll region upwards by a given number of lines.
2133 *
2134 * New rows are inserted at the bottom of the scroll region to fill the
2135 * vacated rows. The new rows not filled out with the current text attributes.
2136 *
2137 * This function does not affect the scrollback rows at all. Rows shifted
2138 * off the top are lost.
2139 *
rginda87b86462011-12-14 13:48:03 -08002140 * The cursor position is not altered.
2141 *
rginda8ba33642011-12-14 12:31:31 -08002142 * @param {integer} count The number of rows to scroll.
2143 */
2144hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002145 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002146
rginda87b86462011-12-14 13:48:03 -08002147 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002148 this.deleteLines(count);
2149
rginda87b86462011-12-14 13:48:03 -08002150 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002151};
2152
2153/**
2154 * Shift rows below the cursor down by a given number of lines.
2155 *
2156 * This function respects the current scroll region.
2157 *
2158 * New rows are inserted at the top of the scroll region to fill the
2159 * vacated rows. The new rows not filled out with the current text attributes.
2160 *
2161 * This function does not affect the scrollback rows at all. Rows shifted
2162 * off the bottom are lost.
2163 *
2164 * @param {integer} count The number of rows to scroll.
2165 */
2166hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08002167 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002168
rginda87b86462011-12-14 13:48:03 -08002169 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08002170 this.insertLines(opt_count);
2171
rginda87b86462011-12-14 13:48:03 -08002172 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002173};
2174
rginda87b86462011-12-14 13:48:03 -08002175
rginda8ba33642011-12-14 12:31:31 -08002176/**
2177 * Set the cursor position.
2178 *
2179 * The cursor row is relative to the scroll region if the terminal has
2180 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2181 *
2182 * @param {integer} row The new zero-based cursor row.
2183 * @param {integer} row The new zero-based cursor column.
2184 */
2185hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2186 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002187 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002188 } else {
rginda87b86462011-12-14 13:48:03 -08002189 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002190 }
rginda87b86462011-12-14 13:48:03 -08002191};
rginda8ba33642011-12-14 12:31:31 -08002192
Evan Jones2600d4f2016-12-06 09:29:36 -05002193/**
2194 * Move the cursor relative to its current position.
2195 *
2196 * @param {number} row
2197 * @param {number} column
2198 */
rginda87b86462011-12-14 13:48:03 -08002199hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2200 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002201 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2202 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002203 this.screen_.setCursorPosition(row, column);
2204};
2205
Evan Jones2600d4f2016-12-06 09:29:36 -05002206/**
2207 * Move the cursor to the specified position.
2208 *
2209 * @param {number} row
2210 * @param {number} column
2211 */
rginda87b86462011-12-14 13:48:03 -08002212hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002213 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2214 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002215 this.screen_.setCursorPosition(row, column);
2216};
2217
2218/**
2219 * Set the cursor column.
2220 *
2221 * @param {integer} column The new zero-based cursor column.
2222 */
2223hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002224 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002225};
2226
2227/**
2228 * Return the cursor column.
2229 *
2230 * @return {integer} The zero-based cursor column.
2231 */
2232hterm.Terminal.prototype.getCursorColumn = function() {
2233 return this.screen_.cursorPosition.column;
2234};
2235
2236/**
2237 * Set the cursor row.
2238 *
2239 * The cursor row is relative to the scroll region if the terminal has
2240 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2241 *
2242 * @param {integer} row The new cursor row.
2243 */
rginda87b86462011-12-14 13:48:03 -08002244hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2245 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002246};
2247
2248/**
2249 * Return the cursor row.
2250 *
2251 * @return {integer} The zero-based cursor row.
2252 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002253hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002254 return this.screen_.cursorPosition.row;
2255};
2256
2257/**
2258 * Request that the ScrollPort redraw itself soon.
2259 *
2260 * The redraw will happen asynchronously, soon after the call stack winds down.
2261 * Multiple calls will be coalesced into a single redraw.
2262 */
2263hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002264 if (this.timeouts_.redraw)
2265 return;
rginda8ba33642011-12-14 12:31:31 -08002266
2267 var self = this;
rginda87b86462011-12-14 13:48:03 -08002268 this.timeouts_.redraw = setTimeout(function() {
2269 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002270 self.scrollPort_.redraw_();
2271 }, 0);
2272};
2273
2274/**
2275 * Request that the ScrollPort be scrolled to the bottom.
2276 *
2277 * The scroll will happen asynchronously, soon after the call stack winds down.
2278 * Multiple calls will be coalesced into a single scroll.
2279 *
2280 * This affects the scrollbar position of the ScrollPort, and has nothing to
2281 * do with the VT scroll commands.
2282 */
2283hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2284 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002285 return;
rginda8ba33642011-12-14 12:31:31 -08002286
2287 var self = this;
2288 this.timeouts_.scrollDown = setTimeout(function() {
2289 delete self.timeouts_.scrollDown;
2290 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2291 }, 10);
2292};
2293
2294/**
2295 * Move the cursor up a specified number of rows.
2296 *
2297 * @param {integer} count The number of rows to move the cursor.
2298 */
2299hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002300 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002301};
2302
2303/**
2304 * Move the cursor down a specified number of rows.
2305 *
2306 * @param {integer} count The number of rows to move the cursor.
2307 */
2308hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002309 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002310 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2311 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2312 this.screenSize.height - 1);
2313
rgindacbbd7482012-06-13 15:06:16 -07002314 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002315 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002316 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002317};
2318
2319/**
2320 * Move the cursor left a specified number of columns.
2321 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002322 * If reverse wraparound mode is enabled and the previous row wrapped into
2323 * the current row then we back up through the wraparound as well.
2324 *
rginda8ba33642011-12-14 12:31:31 -08002325 * @param {integer} count The number of columns to move the cursor.
2326 */
2327hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002328 count = count || 1;
2329
2330 if (count < 1)
2331 return;
2332
2333 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002334 if (this.options_.reverseWraparound) {
2335 if (this.screen_.cursorPosition.overflow) {
2336 // If this cursor is in the right margin, consume one count to get it
2337 // back to the last column. This only applies when we're in reverse
2338 // wraparound mode.
2339 count--;
2340 this.clearCursorOverflow();
2341
2342 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002343 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002344 }
2345
Robert Gindabfb32622014-07-17 13:20:27 -07002346 var newRow = this.screen_.cursorPosition.row;
2347 var newColumn = currentColumn - count;
2348 if (newColumn < 0) {
2349 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2350 if (newRow < 0) {
2351 // xterm also wraps from row 0 to the last row.
2352 newRow = this.screenSize.height + newRow % this.screenSize.height;
2353 }
2354 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2355 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002356
Robert Gindabfb32622014-07-17 13:20:27 -07002357 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2358
2359 } else {
2360 var newColumn = Math.max(currentColumn - count, 0);
2361 this.setCursorColumn(newColumn);
2362 }
rginda8ba33642011-12-14 12:31:31 -08002363};
2364
2365/**
2366 * Move the cursor right a specified number of columns.
2367 *
2368 * @param {integer} count The number of columns to move the cursor.
2369 */
2370hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002371 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002372
2373 if (count < 1)
2374 return;
2375
rgindacbbd7482012-06-13 15:06:16 -07002376 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002377 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002378 this.setCursorColumn(column);
2379};
2380
2381/**
2382 * Reverse the foreground and background colors of the terminal.
2383 *
2384 * This only affects text that was drawn with no attributes.
2385 *
2386 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2387 * been drawn with attributes that happen to coincide with the default
2388 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002389 *
2390 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002391 */
2392hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002393 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002394 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002395 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2396 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002397 } else {
rginda9f5222b2012-03-05 11:53:28 -08002398 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2399 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002400 }
2401};
2402
2403/**
rginda87b86462011-12-14 13:48:03 -08002404 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002405 *
2406 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002407 */
2408hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002409 this.cursorNode_.style.backgroundColor =
2410 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002411
2412 var self = this;
2413 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08002414 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08002415 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002416
Michael Kelly485ecd12014-06-09 11:41:56 -04002417 // bellSquelchTimeout_ affects both audio and notification bells.
2418 if (this.bellSquelchTimeout_)
2419 return;
2420
Robert Ginda92e18102013-03-14 13:56:37 -07002421 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002422 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002423 this.bellSequelchTimeout_ = setTimeout(function() {
2424 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002425 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002426 } else {
2427 delete this.bellSquelchTimeout_;
2428 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002429
2430 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002431 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002432 this.bellNotificationList_.push(n);
2433 // TODO: Should we try to raise the window here?
2434 n.onclick = function() { self.closeBellNotifications_(); };
2435 }
rginda87b86462011-12-14 13:48:03 -08002436};
2437
2438/**
rginda8ba33642011-12-14 12:31:31 -08002439 * Set the origin mode bit.
2440 *
2441 * If origin mode is on, certain VT cursor and scrolling commands measure their
2442 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2443 * to the top of the addressable screen.
2444 *
2445 * Defaults to off.
2446 *
2447 * @param {boolean} state True to set origin mode, false to unset.
2448 */
2449hterm.Terminal.prototype.setOriginMode = function(state) {
2450 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002451 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002452};
2453
2454/**
2455 * Set the insert mode bit.
2456 *
2457 * If insert mode is on, existing text beyond the cursor position will be
2458 * shifted right to make room for new text. Otherwise, new text overwrites
2459 * any existing text.
2460 *
2461 * Defaults to off.
2462 *
2463 * @param {boolean} state True to set insert mode, false to unset.
2464 */
2465hterm.Terminal.prototype.setInsertMode = function(state) {
2466 this.options_.insertMode = state;
2467};
2468
2469/**
rginda87b86462011-12-14 13:48:03 -08002470 * Set the auto carriage return bit.
2471 *
2472 * If auto carriage return is on then a formfeed character is interpreted
2473 * as a newline, otherwise it's the same as a linefeed. The difference boils
2474 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002475 *
2476 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002477 */
2478hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2479 this.options_.autoCarriageReturn = state;
2480};
2481
2482/**
rginda8ba33642011-12-14 12:31:31 -08002483 * Set the wraparound mode bit.
2484 *
2485 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2486 * to the start of the following row. Otherwise, the cursor is clamped to the
2487 * end of the screen and attempts to write past it are ignored.
2488 *
2489 * Defaults to on.
2490 *
2491 * @param {boolean} state True to set wraparound mode, false to unset.
2492 */
2493hterm.Terminal.prototype.setWraparound = function(state) {
2494 this.options_.wraparound = state;
2495};
2496
2497/**
2498 * Set the reverse-wraparound mode bit.
2499 *
2500 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2501 * to the end of the previous row. Otherwise, the cursor is clamped to column
2502 * 0.
2503 *
2504 * Defaults to off.
2505 *
2506 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2507 */
2508hterm.Terminal.prototype.setReverseWraparound = function(state) {
2509 this.options_.reverseWraparound = state;
2510};
2511
2512/**
2513 * Selects between the primary and alternate screens.
2514 *
2515 * If alternate mode is on, the alternate screen is active. Otherwise the
2516 * primary screen is active.
2517 *
2518 * Swapping screens has no effect on the scrollback buffer.
2519 *
2520 * Each screen maintains its own cursor position.
2521 *
2522 * Defaults to off.
2523 *
2524 * @param {boolean} state True to set alternate mode, false to unset.
2525 */
2526hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002527 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002528 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2529
rginda35c456b2012-02-09 17:29:05 -08002530 if (this.screen_.rowsArray.length &&
2531 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2532 // If the screen changed sizes while we were away, our rowIndexes may
2533 // be incorrect.
2534 var offset = this.scrollbackRows_.length;
2535 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002536 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002537 ary[i].rowIndex = offset + i;
2538 }
2539 }
rginda8ba33642011-12-14 12:31:31 -08002540
rginda35c456b2012-02-09 17:29:05 -08002541 this.realizeWidth_(this.screenSize.width);
2542 this.realizeHeight_(this.screenSize.height);
2543 this.scrollPort_.syncScrollHeight();
2544 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002545
rginda6d397402012-01-17 10:58:29 -08002546 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002547 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002548};
2549
2550/**
2551 * Set the cursor-blink mode bit.
2552 *
2553 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2554 * a visible cursor does not blink.
2555 *
2556 * You should make sure to turn blinking off if you're going to dispose of a
2557 * terminal, otherwise you'll leak a timeout.
2558 *
2559 * Defaults to on.
2560 *
2561 * @param {boolean} state True to set cursor-blink mode, false to unset.
2562 */
2563hterm.Terminal.prototype.setCursorBlink = function(state) {
2564 this.options_.cursorBlink = state;
2565
2566 if (!state && this.timeouts_.cursorBlink) {
2567 clearTimeout(this.timeouts_.cursorBlink);
2568 delete this.timeouts_.cursorBlink;
2569 }
2570
2571 if (this.options_.cursorVisible)
2572 this.setCursorVisible(true);
2573};
2574
2575/**
2576 * Set the cursor-visible mode bit.
2577 *
2578 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2579 *
2580 * Defaults to on.
2581 *
2582 * @param {boolean} state True to set cursor-visible mode, false to unset.
2583 */
2584hterm.Terminal.prototype.setCursorVisible = function(state) {
2585 this.options_.cursorVisible = state;
2586
2587 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002588 if (this.timeouts_.cursorBlink) {
2589 clearTimeout(this.timeouts_.cursorBlink);
2590 delete this.timeouts_.cursorBlink;
2591 }
rginda87b86462011-12-14 13:48:03 -08002592 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002593 return;
2594 }
2595
rginda87b86462011-12-14 13:48:03 -08002596 this.syncCursorPosition_();
2597
2598 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002599
2600 if (this.options_.cursorBlink) {
2601 if (this.timeouts_.cursorBlink)
2602 return;
2603
Robert Gindaea2183e2014-07-17 09:51:51 -07002604 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002605 } else {
2606 if (this.timeouts_.cursorBlink) {
2607 clearTimeout(this.timeouts_.cursorBlink);
2608 delete this.timeouts_.cursorBlink;
2609 }
2610 }
2611};
2612
2613/**
rginda87b86462011-12-14 13:48:03 -08002614 * Synchronizes the visible cursor and document selection with the current
2615 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002616 */
2617hterm.Terminal.prototype.syncCursorPosition_ = function() {
2618 var topRowIndex = this.scrollPort_.getTopRowIndex();
2619 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2620 var cursorRowIndex = this.scrollbackRows_.length +
2621 this.screen_.cursorPosition.row;
2622
2623 if (cursorRowIndex > bottomRowIndex) {
2624 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04002625 this.setCssVar('cursor-offset-row', '-1');
rginda8ba33642011-12-14 12:31:31 -08002626 return;
2627 }
2628
Robert Gindab837c052014-08-11 11:17:51 -07002629 if (this.options_.cursorVisible &&
2630 this.cursorNode_.style.display == 'none') {
2631 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2632 this.cursorNode_.style.display = '';
2633 }
2634
Mike Frysinger44c32202017-08-05 01:13:09 -04002635 // Position the cursor using CSS variable math. If we do the math in JS,
2636 // the float math will end up being more precise than the CSS which will
2637 // cause the cursor tracking to be off.
2638 this.setCssVar(
2639 'cursor-offset-row',
2640 `${cursorRowIndex - topRowIndex} + ` +
2641 `${this.scrollPort_.visibleRowTopMargin}px`);
2642 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08002643
2644 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04002645 '(' + this.screen_.cursorPosition.column +
2646 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08002647 ')');
2648
2649 // Update the caret for a11y purposes.
2650 var selection = this.document_.getSelection();
2651 if (selection && selection.isCollapsed)
2652 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002653};
2654
Robert Gindafb1be6a2013-12-11 11:56:22 -08002655/**
2656 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2657 * and character cell dimensions.
2658 */
Robert Ginda830583c2013-08-07 13:20:46 -07002659hterm.Terminal.prototype.restyleCursor_ = function() {
2660 var shape = this.cursorShape_;
2661
2662 if (this.cursorNode_.getAttribute('focus') == 'false') {
2663 // Always show a block cursor when unfocused.
2664 shape = hterm.Terminal.cursorShape.BLOCK;
2665 }
2666
2667 var style = this.cursorNode_.style;
2668
2669 switch (shape) {
2670 case hterm.Terminal.cursorShape.BEAM:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002671 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002672 style.backgroundColor = 'transparent';
2673 style.borderBottomStyle = null;
2674 style.borderLeftStyle = 'solid';
2675 break;
2676
2677 case hterm.Terminal.cursorShape.UNDERLINE:
2678 style.height = this.scrollPort_.characterSize.baseline + 'px';
2679 style.backgroundColor = 'transparent';
2680 style.borderBottomStyle = 'solid';
2681 // correct the size to put it exactly at the baseline
2682 style.borderLeftStyle = null;
2683 break;
2684
2685 default:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002686 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002687 style.backgroundColor = this.cursorColor_;
2688 style.borderBottomStyle = null;
2689 style.borderLeftStyle = null;
2690 break;
2691 }
2692};
2693
rginda8ba33642011-12-14 12:31:31 -08002694/**
2695 * Synchronizes the visible cursor with the current cursor coordinates.
2696 *
2697 * The sync will happen asynchronously, soon after the call stack winds down.
2698 * Multiple calls will be coalesced into a single sync.
2699 */
2700hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2701 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002702 return;
rginda8ba33642011-12-14 12:31:31 -08002703
2704 var self = this;
2705 this.timeouts_.syncCursor = setTimeout(function() {
2706 self.syncCursorPosition_();
2707 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002708 }, 0);
2709};
2710
rgindacc2996c2012-02-24 14:59:31 -08002711/**
rgindaf522ce02012-04-17 17:49:17 -07002712 * Show or hide the zoom warning.
2713 *
2714 * The zoom warning is a message warning the user that their browser zoom must
2715 * be set to 100% in order for hterm to function properly.
2716 *
2717 * @param {boolean} state True to show the message, false to hide it.
2718 */
2719hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2720 if (!this.zoomWarningNode_) {
2721 if (!state)
2722 return;
2723
2724 this.zoomWarningNode_ = this.document_.createElement('div');
2725 this.zoomWarningNode_.style.cssText = (
2726 'color: black;' +
2727 'background-color: #ff2222;' +
2728 'font-size: large;' +
2729 'border-radius: 8px;' +
2730 'opacity: 0.75;' +
2731 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2732 'top: 0.5em;' +
2733 'right: 1.2em;' +
2734 'position: absolute;' +
2735 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002736 '-webkit-user-select: none;' +
2737 '-moz-text-size-adjust: none;' +
2738 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002739
2740 this.zoomWarningNode_.addEventListener('click', function(e) {
2741 this.parentNode.removeChild(this);
2742 });
rgindaf522ce02012-04-17 17:49:17 -07002743 }
2744
Robert Gindab4839c22013-02-28 16:52:10 -08002745 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2746 hterm.zoomWarningMessage,
2747 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2748
rgindaf522ce02012-04-17 17:49:17 -07002749 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2750
2751 if (state) {
2752 if (!this.zoomWarningNode_.parentNode)
2753 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2754 } else if (this.zoomWarningNode_.parentNode) {
2755 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2756 }
2757};
2758
2759/**
rgindacc2996c2012-02-24 14:59:31 -08002760 * Show the terminal overlay for a given amount of time.
2761 *
2762 * The terminal overlay appears in inverse video in a large font, centered
2763 * over the terminal. You should probably keep the overlay message brief,
2764 * since it's in a large font and you probably aren't going to check the size
2765 * of the terminal first.
2766 *
2767 * @param {string} msg The text (not HTML) message to display in the overlay.
2768 * @param {number} opt_timeout The amount of time to wait before fading out
2769 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2770 * stay up forever (or until the next overlay).
2771 */
2772hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002773 if (!this.overlayNode_) {
2774 if (!this.div_)
2775 return;
2776
2777 this.overlayNode_ = this.document_.createElement('div');
2778 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002779 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002780 'font-size: xx-large;' +
2781 'opacity: 0.75;' +
2782 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2783 'position: absolute;' +
2784 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002785 '-webkit-transition: opacity 180ms ease-in;' +
2786 '-moz-user-select: none;' +
2787 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002788
2789 this.overlayNode_.addEventListener('mousedown', function(e) {
2790 e.preventDefault();
2791 e.stopPropagation();
2792 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002793 }
2794
rginda9f5222b2012-03-05 11:53:28 -08002795 this.overlayNode_.style.color = this.prefs_.get('background-color');
2796 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2797 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2798
rgindaf0090c92012-02-10 14:58:52 -08002799 this.overlayNode_.textContent = msg;
2800 this.overlayNode_.style.opacity = '0.75';
2801
2802 if (!this.overlayNode_.parentNode)
2803 this.div_.appendChild(this.overlayNode_);
2804
Robert Ginda97769282013-02-01 15:30:30 -08002805 var divSize = hterm.getClientSize(this.div_);
2806 var overlaySize = hterm.getClientSize(this.overlayNode_);
2807
Robert Ginda8a59f762014-07-23 11:29:55 -07002808 this.overlayNode_.style.top =
2809 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08002810 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07002811 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08002812
2813 var self = this;
2814
2815 if (this.overlayTimeout_)
2816 clearTimeout(this.overlayTimeout_);
2817
rgindacc2996c2012-02-24 14:59:31 -08002818 if (opt_timeout === null)
2819 return;
2820
rgindaf0090c92012-02-10 14:58:52 -08002821 this.overlayTimeout_ = setTimeout(function() {
2822 self.overlayNode_.style.opacity = '0';
Robert Ginda70926e42013-11-25 14:56:36 -08002823 self.overlayTimeout_ = setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002824 if (self.overlayNode_.parentNode)
2825 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002826 self.overlayTimeout_ = null;
2827 self.overlayNode_.style.opacity = '0.75';
2828 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002829 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002830};
2831
rginda4bba5e12012-06-20 16:15:30 -07002832/**
2833 * Paste from the system clipboard to the terminal.
2834 */
2835hterm.Terminal.prototype.paste = function() {
Mike Frysinger4628ad22017-07-20 02:44:20 -04002836 return hterm.pasteFromClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002837};
2838
2839/**
2840 * Copy a string to the system clipboard.
2841 *
2842 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05002843 *
2844 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07002845 */
2846hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002847 if (this.prefs_.get('enable-clipboard-notice'))
2848 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2849
rgindaa09e7332012-08-17 12:49:51 -07002850 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002851 copySource.textContent = str;
2852 copySource.style.cssText = (
2853 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002854 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07002855 'position: absolute;' +
2856 'top: -99px');
2857
2858 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002859
rginda4bba5e12012-06-20 16:15:30 -07002860 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002861 var anchorNode = selection.anchorNode;
2862 var anchorOffset = selection.anchorOffset;
2863 var focusNode = selection.focusNode;
2864 var focusOffset = selection.focusOffset;
2865
rginda4bba5e12012-06-20 16:15:30 -07002866 selection.selectAllChildren(copySource);
2867
rgindaa09e7332012-08-17 12:49:51 -07002868 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002869
Rob Spies56953412014-04-28 14:09:47 -07002870 // IE doesn't support selection.extend. This means that the selection
2871 // won't return on IE.
Robert Ginda6f4f0aa2014-07-28 10:21:39 -07002872 if (selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07002873 selection.collapse(anchorNode, anchorOffset);
2874 selection.extend(focusNode, focusOffset);
2875 }
rgindafaa74742012-08-21 13:34:03 -07002876
rginda4bba5e12012-06-20 16:15:30 -07002877 copySource.parentNode.removeChild(copySource);
2878};
2879
Evan Jones2600d4f2016-12-06 09:29:36 -05002880/**
2881 * Returns the selected text, or null if no text is selected.
2882 *
2883 * @return {string|null}
2884 */
rgindaa09e7332012-08-17 12:49:51 -07002885hterm.Terminal.prototype.getSelectionText = function() {
2886 var selection = this.scrollPort_.selection;
2887 selection.sync();
2888
2889 if (selection.isCollapsed)
2890 return null;
2891
2892
2893 // Start offset measures from the beginning of the line.
2894 var startOffset = selection.startOffset;
2895 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002896
Robert Gindafdbb3f22012-09-06 20:23:06 -07002897 if (node.nodeName != 'X-ROW') {
2898 // If the selection doesn't start on an x-row node, then it must be
2899 // somewhere inside the x-row. Add any characters from previous siblings
2900 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002901
2902 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2903 // If node is the text node in a styled span, move up to the span node.
2904 node = node.parentNode;
2905 }
2906
Robert Gindafdbb3f22012-09-06 20:23:06 -07002907 while (node.previousSibling) {
2908 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002909 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002910 }
rgindaa09e7332012-08-17 12:49:51 -07002911 }
2912
2913 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002914 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
2915 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05002916 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002917
Robert Gindafdbb3f22012-09-06 20:23:06 -07002918 if (node.nodeName != 'X-ROW') {
2919 // If the selection doesn't end on an x-row node, then it must be
2920 // somewhere inside the x-row. Add any characters from following siblings
2921 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002922
2923 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2924 // If node is the text node in a styled span, move up to the span node.
2925 node = node.parentNode;
2926 }
2927
Robert Gindafdbb3f22012-09-06 20:23:06 -07002928 while (node.nextSibling) {
2929 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002930 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002931 }
rgindaa09e7332012-08-17 12:49:51 -07002932 }
2933
2934 var rv = this.getRowsText(selection.startRow.rowIndex,
2935 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002936 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002937};
2938
rginda4bba5e12012-06-20 16:15:30 -07002939/**
2940 * Copy the current selection to the system clipboard, then clear it after a
2941 * short delay.
2942 */
2943hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002944 var text = this.getSelectionText();
2945 if (text != null)
2946 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002947};
2948
rgindaf0090c92012-02-10 14:58:52 -08002949hterm.Terminal.prototype.overlaySize = function() {
2950 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2951};
2952
rginda87b86462011-12-14 13:48:03 -08002953/**
2954 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2955 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07002956 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08002957 */
2958hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002959 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002960 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2961
Robert Ginda8cb7d902013-06-20 14:37:18 -07002962 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08002963};
2964
2965/**
Mike Frysinger70b94692017-01-26 18:57:50 -10002966 * Launches url in a new tab.
2967 *
2968 * @param {string} url URL to launch in a new tab.
2969 */
2970hterm.Terminal.prototype.openUrl = function(url) {
Mike Frysingerac437a12017-07-13 02:35:59 -04002971 if (window.chrome && window.chrome.browser) {
2972 // For Chrome v2 apps, we need to use this API to properly open windows.
2973 chrome.browser.openTab({'url': url});
2974 } else {
2975 var win = window.open(url, '_blank');
2976 win.focus();
2977 }
Mike Frysinger70b94692017-01-26 18:57:50 -10002978}
2979
2980/**
2981 * Open the selected url.
2982 */
2983hterm.Terminal.prototype.openSelectedUrl_ = function() {
2984 var str = this.getSelectionText();
2985
2986 // If there is no selection, try and expand wherever they clicked.
2987 if (str == null) {
2988 this.screen_.expandSelection(this.document_.getSelection());
2989 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04002990
2991 // If clicking in empty space, return.
2992 if (str == null)
2993 return;
Mike Frysinger70b94692017-01-26 18:57:50 -10002994 }
2995
2996 // Make sure URL is valid before opening.
2997 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
2998 return;
Mike Frysinger43472622017-06-26 18:11:07 -04002999
3000 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003001 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003002 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3003 // We have to whitelist a few protocols that lack authorities and thus
3004 // never use the //. Like mailto.
3005 switch (str.split(':', 1)[0]) {
3006 case 'mailto':
3007 break;
3008 default:
3009 str = 'http://' + str;
3010 break;
3011 }
3012 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003013
3014 this.openUrl(str);
3015}
3016
3017
3018/**
rgindad5613292012-06-19 15:40:37 -07003019 * Add the terminalRow and terminalColumn properties to mouse events and
3020 * then forward on to onMouse().
3021 *
3022 * The terminalRow and terminalColumn properties contain the (row, column)
3023 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003024 *
3025 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003026 */
3027hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003028 if (e.processedByTerminalHandler_) {
3029 // We register our event handlers on the document, as well as the cursor
3030 // and the scroll blocker. Mouse events that occur on the cursor or
3031 // scroll blocker will also appear on the document, but we don't want to
3032 // process them twice.
3033 //
3034 // We can't just prevent bubbling because that has other side effects, so
3035 // we decorate the event object with this property instead.
3036 return;
3037 }
3038
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003039 var reportMouseEvents = (!this.defeatMouseReports_ &&
3040 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3041
rgindafaa74742012-08-21 13:34:03 -07003042 e.processedByTerminalHandler_ = true;
3043
Robert Gindaeda48db2014-07-17 09:25:30 -07003044 // One based row/column stored on the mouse event.
3045 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
3046 this.scrollPort_.characterSize.height) + 1;
3047 e.terminalColumn = parseInt(e.clientX /
3048 this.scrollPort_.characterSize.width) + 1;
3049
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003050 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3051 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003052 return;
3053 }
3054
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003055 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003056 // If the cursor is visible and we're not sending mouse events to the
3057 // host app, then we want to hide the terminal cursor when the mouse
3058 // cursor is over top. This keeps the terminal cursor from interfering
3059 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003060 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3061 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3062 this.cursorNode_.style.display = 'none';
3063 } else if (this.cursorNode_.style.display == 'none') {
3064 this.cursorNode_.style.display = '';
3065 }
3066 }
rgindad5613292012-06-19 15:40:37 -07003067
Robert Ginda928cf632014-03-05 15:07:41 -08003068 if (e.type == 'mousedown') {
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003069 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003070 // If VT mouse reporting is disabled, or has been defeated with
3071 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003072 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003073 this.setSelectionEnabled(true);
3074 } else {
3075 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003076 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003077 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003078 this.setSelectionEnabled(false);
3079 e.preventDefault();
3080 }
3081 }
3082
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003083 if (!reportMouseEvents) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003084 if (e.type == 'dblclick' && this.copyOnSelect) {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003085 this.screen_.expandSelection(this.document_.getSelection());
Robert Ginda15ed4902016-07-12 10:43:22 -07003086 this.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07003087 }
3088
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003089 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003090 // Debounce this event with the dblclick event. If you try to doubleclick
3091 // a URL to open it, Chrome will fire click then dblclick, but we won't
3092 // have expanded the selection text at the first click event.
3093 clearTimeout(this.timeouts_.openUrl);
3094 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3095 500);
3096 return;
3097 }
3098
Mike Frysinger847577f2017-05-23 23:25:57 -04003099 if (e.type == 'mousedown') {
3100 if ((this.mouseRightClickPaste && e.button == 2 /* right button */) ||
Mike Frysinger2edd3612017-05-24 00:54:39 -04003101 e.button == this.mousePasteButton) {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003102 if (!this.paste())
3103 console.warning('Could not paste manually due to web restrictions');;
Mike Frysinger847577f2017-05-23 23:25:57 -04003104 }
3105 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003106
Mike Frysinger2edd3612017-05-24 00:54:39 -04003107 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003108 !this.document_.getSelection().isCollapsed) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003109 this.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003110 }
3111
3112 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3113 this.scrollBlockerNode_.engaged) {
3114 // Disengage the scroll-blocker after one of these events.
3115 this.scrollBlockerNode_.engaged = false;
3116 this.scrollBlockerNode_.style.top = '-99px';
3117 }
3118
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003119 // Emulate arrow key presses via scroll wheel events.
3120 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3121 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003122 if (e.type == 'wheel') {
3123 var delta = this.scrollPort_.scrollWheelDelta(e);
3124 var lines = lib.f.smartFloorDivide(
3125 Math.abs(delta), this.scrollPort_.characterSize.height);
3126
3127 var data = '\x1bO' + (delta < 0 ? 'B' : 'A');
3128 this.io.sendString(data.repeat(lines));
3129
3130 e.preventDefault();
3131 }
3132 }
Robert Ginda928cf632014-03-05 15:07:41 -08003133 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003134 if (!this.scrollBlockerNode_.engaged) {
3135 if (e.type == 'mousedown') {
3136 // Move the scroll-blocker into place if we want to keep the scrollport
3137 // from scrolling.
3138 this.scrollBlockerNode_.engaged = true;
3139 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3140 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3141 } else if (e.type == 'mousemove') {
3142 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3143 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003144 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003145 e.preventDefault();
3146 }
3147 }
Robert Ginda928cf632014-03-05 15:07:41 -08003148
3149 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003150 }
3151
Robert Ginda928cf632014-03-05 15:07:41 -08003152 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3153 // Restore this on mouseup in case it was temporarily defeated with a
3154 // alt-mousedown. Only do this when the selection is empty so that
3155 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003156 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003157 }
rgindad5613292012-06-19 15:40:37 -07003158};
3159
3160/**
3161 * Clients should override this if they care to know about mouse events.
3162 *
3163 * The event parameter will be a normal DOM mouse click event with additional
3164 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003165 *
3166 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003167 */
3168hterm.Terminal.prototype.onMouse = function(e) { };
3169
3170/**
rginda8e92a692012-05-20 19:37:20 -07003171 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003172 *
3173 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003174 */
Rob Spies06533ba2014-04-24 11:20:37 -07003175hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3176 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003177 this.restyleCursor_();
Michael Kelly485ecd12014-06-09 11:41:56 -04003178 if (focused === true)
3179 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003180};
3181
3182/**
rginda8ba33642011-12-14 12:31:31 -08003183 * React when the ScrollPort is scrolled.
3184 */
3185hterm.Terminal.prototype.onScroll_ = function() {
3186 this.scheduleSyncCursorPosition_();
3187};
3188
3189/**
rginda9846e2f2012-01-27 13:53:33 -08003190 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003191 *
3192 * @param {Event} e The DOM paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003193 */
3194hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07003195 var data = e.text.replace(/\n/mg, '\r');
Connor Hegartyf525ceb2014-09-03 13:36:03 -07003196 data = this.keyboard.encode(data);
Robert Gindaa063b202014-07-21 11:08:25 -07003197 if (this.options_.bracketedPaste)
3198 data = '\x1b[200~' + data + '\x1b[201~';
3199
3200 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003201};
3202
3203/**
rgindaa09e7332012-08-17 12:49:51 -07003204 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003205 *
3206 * @param {Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003207 */
3208hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003209 if (!this.useDefaultWindowCopy) {
3210 e.preventDefault();
3211 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3212 }
rgindaa09e7332012-08-17 12:49:51 -07003213};
3214
3215/**
rginda8ba33642011-12-14 12:31:31 -08003216 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003217 *
3218 * Note: This function should not directly contain code that alters the internal
3219 * state of the terminal. That kind of code belongs in realizeWidth or
3220 * realizeHeight, so that it can be executed synchronously in the case of a
3221 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003222 */
3223hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003224 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003225 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003226 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003227 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003228
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003229 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003230 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003231 // gets removed from the document or during the initial load, and we can't
3232 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003233 // This can also happen if called before the scrollPort calculates the
3234 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003235 return;
3236 }
3237
rgindaa8ba17d2012-08-15 14:41:10 -07003238 var isNewSize = (columnCount != this.screenSize.width ||
3239 rowCount != this.screenSize.height);
3240
3241 // We do this even if the size didn't change, just to be sure everything is
3242 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003243 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003244 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003245
3246 if (isNewSize)
3247 this.overlaySize();
3248
Robert Gindafb1be6a2013-12-11 11:56:22 -08003249 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003250 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08003251};
3252
3253/**
3254 * Service the cursor blink timeout.
3255 */
3256hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003257 if (!this.options_.cursorBlink) {
3258 delete this.timeouts_.cursorBlink;
3259 return;
3260 }
3261
Robert Ginda830583c2013-08-07 13:20:46 -07003262 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3263 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003264 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003265 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3266 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003267 } else {
rginda87b86462011-12-14 13:48:03 -08003268 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003269 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3270 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003271 }
3272};
David Reveman8f552492012-03-28 12:18:41 -04003273
3274/**
3275 * Set the scrollbar-visible mode bit.
3276 *
3277 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3278 * Otherwise it will not.
3279 *
3280 * Defaults to on.
3281 *
3282 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3283 */
3284hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3285 this.scrollPort_.setScrollbarVisible(state);
3286};
Michael Kelly485ecd12014-06-09 11:41:56 -04003287
3288/**
Rob Spies49039e52014-12-17 13:40:04 -08003289 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003290 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003291 *
3292 * Defaults to 1.
3293 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003294 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003295 */
3296hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3297 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3298};
3299
3300/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003301 * Close all web notifications created by terminal bells.
3302 */
3303hterm.Terminal.prototype.closeBellNotifications_ = function() {
3304 this.bellNotificationList_.forEach(function(n) {
3305 n.close();
3306 });
3307 this.bellNotificationList_.length = 0;
3308};