blob: 0d58231a283de681757190aa050647d8f5c59711 [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
Robert Gindae76aa9f2014-03-14 12:29:12 -0700540 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400541 terminal.scrollPort_.setUserCssUrl(v);
542 },
543
544 'user-css-text': function(v) {
545 terminal.scrollPort_.setUserCssText(v);
546 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400547
548 'word-break-match-left': function(v) {
549 terminal.primaryScreen_.wordBreakMatchLeft = v;
550 terminal.alternateScreen_.wordBreakMatchLeft = v;
551 },
552
553 'word-break-match-right': function(v) {
554 terminal.primaryScreen_.wordBreakMatchRight = v;
555 terminal.alternateScreen_.wordBreakMatchRight = v;
556 },
557
558 'word-break-match-middle': function(v) {
559 terminal.primaryScreen_.wordBreakMatchMiddle = v;
560 terminal.alternateScreen_.wordBreakMatchMiddle = v;
561 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700562 });
rginda30f20f62012-04-05 16:36:19 -0700563
Robert Ginda57f03b42012-09-13 11:02:48 -0700564 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800565 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700566
567 if (opt_callback)
568 opt_callback();
569 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800570};
571
Rob Spies56953412014-04-28 14:09:47 -0700572
573/**
574 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500575 *
576 * @return {hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700577 */
578hterm.Terminal.prototype.getPrefs = function() {
579 return this.prefs_;
580};
581
Robert Gindaa063b202014-07-21 11:08:25 -0700582/**
583 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500584 *
585 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700586 */
587hterm.Terminal.prototype.setBracketedPaste = function(state) {
588 this.options_.bracketedPaste = state;
589};
Rob Spies56953412014-04-28 14:09:47 -0700590
rginda8e92a692012-05-20 19:37:20 -0700591/**
592 * Set the color for the cursor.
593 *
594 * If you want this setting to persist, set it through prefs_, rather than
595 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500596 *
597 * @param {string} color The color to set.
rginda8e92a692012-05-20 19:37:20 -0700598 */
599hterm.Terminal.prototype.setCursorColor = function(color) {
Robert Ginda830583c2013-08-07 13:20:46 -0700600 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700601 this.cursorNode_.style.backgroundColor = color;
602 this.cursorNode_.style.borderColor = color;
603};
604
605/**
606 * Return the current cursor color as a string.
Evan Jones2600d4f2016-12-06 09:29:36 -0500607 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700608 */
609hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700610 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700611};
612
613/**
rgindad5613292012-06-19 15:40:37 -0700614 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500615 *
616 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700617 */
618hterm.Terminal.prototype.setSelectionEnabled = function(state) {
619 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700620};
621
622/**
rginda8e92a692012-05-20 19:37:20 -0700623 * Set the background color.
624 *
625 * If you want this setting to persist, set it through prefs_, rather than
626 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500627 *
628 * @param {string} color The color to set.
rginda8e92a692012-05-20 19:37:20 -0700629 */
630hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700631 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700632 this.primaryScreen_.textAttributes.setDefaults(
633 this.foregroundColor_, this.backgroundColor_);
634 this.alternateScreen_.textAttributes.setDefaults(
635 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700636 this.scrollPort_.setBackgroundColor(color);
637};
638
rginda9f5222b2012-03-05 11:53:28 -0800639/**
640 * Return the current terminal background color.
641 *
642 * Intended for use by other classes, so we don't have to expose the entire
643 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500644 *
645 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800646 */
647hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700648 return this.backgroundColor_;
649};
650
651/**
652 * Set the foreground color.
653 *
654 * If you want this setting to persist, set it through prefs_, rather than
655 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500656 *
657 * @param {string} color The color to set.
rginda8e92a692012-05-20 19:37:20 -0700658 */
659hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700660 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700661 this.primaryScreen_.textAttributes.setDefaults(
662 this.foregroundColor_, this.backgroundColor_);
663 this.alternateScreen_.textAttributes.setDefaults(
664 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700665 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800666};
667
668/**
669 * Return the current terminal foreground color.
670 *
671 * Intended for use by other classes, so we don't have to expose the entire
672 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500673 *
674 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800675 */
676hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700677 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800678};
679
680/**
rginda87b86462011-12-14 13:48:03 -0800681 * Create a new instance of a terminal command and run it with a given
682 * argument string.
683 *
684 * @param {function} commandClass The constructor for a terminal command.
685 * @param {string} argString The argument string to pass to the command.
686 */
687hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700688 var environment = this.prefs_.get('environment');
689 if (typeof environment != 'object' || environment == null)
690 environment = {};
691
rginda87b86462011-12-14 13:48:03 -0800692 var self = this;
693 this.command = new commandClass(
694 { argString: argString || '',
695 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700696 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800697 onExit: function(code) {
698 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800699 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700700 if (self.prefs_.get('close-on-exit'))
701 window.close();
rginda87b86462011-12-14 13:48:03 -0800702 }
703 });
704
rgindafeaf3142012-01-31 15:14:20 -0800705 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800706 this.command.run();
707};
708
709/**
rgindafeaf3142012-01-31 15:14:20 -0800710 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500711 *
712 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800713 */
714hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700715 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800716};
717
718/**
719 * Install the keyboard handler for this terminal.
720 *
721 * This will prevent the browser from seeing any keystrokes sent to the
722 * terminal.
723 */
724hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700725 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
rgindafeaf3142012-01-31 15:14:20 -0800726}
727
728/**
729 * Uninstall the keyboard handler for this terminal.
730 */
731hterm.Terminal.prototype.uninstallKeyboard = function() {
732 this.keyboard.installKeyboard(null);
733}
734
735/**
rginda35c456b2012-02-09 17:29:05 -0800736 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800737 *
738 * Call setFontSize(0) to reset to the default font size.
739 *
740 * This function does not modify the font-size preference.
741 *
742 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800743 */
744hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800745 if (px === 0)
746 px = this.prefs_.get('font-size');
747
rginda35c456b2012-02-09 17:29:05 -0800748 this.scrollPort_.setFontSize(px);
Mike Frysinger66beb0b2017-05-30 19:44:51 -0400749 this.document_.documentElement.style.setProperty(
750 '--hterm-charsize-width', this.scrollPort_.characterSize.width + 'px');
751 this.document_.documentElement.style.setProperty(
752 '--hterm-charsize-height', this.scrollPort_.characterSize.height + 'px');
rginda35c456b2012-02-09 17:29:05 -0800753};
754
755/**
756 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500757 *
758 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800759 */
760hterm.Terminal.prototype.getFontSize = function() {
761 return this.scrollPort_.getFontSize();
762};
763
764/**
rginda8e92a692012-05-20 19:37:20 -0700765 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500766 *
767 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700768 */
769hterm.Terminal.prototype.getFontFamily = function() {
770 return this.scrollPort_.getFontFamily();
771};
772
773/**
rginda35c456b2012-02-09 17:29:05 -0800774 * Set the CSS "font-family" for this terminal.
775 */
rginda9f5222b2012-03-05 11:53:28 -0800776hterm.Terminal.prototype.syncFontFamily = function() {
777 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
778 this.prefs_.get('font-smoothing'));
779 this.syncBoldSafeState();
780};
781
rginda4bba5e12012-06-20 16:15:30 -0700782/**
783 * Set this.mousePasteButton based on the mouse-paste-button pref,
784 * autodetecting if necessary.
785 */
786hterm.Terminal.prototype.syncMousePasteButton = function() {
787 var button = this.prefs_.get('mouse-paste-button');
788 if (typeof button == 'number') {
789 this.mousePasteButton = button;
790 return;
791 }
792
793 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
Mike Frysinger98dd15b2017-05-18 22:52:23 -0400794 if (!ary || ary[1] == 'CrOS') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400795 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700796 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400797 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700798 }
799};
800
801/**
802 * Enable or disable bold based on the enable-bold pref, autodetecting if
803 * necessary.
804 */
rginda9f5222b2012-03-05 11:53:28 -0800805hterm.Terminal.prototype.syncBoldSafeState = function() {
806 var enableBold = this.prefs_.get('enable-bold');
807 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700808 this.primaryScreen_.textAttributes.enableBold = enableBold;
809 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800810 return;
811 }
812
rgindaf7521392012-02-28 17:20:34 -0800813 var normalSize = this.scrollPort_.measureCharacterSize();
814 var boldSize = this.scrollPort_.measureCharacterSize('bold');
815
816 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800817 if (!isBoldSafe) {
818 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700819 'from normal. Font family is: ' +
820 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800821 }
rginda9f5222b2012-03-05 11:53:28 -0800822
Robert Gindaed016262012-10-26 16:27:09 -0700823 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
824 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800825};
826
827/**
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400828 * Enable or disable blink based on the enable-blink pref.
829 */
830hterm.Terminal.prototype.syncBlinkState = function() {
831 this.document_.documentElement.style.setProperty(
832 '--hterm-blink-node-duration',
833 this.prefs_.get('enable-blink') ? '0.7s' : '0');
834};
835
836/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400837 * Set the mouse cursor style based on the current terminal mode.
838 */
839hterm.Terminal.prototype.syncMouseStyle = function() {
840 this.document_.documentElement.style.setProperty(
841 '--hterm-mouse-cursor-style',
842 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
843 'var(--hterm-mouse-cursor-text)' :
844 'var(--hterm-mouse-cursor-pointer)');
845};
846
847/**
rginda87b86462011-12-14 13:48:03 -0800848 * Return a copy of the current cursor position.
849 *
850 * @return {hterm.RowCol} The RowCol object representing the current position.
851 */
852hterm.Terminal.prototype.saveCursor = function() {
853 return this.screen_.cursorPosition.clone();
854};
855
Evan Jones2600d4f2016-12-06 09:29:36 -0500856/**
857 * Return the current text attributes.
858 *
859 * @return {string}
860 */
rgindaa19afe22012-01-25 15:40:22 -0800861hterm.Terminal.prototype.getTextAttributes = function() {
862 return this.screen_.textAttributes;
863};
864
Evan Jones2600d4f2016-12-06 09:29:36 -0500865/**
866 * Set the text attributes.
867 *
868 * @param {string} textAttributes The attributes to set.
869 */
rginda1a09aa02012-06-18 21:11:25 -0700870hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
871 this.screen_.textAttributes = textAttributes;
872};
873
rginda87b86462011-12-14 13:48:03 -0800874/**
rgindaf522ce02012-04-17 17:49:17 -0700875 * Return the current browser zoom factor applied to the terminal.
876 *
877 * @return {number} The current browser zoom factor.
878 */
879hterm.Terminal.prototype.getZoomFactor = function() {
880 return this.scrollPort_.characterSize.zoomFactor;
881};
882
883/**
rginda9846e2f2012-01-27 13:53:33 -0800884 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500885 *
886 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800887 */
888hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800889 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800890};
891
892/**
rginda87b86462011-12-14 13:48:03 -0800893 * Restore a previously saved cursor position.
894 *
895 * @param {hterm.RowCol} cursor The position to restore.
896 */
897hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700898 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
899 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800900 this.screen_.setCursorPosition(row, column);
901 if (cursor.column > column ||
902 cursor.column == column && cursor.overflow) {
903 this.screen_.cursorPosition.overflow = true;
904 }
rginda87b86462011-12-14 13:48:03 -0800905};
906
907/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400908 * Clear the cursor's overflow flag.
909 */
910hterm.Terminal.prototype.clearCursorOverflow = function() {
911 this.screen_.cursorPosition.overflow = false;
912};
913
914/**
Robert Ginda830583c2013-08-07 13:20:46 -0700915 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -0500916 *
917 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -0700918 */
919hterm.Terminal.prototype.setCursorShape = function(shape) {
920 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -0800921 this.restyleCursor_();
Robert Ginda830583c2013-08-07 13:20:46 -0700922}
923
924/**
925 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -0500926 *
927 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -0700928 */
929hterm.Terminal.prototype.getCursorShape = function() {
930 return this.cursorShape_;
931}
932
933/**
rginda87b86462011-12-14 13:48:03 -0800934 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -0500935 *
936 * @param {number} columnCount
rginda87b86462011-12-14 13:48:03 -0800937 */
938hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800939 if (columnCount == null) {
940 this.div_.style.width = '100%';
941 return;
942 }
943
Robert Ginda26806d12014-07-24 13:44:07 -0700944 this.div_.style.width = Math.ceil(
945 this.scrollPort_.characterSize.width *
946 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400947 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800948 this.scheduleSyncCursorPosition_();
949};
rginda87b86462011-12-14 13:48:03 -0800950
rgindac9bc5502012-01-18 11:48:44 -0800951/**
rginda35c456b2012-02-09 17:29:05 -0800952 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -0500953 *
954 * @param {number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -0800955 */
956hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800957 if (rowCount == null) {
958 this.div_.style.height = '100%';
959 return;
960 }
961
rginda35c456b2012-02-09 17:29:05 -0800962 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700963 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800964 this.realizeSize_(this.screenSize.width, rowCount);
965 this.scheduleSyncCursorPosition_();
966};
967
968/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400969 * Deal with terminal size changes.
970 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500971 * @param {number} columnCount The number of columns.
972 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400973 */
974hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
975 if (columnCount != this.screenSize.width)
976 this.realizeWidth_(columnCount);
977
978 if (rowCount != this.screenSize.height)
979 this.realizeHeight_(rowCount);
980
981 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -0700982 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400983};
984
985/**
rgindac9bc5502012-01-18 11:48:44 -0800986 * Deal with terminal width changes.
987 *
988 * This function does what needs to be done when the terminal width changes
989 * out from under us. It happens here rather than in onResize_() because this
990 * code may need to run synchronously to handle programmatic changes of
991 * terminal width.
992 *
993 * Relying on the browser to send us an async resize event means we may not be
994 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -0500995 *
996 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -0800997 */
998hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700999 if (columnCount <= 0)
1000 throw new Error('Attempt to realize bad width: ' + columnCount);
1001
rgindac9bc5502012-01-18 11:48:44 -08001002 var deltaColumns = columnCount - this.screen_.getWidth();
1003
rginda87b86462011-12-14 13:48:03 -08001004 this.screenSize.width = columnCount;
1005 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001006
1007 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -04001008 if (this.defaultTabStops)
1009 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -08001010 } else {
1011 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -04001012 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -08001013 break;
1014
1015 this.tabStops_.pop();
1016 }
1017 }
1018
1019 this.screen_.setColumnCount(this.screenSize.width);
1020};
1021
1022/**
1023 * Deal with terminal height changes.
1024 *
1025 * This function does what needs to be done when the terminal height changes
1026 * out from under us. It happens here rather than in onResize_() because this
1027 * code may need to run synchronously to handle programmatic changes of
1028 * terminal height.
1029 *
1030 * Relying on the browser to send us an async resize event means we may not be
1031 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001032 *
1033 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001034 */
1035hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001036 if (rowCount <= 0)
1037 throw new Error('Attempt to realize bad height: ' + rowCount);
1038
rgindac9bc5502012-01-18 11:48:44 -08001039 var deltaRows = rowCount - this.screen_.getHeight();
1040
1041 this.screenSize.height = rowCount;
1042
1043 var cursor = this.saveCursor();
1044
1045 if (deltaRows < 0) {
1046 // Screen got smaller.
1047 deltaRows *= -1;
1048 while (deltaRows) {
1049 var lastRow = this.getRowCount() - 1;
1050 if (lastRow - this.scrollbackRows_.length == cursor.row)
1051 break;
1052
1053 if (this.getRowText(lastRow))
1054 break;
1055
1056 this.screen_.popRow();
1057 deltaRows--;
1058 }
1059
1060 var ary = this.screen_.shiftRows(deltaRows);
1061 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1062
1063 // We just removed rows from the top of the screen, we need to update
1064 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001065 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001066 } else if (deltaRows > 0) {
1067 // Screen got larger.
1068
1069 if (deltaRows <= this.scrollbackRows_.length) {
1070 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1071 var rows = this.scrollbackRows_.splice(
1072 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1073 this.screen_.unshiftRows(rows);
1074 deltaRows -= scrollbackCount;
1075 cursor.row += scrollbackCount;
1076 }
1077
1078 if (deltaRows)
1079 this.appendRows_(deltaRows);
1080 }
1081
rginda35c456b2012-02-09 17:29:05 -08001082 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001083 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001084};
1085
1086/**
1087 * Scroll the terminal to the top of the scrollback buffer.
1088 */
1089hterm.Terminal.prototype.scrollHome = function() {
1090 this.scrollPort_.scrollRowToTop(0);
1091};
1092
1093/**
1094 * Scroll the terminal to the end.
1095 */
1096hterm.Terminal.prototype.scrollEnd = function() {
1097 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1098};
1099
1100/**
1101 * Scroll the terminal one page up (minus one line) relative to the current
1102 * position.
1103 */
1104hterm.Terminal.prototype.scrollPageUp = function() {
1105 var i = this.scrollPort_.getTopRowIndex();
1106 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
1107};
1108
1109/**
1110 * Scroll the terminal one page down (minus one line) relative to the current
1111 * position.
1112 */
1113hterm.Terminal.prototype.scrollPageDown = function() {
1114 var i = this.scrollPort_.getTopRowIndex();
1115 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -08001116};
1117
rgindac9bc5502012-01-18 11:48:44 -08001118/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001119 * Scroll the terminal one line up relative to the current position.
1120 */
1121hterm.Terminal.prototype.scrollLineUp = function() {
1122 var i = this.scrollPort_.getTopRowIndex();
1123 this.scrollPort_.scrollRowToTop(i - 1);
1124};
1125
1126/**
1127 * Scroll the terminal one line down relative to the current position.
1128 */
1129hterm.Terminal.prototype.scrollLineDown = function() {
1130 var i = this.scrollPort_.getTopRowIndex();
1131 this.scrollPort_.scrollRowToTop(i + 1);
1132};
1133
1134/**
Robert Ginda40932892012-12-10 17:26:40 -08001135 * Clear primary screen, secondary screen, and the scrollback buffer.
1136 */
1137hterm.Terminal.prototype.wipeContents = function() {
1138 this.scrollbackRows_.length = 0;
1139 this.scrollPort_.resetCache();
1140
1141 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
1142 var bottom = screen.getHeight();
1143 if (bottom > 0) {
1144 this.renumberRows_(0, bottom);
1145 this.clearHome(screen);
1146 }
1147 }.bind(this));
1148
1149 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001150 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001151};
1152
1153/**
rgindac9bc5502012-01-18 11:48:44 -08001154 * Full terminal reset.
1155 */
rginda87b86462011-12-14 13:48:03 -08001156hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -08001157 this.clearAllTabStops();
1158 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001159
1160 this.clearHome(this.primaryScreen_);
1161 this.primaryScreen_.textAttributes.reset();
1162
1163 this.clearHome(this.alternateScreen_);
1164 this.alternateScreen_.textAttributes.reset();
1165
rgindab8bc8932012-04-27 12:45:03 -07001166 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1167
Robert Ginda92e18102013-03-14 13:56:37 -07001168 this.vt.reset();
1169
rgindac9bc5502012-01-18 11:48:44 -08001170 this.softReset();
rginda87b86462011-12-14 13:48:03 -08001171};
1172
rgindac9bc5502012-01-18 11:48:44 -08001173/**
1174 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001175 *
1176 * Perform a soft reset to the default values listed in
1177 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001178 */
rginda0f5c0292012-01-13 11:00:13 -08001179hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -07001180 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001181 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001182
Brad Townb62dfdc2015-03-16 19:07:15 -07001183 // We show the cursor on soft reset but do not alter the blink state.
1184 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1185
rgindab8bc8932012-04-27 12:45:03 -07001186 // Xterm also resets the color palette on soft reset, even though it doesn't
1187 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -07001188 this.primaryScreen_.textAttributes.resetColorPalette();
1189 this.alternateScreen_.textAttributes.resetColorPalette();
1190
rgindab8bc8932012-04-27 12:45:03 -07001191 // The xterm man page explicitly says this will happen on soft reset.
1192 this.setVTScrollRegion(null, null);
1193
1194 // Xterm also shows the cursor on soft reset, but does not alter the blink
1195 // state.
rgindaa19afe22012-01-25 15:40:22 -08001196 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001197};
1198
rgindac9bc5502012-01-18 11:48:44 -08001199/**
1200 * Move the cursor forward to the next tab stop, or to the last column
1201 * if no more tab stops are set.
1202 */
1203hterm.Terminal.prototype.forwardTabStop = function() {
1204 var column = this.screen_.cursorPosition.column;
1205
1206 for (var i = 0; i < this.tabStops_.length; i++) {
1207 if (this.tabStops_[i] > column) {
1208 this.setCursorColumn(this.tabStops_[i]);
1209 return;
1210 }
1211 }
1212
David Benjamin66e954d2012-05-05 21:08:12 -04001213 // xterm does not clear the overflow flag on HT or CHT.
1214 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001215 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001216 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001217};
1218
rgindac9bc5502012-01-18 11:48:44 -08001219/**
1220 * Move the cursor backward to the previous tab stop, or to the first column
1221 * if no previous tab stops are set.
1222 */
1223hterm.Terminal.prototype.backwardTabStop = function() {
1224 var column = this.screen_.cursorPosition.column;
1225
1226 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1227 if (this.tabStops_[i] < column) {
1228 this.setCursorColumn(this.tabStops_[i]);
1229 return;
1230 }
1231 }
1232
1233 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001234};
1235
rgindac9bc5502012-01-18 11:48:44 -08001236/**
1237 * Set a tab stop at the given column.
1238 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001239 * @param {integer} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001240 */
1241hterm.Terminal.prototype.setTabStop = function(column) {
1242 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1243 if (this.tabStops_[i] == column)
1244 return;
1245
1246 if (this.tabStops_[i] < column) {
1247 this.tabStops_.splice(i + 1, 0, column);
1248 return;
1249 }
1250 }
1251
1252 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001253};
1254
rgindac9bc5502012-01-18 11:48:44 -08001255/**
1256 * Clear the tab stop at the current cursor position.
1257 *
1258 * No effect if there is no tab stop at the current cursor position.
1259 */
1260hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1261 var column = this.screen_.cursorPosition.column;
1262
1263 var i = this.tabStops_.indexOf(column);
1264 if (i == -1)
1265 return;
1266
1267 this.tabStops_.splice(i, 1);
1268};
1269
1270/**
1271 * Clear all tab stops.
1272 */
1273hterm.Terminal.prototype.clearAllTabStops = function() {
1274 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001275 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001276};
1277
1278/**
1279 * Set up the default tab stops, starting from a given column.
1280 *
1281 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001282 * from the specified column, or 0 if no column is provided. It also flags
1283 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001284 *
1285 * This does not clear the existing tab stops first, use clearAllTabStops
1286 * for that.
1287 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001288 * @param {integer} opt_start Optional starting zero based starting column, useful
rgindac9bc5502012-01-18 11:48:44 -08001289 * for filling out missing tab stops when the terminal is resized.
1290 */
1291hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1292 var start = opt_start || 0;
1293 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001294 // Round start up to a default tab stop.
1295 start = start - 1 - ((start - 1) % w) + w;
1296 for (var i = start; i < this.screenSize.width; i += w) {
1297 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001298 }
David Benjamin66e954d2012-05-05 21:08:12 -04001299
1300 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001301};
1302
rginda6d397402012-01-17 10:58:29 -08001303/**
rginda8ba33642011-12-14 12:31:31 -08001304 * Interpret a sequence of characters.
1305 *
1306 * Incomplete escape sequences are buffered until the next call.
1307 *
1308 * @param {string} str Sequence of characters to interpret or pass through.
1309 */
1310hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001311 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001312 this.scheduleSyncCursorPosition_();
1313};
1314
1315/**
1316 * Take over the given DIV for use as the terminal display.
1317 *
1318 * @param {HTMLDivElement} div The div to use as the terminal display.
1319 */
1320hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001321 this.div_ = div;
1322
rginda8ba33642011-12-14 12:31:31 -08001323 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001324 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001325 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1326 this.scrollPort_.setBackgroundPosition(
1327 this.prefs_.get('background-position'));
Mike Frysinger08bad432017-04-24 00:50:54 -04001328 this.scrollPort_.setUserCssUrl(this.prefs_.get('user-css'));
1329 this.scrollPort_.setUserCssText(this.prefs_.get('user-css-text'));
rginda30f20f62012-04-05 16:36:19 -07001330
rginda0918b652012-04-04 11:26:24 -07001331 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001332
rginda9f5222b2012-03-05 11:53:28 -08001333 this.setFontSize(this.prefs_.get('font-size'));
1334 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001335
David Reveman8f552492012-03-28 12:18:41 -04001336 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001337 this.setScrollWheelMoveMultipler(
1338 this.prefs_.get('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001339
rginda8ba33642011-12-14 12:31:31 -08001340 this.document_ = this.scrollPort_.getDocument();
1341
Evan Jones5f9df812016-12-06 09:38:58 -05001342 this.document_.body.oncontextmenu = function() { return false; };
rginda4bba5e12012-06-20 16:15:30 -07001343
1344 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001345 var screenNode = this.scrollPort_.getScreenNode();
1346 screenNode.addEventListener('mousedown', onMouse);
1347 screenNode.addEventListener('mouseup', onMouse);
1348 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001349 this.scrollPort_.onScrollWheel = onMouse;
1350
Toni Barzic0bfa8922013-11-22 11:18:35 -08001351 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001352 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001353 // Listen for mousedown events on the screenNode as in FF the focus
1354 // events don't bubble.
1355 screenNode.addEventListener('mousedown', function() {
1356 setTimeout(this.onFocusChange_.bind(this, true));
1357 }.bind(this));
1358
Toni Barzic0bfa8922013-11-22 11:18:35 -08001359 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001360 'blur', this.onFocusChange_.bind(this, false));
1361
1362 var style = this.document_.createElement('style');
1363 style.textContent =
1364 ('.cursor-node[focus="false"] {' +
1365 ' box-sizing: border-box;' +
1366 ' background-color: transparent !important;' +
1367 ' border-width: 2px;' +
1368 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001369 '}' +
1370 '.wc-node {' +
1371 ' display: inline-block;' +
1372 ' text-align: center;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001373 ' width: calc(var(--hterm-charsize-width) * 2);' +
Mike Frysinger4036f6e2017-05-31 14:02:55 -04001374 ' line-height: var(--hterm-charsize-height);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001375 '}' +
1376 ':root {' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001377 ' --hterm-charsize-width: ' + this.scrollPort_.characterSize.width + 'px;' +
1378 ' --hterm-charsize-height: ' + this.scrollPort_.characterSize.height + 'px;' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001379 ' --hterm-blink-node-duration: 0.7s;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001380 ' --hterm-mouse-cursor-text: text;' +
1381 ' --hterm-mouse-cursor-pointer: default;' +
1382 ' --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001383 '}' +
1384 '@keyframes blink {' +
1385 ' from { opacity: 1.0; }' +
1386 ' to { opacity: 0.0; }' +
1387 '}' +
1388 '.blink-node {' +
1389 ' animation-name: blink;' +
1390 ' animation-duration: var(--hterm-blink-node-duration);' +
1391 ' animation-iteration-count: infinite;' +
1392 ' animation-timing-function: ease-in-out;' +
1393 ' animation-direction: alternate;' +
rginda8e92a692012-05-20 19:37:20 -07001394 '}');
1395 this.document_.head.appendChild(style);
1396
rginda8ba33642011-12-14 12:31:31 -08001397 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001398 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001399 this.cursorNode_.style.cssText =
1400 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001401 'top: -99px;' +
1402 'display: block;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001403 'width: var(--hterm-charsize-width);' +
1404 'height: var(--hterm-charsize-height);' +
Rob Spies06533ba2014-04-24 11:20:37 -07001405 '-webkit-transition: opacity, background-color 100ms linear;' +
1406 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001407
rginda8e92a692012-05-20 19:37:20 -07001408 this.setCursorColor(this.prefs_.get('cursor-color'));
Robert Gindafb1be6a2013-12-11 11:56:22 -08001409 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1410 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001411
rginda8ba33642011-12-14 12:31:31 -08001412 this.document_.body.appendChild(this.cursorNode_);
1413
rgindad5613292012-06-19 15:40:37 -07001414 // When 'enableMouseDragScroll' is off we reposition this element directly
1415 // under the mouse cursor after a click. This makes Chrome associate
1416 // subsequent mousemove events with the scroll-blocker. Since the
1417 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1418 // events do not cause the scrollport to scroll.
1419 //
1420 // It's a hack, but it's the cleanest way I could find.
1421 this.scrollBlockerNode_ = this.document_.createElement('div');
1422 this.scrollBlockerNode_.style.cssText =
1423 ('position: absolute;' +
1424 'top: -99px;' +
1425 'display: block;' +
1426 'width: 10px;' +
1427 'height: 10px;');
1428 this.document_.body.appendChild(this.scrollBlockerNode_);
1429
rgindad5613292012-06-19 15:40:37 -07001430 this.scrollPort_.onScrollWheel = onMouse;
1431 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1432 ].forEach(function(event) {
1433 this.scrollBlockerNode_.addEventListener(event, onMouse);
1434 this.cursorNode_.addEventListener(event, onMouse);
1435 this.document_.addEventListener(event, onMouse);
1436 }.bind(this));
1437
1438 this.cursorNode_.addEventListener('mousedown', function() {
1439 setTimeout(this.focus.bind(this));
1440 }.bind(this));
1441
rginda8ba33642011-12-14 12:31:31 -08001442 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001443
rginda87b86462011-12-14 13:48:03 -08001444 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001445 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001446};
1447
rginda0918b652012-04-04 11:26:24 -07001448/**
1449 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001450 *
1451 * @return {HTMLDocument}
rginda0918b652012-04-04 11:26:24 -07001452 */
rginda87b86462011-12-14 13:48:03 -08001453hterm.Terminal.prototype.getDocument = function() {
1454 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001455};
1456
1457/**
rginda0918b652012-04-04 11:26:24 -07001458 * Focus the terminal.
1459 */
1460hterm.Terminal.prototype.focus = function() {
1461 this.scrollPort_.focus();
1462};
1463
1464/**
rginda8ba33642011-12-14 12:31:31 -08001465 * Return the HTML Element for a given row index.
1466 *
1467 * This is a method from the RowProvider interface. The ScrollPort uses
1468 * it to fetch rows on demand as they are scrolled into view.
1469 *
1470 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1471 * pairs to conserve memory.
1472 *
1473 * @param {integer} index The zero-based row index, measured relative to the
1474 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001475 * largest indices.
rginda8ba33642011-12-14 12:31:31 -08001476 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1477 */
1478hterm.Terminal.prototype.getRowNode = function(index) {
1479 if (index < this.scrollbackRows_.length)
1480 return this.scrollbackRows_[index];
1481
1482 var screenIndex = index - this.scrollbackRows_.length;
1483 return this.screen_.rowsArray[screenIndex];
1484};
1485
1486/**
1487 * Return the text content for a given range of rows.
1488 *
1489 * This is a method from the RowProvider interface. The ScrollPort uses
1490 * it to fetch text content on demand when the user attempts to copy their
1491 * selection to the clipboard.
1492 *
1493 * @param {integer} start The zero-based row index to start from, measured
1494 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001495 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001496 * @param {integer} end The zero-based row index to end on, measured
1497 * relative to the start of the scrollback buffer.
1498 * @return {string} A single string containing the text value of the range of
1499 * rows. Lines will be newline delimited, with no trailing newline.
1500 */
1501hterm.Terminal.prototype.getRowsText = function(start, end) {
1502 var ary = [];
1503 for (var i = start; i < end; i++) {
1504 var node = this.getRowNode(i);
1505 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001506 if (i < end - 1 && !node.getAttribute('line-overflow'))
1507 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001508 }
1509
rgindaa09e7332012-08-17 12:49:51 -07001510 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001511};
1512
1513/**
1514 * Return the text content for a given row.
1515 *
1516 * This is a method from the RowProvider interface. The ScrollPort uses
1517 * it to fetch text content on demand when the user attempts to copy their
1518 * selection to the clipboard.
1519 *
1520 * @param {integer} index The zero-based row index to return, measured
1521 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001522 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001523 * @return {string} A string containing the text value of the selected row.
1524 */
1525hterm.Terminal.prototype.getRowText = function(index) {
1526 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001527 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001528};
1529
1530/**
1531 * Return the total number of rows in the addressable screen and in the
1532 * scrollback buffer of this terminal.
1533 *
1534 * This is a method from the RowProvider interface. The ScrollPort uses
1535 * it to compute the size of the scrollbar.
1536 *
1537 * @return {integer} The number of rows in this terminal.
1538 */
1539hterm.Terminal.prototype.getRowCount = function() {
1540 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1541};
1542
1543/**
1544 * Create DOM nodes for new rows and append them to the end of the terminal.
1545 *
1546 * This is the only correct way to add a new DOM node for a row. Notice that
1547 * the new row is appended to the bottom of the list of rows, and does not
1548 * require renumbering (of the rowIndex property) of previous rows.
1549 *
1550 * If you think you want a new blank row somewhere in the middle of the
1551 * terminal, look into moveRows_().
1552 *
1553 * This method does not pay attention to vtScrollTop/Bottom, since you should
1554 * be using moveRows() in cases where they would matter.
1555 *
1556 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001557 *
1558 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001559 */
1560hterm.Terminal.prototype.appendRows_ = function(count) {
1561 var cursorRow = this.screen_.rowsArray.length;
1562 var offset = this.scrollbackRows_.length + cursorRow;
1563 for (var i = 0; i < count; i++) {
1564 var row = this.document_.createElement('x-row');
1565 row.appendChild(this.document_.createTextNode(''));
1566 row.rowIndex = offset + i;
1567 this.screen_.pushRow(row);
1568 }
1569
1570 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1571 if (extraRows > 0) {
1572 var ary = this.screen_.shiftRows(extraRows);
1573 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001574 if (this.scrollPort_.isScrolledEnd)
1575 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001576 }
1577
1578 if (cursorRow >= this.screen_.rowsArray.length)
1579 cursorRow = this.screen_.rowsArray.length - 1;
1580
rginda87b86462011-12-14 13:48:03 -08001581 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001582};
1583
1584/**
1585 * Relocate rows from one part of the addressable screen to another.
1586 *
1587 * This is used to recycle rows during VT scrolls (those which are driven
1588 * by VT commands, rather than by the user manipulating the scrollbar.)
1589 *
1590 * In this case, the blank lines scrolled into the scroll region are made of
1591 * the nodes we scrolled off. These have their rowIndex properties carefully
1592 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001593 *
1594 * @param {number} fromIndex The start index.
1595 * @param {number} count The number of rows to move.
1596 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001597 */
1598hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1599 var ary = this.screen_.removeRows(fromIndex, count);
1600 this.screen_.insertRows(toIndex, ary);
1601
1602 var start, end;
1603 if (fromIndex < toIndex) {
1604 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001605 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001606 } else {
1607 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001608 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001609 }
1610
1611 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001612 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001613};
1614
1615/**
1616 * Renumber the rowIndex property of the given range of rows.
1617 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001618 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001619 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001620 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001621 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001622 *
1623 * @param {number} start The start index.
1624 * @param {number} end The end index.
1625 * @param {hterm.Screen} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001626 */
Robert Ginda40932892012-12-10 17:26:40 -08001627hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1628 var screen = opt_screen || this.screen_;
1629
rginda8ba33642011-12-14 12:31:31 -08001630 var offset = this.scrollbackRows_.length;
1631 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001632 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001633 }
1634};
1635
1636/**
1637 * Print a string to the terminal.
1638 *
1639 * This respects the current insert and wraparound modes. It will add new lines
1640 * to the end of the terminal, scrolling off the top into the scrollback buffer
1641 * if necessary.
1642 *
1643 * The string is *not* parsed for escape codes. Use the interpret() method if
1644 * that's what you're after.
1645 *
1646 * @param{string} str The string to print.
1647 */
1648hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001649 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001650
Ricky Liang48f05cb2013-12-31 23:35:29 +08001651 var strWidth = lib.wc.strWidth(str);
1652
1653 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001654 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1655 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001656 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001657 }
rgindaa19afe22012-01-25 15:40:22 -08001658
Ricky Liang48f05cb2013-12-31 23:35:29 +08001659 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001660 var didOverflow = false;
1661 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001662
rgindaa9abdd82012-08-06 18:05:09 -07001663 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1664 didOverflow = true;
1665 count = this.screenSize.width - this.screen_.cursorPosition.column;
1666 }
rgindaa19afe22012-01-25 15:40:22 -08001667
rgindaa9abdd82012-08-06 18:05:09 -07001668 if (didOverflow && !this.options_.wraparound) {
1669 // If the string overflowed the line but wraparound is off, then the
1670 // last printed character should be the last of the string.
1671 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001672 substr = lib.wc.substr(str, startOffset, count - 1) +
1673 lib.wc.substr(str, strWidth - 1);
1674 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001675 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001676 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001677 }
rgindaa19afe22012-01-25 15:40:22 -08001678
Ricky Liang48f05cb2013-12-31 23:35:29 +08001679 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1680 for (var i = 0; i < tokens.length; i++) {
1681 if (tokens[i].wcNode)
1682 this.screen_.textAttributes.wcNode = true;
1683
1684 if (this.options_.insertMode) {
1685 this.screen_.insertString(tokens[i].str);
1686 } else {
1687 this.screen_.overwriteString(tokens[i].str);
1688 }
1689 this.screen_.textAttributes.wcNode = false;
rgindaa9abdd82012-08-06 18:05:09 -07001690 }
1691
1692 this.screen_.maybeClipCurrentRow();
1693 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001694 }
rginda8ba33642011-12-14 12:31:31 -08001695
1696 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001697
rginda9f5222b2012-03-05 11:53:28 -08001698 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001699 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001700};
1701
1702/**
rginda87b86462011-12-14 13:48:03 -08001703 * Set the VT scroll region.
1704 *
rginda87b86462011-12-14 13:48:03 -08001705 * This also resets the cursor position to the absolute (0, 0) position, since
1706 * that's what xterm appears to do.
1707 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001708 * Setting the scroll region to the full height of the terminal will clear
1709 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1710 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1711 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1712 * continue to work as most users would expect.
1713 *
rginda87b86462011-12-14 13:48:03 -08001714 * @param {integer} scrollTop The zero-based top of the scroll region.
1715 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1716 * inclusive.
1717 */
1718hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001719 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001720 this.vtScrollTop_ = null;
1721 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001722 } else {
1723 this.vtScrollTop_ = scrollTop;
1724 this.vtScrollBottom_ = scrollBottom;
1725 }
rginda87b86462011-12-14 13:48:03 -08001726};
1727
1728/**
rginda8ba33642011-12-14 12:31:31 -08001729 * Return the top row index according to the VT.
1730 *
1731 * This will return 0 unless the terminal has been told to restrict scrolling
1732 * to some lower row. It is used for some VT cursor positioning and scrolling
1733 * commands.
1734 *
1735 * @return {integer} The topmost row in the terminal's scroll region.
1736 */
1737hterm.Terminal.prototype.getVTScrollTop = function() {
1738 if (this.vtScrollTop_ != null)
1739 return this.vtScrollTop_;
1740
1741 return 0;
rginda87b86462011-12-14 13:48:03 -08001742};
rginda8ba33642011-12-14 12:31:31 -08001743
1744/**
1745 * Return the bottom row index according to the VT.
1746 *
1747 * This will return the height of the terminal unless the it has been told to
1748 * restrict scrolling to some higher row. It is used for some VT cursor
1749 * positioning and scrolling commands.
1750 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001751 * @return {integer} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001752 */
1753hterm.Terminal.prototype.getVTScrollBottom = function() {
1754 if (this.vtScrollBottom_ != null)
1755 return this.vtScrollBottom_;
1756
rginda87b86462011-12-14 13:48:03 -08001757 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001758}
1759
1760/**
1761 * Process a '\n' character.
1762 *
1763 * If the cursor is on the final row of the terminal this will append a new
1764 * blank row to the screen and scroll the topmost row into the scrollback
1765 * buffer.
1766 *
1767 * Otherwise, this moves the cursor to column zero of the next row.
1768 */
1769hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001770 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1771 this.screen_.rowsArray.length - 1);
1772
1773 if (this.vtScrollBottom_ != null) {
1774 // A VT Scroll region is active, we never append new rows.
1775 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1776 // We're at the end of the VT Scroll Region, perform a VT scroll.
1777 this.vtScrollUp(1);
1778 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1779 } else if (cursorAtEndOfScreen) {
1780 // We're at the end of the screen, the only thing to do is put the
1781 // cursor to column 0.
1782 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1783 } else {
1784 // Anywhere else, advance the cursor row, and reset the column.
1785 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1786 }
1787 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001788 // We're at the end of the screen. Append a new row to the terminal,
1789 // shifting the top row into the scrollback.
1790 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001791 } else {
rginda87b86462011-12-14 13:48:03 -08001792 // Anywhere else in the screen just moves the cursor.
1793 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001794 }
1795};
1796
1797/**
1798 * Like newLine(), except maintain the cursor column.
1799 */
1800hterm.Terminal.prototype.lineFeed = function() {
1801 var column = this.screen_.cursorPosition.column;
1802 this.newLine();
1803 this.setCursorColumn(column);
1804};
1805
1806/**
rginda87b86462011-12-14 13:48:03 -08001807 * If autoCarriageReturn is set then newLine(), else lineFeed().
1808 */
1809hterm.Terminal.prototype.formFeed = function() {
1810 if (this.options_.autoCarriageReturn) {
1811 this.newLine();
1812 } else {
1813 this.lineFeed();
1814 }
1815};
1816
1817/**
1818 * Move the cursor up one row, possibly inserting a blank line.
1819 *
1820 * The cursor column is not changed.
1821 */
1822hterm.Terminal.prototype.reverseLineFeed = function() {
1823 var scrollTop = this.getVTScrollTop();
1824 var currentRow = this.screen_.cursorPosition.row;
1825
1826 if (currentRow == scrollTop) {
1827 this.insertLines(1);
1828 } else {
1829 this.setAbsoluteCursorRow(currentRow - 1);
1830 }
1831};
1832
1833/**
rginda8ba33642011-12-14 12:31:31 -08001834 * Replace all characters to the left of the current cursor with the space
1835 * character.
1836 *
1837 * TODO(rginda): This should probably *remove* the characters (not just replace
1838 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001839 * position.
rginda8ba33642011-12-14 12:31:31 -08001840 */
1841hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001842 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001843 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001844 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001845 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001846};
1847
1848/**
David Benjamin684a9b72012-05-01 17:19:58 -04001849 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001850 *
1851 * The cursor position is unchanged.
1852 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001853 * If the current background color is not the default background color this
1854 * will insert spaces rather than delete. This is unfortunate because the
1855 * trailing space will affect text selection, but it's difficult to come up
1856 * with a way to style empty space that wouldn't trip up the hterm.Screen
1857 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001858 *
1859 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1860 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1861 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05001862 *
1863 * @param {number} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08001864 */
1865hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001866 if (this.screen_.cursorPosition.overflow)
1867 return;
1868
Robert Ginda7fd57082012-09-25 14:41:47 -07001869 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1870 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001871
1872 if (this.screen_.textAttributes.background ===
1873 this.screen_.textAttributes.DEFAULT_COLOR) {
1874 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08001875 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07001876 this.screen_.cursorPosition.column + count) {
1877 this.screen_.deleteChars(count);
1878 this.clearCursorOverflow();
1879 return;
1880 }
1881 }
1882
rginda87b86462011-12-14 13:48:03 -08001883 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001884 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001885 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001886 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001887};
1888
1889/**
1890 * Erase the current line.
1891 *
1892 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001893 */
1894hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001895 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001896 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001897 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001898 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001899};
1900
1901/**
David Benjamina08d78f2012-05-05 00:28:49 -04001902 * Erase all characters from the start of the screen to the current cursor
1903 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001904 *
1905 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001906 */
1907hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001908 var cursor = this.saveCursor();
1909
1910 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001911
David Benjamina08d78f2012-05-05 00:28:49 -04001912 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001913 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001914 this.screen_.clearCursorRow();
1915 }
1916
rginda87b86462011-12-14 13:48:03 -08001917 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001918 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001919};
1920
1921/**
1922 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001923 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001924 *
1925 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001926 */
1927hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001928 var cursor = this.saveCursor();
1929
1930 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001931
David Benjamina08d78f2012-05-05 00:28:49 -04001932 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001933 for (var i = cursor.row + 1; i <= bottom; i++) {
1934 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001935 this.screen_.clearCursorRow();
1936 }
1937
rginda87b86462011-12-14 13:48:03 -08001938 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001939 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001940};
1941
1942/**
1943 * Fill the terminal with a given character.
1944 *
1945 * This methods does not respect the VT scroll region.
1946 *
1947 * @param {string} ch The character to use for the fill.
1948 */
1949hterm.Terminal.prototype.fill = function(ch) {
1950 var cursor = this.saveCursor();
1951
1952 this.setAbsoluteCursorPosition(0, 0);
1953 for (var row = 0; row < this.screenSize.height; row++) {
1954 for (var col = 0; col < this.screenSize.width; col++) {
1955 this.setAbsoluteCursorPosition(row, col);
1956 this.screen_.overwriteString(ch);
1957 }
1958 }
1959
1960 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001961};
1962
1963/**
rginda9ea433c2012-03-16 11:57:00 -07001964 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001965 *
rginda9ea433c2012-03-16 11:57:00 -07001966 * This does not respect the scroll region.
1967 *
1968 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1969 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001970 */
rginda9ea433c2012-03-16 11:57:00 -07001971hterm.Terminal.prototype.clearHome = function(opt_screen) {
1972 var screen = opt_screen || this.screen_;
1973 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001974
rginda11057d52012-04-25 12:29:56 -07001975 if (bottom == 0) {
1976 // Empty screen, nothing to do.
1977 return;
1978 }
1979
rgindae4d29232012-01-19 10:47:13 -08001980 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001981 screen.setCursorPosition(i, 0);
1982 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001983 }
1984
rginda9ea433c2012-03-16 11:57:00 -07001985 screen.setCursorPosition(0, 0);
1986};
1987
1988/**
1989 * Erase the entire display without changing the cursor position.
1990 *
1991 * The cursor position is unchanged. This does not respect the scroll
1992 * region.
1993 *
1994 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1995 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001996 */
1997hterm.Terminal.prototype.clear = function(opt_screen) {
1998 var screen = opt_screen || this.screen_;
1999 var cursor = screen.cursorPosition.clone();
2000 this.clearHome(screen);
2001 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002002};
2003
2004/**
2005 * VT command to insert lines at the current cursor row.
2006 *
2007 * This respects the current scroll region. Rows pushed off the bottom are
2008 * lost (they won't show up in the scrollback buffer).
2009 *
rginda8ba33642011-12-14 12:31:31 -08002010 * @param {integer} count The number of lines to insert.
2011 */
2012hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002013 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002014
2015 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002016 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002017
Robert Ginda579186b2012-09-26 11:40:04 -07002018 // The moveCount is the number of rows we need to relocate to make room for
2019 // the new row(s). The count is the distance to move them.
2020 var moveCount = bottom - cursorRow - count + 1;
2021 if (moveCount)
2022 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002023
Robert Ginda579186b2012-09-26 11:40:04 -07002024 for (var i = count - 1; i >= 0; i--) {
2025 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002026 this.screen_.clearCursorRow();
2027 }
rginda8ba33642011-12-14 12:31:31 -08002028};
2029
2030/**
2031 * VT command to delete lines at the current cursor row.
2032 *
2033 * New rows are added to the bottom of scroll region to take their place. New
2034 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002035 *
2036 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002037 */
2038hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002039 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002040
rginda87b86462011-12-14 13:48:03 -08002041 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002042 var bottom = this.getVTScrollBottom();
2043
rginda87b86462011-12-14 13:48:03 -08002044 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002045 count = Math.min(count, maxCount);
2046
rginda87b86462011-12-14 13:48:03 -08002047 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002048 if (count != maxCount)
2049 this.moveRows_(top, count, moveStart);
2050
2051 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002052 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002053 this.screen_.clearCursorRow();
2054 }
2055
rginda87b86462011-12-14 13:48:03 -08002056 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002057 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002058};
2059
2060/**
2061 * Inserts the given number of spaces at the current cursor position.
2062 *
rginda87b86462011-12-14 13:48:03 -08002063 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002064 *
2065 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002066 */
2067hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002068 var cursor = this.saveCursor();
2069
rgindacbbd7482012-06-13 15:06:16 -07002070 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08002071 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08002072 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002073
2074 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002075 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002076};
2077
2078/**
2079 * Forward-delete the specified number of characters starting at the cursor
2080 * position.
2081 *
2082 * @param {integer} count The number of characters to delete.
2083 */
2084hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002085 var deleted = this.screen_.deleteChars(count);
2086 if (deleted && !this.screen_.textAttributes.isDefault()) {
2087 var cursor = this.saveCursor();
2088 this.setCursorColumn(this.screenSize.width - deleted);
2089 this.screen_.insertString(lib.f.getWhitespace(deleted));
2090 this.restoreCursor(cursor);
2091 }
2092
David Benjamin54e8bf62012-06-01 22:31:40 -04002093 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002094};
2095
2096/**
2097 * Shift rows in the scroll region upwards by a given number of lines.
2098 *
2099 * New rows are inserted at the bottom of the scroll region to fill the
2100 * vacated rows. The new rows not filled out with the current text attributes.
2101 *
2102 * This function does not affect the scrollback rows at all. Rows shifted
2103 * off the top are lost.
2104 *
rginda87b86462011-12-14 13:48:03 -08002105 * The cursor position is not altered.
2106 *
rginda8ba33642011-12-14 12:31:31 -08002107 * @param {integer} count The number of rows to scroll.
2108 */
2109hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002110 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002111
rginda87b86462011-12-14 13:48:03 -08002112 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002113 this.deleteLines(count);
2114
rginda87b86462011-12-14 13:48:03 -08002115 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002116};
2117
2118/**
2119 * Shift rows below the cursor down by a given number of lines.
2120 *
2121 * This function respects the current scroll region.
2122 *
2123 * New rows are inserted at the top of the scroll region to fill the
2124 * vacated rows. The new rows not filled out with the current text attributes.
2125 *
2126 * This function does not affect the scrollback rows at all. Rows shifted
2127 * off the bottom are lost.
2128 *
2129 * @param {integer} count The number of rows to scroll.
2130 */
2131hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08002132 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002133
rginda87b86462011-12-14 13:48:03 -08002134 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08002135 this.insertLines(opt_count);
2136
rginda87b86462011-12-14 13:48:03 -08002137 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002138};
2139
rginda87b86462011-12-14 13:48:03 -08002140
rginda8ba33642011-12-14 12:31:31 -08002141/**
2142 * Set the cursor position.
2143 *
2144 * The cursor row is relative to the scroll region if the terminal has
2145 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2146 *
2147 * @param {integer} row The new zero-based cursor row.
2148 * @param {integer} row The new zero-based cursor column.
2149 */
2150hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2151 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002152 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002153 } else {
rginda87b86462011-12-14 13:48:03 -08002154 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002155 }
rginda87b86462011-12-14 13:48:03 -08002156};
rginda8ba33642011-12-14 12:31:31 -08002157
Evan Jones2600d4f2016-12-06 09:29:36 -05002158/**
2159 * Move the cursor relative to its current position.
2160 *
2161 * @param {number} row
2162 * @param {number} column
2163 */
rginda87b86462011-12-14 13:48:03 -08002164hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2165 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002166 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2167 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002168 this.screen_.setCursorPosition(row, column);
2169};
2170
Evan Jones2600d4f2016-12-06 09:29:36 -05002171/**
2172 * Move the cursor to the specified position.
2173 *
2174 * @param {number} row
2175 * @param {number} column
2176 */
rginda87b86462011-12-14 13:48:03 -08002177hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002178 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2179 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002180 this.screen_.setCursorPosition(row, column);
2181};
2182
2183/**
2184 * Set the cursor column.
2185 *
2186 * @param {integer} column The new zero-based cursor column.
2187 */
2188hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002189 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002190};
2191
2192/**
2193 * Return the cursor column.
2194 *
2195 * @return {integer} The zero-based cursor column.
2196 */
2197hterm.Terminal.prototype.getCursorColumn = function() {
2198 return this.screen_.cursorPosition.column;
2199};
2200
2201/**
2202 * Set the cursor row.
2203 *
2204 * The cursor row is relative to the scroll region if the terminal has
2205 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2206 *
2207 * @param {integer} row The new cursor row.
2208 */
rginda87b86462011-12-14 13:48:03 -08002209hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2210 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002211};
2212
2213/**
2214 * Return the cursor row.
2215 *
2216 * @return {integer} The zero-based cursor row.
2217 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002218hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002219 return this.screen_.cursorPosition.row;
2220};
2221
2222/**
2223 * Request that the ScrollPort redraw itself soon.
2224 *
2225 * The redraw will happen asynchronously, soon after the call stack winds down.
2226 * Multiple calls will be coalesced into a single redraw.
2227 */
2228hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002229 if (this.timeouts_.redraw)
2230 return;
rginda8ba33642011-12-14 12:31:31 -08002231
2232 var self = this;
rginda87b86462011-12-14 13:48:03 -08002233 this.timeouts_.redraw = setTimeout(function() {
2234 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002235 self.scrollPort_.redraw_();
2236 }, 0);
2237};
2238
2239/**
2240 * Request that the ScrollPort be scrolled to the bottom.
2241 *
2242 * The scroll will happen asynchronously, soon after the call stack winds down.
2243 * Multiple calls will be coalesced into a single scroll.
2244 *
2245 * This affects the scrollbar position of the ScrollPort, and has nothing to
2246 * do with the VT scroll commands.
2247 */
2248hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2249 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002250 return;
rginda8ba33642011-12-14 12:31:31 -08002251
2252 var self = this;
2253 this.timeouts_.scrollDown = setTimeout(function() {
2254 delete self.timeouts_.scrollDown;
2255 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2256 }, 10);
2257};
2258
2259/**
2260 * Move the cursor up a specified number of rows.
2261 *
2262 * @param {integer} count The number of rows to move the cursor.
2263 */
2264hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002265 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002266};
2267
2268/**
2269 * Move the cursor down a specified number of rows.
2270 *
2271 * @param {integer} count The number of rows to move the cursor.
2272 */
2273hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002274 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002275 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2276 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2277 this.screenSize.height - 1);
2278
rgindacbbd7482012-06-13 15:06:16 -07002279 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002280 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002281 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002282};
2283
2284/**
2285 * Move the cursor left a specified number of columns.
2286 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002287 * If reverse wraparound mode is enabled and the previous row wrapped into
2288 * the current row then we back up through the wraparound as well.
2289 *
rginda8ba33642011-12-14 12:31:31 -08002290 * @param {integer} count The number of columns to move the cursor.
2291 */
2292hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002293 count = count || 1;
2294
2295 if (count < 1)
2296 return;
2297
2298 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002299 if (this.options_.reverseWraparound) {
2300 if (this.screen_.cursorPosition.overflow) {
2301 // If this cursor is in the right margin, consume one count to get it
2302 // back to the last column. This only applies when we're in reverse
2303 // wraparound mode.
2304 count--;
2305 this.clearCursorOverflow();
2306
2307 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002308 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002309 }
2310
Robert Gindabfb32622014-07-17 13:20:27 -07002311 var newRow = this.screen_.cursorPosition.row;
2312 var newColumn = currentColumn - count;
2313 if (newColumn < 0) {
2314 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2315 if (newRow < 0) {
2316 // xterm also wraps from row 0 to the last row.
2317 newRow = this.screenSize.height + newRow % this.screenSize.height;
2318 }
2319 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2320 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002321
Robert Gindabfb32622014-07-17 13:20:27 -07002322 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2323
2324 } else {
2325 var newColumn = Math.max(currentColumn - count, 0);
2326 this.setCursorColumn(newColumn);
2327 }
rginda8ba33642011-12-14 12:31:31 -08002328};
2329
2330/**
2331 * Move the cursor right a specified number of columns.
2332 *
2333 * @param {integer} count The number of columns to move the cursor.
2334 */
2335hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002336 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002337
2338 if (count < 1)
2339 return;
2340
rgindacbbd7482012-06-13 15:06:16 -07002341 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002342 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002343 this.setCursorColumn(column);
2344};
2345
2346/**
2347 * Reverse the foreground and background colors of the terminal.
2348 *
2349 * This only affects text that was drawn with no attributes.
2350 *
2351 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2352 * been drawn with attributes that happen to coincide with the default
2353 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002354 *
2355 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002356 */
2357hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002358 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002359 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002360 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2361 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002362 } else {
rginda9f5222b2012-03-05 11:53:28 -08002363 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2364 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002365 }
2366};
2367
2368/**
rginda87b86462011-12-14 13:48:03 -08002369 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002370 *
2371 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002372 */
2373hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002374 this.cursorNode_.style.backgroundColor =
2375 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002376
2377 var self = this;
2378 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08002379 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08002380 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002381
Michael Kelly485ecd12014-06-09 11:41:56 -04002382 // bellSquelchTimeout_ affects both audio and notification bells.
2383 if (this.bellSquelchTimeout_)
2384 return;
2385
Robert Ginda92e18102013-03-14 13:56:37 -07002386 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002387 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002388 this.bellSequelchTimeout_ = setTimeout(function() {
2389 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002390 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002391 } else {
2392 delete this.bellSquelchTimeout_;
2393 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002394
2395 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002396 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002397 this.bellNotificationList_.push(n);
2398 // TODO: Should we try to raise the window here?
2399 n.onclick = function() { self.closeBellNotifications_(); };
2400 }
rginda87b86462011-12-14 13:48:03 -08002401};
2402
2403/**
rginda8ba33642011-12-14 12:31:31 -08002404 * Set the origin mode bit.
2405 *
2406 * If origin mode is on, certain VT cursor and scrolling commands measure their
2407 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2408 * to the top of the addressable screen.
2409 *
2410 * Defaults to off.
2411 *
2412 * @param {boolean} state True to set origin mode, false to unset.
2413 */
2414hterm.Terminal.prototype.setOriginMode = function(state) {
2415 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002416 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002417};
2418
2419/**
2420 * Set the insert mode bit.
2421 *
2422 * If insert mode is on, existing text beyond the cursor position will be
2423 * shifted right to make room for new text. Otherwise, new text overwrites
2424 * any existing text.
2425 *
2426 * Defaults to off.
2427 *
2428 * @param {boolean} state True to set insert mode, false to unset.
2429 */
2430hterm.Terminal.prototype.setInsertMode = function(state) {
2431 this.options_.insertMode = state;
2432};
2433
2434/**
rginda87b86462011-12-14 13:48:03 -08002435 * Set the auto carriage return bit.
2436 *
2437 * If auto carriage return is on then a formfeed character is interpreted
2438 * as a newline, otherwise it's the same as a linefeed. The difference boils
2439 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002440 *
2441 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002442 */
2443hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2444 this.options_.autoCarriageReturn = state;
2445};
2446
2447/**
rginda8ba33642011-12-14 12:31:31 -08002448 * Set the wraparound mode bit.
2449 *
2450 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2451 * to the start of the following row. Otherwise, the cursor is clamped to the
2452 * end of the screen and attempts to write past it are ignored.
2453 *
2454 * Defaults to on.
2455 *
2456 * @param {boolean} state True to set wraparound mode, false to unset.
2457 */
2458hterm.Terminal.prototype.setWraparound = function(state) {
2459 this.options_.wraparound = state;
2460};
2461
2462/**
2463 * Set the reverse-wraparound mode bit.
2464 *
2465 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2466 * to the end of the previous row. Otherwise, the cursor is clamped to column
2467 * 0.
2468 *
2469 * Defaults to off.
2470 *
2471 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2472 */
2473hterm.Terminal.prototype.setReverseWraparound = function(state) {
2474 this.options_.reverseWraparound = state;
2475};
2476
2477/**
2478 * Selects between the primary and alternate screens.
2479 *
2480 * If alternate mode is on, the alternate screen is active. Otherwise the
2481 * primary screen is active.
2482 *
2483 * Swapping screens has no effect on the scrollback buffer.
2484 *
2485 * Each screen maintains its own cursor position.
2486 *
2487 * Defaults to off.
2488 *
2489 * @param {boolean} state True to set alternate mode, false to unset.
2490 */
2491hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002492 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002493 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2494
rginda35c456b2012-02-09 17:29:05 -08002495 if (this.screen_.rowsArray.length &&
2496 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2497 // If the screen changed sizes while we were away, our rowIndexes may
2498 // be incorrect.
2499 var offset = this.scrollbackRows_.length;
2500 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002501 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002502 ary[i].rowIndex = offset + i;
2503 }
2504 }
rginda8ba33642011-12-14 12:31:31 -08002505
rginda35c456b2012-02-09 17:29:05 -08002506 this.realizeWidth_(this.screenSize.width);
2507 this.realizeHeight_(this.screenSize.height);
2508 this.scrollPort_.syncScrollHeight();
2509 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002510
rginda6d397402012-01-17 10:58:29 -08002511 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002512 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002513};
2514
2515/**
2516 * Set the cursor-blink mode bit.
2517 *
2518 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2519 * a visible cursor does not blink.
2520 *
2521 * You should make sure to turn blinking off if you're going to dispose of a
2522 * terminal, otherwise you'll leak a timeout.
2523 *
2524 * Defaults to on.
2525 *
2526 * @param {boolean} state True to set cursor-blink mode, false to unset.
2527 */
2528hterm.Terminal.prototype.setCursorBlink = function(state) {
2529 this.options_.cursorBlink = state;
2530
2531 if (!state && this.timeouts_.cursorBlink) {
2532 clearTimeout(this.timeouts_.cursorBlink);
2533 delete this.timeouts_.cursorBlink;
2534 }
2535
2536 if (this.options_.cursorVisible)
2537 this.setCursorVisible(true);
2538};
2539
2540/**
2541 * Set the cursor-visible mode bit.
2542 *
2543 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2544 *
2545 * Defaults to on.
2546 *
2547 * @param {boolean} state True to set cursor-visible mode, false to unset.
2548 */
2549hterm.Terminal.prototype.setCursorVisible = function(state) {
2550 this.options_.cursorVisible = state;
2551
2552 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002553 if (this.timeouts_.cursorBlink) {
2554 clearTimeout(this.timeouts_.cursorBlink);
2555 delete this.timeouts_.cursorBlink;
2556 }
rginda87b86462011-12-14 13:48:03 -08002557 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002558 return;
2559 }
2560
rginda87b86462011-12-14 13:48:03 -08002561 this.syncCursorPosition_();
2562
2563 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002564
2565 if (this.options_.cursorBlink) {
2566 if (this.timeouts_.cursorBlink)
2567 return;
2568
Robert Gindaea2183e2014-07-17 09:51:51 -07002569 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002570 } else {
2571 if (this.timeouts_.cursorBlink) {
2572 clearTimeout(this.timeouts_.cursorBlink);
2573 delete this.timeouts_.cursorBlink;
2574 }
2575 }
2576};
2577
2578/**
rginda87b86462011-12-14 13:48:03 -08002579 * Synchronizes the visible cursor and document selection with the current
2580 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002581 */
2582hterm.Terminal.prototype.syncCursorPosition_ = function() {
2583 var topRowIndex = this.scrollPort_.getTopRowIndex();
2584 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2585 var cursorRowIndex = this.scrollbackRows_.length +
2586 this.screen_.cursorPosition.row;
2587
2588 if (cursorRowIndex > bottomRowIndex) {
2589 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002590 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002591 return;
2592 }
2593
Robert Gindab837c052014-08-11 11:17:51 -07002594 if (this.options_.cursorVisible &&
2595 this.cursorNode_.style.display == 'none') {
2596 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2597 this.cursorNode_.style.display = '';
2598 }
2599
2600
rginda8ba33642011-12-14 12:31:31 -08002601 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002602 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2603 'px';
2604 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2605 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002606
2607 this.cursorNode_.setAttribute('title',
2608 '(' + this.screen_.cursorPosition.row +
2609 ', ' + this.screen_.cursorPosition.column +
2610 ')');
2611
2612 // Update the caret for a11y purposes.
2613 var selection = this.document_.getSelection();
2614 if (selection && selection.isCollapsed)
2615 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002616};
2617
Robert Gindafb1be6a2013-12-11 11:56:22 -08002618/**
2619 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2620 * and character cell dimensions.
2621 */
Robert Ginda830583c2013-08-07 13:20:46 -07002622hterm.Terminal.prototype.restyleCursor_ = function() {
2623 var shape = this.cursorShape_;
2624
2625 if (this.cursorNode_.getAttribute('focus') == 'false') {
2626 // Always show a block cursor when unfocused.
2627 shape = hterm.Terminal.cursorShape.BLOCK;
2628 }
2629
2630 var style = this.cursorNode_.style;
2631
2632 switch (shape) {
2633 case hterm.Terminal.cursorShape.BEAM:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002634 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002635 style.backgroundColor = 'transparent';
2636 style.borderBottomStyle = null;
2637 style.borderLeftStyle = 'solid';
2638 break;
2639
2640 case hterm.Terminal.cursorShape.UNDERLINE:
2641 style.height = this.scrollPort_.characterSize.baseline + 'px';
2642 style.backgroundColor = 'transparent';
2643 style.borderBottomStyle = 'solid';
2644 // correct the size to put it exactly at the baseline
2645 style.borderLeftStyle = null;
2646 break;
2647
2648 default:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002649 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002650 style.backgroundColor = this.cursorColor_;
2651 style.borderBottomStyle = null;
2652 style.borderLeftStyle = null;
2653 break;
2654 }
2655};
2656
rginda8ba33642011-12-14 12:31:31 -08002657/**
2658 * Synchronizes the visible cursor with the current cursor coordinates.
2659 *
2660 * The sync will happen asynchronously, soon after the call stack winds down.
2661 * Multiple calls will be coalesced into a single sync.
2662 */
2663hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2664 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002665 return;
rginda8ba33642011-12-14 12:31:31 -08002666
2667 var self = this;
2668 this.timeouts_.syncCursor = setTimeout(function() {
2669 self.syncCursorPosition_();
2670 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002671 }, 0);
2672};
2673
rgindacc2996c2012-02-24 14:59:31 -08002674/**
rgindaf522ce02012-04-17 17:49:17 -07002675 * Show or hide the zoom warning.
2676 *
2677 * The zoom warning is a message warning the user that their browser zoom must
2678 * be set to 100% in order for hterm to function properly.
2679 *
2680 * @param {boolean} state True to show the message, false to hide it.
2681 */
2682hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2683 if (!this.zoomWarningNode_) {
2684 if (!state)
2685 return;
2686
2687 this.zoomWarningNode_ = this.document_.createElement('div');
2688 this.zoomWarningNode_.style.cssText = (
2689 'color: black;' +
2690 'background-color: #ff2222;' +
2691 'font-size: large;' +
2692 'border-radius: 8px;' +
2693 'opacity: 0.75;' +
2694 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2695 'top: 0.5em;' +
2696 'right: 1.2em;' +
2697 'position: absolute;' +
2698 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002699 '-webkit-user-select: none;' +
2700 '-moz-text-size-adjust: none;' +
2701 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002702
2703 this.zoomWarningNode_.addEventListener('click', function(e) {
2704 this.parentNode.removeChild(this);
2705 });
rgindaf522ce02012-04-17 17:49:17 -07002706 }
2707
Robert Gindab4839c22013-02-28 16:52:10 -08002708 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2709 hterm.zoomWarningMessage,
2710 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2711
rgindaf522ce02012-04-17 17:49:17 -07002712 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2713
2714 if (state) {
2715 if (!this.zoomWarningNode_.parentNode)
2716 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2717 } else if (this.zoomWarningNode_.parentNode) {
2718 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2719 }
2720};
2721
2722/**
rgindacc2996c2012-02-24 14:59:31 -08002723 * Show the terminal overlay for a given amount of time.
2724 *
2725 * The terminal overlay appears in inverse video in a large font, centered
2726 * over the terminal. You should probably keep the overlay message brief,
2727 * since it's in a large font and you probably aren't going to check the size
2728 * of the terminal first.
2729 *
2730 * @param {string} msg The text (not HTML) message to display in the overlay.
2731 * @param {number} opt_timeout The amount of time to wait before fading out
2732 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2733 * stay up forever (or until the next overlay).
2734 */
2735hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002736 if (!this.overlayNode_) {
2737 if (!this.div_)
2738 return;
2739
2740 this.overlayNode_ = this.document_.createElement('div');
2741 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002742 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002743 'font-size: xx-large;' +
2744 'opacity: 0.75;' +
2745 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2746 'position: absolute;' +
2747 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002748 '-webkit-transition: opacity 180ms ease-in;' +
2749 '-moz-user-select: none;' +
2750 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002751
2752 this.overlayNode_.addEventListener('mousedown', function(e) {
2753 e.preventDefault();
2754 e.stopPropagation();
2755 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002756 }
2757
rginda9f5222b2012-03-05 11:53:28 -08002758 this.overlayNode_.style.color = this.prefs_.get('background-color');
2759 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2760 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2761
rgindaf0090c92012-02-10 14:58:52 -08002762 this.overlayNode_.textContent = msg;
2763 this.overlayNode_.style.opacity = '0.75';
2764
2765 if (!this.overlayNode_.parentNode)
2766 this.div_.appendChild(this.overlayNode_);
2767
Robert Ginda97769282013-02-01 15:30:30 -08002768 var divSize = hterm.getClientSize(this.div_);
2769 var overlaySize = hterm.getClientSize(this.overlayNode_);
2770
Robert Ginda8a59f762014-07-23 11:29:55 -07002771 this.overlayNode_.style.top =
2772 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08002773 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07002774 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08002775
2776 var self = this;
2777
2778 if (this.overlayTimeout_)
2779 clearTimeout(this.overlayTimeout_);
2780
rgindacc2996c2012-02-24 14:59:31 -08002781 if (opt_timeout === null)
2782 return;
2783
rgindaf0090c92012-02-10 14:58:52 -08002784 this.overlayTimeout_ = setTimeout(function() {
2785 self.overlayNode_.style.opacity = '0';
Robert Ginda70926e42013-11-25 14:56:36 -08002786 self.overlayTimeout_ = setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002787 if (self.overlayNode_.parentNode)
2788 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002789 self.overlayTimeout_ = null;
2790 self.overlayNode_.style.opacity = '0.75';
2791 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002792 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002793};
2794
rginda4bba5e12012-06-20 16:15:30 -07002795/**
2796 * Paste from the system clipboard to the terminal.
2797 */
2798hterm.Terminal.prototype.paste = function() {
2799 hterm.pasteFromClipboard(this.document_);
2800};
2801
2802/**
2803 * Copy a string to the system clipboard.
2804 *
2805 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05002806 *
2807 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07002808 */
2809hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002810 if (this.prefs_.get('enable-clipboard-notice'))
2811 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2812
rgindaa09e7332012-08-17 12:49:51 -07002813 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002814 copySource.textContent = str;
2815 copySource.style.cssText = (
2816 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002817 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07002818 'position: absolute;' +
2819 'top: -99px');
2820
2821 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002822
rginda4bba5e12012-06-20 16:15:30 -07002823 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002824 var anchorNode = selection.anchorNode;
2825 var anchorOffset = selection.anchorOffset;
2826 var focusNode = selection.focusNode;
2827 var focusOffset = selection.focusOffset;
2828
rginda4bba5e12012-06-20 16:15:30 -07002829 selection.selectAllChildren(copySource);
2830
rgindaa09e7332012-08-17 12:49:51 -07002831 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002832
Rob Spies56953412014-04-28 14:09:47 -07002833 // IE doesn't support selection.extend. This means that the selection
2834 // won't return on IE.
Robert Ginda6f4f0aa2014-07-28 10:21:39 -07002835 if (selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07002836 selection.collapse(anchorNode, anchorOffset);
2837 selection.extend(focusNode, focusOffset);
2838 }
rgindafaa74742012-08-21 13:34:03 -07002839
rginda4bba5e12012-06-20 16:15:30 -07002840 copySource.parentNode.removeChild(copySource);
2841};
2842
Evan Jones2600d4f2016-12-06 09:29:36 -05002843/**
2844 * Returns the selected text, or null if no text is selected.
2845 *
2846 * @return {string|null}
2847 */
rgindaa09e7332012-08-17 12:49:51 -07002848hterm.Terminal.prototype.getSelectionText = function() {
2849 var selection = this.scrollPort_.selection;
2850 selection.sync();
2851
2852 if (selection.isCollapsed)
2853 return null;
2854
2855
2856 // Start offset measures from the beginning of the line.
2857 var startOffset = selection.startOffset;
2858 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002859
Robert Gindafdbb3f22012-09-06 20:23:06 -07002860 if (node.nodeName != 'X-ROW') {
2861 // If the selection doesn't start on an x-row node, then it must be
2862 // somewhere inside the x-row. Add any characters from previous siblings
2863 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002864
2865 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2866 // If node is the text node in a styled span, move up to the span node.
2867 node = node.parentNode;
2868 }
2869
Robert Gindafdbb3f22012-09-06 20:23:06 -07002870 while (node.previousSibling) {
2871 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002872 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002873 }
rgindaa09e7332012-08-17 12:49:51 -07002874 }
2875
2876 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002877 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
2878 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05002879 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002880
Robert Gindafdbb3f22012-09-06 20:23:06 -07002881 if (node.nodeName != 'X-ROW') {
2882 // If the selection doesn't end on an x-row node, then it must be
2883 // somewhere inside the x-row. Add any characters from following siblings
2884 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002885
2886 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2887 // If node is the text node in a styled span, move up to the span node.
2888 node = node.parentNode;
2889 }
2890
Robert Gindafdbb3f22012-09-06 20:23:06 -07002891 while (node.nextSibling) {
2892 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002893 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002894 }
rgindaa09e7332012-08-17 12:49:51 -07002895 }
2896
2897 var rv = this.getRowsText(selection.startRow.rowIndex,
2898 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002899 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002900};
2901
rginda4bba5e12012-06-20 16:15:30 -07002902/**
2903 * Copy the current selection to the system clipboard, then clear it after a
2904 * short delay.
2905 */
2906hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002907 var text = this.getSelectionText();
2908 if (text != null)
2909 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002910};
2911
rgindaf0090c92012-02-10 14:58:52 -08002912hterm.Terminal.prototype.overlaySize = function() {
2913 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2914};
2915
rginda87b86462011-12-14 13:48:03 -08002916/**
2917 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2918 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07002919 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08002920 */
2921hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002922 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002923 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2924
Robert Ginda8cb7d902013-06-20 14:37:18 -07002925 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08002926};
2927
2928/**
Mike Frysinger70b94692017-01-26 18:57:50 -10002929 * Launches url in a new tab.
2930 *
2931 * @param {string} url URL to launch in a new tab.
2932 */
2933hterm.Terminal.prototype.openUrl = function(url) {
Mike Frysingerac437a12017-07-13 02:35:59 -04002934 if (window.chrome && window.chrome.browser) {
2935 // For Chrome v2 apps, we need to use this API to properly open windows.
2936 chrome.browser.openTab({'url': url});
2937 } else {
2938 var win = window.open(url, '_blank');
2939 win.focus();
2940 }
Mike Frysinger70b94692017-01-26 18:57:50 -10002941}
2942
2943/**
2944 * Open the selected url.
2945 */
2946hterm.Terminal.prototype.openSelectedUrl_ = function() {
2947 var str = this.getSelectionText();
2948
2949 // If there is no selection, try and expand wherever they clicked.
2950 if (str == null) {
2951 this.screen_.expandSelection(this.document_.getSelection());
2952 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04002953
2954 // If clicking in empty space, return.
2955 if (str == null)
2956 return;
Mike Frysinger70b94692017-01-26 18:57:50 -10002957 }
2958
2959 // Make sure URL is valid before opening.
2960 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
2961 return;
Mike Frysinger43472622017-06-26 18:11:07 -04002962
2963 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10002964 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04002965 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
2966 // We have to whitelist a few protocols that lack authorities and thus
2967 // never use the //. Like mailto.
2968 switch (str.split(':', 1)[0]) {
2969 case 'mailto':
2970 break;
2971 default:
2972 str = 'http://' + str;
2973 break;
2974 }
2975 }
Mike Frysinger70b94692017-01-26 18:57:50 -10002976
2977 this.openUrl(str);
2978}
2979
2980
2981/**
rgindad5613292012-06-19 15:40:37 -07002982 * Add the terminalRow and terminalColumn properties to mouse events and
2983 * then forward on to onMouse().
2984 *
2985 * The terminalRow and terminalColumn properties contain the (row, column)
2986 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05002987 *
2988 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07002989 */
2990hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002991 if (e.processedByTerminalHandler_) {
2992 // We register our event handlers on the document, as well as the cursor
2993 // and the scroll blocker. Mouse events that occur on the cursor or
2994 // scroll blocker will also appear on the document, but we don't want to
2995 // process them twice.
2996 //
2997 // We can't just prevent bubbling because that has other side effects, so
2998 // we decorate the event object with this property instead.
2999 return;
3000 }
3001
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003002 var reportMouseEvents = (!this.defeatMouseReports_ &&
3003 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3004
rgindafaa74742012-08-21 13:34:03 -07003005 e.processedByTerminalHandler_ = true;
3006
Robert Gindaeda48db2014-07-17 09:25:30 -07003007 // One based row/column stored on the mouse event.
3008 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
3009 this.scrollPort_.characterSize.height) + 1;
3010 e.terminalColumn = parseInt(e.clientX /
3011 this.scrollPort_.characterSize.width) + 1;
3012
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003013 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3014 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003015 return;
3016 }
3017
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003018 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003019 // If the cursor is visible and we're not sending mouse events to the
3020 // host app, then we want to hide the terminal cursor when the mouse
3021 // cursor is over top. This keeps the terminal cursor from interfering
3022 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003023 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3024 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3025 this.cursorNode_.style.display = 'none';
3026 } else if (this.cursorNode_.style.display == 'none') {
3027 this.cursorNode_.style.display = '';
3028 }
3029 }
rgindad5613292012-06-19 15:40:37 -07003030
Robert Ginda928cf632014-03-05 15:07:41 -08003031 if (e.type == 'mousedown') {
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003032 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003033 // If VT mouse reporting is disabled, or has been defeated with
3034 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003035 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003036 this.setSelectionEnabled(true);
3037 } else {
3038 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003039 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003040 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003041 this.setSelectionEnabled(false);
3042 e.preventDefault();
3043 }
3044 }
3045
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003046 if (!reportMouseEvents) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003047 if (e.type == 'dblclick' && this.copyOnSelect) {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003048 this.screen_.expandSelection(this.document_.getSelection());
Robert Ginda15ed4902016-07-12 10:43:22 -07003049 this.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07003050 }
3051
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003052 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003053 // Debounce this event with the dblclick event. If you try to doubleclick
3054 // a URL to open it, Chrome will fire click then dblclick, but we won't
3055 // have expanded the selection text at the first click event.
3056 clearTimeout(this.timeouts_.openUrl);
3057 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3058 500);
3059 return;
3060 }
3061
Mike Frysinger847577f2017-05-23 23:25:57 -04003062 if (e.type == 'mousedown') {
3063 if ((this.mouseRightClickPaste && e.button == 2 /* right button */) ||
Mike Frysinger2edd3612017-05-24 00:54:39 -04003064 e.button == this.mousePasteButton) {
Mike Frysinger847577f2017-05-23 23:25:57 -04003065 this.paste();
3066 }
3067 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003068
Mike Frysinger2edd3612017-05-24 00:54:39 -04003069 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003070 !this.document_.getSelection().isCollapsed) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003071 this.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003072 }
3073
3074 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3075 this.scrollBlockerNode_.engaged) {
3076 // Disengage the scroll-blocker after one of these events.
3077 this.scrollBlockerNode_.engaged = false;
3078 this.scrollBlockerNode_.style.top = '-99px';
3079 }
3080
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003081 // Emulate arrow key presses via scroll wheel events.
3082 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3083 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003084 if (e.type == 'wheel') {
3085 var delta = this.scrollPort_.scrollWheelDelta(e);
3086 var lines = lib.f.smartFloorDivide(
3087 Math.abs(delta), this.scrollPort_.characterSize.height);
3088
3089 var data = '\x1bO' + (delta < 0 ? 'B' : 'A');
3090 this.io.sendString(data.repeat(lines));
3091
3092 e.preventDefault();
3093 }
3094 }
Robert Ginda928cf632014-03-05 15:07:41 -08003095 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003096 if (!this.scrollBlockerNode_.engaged) {
3097 if (e.type == 'mousedown') {
3098 // Move the scroll-blocker into place if we want to keep the scrollport
3099 // from scrolling.
3100 this.scrollBlockerNode_.engaged = true;
3101 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3102 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3103 } else if (e.type == 'mousemove') {
3104 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3105 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003106 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003107 e.preventDefault();
3108 }
3109 }
Robert Ginda928cf632014-03-05 15:07:41 -08003110
3111 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003112 }
3113
Robert Ginda928cf632014-03-05 15:07:41 -08003114 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3115 // Restore this on mouseup in case it was temporarily defeated with a
3116 // alt-mousedown. Only do this when the selection is empty so that
3117 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003118 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003119 }
rgindad5613292012-06-19 15:40:37 -07003120};
3121
3122/**
3123 * Clients should override this if they care to know about mouse events.
3124 *
3125 * The event parameter will be a normal DOM mouse click event with additional
3126 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003127 *
3128 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003129 */
3130hterm.Terminal.prototype.onMouse = function(e) { };
3131
3132/**
rginda8e92a692012-05-20 19:37:20 -07003133 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003134 *
3135 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003136 */
Rob Spies06533ba2014-04-24 11:20:37 -07003137hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3138 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003139 this.restyleCursor_();
Michael Kelly485ecd12014-06-09 11:41:56 -04003140 if (focused === true)
3141 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003142};
3143
3144/**
rginda8ba33642011-12-14 12:31:31 -08003145 * React when the ScrollPort is scrolled.
3146 */
3147hterm.Terminal.prototype.onScroll_ = function() {
3148 this.scheduleSyncCursorPosition_();
3149};
3150
3151/**
rginda9846e2f2012-01-27 13:53:33 -08003152 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003153 *
3154 * @param {Event} e The DOM paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003155 */
3156hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07003157 var data = e.text.replace(/\n/mg, '\r');
Connor Hegartyf525ceb2014-09-03 13:36:03 -07003158 data = this.keyboard.encode(data);
Robert Gindaa063b202014-07-21 11:08:25 -07003159 if (this.options_.bracketedPaste)
3160 data = '\x1b[200~' + data + '\x1b[201~';
3161
3162 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003163};
3164
3165/**
rgindaa09e7332012-08-17 12:49:51 -07003166 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003167 *
3168 * @param {Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003169 */
3170hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003171 if (!this.useDefaultWindowCopy) {
3172 e.preventDefault();
3173 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3174 }
rgindaa09e7332012-08-17 12:49:51 -07003175};
3176
3177/**
rginda8ba33642011-12-14 12:31:31 -08003178 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003179 *
3180 * Note: This function should not directly contain code that alters the internal
3181 * state of the terminal. That kind of code belongs in realizeWidth or
3182 * realizeHeight, so that it can be executed synchronously in the case of a
3183 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003184 */
3185hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003186 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003187 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003188 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003189 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003190
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003191 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003192 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003193 // gets removed from the document or during the initial load, and we can't
3194 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003195 // This can also happen if called before the scrollPort calculates the
3196 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003197 return;
3198 }
3199
rgindaa8ba17d2012-08-15 14:41:10 -07003200 var isNewSize = (columnCount != this.screenSize.width ||
3201 rowCount != this.screenSize.height);
3202
3203 // We do this even if the size didn't change, just to be sure everything is
3204 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003205 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003206 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003207
3208 if (isNewSize)
3209 this.overlaySize();
3210
Robert Gindafb1be6a2013-12-11 11:56:22 -08003211 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003212 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08003213};
3214
3215/**
3216 * Service the cursor blink timeout.
3217 */
3218hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003219 if (!this.options_.cursorBlink) {
3220 delete this.timeouts_.cursorBlink;
3221 return;
3222 }
3223
Robert Ginda830583c2013-08-07 13:20:46 -07003224 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3225 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003226 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003227 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3228 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003229 } else {
rginda87b86462011-12-14 13:48:03 -08003230 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003231 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3232 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003233 }
3234};
David Reveman8f552492012-03-28 12:18:41 -04003235
3236/**
3237 * Set the scrollbar-visible mode bit.
3238 *
3239 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3240 * Otherwise it will not.
3241 *
3242 * Defaults to on.
3243 *
3244 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3245 */
3246hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3247 this.scrollPort_.setScrollbarVisible(state);
3248};
Michael Kelly485ecd12014-06-09 11:41:56 -04003249
3250/**
Rob Spies49039e52014-12-17 13:40:04 -08003251 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003252 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003253 *
3254 * Defaults to 1.
3255 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003256 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003257 */
3258hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3259 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3260};
3261
3262/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003263 * Close all web notifications created by terminal bells.
3264 */
3265hterm.Terminal.prototype.closeBellNotifications_ = function() {
3266 this.bellNotificationList_.forEach(function(n) {
3267 n.close();
3268 });
3269 this.bellNotificationList_.length = 0;
3270};