blob: 5bdd7fdb7fc73893432d3d958082f504b5e979e3 [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;
rginda9f5222b2012-03-05 11:53:28 -0800100
Robert Ginda6aec7eb2015-06-16 10:31:30 -0700101 // True if we should override mouse event reporting to allow local selection.
102 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -0800103
rgindaf0090c92012-02-10 14:58:52 -0800104 // Terminal bell sound.
105 this.bellAudio_ = this.document_.createElement('audio');
rgindaf0090c92012-02-10 14:58:52 -0800106 this.bellAudio_.setAttribute('preload', 'auto');
107
Michael Kelly485ecd12014-06-09 11:41:56 -0400108 // All terminal bell notifications that have been generated (not necessarily
109 // shown).
110 this.bellNotificationList_ = [];
111
112 // Whether we have permission to display notifications.
113 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400114
rginda6d397402012-01-17 10:58:29 -0800115 // Cursor position and attributes saved with DECSC.
116 this.savedOptions_ = {};
117
rginda8ba33642011-12-14 12:31:31 -0800118 // The current mode bits for the terminal.
119 this.options_ = new hterm.Options();
120
121 // Timeouts we might need to clear.
122 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800123
124 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800125 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800126
Zhu Qunying30d40712017-03-14 16:27:00 -0700127 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800128 this.keyboard = new hterm.Keyboard(this);
129
rginda87b86462011-12-14 13:48:03 -0800130 // General IO interface that can be given to third parties without exposing
131 // the entire terminal object.
132 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800133
rgindad5613292012-06-19 15:40:37 -0700134 // True if mouse-click-drag should scroll the terminal.
135 this.enableMouseDragScroll = true;
136
Robert Ginda57f03b42012-09-13 11:02:48 -0700137 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400138 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700139 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700140
Zhu Qunying30d40712017-03-14 16:27:00 -0700141 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700142 this.useDefaultWindowCopy = false;
143
144 this.clearSelectionAfterCopy = true;
145
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400146 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800147 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700148
149 this.setProfile(opt_profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500150 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800151};
152
153/**
Robert Ginda830583c2013-08-07 13:20:46 -0700154 * Possible cursor shapes.
155 */
156hterm.Terminal.cursorShape = {
157 BLOCK: 'BLOCK',
158 BEAM: 'BEAM',
159 UNDERLINE: 'UNDERLINE'
160};
161
162/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700163 * Clients should override this to be notified when the terminal is ready
164 * for use.
165 *
166 * The terminal initialization is asynchronous, and shouldn't be used before
167 * this method is called.
168 */
169hterm.Terminal.prototype.onTerminalReady = function() { };
170
171/**
rginda35c456b2012-02-09 17:29:05 -0800172 * Default tab with of 8 to match xterm.
173 */
174hterm.Terminal.prototype.tabWidth = 8;
175
176/**
rginda9f5222b2012-03-05 11:53:28 -0800177 * Select a preference profile.
178 *
179 * This will load the terminal preferences for the given profile name and
180 * associate subsequent preference changes with the new preference profile.
181 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500182 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800183 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700184 * @param {function} opt_callback Optional callback to invoke when the profile
185 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800186 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700187hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
188 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800189
Robert Ginda57f03b42012-09-13 11:02:48 -0700190 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800191
Robert Ginda57f03b42012-09-13 11:02:48 -0700192 if (this.prefs_)
193 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800194
Robert Ginda57f03b42012-09-13 11:02:48 -0700195 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
196 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800197 'alt-gr-mode': function(v) {
198 if (v == null) {
199 if (navigator.language.toLowerCase() == 'en-us') {
200 v = 'none';
201 } else {
202 v = 'right-alt';
203 }
204 } else if (typeof v == 'string') {
205 v = v.toLowerCase();
206 } else {
207 v = 'none';
208 }
209
210 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v))
211 v = 'none';
212
213 terminal.keyboard.altGrMode = v;
214 },
215
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700216 'alt-backspace-is-meta-backspace': function(v) {
217 terminal.keyboard.altBackspaceIsMetaBackspace = v;
218 },
219
Robert Ginda57f03b42012-09-13 11:02:48 -0700220 'alt-is-meta': function(v) {
221 terminal.keyboard.altIsMeta = v;
222 },
223
224 'alt-sends-what': function(v) {
225 if (!/^(escape|8-bit|browser-key)$/.test(v))
226 v = 'escape';
227
228 terminal.keyboard.altSendsWhat = v;
229 },
230
231 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800232 var ary = v.match(/^lib-resource:(\S+)/);
233 if (ary) {
234 terminal.bellAudio_.setAttribute('src',
235 lib.resource.getDataUrl(ary[1]));
236 } else {
237 terminal.bellAudio_.setAttribute('src', v);
238 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700239 },
240
Michael Kelly485ecd12014-06-09 11:41:56 -0400241 'desktop-notification-bell': function(v) {
242 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700243 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400244 Notification.permission === 'granted';
245 if (!terminal.desktopNotificationBell_) {
246 // Note: We don't call Notification.requestPermission here because
247 // Chrome requires the call be the result of a user action (such as an
248 // onclick handler), and pref listeners are run asynchronously.
249 //
250 // A way of working around this would be to display a dialog in the
251 // terminal with a "click-to-request-permission" button.
252 console.warn('desktop-notification-bell is true but we do not have ' +
253 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400254 }
255 } else {
256 terminal.desktopNotificationBell_ = false;
257 }
258 },
259
Robert Ginda57f03b42012-09-13 11:02:48 -0700260 'background-color': function(v) {
261 terminal.setBackgroundColor(v);
262 },
263
264 'background-image': function(v) {
265 terminal.scrollPort_.setBackgroundImage(v);
266 },
267
268 'background-size': function(v) {
269 terminal.scrollPort_.setBackgroundSize(v);
270 },
271
272 'background-position': function(v) {
273 terminal.scrollPort_.setBackgroundPosition(v);
274 },
275
276 'backspace-sends-backspace': function(v) {
277 terminal.keyboard.backspaceSendsBackspace = v;
278 },
279
Brad Town18654b62015-03-12 00:27:45 -0700280 'character-map-overrides': function(v) {
281 if (!(v == null || v instanceof Object)) {
282 console.warn('Preference character-map-modifications is not an ' +
283 'object: ' + v);
284 return;
285 }
286
287 for (var code in v) {
288 var glmap = hterm.VT.CharacterMap.maps[code].glmap;
289 for (var received in v[code]) {
290 glmap[received] = v[code][received];
291 }
292 hterm.VT.CharacterMap.maps[code].reset(glmap);
293 }
294 },
295
Robert Ginda57f03b42012-09-13 11:02:48 -0700296 'cursor-blink': function(v) {
297 terminal.setCursorBlink(!!v);
298 },
299
Robert Gindaea2183e2014-07-17 09:51:51 -0700300 'cursor-blink-cycle': function(v) {
301 if (v instanceof Array &&
302 typeof v[0] == 'number' &&
303 typeof v[1] == 'number') {
304 terminal.cursorBlinkCycle_ = v;
305 } else if (typeof v == 'number') {
306 terminal.cursorBlinkCycle_ = [v, v];
307 } else {
308 // Fast blink indicates an error.
309 terminal.cursorBlinkCycle_ = [100, 100];
310 }
311 },
312
Robert Ginda57f03b42012-09-13 11:02:48 -0700313 'cursor-color': function(v) {
314 terminal.setCursorColor(v);
315 },
316
317 'color-palette-overrides': function(v) {
318 if (!(v == null || v instanceof Object || v instanceof Array)) {
319 console.warn('Preference color-palette-overrides is not an array or ' +
320 'object: ' + v);
321 return;
rginda9f5222b2012-03-05 11:53:28 -0800322 }
rginda9f5222b2012-03-05 11:53:28 -0800323
Robert Ginda57f03b42012-09-13 11:02:48 -0700324 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700325
Robert Ginda57f03b42012-09-13 11:02:48 -0700326 if (v) {
327 for (var key in v) {
328 var i = parseInt(key);
329 if (isNaN(i) || i < 0 || i > 255) {
330 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
331 continue;
332 }
333
334 if (v[i]) {
335 var rgb = lib.colors.normalizeCSS(v[i]);
336 if (rgb)
337 lib.colors.colorPalette[i] = rgb;
338 }
339 }
rginda30f20f62012-04-05 16:36:19 -0700340 }
rginda30f20f62012-04-05 16:36:19 -0700341
Evan Jones5f9df812016-12-06 09:38:58 -0500342 terminal.primaryScreen_.textAttributes.resetColorPalette();
Robert Ginda57f03b42012-09-13 11:02:48 -0700343 terminal.alternateScreen_.textAttributes.resetColorPalette();
344 },
rginda30f20f62012-04-05 16:36:19 -0700345
Robert Ginda57f03b42012-09-13 11:02:48 -0700346 'copy-on-select': function(v) {
347 terminal.copyOnSelect = !!v;
348 },
rginda9f5222b2012-03-05 11:53:28 -0800349
Rob Spies0bec09b2014-06-06 15:58:09 -0700350 'use-default-window-copy': function(v) {
351 terminal.useDefaultWindowCopy = !!v;
352 },
353
354 'clear-selection-after-copy': function(v) {
355 terminal.clearSelectionAfterCopy = !!v;
356 },
357
Robert Ginda7e5e9522014-03-14 12:23:58 -0700358 'ctrl-plus-minus-zero-zoom': function(v) {
359 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
360 },
361
Robert Gindafb5a3f92014-05-13 14:12:00 -0700362 'ctrl-c-copy': function(v) {
363 terminal.keyboard.ctrlCCopy = v;
364 },
365
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100366 'ctrl-v-paste': function(v) {
367 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700368 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100369 },
370
Masaya Suzuki273aa982014-05-31 07:25:55 +0900371 'east-asian-ambiguous-as-two-column': function(v) {
372 lib.wc.regardCjkAmbiguous = v;
373 },
374
Robert Ginda57f03b42012-09-13 11:02:48 -0700375 'enable-8-bit-control': function(v) {
376 terminal.vt.enable8BitControl = !!v;
377 },
rginda30f20f62012-04-05 16:36:19 -0700378
Robert Ginda57f03b42012-09-13 11:02:48 -0700379 'enable-bold': function(v) {
380 terminal.syncBoldSafeState();
381 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400382
Robert Ginda3e278d72014-03-25 13:18:51 -0700383 'enable-bold-as-bright': function(v) {
384 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
385 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
386 },
387
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400388 'enable-blink': function(v) {
389 terminal.syncBlinkState();
390 },
391
Robert Ginda57f03b42012-09-13 11:02:48 -0700392 'enable-clipboard-write': function(v) {
393 terminal.vt.enableClipboardWrite = !!v;
394 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400395
Robert Ginda3755e752013-05-31 13:34:09 -0700396 'enable-dec12': function(v) {
397 terminal.vt.enableDec12 = !!v;
398 },
399
Robert Ginda57f03b42012-09-13 11:02:48 -0700400 'font-family': function(v) {
401 terminal.syncFontFamily();
402 },
rginda30f20f62012-04-05 16:36:19 -0700403
Robert Ginda57f03b42012-09-13 11:02:48 -0700404 'font-size': function(v) {
405 terminal.setFontSize(v);
406 },
rginda9875d902012-08-20 16:21:57 -0700407
Robert Ginda57f03b42012-09-13 11:02:48 -0700408 'font-smoothing': function(v) {
409 terminal.syncFontFamily();
410 },
rgindade84e382012-04-20 15:39:31 -0700411
Robert Ginda57f03b42012-09-13 11:02:48 -0700412 'foreground-color': function(v) {
413 terminal.setForegroundColor(v);
414 },
rginda30f20f62012-04-05 16:36:19 -0700415
Robert Ginda57f03b42012-09-13 11:02:48 -0700416 'home-keys-scroll': function(v) {
417 terminal.keyboard.homeKeysScroll = v;
418 },
rginda4bba5e12012-06-20 16:15:30 -0700419
Robert Gindaa8165692015-06-15 14:46:31 -0700420 'keybindings': function(v) {
421 terminal.keyboard.bindings.clear();
422
423 if (!v)
424 return;
425
426 if (!(v instanceof Object)) {
427 console.error('Error in keybindings preference: Expected object');
428 return;
429 }
430
431 try {
432 terminal.keyboard.bindings.addBindings(v);
433 } catch (ex) {
434 console.error('Error in keybindings preference: ' + ex);
435 }
436 },
437
Robert Ginda57f03b42012-09-13 11:02:48 -0700438 'max-string-sequence': function(v) {
439 terminal.vt.maxStringSequence = v;
440 },
rginda11057d52012-04-25 12:29:56 -0700441
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700442 'media-keys-are-fkeys': function(v) {
443 terminal.keyboard.mediaKeysAreFKeys = v;
444 },
445
Robert Ginda57f03b42012-09-13 11:02:48 -0700446 'meta-sends-escape': function(v) {
447 terminal.keyboard.metaSendsEscape = v;
448 },
rginda30f20f62012-04-05 16:36:19 -0700449
Mike Frysinger847577f2017-05-23 23:25:57 -0400450 'mouse-right-click-paste': function(v) {
451 terminal.mouseRightClickPaste = v;
452 },
453
Robert Ginda57f03b42012-09-13 11:02:48 -0700454 'mouse-paste-button': function(v) {
455 terminal.syncMousePasteButton();
456 },
rgindaa8ba17d2012-08-15 14:41:10 -0700457
Robert Gindae76aa9f2014-03-14 12:29:12 -0700458 'page-keys-scroll': function(v) {
459 terminal.keyboard.pageKeysScroll = v;
460 },
461
Robert Ginda40932892012-12-10 17:26:40 -0800462 'pass-alt-number': function(v) {
463 if (v == null) {
464 var osx = window.navigator.userAgent.match(/Mac OS X/);
465
466 // Let Alt-1..9 pass to the browser (to control tab switching) on
467 // non-OS X systems, or if hterm is not opened in an app window.
468 v = (!osx && hterm.windowType != 'popup');
469 }
470
471 terminal.passAltNumber = v;
472 },
473
474 'pass-ctrl-number': function(v) {
475 if (v == null) {
476 var osx = window.navigator.userAgent.match(/Mac OS X/);
477
478 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
479 // non-OS X systems, or if hterm is not opened in an app window.
480 v = (!osx && hterm.windowType != 'popup');
481 }
482
483 terminal.passCtrlNumber = v;
484 },
485
486 'pass-meta-number': function(v) {
487 if (v == null) {
488 var osx = window.navigator.userAgent.match(/Mac OS X/);
489
490 // Let Meta-1..9 pass to the browser (to control tab switching) on
491 // OS X systems, or if hterm is not opened in an app window.
492 v = (osx && hterm.windowType != 'popup');
493 }
494
495 terminal.passMetaNumber = v;
496 },
497
Marius Schilder77857b32014-05-14 16:21:26 -0700498 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700499 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700500 },
501
Robert Ginda8cb7d902013-06-20 14:37:18 -0700502 'receive-encoding': function(v) {
503 if (!(/^(utf-8|raw)$/).test(v)) {
504 console.warn('Invalid value for "receive-encoding": ' + v);
505 v = 'utf-8';
506 }
507
508 terminal.vt.characterEncoding = v;
509 },
510
Robert Ginda57f03b42012-09-13 11:02:48 -0700511 'scroll-on-keystroke': function(v) {
512 terminal.scrollOnKeystroke_ = v;
513 },
rginda9f5222b2012-03-05 11:53:28 -0800514
Robert Ginda57f03b42012-09-13 11:02:48 -0700515 'scroll-on-output': function(v) {
516 terminal.scrollOnOutput_ = v;
517 },
rginda30f20f62012-04-05 16:36:19 -0700518
Robert Ginda57f03b42012-09-13 11:02:48 -0700519 'scrollbar-visible': function(v) {
520 terminal.setScrollbarVisible(v);
521 },
rginda9f5222b2012-03-05 11:53:28 -0800522
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);
Ricky Liang48f05cb2013-12-31 23:35:29 +0800749 if (this.wcCssRule_) {
750 this.wcCssRule_.style.width = this.scrollPort_.characterSize.width * 2 +
751 'px';
752 }
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') {
rginda4bba5e12012-06-20 16:15:30 -0700795 this.mousePasteButton = 2;
796 } else {
797 this.mousePasteButton = 3;
798 }
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/**
rginda87b86462011-12-14 13:48:03 -0800837 * Return a copy of the current cursor position.
838 *
839 * @return {hterm.RowCol} The RowCol object representing the current position.
840 */
841hterm.Terminal.prototype.saveCursor = function() {
842 return this.screen_.cursorPosition.clone();
843};
844
Evan Jones2600d4f2016-12-06 09:29:36 -0500845/**
846 * Return the current text attributes.
847 *
848 * @return {string}
849 */
rgindaa19afe22012-01-25 15:40:22 -0800850hterm.Terminal.prototype.getTextAttributes = function() {
851 return this.screen_.textAttributes;
852};
853
Evan Jones2600d4f2016-12-06 09:29:36 -0500854/**
855 * Set the text attributes.
856 *
857 * @param {string} textAttributes The attributes to set.
858 */
rginda1a09aa02012-06-18 21:11:25 -0700859hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
860 this.screen_.textAttributes = textAttributes;
861};
862
rginda87b86462011-12-14 13:48:03 -0800863/**
rgindaf522ce02012-04-17 17:49:17 -0700864 * Return the current browser zoom factor applied to the terminal.
865 *
866 * @return {number} The current browser zoom factor.
867 */
868hterm.Terminal.prototype.getZoomFactor = function() {
869 return this.scrollPort_.characterSize.zoomFactor;
870};
871
872/**
rginda9846e2f2012-01-27 13:53:33 -0800873 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500874 *
875 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800876 */
877hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800878 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800879};
880
881/**
rginda87b86462011-12-14 13:48:03 -0800882 * Restore a previously saved cursor position.
883 *
884 * @param {hterm.RowCol} cursor The position to restore.
885 */
886hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700887 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
888 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800889 this.screen_.setCursorPosition(row, column);
890 if (cursor.column > column ||
891 cursor.column == column && cursor.overflow) {
892 this.screen_.cursorPosition.overflow = true;
893 }
rginda87b86462011-12-14 13:48:03 -0800894};
895
896/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400897 * Clear the cursor's overflow flag.
898 */
899hterm.Terminal.prototype.clearCursorOverflow = function() {
900 this.screen_.cursorPosition.overflow = false;
901};
902
903/**
Robert Ginda830583c2013-08-07 13:20:46 -0700904 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -0500905 *
906 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -0700907 */
908hterm.Terminal.prototype.setCursorShape = function(shape) {
909 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -0800910 this.restyleCursor_();
Robert Ginda830583c2013-08-07 13:20:46 -0700911}
912
913/**
914 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -0500915 *
916 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -0700917 */
918hterm.Terminal.prototype.getCursorShape = function() {
919 return this.cursorShape_;
920}
921
922/**
rginda87b86462011-12-14 13:48:03 -0800923 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -0500924 *
925 * @param {number} columnCount
rginda87b86462011-12-14 13:48:03 -0800926 */
927hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800928 if (columnCount == null) {
929 this.div_.style.width = '100%';
930 return;
931 }
932
Robert Ginda26806d12014-07-24 13:44:07 -0700933 this.div_.style.width = Math.ceil(
934 this.scrollPort_.characterSize.width *
935 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400936 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800937 this.scheduleSyncCursorPosition_();
938};
rginda87b86462011-12-14 13:48:03 -0800939
rgindac9bc5502012-01-18 11:48:44 -0800940/**
rginda35c456b2012-02-09 17:29:05 -0800941 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -0500942 *
943 * @param {number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -0800944 */
945hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800946 if (rowCount == null) {
947 this.div_.style.height = '100%';
948 return;
949 }
950
rginda35c456b2012-02-09 17:29:05 -0800951 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700952 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800953 this.realizeSize_(this.screenSize.width, rowCount);
954 this.scheduleSyncCursorPosition_();
955};
956
957/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400958 * Deal with terminal size changes.
959 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500960 * @param {number} columnCount The number of columns.
961 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400962 */
963hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
964 if (columnCount != this.screenSize.width)
965 this.realizeWidth_(columnCount);
966
967 if (rowCount != this.screenSize.height)
968 this.realizeHeight_(rowCount);
969
970 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -0700971 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400972};
973
974/**
rgindac9bc5502012-01-18 11:48:44 -0800975 * Deal with terminal width changes.
976 *
977 * This function does what needs to be done when the terminal width changes
978 * out from under us. It happens here rather than in onResize_() because this
979 * code may need to run synchronously to handle programmatic changes of
980 * terminal width.
981 *
982 * Relying on the browser to send us an async resize event means we may not be
983 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -0500984 *
985 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -0800986 */
987hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700988 if (columnCount <= 0)
989 throw new Error('Attempt to realize bad width: ' + columnCount);
990
rgindac9bc5502012-01-18 11:48:44 -0800991 var deltaColumns = columnCount - this.screen_.getWidth();
992
rginda87b86462011-12-14 13:48:03 -0800993 this.screenSize.width = columnCount;
994 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800995
996 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400997 if (this.defaultTabStops)
998 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800999 } else {
1000 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -04001001 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -08001002 break;
1003
1004 this.tabStops_.pop();
1005 }
1006 }
1007
1008 this.screen_.setColumnCount(this.screenSize.width);
1009};
1010
1011/**
1012 * Deal with terminal height changes.
1013 *
1014 * This function does what needs to be done when the terminal height changes
1015 * out from under us. It happens here rather than in onResize_() because this
1016 * code may need to run synchronously to handle programmatic changes of
1017 * terminal height.
1018 *
1019 * Relying on the browser to send us an async resize event means we may not be
1020 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001021 *
1022 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001023 */
1024hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001025 if (rowCount <= 0)
1026 throw new Error('Attempt to realize bad height: ' + rowCount);
1027
rgindac9bc5502012-01-18 11:48:44 -08001028 var deltaRows = rowCount - this.screen_.getHeight();
1029
1030 this.screenSize.height = rowCount;
1031
1032 var cursor = this.saveCursor();
1033
1034 if (deltaRows < 0) {
1035 // Screen got smaller.
1036 deltaRows *= -1;
1037 while (deltaRows) {
1038 var lastRow = this.getRowCount() - 1;
1039 if (lastRow - this.scrollbackRows_.length == cursor.row)
1040 break;
1041
1042 if (this.getRowText(lastRow))
1043 break;
1044
1045 this.screen_.popRow();
1046 deltaRows--;
1047 }
1048
1049 var ary = this.screen_.shiftRows(deltaRows);
1050 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1051
1052 // We just removed rows from the top of the screen, we need to update
1053 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001054 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001055 } else if (deltaRows > 0) {
1056 // Screen got larger.
1057
1058 if (deltaRows <= this.scrollbackRows_.length) {
1059 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1060 var rows = this.scrollbackRows_.splice(
1061 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1062 this.screen_.unshiftRows(rows);
1063 deltaRows -= scrollbackCount;
1064 cursor.row += scrollbackCount;
1065 }
1066
1067 if (deltaRows)
1068 this.appendRows_(deltaRows);
1069 }
1070
rginda35c456b2012-02-09 17:29:05 -08001071 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001072 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001073};
1074
1075/**
1076 * Scroll the terminal to the top of the scrollback buffer.
1077 */
1078hterm.Terminal.prototype.scrollHome = function() {
1079 this.scrollPort_.scrollRowToTop(0);
1080};
1081
1082/**
1083 * Scroll the terminal to the end.
1084 */
1085hterm.Terminal.prototype.scrollEnd = function() {
1086 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1087};
1088
1089/**
1090 * Scroll the terminal one page up (minus one line) relative to the current
1091 * position.
1092 */
1093hterm.Terminal.prototype.scrollPageUp = function() {
1094 var i = this.scrollPort_.getTopRowIndex();
1095 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
1096};
1097
1098/**
1099 * Scroll the terminal one page down (minus one line) relative to the current
1100 * position.
1101 */
1102hterm.Terminal.prototype.scrollPageDown = function() {
1103 var i = this.scrollPort_.getTopRowIndex();
1104 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -08001105};
1106
rgindac9bc5502012-01-18 11:48:44 -08001107/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001108 * Scroll the terminal one line up relative to the current position.
1109 */
1110hterm.Terminal.prototype.scrollLineUp = function() {
1111 var i = this.scrollPort_.getTopRowIndex();
1112 this.scrollPort_.scrollRowToTop(i - 1);
1113};
1114
1115/**
1116 * Scroll the terminal one line down relative to the current position.
1117 */
1118hterm.Terminal.prototype.scrollLineDown = function() {
1119 var i = this.scrollPort_.getTopRowIndex();
1120 this.scrollPort_.scrollRowToTop(i + 1);
1121};
1122
1123/**
Robert Ginda40932892012-12-10 17:26:40 -08001124 * Clear primary screen, secondary screen, and the scrollback buffer.
1125 */
1126hterm.Terminal.prototype.wipeContents = function() {
1127 this.scrollbackRows_.length = 0;
1128 this.scrollPort_.resetCache();
1129
1130 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
1131 var bottom = screen.getHeight();
1132 if (bottom > 0) {
1133 this.renumberRows_(0, bottom);
1134 this.clearHome(screen);
1135 }
1136 }.bind(this));
1137
1138 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001139 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001140};
1141
1142/**
rgindac9bc5502012-01-18 11:48:44 -08001143 * Full terminal reset.
1144 */
rginda87b86462011-12-14 13:48:03 -08001145hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -08001146 this.clearAllTabStops();
1147 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001148
1149 this.clearHome(this.primaryScreen_);
1150 this.primaryScreen_.textAttributes.reset();
1151
1152 this.clearHome(this.alternateScreen_);
1153 this.alternateScreen_.textAttributes.reset();
1154
rgindab8bc8932012-04-27 12:45:03 -07001155 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1156
Robert Ginda92e18102013-03-14 13:56:37 -07001157 this.vt.reset();
1158
rgindac9bc5502012-01-18 11:48:44 -08001159 this.softReset();
rginda87b86462011-12-14 13:48:03 -08001160};
1161
rgindac9bc5502012-01-18 11:48:44 -08001162/**
1163 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001164 *
1165 * Perform a soft reset to the default values listed in
1166 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001167 */
rginda0f5c0292012-01-13 11:00:13 -08001168hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -07001169 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001170 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001171
Brad Townb62dfdc2015-03-16 19:07:15 -07001172 // We show the cursor on soft reset but do not alter the blink state.
1173 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1174
rgindab8bc8932012-04-27 12:45:03 -07001175 // Xterm also resets the color palette on soft reset, even though it doesn't
1176 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -07001177 this.primaryScreen_.textAttributes.resetColorPalette();
1178 this.alternateScreen_.textAttributes.resetColorPalette();
1179
rgindab8bc8932012-04-27 12:45:03 -07001180 // The xterm man page explicitly says this will happen on soft reset.
1181 this.setVTScrollRegion(null, null);
1182
1183 // Xterm also shows the cursor on soft reset, but does not alter the blink
1184 // state.
rgindaa19afe22012-01-25 15:40:22 -08001185 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001186};
1187
rgindac9bc5502012-01-18 11:48:44 -08001188/**
1189 * Move the cursor forward to the next tab stop, or to the last column
1190 * if no more tab stops are set.
1191 */
1192hterm.Terminal.prototype.forwardTabStop = function() {
1193 var column = this.screen_.cursorPosition.column;
1194
1195 for (var i = 0; i < this.tabStops_.length; i++) {
1196 if (this.tabStops_[i] > column) {
1197 this.setCursorColumn(this.tabStops_[i]);
1198 return;
1199 }
1200 }
1201
David Benjamin66e954d2012-05-05 21:08:12 -04001202 // xterm does not clear the overflow flag on HT or CHT.
1203 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001204 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001205 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001206};
1207
rgindac9bc5502012-01-18 11:48:44 -08001208/**
1209 * Move the cursor backward to the previous tab stop, or to the first column
1210 * if no previous tab stops are set.
1211 */
1212hterm.Terminal.prototype.backwardTabStop = function() {
1213 var column = this.screen_.cursorPosition.column;
1214
1215 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1216 if (this.tabStops_[i] < column) {
1217 this.setCursorColumn(this.tabStops_[i]);
1218 return;
1219 }
1220 }
1221
1222 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001223};
1224
rgindac9bc5502012-01-18 11:48:44 -08001225/**
1226 * Set a tab stop at the given column.
1227 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001228 * @param {integer} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001229 */
1230hterm.Terminal.prototype.setTabStop = function(column) {
1231 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1232 if (this.tabStops_[i] == column)
1233 return;
1234
1235 if (this.tabStops_[i] < column) {
1236 this.tabStops_.splice(i + 1, 0, column);
1237 return;
1238 }
1239 }
1240
1241 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001242};
1243
rgindac9bc5502012-01-18 11:48:44 -08001244/**
1245 * Clear the tab stop at the current cursor position.
1246 *
1247 * No effect if there is no tab stop at the current cursor position.
1248 */
1249hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1250 var column = this.screen_.cursorPosition.column;
1251
1252 var i = this.tabStops_.indexOf(column);
1253 if (i == -1)
1254 return;
1255
1256 this.tabStops_.splice(i, 1);
1257};
1258
1259/**
1260 * Clear all tab stops.
1261 */
1262hterm.Terminal.prototype.clearAllTabStops = function() {
1263 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001264 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001265};
1266
1267/**
1268 * Set up the default tab stops, starting from a given column.
1269 *
1270 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001271 * from the specified column, or 0 if no column is provided. It also flags
1272 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001273 *
1274 * This does not clear the existing tab stops first, use clearAllTabStops
1275 * for that.
1276 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001277 * @param {integer} opt_start Optional starting zero based starting column, useful
rgindac9bc5502012-01-18 11:48:44 -08001278 * for filling out missing tab stops when the terminal is resized.
1279 */
1280hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1281 var start = opt_start || 0;
1282 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001283 // Round start up to a default tab stop.
1284 start = start - 1 - ((start - 1) % w) + w;
1285 for (var i = start; i < this.screenSize.width; i += w) {
1286 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001287 }
David Benjamin66e954d2012-05-05 21:08:12 -04001288
1289 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001290};
1291
rginda6d397402012-01-17 10:58:29 -08001292/**
rginda8ba33642011-12-14 12:31:31 -08001293 * Interpret a sequence of characters.
1294 *
1295 * Incomplete escape sequences are buffered until the next call.
1296 *
1297 * @param {string} str Sequence of characters to interpret or pass through.
1298 */
1299hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001300 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001301 this.scheduleSyncCursorPosition_();
1302};
1303
1304/**
1305 * Take over the given DIV for use as the terminal display.
1306 *
1307 * @param {HTMLDivElement} div The div to use as the terminal display.
1308 */
1309hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001310 this.div_ = div;
1311
rginda8ba33642011-12-14 12:31:31 -08001312 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001313 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001314 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1315 this.scrollPort_.setBackgroundPosition(
1316 this.prefs_.get('background-position'));
Mike Frysinger08bad432017-04-24 00:50:54 -04001317 this.scrollPort_.setUserCssUrl(this.prefs_.get('user-css'));
1318 this.scrollPort_.setUserCssText(this.prefs_.get('user-css-text'));
rginda30f20f62012-04-05 16:36:19 -07001319
rginda0918b652012-04-04 11:26:24 -07001320 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001321
rginda9f5222b2012-03-05 11:53:28 -08001322 this.setFontSize(this.prefs_.get('font-size'));
1323 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001324
David Reveman8f552492012-03-28 12:18:41 -04001325 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001326 this.setScrollWheelMoveMultipler(
1327 this.prefs_.get('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001328
rginda8ba33642011-12-14 12:31:31 -08001329 this.document_ = this.scrollPort_.getDocument();
1330
Evan Jones5f9df812016-12-06 09:38:58 -05001331 this.document_.body.oncontextmenu = function() { return false; };
rginda4bba5e12012-06-20 16:15:30 -07001332
1333 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001334 var screenNode = this.scrollPort_.getScreenNode();
1335 screenNode.addEventListener('mousedown', onMouse);
1336 screenNode.addEventListener('mouseup', onMouse);
1337 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001338 this.scrollPort_.onScrollWheel = onMouse;
1339
Toni Barzic0bfa8922013-11-22 11:18:35 -08001340 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001341 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001342 // Listen for mousedown events on the screenNode as in FF the focus
1343 // events don't bubble.
1344 screenNode.addEventListener('mousedown', function() {
1345 setTimeout(this.onFocusChange_.bind(this, true));
1346 }.bind(this));
1347
Toni Barzic0bfa8922013-11-22 11:18:35 -08001348 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001349 'blur', this.onFocusChange_.bind(this, false));
1350
1351 var style = this.document_.createElement('style');
1352 style.textContent =
1353 ('.cursor-node[focus="false"] {' +
1354 ' box-sizing: border-box;' +
1355 ' background-color: transparent !important;' +
1356 ' border-width: 2px;' +
1357 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001358 '}' +
1359 '.wc-node {' +
1360 ' display: inline-block;' +
1361 ' text-align: center;' +
1362 ' width: ' + this.scrollPort_.characterSize.width * 2 + 'px;' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001363 '}' +
1364 ':root {' +
1365 ' --hterm-blink-node-duration: 0.7s;' +
1366 '}' +
1367 '@keyframes blink {' +
1368 ' from { opacity: 1.0; }' +
1369 ' to { opacity: 0.0; }' +
1370 '}' +
1371 '.blink-node {' +
1372 ' animation-name: blink;' +
1373 ' animation-duration: var(--hterm-blink-node-duration);' +
1374 ' animation-iteration-count: infinite;' +
1375 ' animation-timing-function: ease-in-out;' +
1376 ' animation-direction: alternate;' +
rginda8e92a692012-05-20 19:37:20 -07001377 '}');
1378 this.document_.head.appendChild(style);
1379
Ricky Liang48f05cb2013-12-31 23:35:29 +08001380 var styleSheets = this.document_.styleSheets;
1381 var cssRules = styleSheets[styleSheets.length - 1].cssRules;
1382 this.wcCssRule_ = cssRules[cssRules.length - 1];
1383
rginda8ba33642011-12-14 12:31:31 -08001384 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001385 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001386 this.cursorNode_.style.cssText =
1387 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001388 'top: -99px;' +
1389 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001390 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1391 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
Rob Spies06533ba2014-04-24 11:20:37 -07001392 '-webkit-transition: opacity, background-color 100ms linear;' +
1393 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001394
rginda8e92a692012-05-20 19:37:20 -07001395 this.setCursorColor(this.prefs_.get('cursor-color'));
Robert Gindafb1be6a2013-12-11 11:56:22 -08001396 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1397 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001398
rginda8ba33642011-12-14 12:31:31 -08001399 this.document_.body.appendChild(this.cursorNode_);
1400
rgindad5613292012-06-19 15:40:37 -07001401 // When 'enableMouseDragScroll' is off we reposition this element directly
1402 // under the mouse cursor after a click. This makes Chrome associate
1403 // subsequent mousemove events with the scroll-blocker. Since the
1404 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1405 // events do not cause the scrollport to scroll.
1406 //
1407 // It's a hack, but it's the cleanest way I could find.
1408 this.scrollBlockerNode_ = this.document_.createElement('div');
1409 this.scrollBlockerNode_.style.cssText =
1410 ('position: absolute;' +
1411 'top: -99px;' +
1412 'display: block;' +
1413 'width: 10px;' +
1414 'height: 10px;');
1415 this.document_.body.appendChild(this.scrollBlockerNode_);
1416
rgindad5613292012-06-19 15:40:37 -07001417 this.scrollPort_.onScrollWheel = onMouse;
1418 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1419 ].forEach(function(event) {
1420 this.scrollBlockerNode_.addEventListener(event, onMouse);
1421 this.cursorNode_.addEventListener(event, onMouse);
1422 this.document_.addEventListener(event, onMouse);
1423 }.bind(this));
1424
1425 this.cursorNode_.addEventListener('mousedown', function() {
1426 setTimeout(this.focus.bind(this));
1427 }.bind(this));
1428
rginda8ba33642011-12-14 12:31:31 -08001429 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001430
rginda87b86462011-12-14 13:48:03 -08001431 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001432 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001433};
1434
rginda0918b652012-04-04 11:26:24 -07001435/**
1436 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001437 *
1438 * @return {HTMLDocument}
rginda0918b652012-04-04 11:26:24 -07001439 */
rginda87b86462011-12-14 13:48:03 -08001440hterm.Terminal.prototype.getDocument = function() {
1441 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001442};
1443
1444/**
rginda0918b652012-04-04 11:26:24 -07001445 * Focus the terminal.
1446 */
1447hterm.Terminal.prototype.focus = function() {
1448 this.scrollPort_.focus();
1449};
1450
1451/**
rginda8ba33642011-12-14 12:31:31 -08001452 * Return the HTML Element for a given row index.
1453 *
1454 * This is a method from the RowProvider interface. The ScrollPort uses
1455 * it to fetch rows on demand as they are scrolled into view.
1456 *
1457 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1458 * pairs to conserve memory.
1459 *
1460 * @param {integer} index The zero-based row index, measured relative to the
1461 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001462 * largest indices.
rginda8ba33642011-12-14 12:31:31 -08001463 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1464 */
1465hterm.Terminal.prototype.getRowNode = function(index) {
1466 if (index < this.scrollbackRows_.length)
1467 return this.scrollbackRows_[index];
1468
1469 var screenIndex = index - this.scrollbackRows_.length;
1470 return this.screen_.rowsArray[screenIndex];
1471};
1472
1473/**
1474 * Return the text content for a given range of rows.
1475 *
1476 * This is a method from the RowProvider interface. The ScrollPort uses
1477 * it to fetch text content on demand when the user attempts to copy their
1478 * selection to the clipboard.
1479 *
1480 * @param {integer} start The zero-based row index to start from, measured
1481 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001482 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001483 * @param {integer} end The zero-based row index to end on, measured
1484 * relative to the start of the scrollback buffer.
1485 * @return {string} A single string containing the text value of the range of
1486 * rows. Lines will be newline delimited, with no trailing newline.
1487 */
1488hterm.Terminal.prototype.getRowsText = function(start, end) {
1489 var ary = [];
1490 for (var i = start; i < end; i++) {
1491 var node = this.getRowNode(i);
1492 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001493 if (i < end - 1 && !node.getAttribute('line-overflow'))
1494 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001495 }
1496
rgindaa09e7332012-08-17 12:49:51 -07001497 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001498};
1499
1500/**
1501 * Return the text content for a given row.
1502 *
1503 * This is a method from the RowProvider interface. The ScrollPort uses
1504 * it to fetch text content on demand when the user attempts to copy their
1505 * selection to the clipboard.
1506 *
1507 * @param {integer} index The zero-based row index to return, measured
1508 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001509 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001510 * @return {string} A string containing the text value of the selected row.
1511 */
1512hterm.Terminal.prototype.getRowText = function(index) {
1513 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001514 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001515};
1516
1517/**
1518 * Return the total number of rows in the addressable screen and in the
1519 * scrollback buffer of this terminal.
1520 *
1521 * This is a method from the RowProvider interface. The ScrollPort uses
1522 * it to compute the size of the scrollbar.
1523 *
1524 * @return {integer} The number of rows in this terminal.
1525 */
1526hterm.Terminal.prototype.getRowCount = function() {
1527 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1528};
1529
1530/**
1531 * Create DOM nodes for new rows and append them to the end of the terminal.
1532 *
1533 * This is the only correct way to add a new DOM node for a row. Notice that
1534 * the new row is appended to the bottom of the list of rows, and does not
1535 * require renumbering (of the rowIndex property) of previous rows.
1536 *
1537 * If you think you want a new blank row somewhere in the middle of the
1538 * terminal, look into moveRows_().
1539 *
1540 * This method does not pay attention to vtScrollTop/Bottom, since you should
1541 * be using moveRows() in cases where they would matter.
1542 *
1543 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001544 *
1545 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001546 */
1547hterm.Terminal.prototype.appendRows_ = function(count) {
1548 var cursorRow = this.screen_.rowsArray.length;
1549 var offset = this.scrollbackRows_.length + cursorRow;
1550 for (var i = 0; i < count; i++) {
1551 var row = this.document_.createElement('x-row');
1552 row.appendChild(this.document_.createTextNode(''));
1553 row.rowIndex = offset + i;
1554 this.screen_.pushRow(row);
1555 }
1556
1557 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1558 if (extraRows > 0) {
1559 var ary = this.screen_.shiftRows(extraRows);
1560 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001561 if (this.scrollPort_.isScrolledEnd)
1562 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001563 }
1564
1565 if (cursorRow >= this.screen_.rowsArray.length)
1566 cursorRow = this.screen_.rowsArray.length - 1;
1567
rginda87b86462011-12-14 13:48:03 -08001568 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001569};
1570
1571/**
1572 * Relocate rows from one part of the addressable screen to another.
1573 *
1574 * This is used to recycle rows during VT scrolls (those which are driven
1575 * by VT commands, rather than by the user manipulating the scrollbar.)
1576 *
1577 * In this case, the blank lines scrolled into the scroll region are made of
1578 * the nodes we scrolled off. These have their rowIndex properties carefully
1579 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001580 *
1581 * @param {number} fromIndex The start index.
1582 * @param {number} count The number of rows to move.
1583 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001584 */
1585hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1586 var ary = this.screen_.removeRows(fromIndex, count);
1587 this.screen_.insertRows(toIndex, ary);
1588
1589 var start, end;
1590 if (fromIndex < toIndex) {
1591 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001592 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001593 } else {
1594 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001595 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001596 }
1597
1598 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001599 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001600};
1601
1602/**
1603 * Renumber the rowIndex property of the given range of rows.
1604 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001605 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001606 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001607 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001608 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001609 *
1610 * @param {number} start The start index.
1611 * @param {number} end The end index.
1612 * @param {hterm.Screen} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001613 */
Robert Ginda40932892012-12-10 17:26:40 -08001614hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1615 var screen = opt_screen || this.screen_;
1616
rginda8ba33642011-12-14 12:31:31 -08001617 var offset = this.scrollbackRows_.length;
1618 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001619 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001620 }
1621};
1622
1623/**
1624 * Print a string to the terminal.
1625 *
1626 * This respects the current insert and wraparound modes. It will add new lines
1627 * to the end of the terminal, scrolling off the top into the scrollback buffer
1628 * if necessary.
1629 *
1630 * The string is *not* parsed for escape codes. Use the interpret() method if
1631 * that's what you're after.
1632 *
1633 * @param{string} str The string to print.
1634 */
1635hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001636 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001637
Ricky Liang48f05cb2013-12-31 23:35:29 +08001638 var strWidth = lib.wc.strWidth(str);
1639
1640 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001641 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1642 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001643 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001644 }
rgindaa19afe22012-01-25 15:40:22 -08001645
Ricky Liang48f05cb2013-12-31 23:35:29 +08001646 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001647 var didOverflow = false;
1648 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001649
rgindaa9abdd82012-08-06 18:05:09 -07001650 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1651 didOverflow = true;
1652 count = this.screenSize.width - this.screen_.cursorPosition.column;
1653 }
rgindaa19afe22012-01-25 15:40:22 -08001654
rgindaa9abdd82012-08-06 18:05:09 -07001655 if (didOverflow && !this.options_.wraparound) {
1656 // If the string overflowed the line but wraparound is off, then the
1657 // last printed character should be the last of the string.
1658 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001659 substr = lib.wc.substr(str, startOffset, count - 1) +
1660 lib.wc.substr(str, strWidth - 1);
1661 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001662 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001663 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001664 }
rgindaa19afe22012-01-25 15:40:22 -08001665
Ricky Liang48f05cb2013-12-31 23:35:29 +08001666 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1667 for (var i = 0; i < tokens.length; i++) {
1668 if (tokens[i].wcNode)
1669 this.screen_.textAttributes.wcNode = true;
1670
1671 if (this.options_.insertMode) {
1672 this.screen_.insertString(tokens[i].str);
1673 } else {
1674 this.screen_.overwriteString(tokens[i].str);
1675 }
1676 this.screen_.textAttributes.wcNode = false;
rgindaa9abdd82012-08-06 18:05:09 -07001677 }
1678
1679 this.screen_.maybeClipCurrentRow();
1680 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001681 }
rginda8ba33642011-12-14 12:31:31 -08001682
1683 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001684
rginda9f5222b2012-03-05 11:53:28 -08001685 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001686 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001687};
1688
1689/**
rginda87b86462011-12-14 13:48:03 -08001690 * Set the VT scroll region.
1691 *
rginda87b86462011-12-14 13:48:03 -08001692 * This also resets the cursor position to the absolute (0, 0) position, since
1693 * that's what xterm appears to do.
1694 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001695 * Setting the scroll region to the full height of the terminal will clear
1696 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1697 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1698 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1699 * continue to work as most users would expect.
1700 *
rginda87b86462011-12-14 13:48:03 -08001701 * @param {integer} scrollTop The zero-based top of the scroll region.
1702 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1703 * inclusive.
1704 */
1705hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001706 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001707 this.vtScrollTop_ = null;
1708 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001709 } else {
1710 this.vtScrollTop_ = scrollTop;
1711 this.vtScrollBottom_ = scrollBottom;
1712 }
rginda87b86462011-12-14 13:48:03 -08001713};
1714
1715/**
rginda8ba33642011-12-14 12:31:31 -08001716 * Return the top row index according to the VT.
1717 *
1718 * This will return 0 unless the terminal has been told to restrict scrolling
1719 * to some lower row. It is used for some VT cursor positioning and scrolling
1720 * commands.
1721 *
1722 * @return {integer} The topmost row in the terminal's scroll region.
1723 */
1724hterm.Terminal.prototype.getVTScrollTop = function() {
1725 if (this.vtScrollTop_ != null)
1726 return this.vtScrollTop_;
1727
1728 return 0;
rginda87b86462011-12-14 13:48:03 -08001729};
rginda8ba33642011-12-14 12:31:31 -08001730
1731/**
1732 * Return the bottom row index according to the VT.
1733 *
1734 * This will return the height of the terminal unless the it has been told to
1735 * restrict scrolling to some higher row. It is used for some VT cursor
1736 * positioning and scrolling commands.
1737 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001738 * @return {integer} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001739 */
1740hterm.Terminal.prototype.getVTScrollBottom = function() {
1741 if (this.vtScrollBottom_ != null)
1742 return this.vtScrollBottom_;
1743
rginda87b86462011-12-14 13:48:03 -08001744 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001745}
1746
1747/**
1748 * Process a '\n' character.
1749 *
1750 * If the cursor is on the final row of the terminal this will append a new
1751 * blank row to the screen and scroll the topmost row into the scrollback
1752 * buffer.
1753 *
1754 * Otherwise, this moves the cursor to column zero of the next row.
1755 */
1756hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001757 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1758 this.screen_.rowsArray.length - 1);
1759
1760 if (this.vtScrollBottom_ != null) {
1761 // A VT Scroll region is active, we never append new rows.
1762 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1763 // We're at the end of the VT Scroll Region, perform a VT scroll.
1764 this.vtScrollUp(1);
1765 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1766 } else if (cursorAtEndOfScreen) {
1767 // We're at the end of the screen, the only thing to do is put the
1768 // cursor to column 0.
1769 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1770 } else {
1771 // Anywhere else, advance the cursor row, and reset the column.
1772 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1773 }
1774 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001775 // We're at the end of the screen. Append a new row to the terminal,
1776 // shifting the top row into the scrollback.
1777 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001778 } else {
rginda87b86462011-12-14 13:48:03 -08001779 // Anywhere else in the screen just moves the cursor.
1780 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001781 }
1782};
1783
1784/**
1785 * Like newLine(), except maintain the cursor column.
1786 */
1787hterm.Terminal.prototype.lineFeed = function() {
1788 var column = this.screen_.cursorPosition.column;
1789 this.newLine();
1790 this.setCursorColumn(column);
1791};
1792
1793/**
rginda87b86462011-12-14 13:48:03 -08001794 * If autoCarriageReturn is set then newLine(), else lineFeed().
1795 */
1796hterm.Terminal.prototype.formFeed = function() {
1797 if (this.options_.autoCarriageReturn) {
1798 this.newLine();
1799 } else {
1800 this.lineFeed();
1801 }
1802};
1803
1804/**
1805 * Move the cursor up one row, possibly inserting a blank line.
1806 *
1807 * The cursor column is not changed.
1808 */
1809hterm.Terminal.prototype.reverseLineFeed = function() {
1810 var scrollTop = this.getVTScrollTop();
1811 var currentRow = this.screen_.cursorPosition.row;
1812
1813 if (currentRow == scrollTop) {
1814 this.insertLines(1);
1815 } else {
1816 this.setAbsoluteCursorRow(currentRow - 1);
1817 }
1818};
1819
1820/**
rginda8ba33642011-12-14 12:31:31 -08001821 * Replace all characters to the left of the current cursor with the space
1822 * character.
1823 *
1824 * TODO(rginda): This should probably *remove* the characters (not just replace
1825 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001826 * position.
rginda8ba33642011-12-14 12:31:31 -08001827 */
1828hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001829 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001830 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001831 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001832 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001833};
1834
1835/**
David Benjamin684a9b72012-05-01 17:19:58 -04001836 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001837 *
1838 * The cursor position is unchanged.
1839 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001840 * If the current background color is not the default background color this
1841 * will insert spaces rather than delete. This is unfortunate because the
1842 * trailing space will affect text selection, but it's difficult to come up
1843 * with a way to style empty space that wouldn't trip up the hterm.Screen
1844 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001845 *
1846 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1847 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1848 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05001849 *
1850 * @param {number} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08001851 */
1852hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001853 if (this.screen_.cursorPosition.overflow)
1854 return;
1855
Robert Ginda7fd57082012-09-25 14:41:47 -07001856 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1857 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001858
1859 if (this.screen_.textAttributes.background ===
1860 this.screen_.textAttributes.DEFAULT_COLOR) {
1861 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08001862 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07001863 this.screen_.cursorPosition.column + count) {
1864 this.screen_.deleteChars(count);
1865 this.clearCursorOverflow();
1866 return;
1867 }
1868 }
1869
rginda87b86462011-12-14 13:48:03 -08001870 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001871 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001872 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001873 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001874};
1875
1876/**
1877 * Erase the current line.
1878 *
1879 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001880 */
1881hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001882 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001883 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001884 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001885 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001886};
1887
1888/**
David Benjamina08d78f2012-05-05 00:28:49 -04001889 * Erase all characters from the start of the screen to the current cursor
1890 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001891 *
1892 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001893 */
1894hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001895 var cursor = this.saveCursor();
1896
1897 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001898
David Benjamina08d78f2012-05-05 00:28:49 -04001899 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001900 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001901 this.screen_.clearCursorRow();
1902 }
1903
rginda87b86462011-12-14 13:48:03 -08001904 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001905 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001906};
1907
1908/**
1909 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001910 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001911 *
1912 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001913 */
1914hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001915 var cursor = this.saveCursor();
1916
1917 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001918
David Benjamina08d78f2012-05-05 00:28:49 -04001919 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001920 for (var i = cursor.row + 1; i <= bottom; i++) {
1921 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001922 this.screen_.clearCursorRow();
1923 }
1924
rginda87b86462011-12-14 13:48:03 -08001925 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001926 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001927};
1928
1929/**
1930 * Fill the terminal with a given character.
1931 *
1932 * This methods does not respect the VT scroll region.
1933 *
1934 * @param {string} ch The character to use for the fill.
1935 */
1936hterm.Terminal.prototype.fill = function(ch) {
1937 var cursor = this.saveCursor();
1938
1939 this.setAbsoluteCursorPosition(0, 0);
1940 for (var row = 0; row < this.screenSize.height; row++) {
1941 for (var col = 0; col < this.screenSize.width; col++) {
1942 this.setAbsoluteCursorPosition(row, col);
1943 this.screen_.overwriteString(ch);
1944 }
1945 }
1946
1947 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001948};
1949
1950/**
rginda9ea433c2012-03-16 11:57:00 -07001951 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001952 *
rginda9ea433c2012-03-16 11:57:00 -07001953 * This does not respect the scroll region.
1954 *
1955 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1956 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001957 */
rginda9ea433c2012-03-16 11:57:00 -07001958hterm.Terminal.prototype.clearHome = function(opt_screen) {
1959 var screen = opt_screen || this.screen_;
1960 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001961
rginda11057d52012-04-25 12:29:56 -07001962 if (bottom == 0) {
1963 // Empty screen, nothing to do.
1964 return;
1965 }
1966
rgindae4d29232012-01-19 10:47:13 -08001967 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001968 screen.setCursorPosition(i, 0);
1969 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001970 }
1971
rginda9ea433c2012-03-16 11:57:00 -07001972 screen.setCursorPosition(0, 0);
1973};
1974
1975/**
1976 * Erase the entire display without changing the cursor position.
1977 *
1978 * The cursor position is unchanged. This does not respect the scroll
1979 * region.
1980 *
1981 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1982 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001983 */
1984hterm.Terminal.prototype.clear = function(opt_screen) {
1985 var screen = opt_screen || this.screen_;
1986 var cursor = screen.cursorPosition.clone();
1987 this.clearHome(screen);
1988 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001989};
1990
1991/**
1992 * VT command to insert lines at the current cursor row.
1993 *
1994 * This respects the current scroll region. Rows pushed off the bottom are
1995 * lost (they won't show up in the scrollback buffer).
1996 *
rginda8ba33642011-12-14 12:31:31 -08001997 * @param {integer} count The number of lines to insert.
1998 */
1999hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002000 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002001
2002 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002003 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002004
Robert Ginda579186b2012-09-26 11:40:04 -07002005 // The moveCount is the number of rows we need to relocate to make room for
2006 // the new row(s). The count is the distance to move them.
2007 var moveCount = bottom - cursorRow - count + 1;
2008 if (moveCount)
2009 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002010
Robert Ginda579186b2012-09-26 11:40:04 -07002011 for (var i = count - 1; i >= 0; i--) {
2012 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002013 this.screen_.clearCursorRow();
2014 }
rginda8ba33642011-12-14 12:31:31 -08002015};
2016
2017/**
2018 * VT command to delete lines at the current cursor row.
2019 *
2020 * New rows are added to the bottom of scroll region to take their place. New
2021 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002022 *
2023 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002024 */
2025hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002026 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002027
rginda87b86462011-12-14 13:48:03 -08002028 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002029 var bottom = this.getVTScrollBottom();
2030
rginda87b86462011-12-14 13:48:03 -08002031 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002032 count = Math.min(count, maxCount);
2033
rginda87b86462011-12-14 13:48:03 -08002034 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002035 if (count != maxCount)
2036 this.moveRows_(top, count, moveStart);
2037
2038 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002039 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002040 this.screen_.clearCursorRow();
2041 }
2042
rginda87b86462011-12-14 13:48:03 -08002043 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002044 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002045};
2046
2047/**
2048 * Inserts the given number of spaces at the current cursor position.
2049 *
rginda87b86462011-12-14 13:48:03 -08002050 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002051 *
2052 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002053 */
2054hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002055 var cursor = this.saveCursor();
2056
rgindacbbd7482012-06-13 15:06:16 -07002057 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08002058 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08002059 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002060
2061 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002062 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002063};
2064
2065/**
2066 * Forward-delete the specified number of characters starting at the cursor
2067 * position.
2068 *
2069 * @param {integer} count The number of characters to delete.
2070 */
2071hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002072 var deleted = this.screen_.deleteChars(count);
2073 if (deleted && !this.screen_.textAttributes.isDefault()) {
2074 var cursor = this.saveCursor();
2075 this.setCursorColumn(this.screenSize.width - deleted);
2076 this.screen_.insertString(lib.f.getWhitespace(deleted));
2077 this.restoreCursor(cursor);
2078 }
2079
David Benjamin54e8bf62012-06-01 22:31:40 -04002080 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002081};
2082
2083/**
2084 * Shift rows in the scroll region upwards by a given number of lines.
2085 *
2086 * New rows are inserted at the bottom of the scroll region to fill the
2087 * vacated rows. The new rows not filled out with the current text attributes.
2088 *
2089 * This function does not affect the scrollback rows at all. Rows shifted
2090 * off the top are lost.
2091 *
rginda87b86462011-12-14 13:48:03 -08002092 * The cursor position is not altered.
2093 *
rginda8ba33642011-12-14 12:31:31 -08002094 * @param {integer} count The number of rows to scroll.
2095 */
2096hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002097 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002098
rginda87b86462011-12-14 13:48:03 -08002099 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002100 this.deleteLines(count);
2101
rginda87b86462011-12-14 13:48:03 -08002102 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002103};
2104
2105/**
2106 * Shift rows below the cursor down by a given number of lines.
2107 *
2108 * This function respects the current scroll region.
2109 *
2110 * New rows are inserted at the top of the scroll region to fill the
2111 * vacated rows. The new rows not filled out with the current text attributes.
2112 *
2113 * This function does not affect the scrollback rows at all. Rows shifted
2114 * off the bottom are lost.
2115 *
2116 * @param {integer} count The number of rows to scroll.
2117 */
2118hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08002119 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002120
rginda87b86462011-12-14 13:48:03 -08002121 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08002122 this.insertLines(opt_count);
2123
rginda87b86462011-12-14 13:48:03 -08002124 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002125};
2126
rginda87b86462011-12-14 13:48:03 -08002127
rginda8ba33642011-12-14 12:31:31 -08002128/**
2129 * Set the cursor position.
2130 *
2131 * The cursor row is relative to the scroll region if the terminal has
2132 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2133 *
2134 * @param {integer} row The new zero-based cursor row.
2135 * @param {integer} row The new zero-based cursor column.
2136 */
2137hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2138 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002139 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002140 } else {
rginda87b86462011-12-14 13:48:03 -08002141 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002142 }
rginda87b86462011-12-14 13:48:03 -08002143};
rginda8ba33642011-12-14 12:31:31 -08002144
Evan Jones2600d4f2016-12-06 09:29:36 -05002145/**
2146 * Move the cursor relative to its current position.
2147 *
2148 * @param {number} row
2149 * @param {number} column
2150 */
rginda87b86462011-12-14 13:48:03 -08002151hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2152 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002153 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2154 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002155 this.screen_.setCursorPosition(row, column);
2156};
2157
Evan Jones2600d4f2016-12-06 09:29:36 -05002158/**
2159 * Move the cursor to the specified position.
2160 *
2161 * @param {number} row
2162 * @param {number} column
2163 */
rginda87b86462011-12-14 13:48:03 -08002164hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002165 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2166 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002167 this.screen_.setCursorPosition(row, column);
2168};
2169
2170/**
2171 * Set the cursor column.
2172 *
2173 * @param {integer} column The new zero-based cursor column.
2174 */
2175hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002176 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002177};
2178
2179/**
2180 * Return the cursor column.
2181 *
2182 * @return {integer} The zero-based cursor column.
2183 */
2184hterm.Terminal.prototype.getCursorColumn = function() {
2185 return this.screen_.cursorPosition.column;
2186};
2187
2188/**
2189 * Set the cursor row.
2190 *
2191 * The cursor row is relative to the scroll region if the terminal has
2192 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2193 *
2194 * @param {integer} row The new cursor row.
2195 */
rginda87b86462011-12-14 13:48:03 -08002196hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2197 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002198};
2199
2200/**
2201 * Return the cursor row.
2202 *
2203 * @return {integer} The zero-based cursor row.
2204 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002205hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002206 return this.screen_.cursorPosition.row;
2207};
2208
2209/**
2210 * Request that the ScrollPort redraw itself soon.
2211 *
2212 * The redraw will happen asynchronously, soon after the call stack winds down.
2213 * Multiple calls will be coalesced into a single redraw.
2214 */
2215hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002216 if (this.timeouts_.redraw)
2217 return;
rginda8ba33642011-12-14 12:31:31 -08002218
2219 var self = this;
rginda87b86462011-12-14 13:48:03 -08002220 this.timeouts_.redraw = setTimeout(function() {
2221 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002222 self.scrollPort_.redraw_();
2223 }, 0);
2224};
2225
2226/**
2227 * Request that the ScrollPort be scrolled to the bottom.
2228 *
2229 * The scroll will happen asynchronously, soon after the call stack winds down.
2230 * Multiple calls will be coalesced into a single scroll.
2231 *
2232 * This affects the scrollbar position of the ScrollPort, and has nothing to
2233 * do with the VT scroll commands.
2234 */
2235hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2236 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002237 return;
rginda8ba33642011-12-14 12:31:31 -08002238
2239 var self = this;
2240 this.timeouts_.scrollDown = setTimeout(function() {
2241 delete self.timeouts_.scrollDown;
2242 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2243 }, 10);
2244};
2245
2246/**
2247 * Move the cursor up a specified number of rows.
2248 *
2249 * @param {integer} count The number of rows to move the cursor.
2250 */
2251hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002252 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002253};
2254
2255/**
2256 * Move the cursor down a specified number of rows.
2257 *
2258 * @param {integer} count The number of rows to move the cursor.
2259 */
2260hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002261 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002262 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2263 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2264 this.screenSize.height - 1);
2265
rgindacbbd7482012-06-13 15:06:16 -07002266 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002267 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002268 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002269};
2270
2271/**
2272 * Move the cursor left a specified number of columns.
2273 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002274 * If reverse wraparound mode is enabled and the previous row wrapped into
2275 * the current row then we back up through the wraparound as well.
2276 *
rginda8ba33642011-12-14 12:31:31 -08002277 * @param {integer} count The number of columns to move the cursor.
2278 */
2279hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002280 count = count || 1;
2281
2282 if (count < 1)
2283 return;
2284
2285 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002286 if (this.options_.reverseWraparound) {
2287 if (this.screen_.cursorPosition.overflow) {
2288 // If this cursor is in the right margin, consume one count to get it
2289 // back to the last column. This only applies when we're in reverse
2290 // wraparound mode.
2291 count--;
2292 this.clearCursorOverflow();
2293
2294 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002295 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002296 }
2297
Robert Gindabfb32622014-07-17 13:20:27 -07002298 var newRow = this.screen_.cursorPosition.row;
2299 var newColumn = currentColumn - count;
2300 if (newColumn < 0) {
2301 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2302 if (newRow < 0) {
2303 // xterm also wraps from row 0 to the last row.
2304 newRow = this.screenSize.height + newRow % this.screenSize.height;
2305 }
2306 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2307 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002308
Robert Gindabfb32622014-07-17 13:20:27 -07002309 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2310
2311 } else {
2312 var newColumn = Math.max(currentColumn - count, 0);
2313 this.setCursorColumn(newColumn);
2314 }
rginda8ba33642011-12-14 12:31:31 -08002315};
2316
2317/**
2318 * Move the cursor right a specified number of columns.
2319 *
2320 * @param {integer} count The number of columns to move the cursor.
2321 */
2322hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002323 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002324
2325 if (count < 1)
2326 return;
2327
rgindacbbd7482012-06-13 15:06:16 -07002328 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002329 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002330 this.setCursorColumn(column);
2331};
2332
2333/**
2334 * Reverse the foreground and background colors of the terminal.
2335 *
2336 * This only affects text that was drawn with no attributes.
2337 *
2338 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2339 * been drawn with attributes that happen to coincide with the default
2340 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002341 *
2342 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002343 */
2344hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002345 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002346 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002347 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2348 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002349 } else {
rginda9f5222b2012-03-05 11:53:28 -08002350 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2351 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002352 }
2353};
2354
2355/**
rginda87b86462011-12-14 13:48:03 -08002356 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002357 *
2358 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002359 */
2360hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002361 this.cursorNode_.style.backgroundColor =
2362 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002363
2364 var self = this;
2365 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08002366 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08002367 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002368
Michael Kelly485ecd12014-06-09 11:41:56 -04002369 // bellSquelchTimeout_ affects both audio and notification bells.
2370 if (this.bellSquelchTimeout_)
2371 return;
2372
Robert Ginda92e18102013-03-14 13:56:37 -07002373 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002374 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002375 this.bellSequelchTimeout_ = setTimeout(function() {
2376 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002377 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002378 } else {
2379 delete this.bellSquelchTimeout_;
2380 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002381
2382 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
2383 var n = new Notification(
2384 lib.f.replaceVars(hterm.desktopNotificationTitle,
Robert Ginda348dc2b2014-06-24 14:42:23 -07002385 {'title': window.document.title || 'hterm'}));
Michael Kelly485ecd12014-06-09 11:41:56 -04002386 this.bellNotificationList_.push(n);
2387 // TODO: Should we try to raise the window here?
2388 n.onclick = function() { self.closeBellNotifications_(); };
2389 }
rginda87b86462011-12-14 13:48:03 -08002390};
2391
2392/**
rginda8ba33642011-12-14 12:31:31 -08002393 * Set the origin mode bit.
2394 *
2395 * If origin mode is on, certain VT cursor and scrolling commands measure their
2396 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2397 * to the top of the addressable screen.
2398 *
2399 * Defaults to off.
2400 *
2401 * @param {boolean} state True to set origin mode, false to unset.
2402 */
2403hterm.Terminal.prototype.setOriginMode = function(state) {
2404 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002405 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002406};
2407
2408/**
2409 * Set the insert mode bit.
2410 *
2411 * If insert mode is on, existing text beyond the cursor position will be
2412 * shifted right to make room for new text. Otherwise, new text overwrites
2413 * any existing text.
2414 *
2415 * Defaults to off.
2416 *
2417 * @param {boolean} state True to set insert mode, false to unset.
2418 */
2419hterm.Terminal.prototype.setInsertMode = function(state) {
2420 this.options_.insertMode = state;
2421};
2422
2423/**
rginda87b86462011-12-14 13:48:03 -08002424 * Set the auto carriage return bit.
2425 *
2426 * If auto carriage return is on then a formfeed character is interpreted
2427 * as a newline, otherwise it's the same as a linefeed. The difference boils
2428 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002429 *
2430 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002431 */
2432hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2433 this.options_.autoCarriageReturn = state;
2434};
2435
2436/**
rginda8ba33642011-12-14 12:31:31 -08002437 * Set the wraparound mode bit.
2438 *
2439 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2440 * to the start of the following row. Otherwise, the cursor is clamped to the
2441 * end of the screen and attempts to write past it are ignored.
2442 *
2443 * Defaults to on.
2444 *
2445 * @param {boolean} state True to set wraparound mode, false to unset.
2446 */
2447hterm.Terminal.prototype.setWraparound = function(state) {
2448 this.options_.wraparound = state;
2449};
2450
2451/**
2452 * Set the reverse-wraparound mode bit.
2453 *
2454 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2455 * to the end of the previous row. Otherwise, the cursor is clamped to column
2456 * 0.
2457 *
2458 * Defaults to off.
2459 *
2460 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2461 */
2462hterm.Terminal.prototype.setReverseWraparound = function(state) {
2463 this.options_.reverseWraparound = state;
2464};
2465
2466/**
2467 * Selects between the primary and alternate screens.
2468 *
2469 * If alternate mode is on, the alternate screen is active. Otherwise the
2470 * primary screen is active.
2471 *
2472 * Swapping screens has no effect on the scrollback buffer.
2473 *
2474 * Each screen maintains its own cursor position.
2475 *
2476 * Defaults to off.
2477 *
2478 * @param {boolean} state True to set alternate mode, false to unset.
2479 */
2480hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002481 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002482 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2483
rginda35c456b2012-02-09 17:29:05 -08002484 if (this.screen_.rowsArray.length &&
2485 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2486 // If the screen changed sizes while we were away, our rowIndexes may
2487 // be incorrect.
2488 var offset = this.scrollbackRows_.length;
2489 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002490 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002491 ary[i].rowIndex = offset + i;
2492 }
2493 }
rginda8ba33642011-12-14 12:31:31 -08002494
rginda35c456b2012-02-09 17:29:05 -08002495 this.realizeWidth_(this.screenSize.width);
2496 this.realizeHeight_(this.screenSize.height);
2497 this.scrollPort_.syncScrollHeight();
2498 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002499
rginda6d397402012-01-17 10:58:29 -08002500 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002501 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002502};
2503
2504/**
2505 * Set the cursor-blink mode bit.
2506 *
2507 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2508 * a visible cursor does not blink.
2509 *
2510 * You should make sure to turn blinking off if you're going to dispose of a
2511 * terminal, otherwise you'll leak a timeout.
2512 *
2513 * Defaults to on.
2514 *
2515 * @param {boolean} state True to set cursor-blink mode, false to unset.
2516 */
2517hterm.Terminal.prototype.setCursorBlink = function(state) {
2518 this.options_.cursorBlink = state;
2519
2520 if (!state && this.timeouts_.cursorBlink) {
2521 clearTimeout(this.timeouts_.cursorBlink);
2522 delete this.timeouts_.cursorBlink;
2523 }
2524
2525 if (this.options_.cursorVisible)
2526 this.setCursorVisible(true);
2527};
2528
2529/**
2530 * Set the cursor-visible mode bit.
2531 *
2532 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2533 *
2534 * Defaults to on.
2535 *
2536 * @param {boolean} state True to set cursor-visible mode, false to unset.
2537 */
2538hterm.Terminal.prototype.setCursorVisible = function(state) {
2539 this.options_.cursorVisible = state;
2540
2541 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002542 if (this.timeouts_.cursorBlink) {
2543 clearTimeout(this.timeouts_.cursorBlink);
2544 delete this.timeouts_.cursorBlink;
2545 }
rginda87b86462011-12-14 13:48:03 -08002546 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002547 return;
2548 }
2549
rginda87b86462011-12-14 13:48:03 -08002550 this.syncCursorPosition_();
2551
2552 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002553
2554 if (this.options_.cursorBlink) {
2555 if (this.timeouts_.cursorBlink)
2556 return;
2557
Robert Gindaea2183e2014-07-17 09:51:51 -07002558 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002559 } else {
2560 if (this.timeouts_.cursorBlink) {
2561 clearTimeout(this.timeouts_.cursorBlink);
2562 delete this.timeouts_.cursorBlink;
2563 }
2564 }
2565};
2566
2567/**
rginda87b86462011-12-14 13:48:03 -08002568 * Synchronizes the visible cursor and document selection with the current
2569 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002570 */
2571hterm.Terminal.prototype.syncCursorPosition_ = function() {
2572 var topRowIndex = this.scrollPort_.getTopRowIndex();
2573 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2574 var cursorRowIndex = this.scrollbackRows_.length +
2575 this.screen_.cursorPosition.row;
2576
2577 if (cursorRowIndex > bottomRowIndex) {
2578 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002579 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002580 return;
2581 }
2582
Robert Gindab837c052014-08-11 11:17:51 -07002583 if (this.options_.cursorVisible &&
2584 this.cursorNode_.style.display == 'none') {
2585 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2586 this.cursorNode_.style.display = '';
2587 }
2588
2589
rginda8ba33642011-12-14 12:31:31 -08002590 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002591 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2592 'px';
2593 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2594 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002595
2596 this.cursorNode_.setAttribute('title',
2597 '(' + this.screen_.cursorPosition.row +
2598 ', ' + this.screen_.cursorPosition.column +
2599 ')');
2600
2601 // Update the caret for a11y purposes.
2602 var selection = this.document_.getSelection();
2603 if (selection && selection.isCollapsed)
2604 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002605};
2606
Robert Gindafb1be6a2013-12-11 11:56:22 -08002607/**
2608 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2609 * and character cell dimensions.
2610 */
Robert Ginda830583c2013-08-07 13:20:46 -07002611hterm.Terminal.prototype.restyleCursor_ = function() {
2612 var shape = this.cursorShape_;
2613
2614 if (this.cursorNode_.getAttribute('focus') == 'false') {
2615 // Always show a block cursor when unfocused.
2616 shape = hterm.Terminal.cursorShape.BLOCK;
2617 }
2618
2619 var style = this.cursorNode_.style;
2620
Robert Gindafb1be6a2013-12-11 11:56:22 -08002621 style.width = this.scrollPort_.characterSize.width + 'px';
2622
Robert Ginda830583c2013-08-07 13:20:46 -07002623 switch (shape) {
2624 case hterm.Terminal.cursorShape.BEAM:
2625 style.height = this.scrollPort_.characterSize.height + 'px';
2626 style.backgroundColor = 'transparent';
2627 style.borderBottomStyle = null;
2628 style.borderLeftStyle = 'solid';
2629 break;
2630
2631 case hterm.Terminal.cursorShape.UNDERLINE:
2632 style.height = this.scrollPort_.characterSize.baseline + 'px';
2633 style.backgroundColor = 'transparent';
2634 style.borderBottomStyle = 'solid';
2635 // correct the size to put it exactly at the baseline
2636 style.borderLeftStyle = null;
2637 break;
2638
2639 default:
2640 style.height = this.scrollPort_.characterSize.height + 'px';
2641 style.backgroundColor = this.cursorColor_;
2642 style.borderBottomStyle = null;
2643 style.borderLeftStyle = null;
2644 break;
2645 }
2646};
2647
rginda8ba33642011-12-14 12:31:31 -08002648/**
2649 * Synchronizes the visible cursor with the current cursor coordinates.
2650 *
2651 * The sync will happen asynchronously, soon after the call stack winds down.
2652 * Multiple calls will be coalesced into a single sync.
2653 */
2654hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2655 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002656 return;
rginda8ba33642011-12-14 12:31:31 -08002657
2658 var self = this;
2659 this.timeouts_.syncCursor = setTimeout(function() {
2660 self.syncCursorPosition_();
2661 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002662 }, 0);
2663};
2664
rgindacc2996c2012-02-24 14:59:31 -08002665/**
rgindaf522ce02012-04-17 17:49:17 -07002666 * Show or hide the zoom warning.
2667 *
2668 * The zoom warning is a message warning the user that their browser zoom must
2669 * be set to 100% in order for hterm to function properly.
2670 *
2671 * @param {boolean} state True to show the message, false to hide it.
2672 */
2673hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2674 if (!this.zoomWarningNode_) {
2675 if (!state)
2676 return;
2677
2678 this.zoomWarningNode_ = this.document_.createElement('div');
2679 this.zoomWarningNode_.style.cssText = (
2680 'color: black;' +
2681 'background-color: #ff2222;' +
2682 'font-size: large;' +
2683 'border-radius: 8px;' +
2684 'opacity: 0.75;' +
2685 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2686 'top: 0.5em;' +
2687 'right: 1.2em;' +
2688 'position: absolute;' +
2689 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002690 '-webkit-user-select: none;' +
2691 '-moz-text-size-adjust: none;' +
2692 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002693
2694 this.zoomWarningNode_.addEventListener('click', function(e) {
2695 this.parentNode.removeChild(this);
2696 });
rgindaf522ce02012-04-17 17:49:17 -07002697 }
2698
Robert Gindab4839c22013-02-28 16:52:10 -08002699 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2700 hterm.zoomWarningMessage,
2701 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2702
rgindaf522ce02012-04-17 17:49:17 -07002703 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2704
2705 if (state) {
2706 if (!this.zoomWarningNode_.parentNode)
2707 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2708 } else if (this.zoomWarningNode_.parentNode) {
2709 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2710 }
2711};
2712
2713/**
rgindacc2996c2012-02-24 14:59:31 -08002714 * Show the terminal overlay for a given amount of time.
2715 *
2716 * The terminal overlay appears in inverse video in a large font, centered
2717 * over the terminal. You should probably keep the overlay message brief,
2718 * since it's in a large font and you probably aren't going to check the size
2719 * of the terminal first.
2720 *
2721 * @param {string} msg The text (not HTML) message to display in the overlay.
2722 * @param {number} opt_timeout The amount of time to wait before fading out
2723 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2724 * stay up forever (or until the next overlay).
2725 */
2726hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002727 if (!this.overlayNode_) {
2728 if (!this.div_)
2729 return;
2730
2731 this.overlayNode_ = this.document_.createElement('div');
2732 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002733 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002734 'font-size: xx-large;' +
2735 'opacity: 0.75;' +
2736 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2737 'position: absolute;' +
2738 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002739 '-webkit-transition: opacity 180ms ease-in;' +
2740 '-moz-user-select: none;' +
2741 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002742
2743 this.overlayNode_.addEventListener('mousedown', function(e) {
2744 e.preventDefault();
2745 e.stopPropagation();
2746 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002747 }
2748
rginda9f5222b2012-03-05 11:53:28 -08002749 this.overlayNode_.style.color = this.prefs_.get('background-color');
2750 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2751 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2752
rgindaf0090c92012-02-10 14:58:52 -08002753 this.overlayNode_.textContent = msg;
2754 this.overlayNode_.style.opacity = '0.75';
2755
2756 if (!this.overlayNode_.parentNode)
2757 this.div_.appendChild(this.overlayNode_);
2758
Robert Ginda97769282013-02-01 15:30:30 -08002759 var divSize = hterm.getClientSize(this.div_);
2760 var overlaySize = hterm.getClientSize(this.overlayNode_);
2761
Robert Ginda8a59f762014-07-23 11:29:55 -07002762 this.overlayNode_.style.top =
2763 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08002764 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07002765 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08002766
2767 var self = this;
2768
2769 if (this.overlayTimeout_)
2770 clearTimeout(this.overlayTimeout_);
2771
rgindacc2996c2012-02-24 14:59:31 -08002772 if (opt_timeout === null)
2773 return;
2774
rgindaf0090c92012-02-10 14:58:52 -08002775 this.overlayTimeout_ = setTimeout(function() {
2776 self.overlayNode_.style.opacity = '0';
Robert Ginda70926e42013-11-25 14:56:36 -08002777 self.overlayTimeout_ = setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002778 if (self.overlayNode_.parentNode)
2779 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002780 self.overlayTimeout_ = null;
2781 self.overlayNode_.style.opacity = '0.75';
2782 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002783 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002784};
2785
rginda4bba5e12012-06-20 16:15:30 -07002786/**
2787 * Paste from the system clipboard to the terminal.
2788 */
2789hterm.Terminal.prototype.paste = function() {
2790 hterm.pasteFromClipboard(this.document_);
2791};
2792
2793/**
2794 * Copy a string to the system clipboard.
2795 *
2796 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05002797 *
2798 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07002799 */
2800hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002801 if (this.prefs_.get('enable-clipboard-notice'))
2802 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2803
rgindaa09e7332012-08-17 12:49:51 -07002804 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002805 copySource.textContent = str;
2806 copySource.style.cssText = (
2807 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002808 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07002809 'position: absolute;' +
2810 'top: -99px');
2811
2812 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002813
rginda4bba5e12012-06-20 16:15:30 -07002814 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002815 var anchorNode = selection.anchorNode;
2816 var anchorOffset = selection.anchorOffset;
2817 var focusNode = selection.focusNode;
2818 var focusOffset = selection.focusOffset;
2819
rginda4bba5e12012-06-20 16:15:30 -07002820 selection.selectAllChildren(copySource);
2821
rgindaa09e7332012-08-17 12:49:51 -07002822 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002823
Rob Spies56953412014-04-28 14:09:47 -07002824 // IE doesn't support selection.extend. This means that the selection
2825 // won't return on IE.
Robert Ginda6f4f0aa2014-07-28 10:21:39 -07002826 if (selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07002827 selection.collapse(anchorNode, anchorOffset);
2828 selection.extend(focusNode, focusOffset);
2829 }
rgindafaa74742012-08-21 13:34:03 -07002830
rginda4bba5e12012-06-20 16:15:30 -07002831 copySource.parentNode.removeChild(copySource);
2832};
2833
Evan Jones2600d4f2016-12-06 09:29:36 -05002834/**
2835 * Returns the selected text, or null if no text is selected.
2836 *
2837 * @return {string|null}
2838 */
rgindaa09e7332012-08-17 12:49:51 -07002839hterm.Terminal.prototype.getSelectionText = function() {
2840 var selection = this.scrollPort_.selection;
2841 selection.sync();
2842
2843 if (selection.isCollapsed)
2844 return null;
2845
2846
2847 // Start offset measures from the beginning of the line.
2848 var startOffset = selection.startOffset;
2849 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002850
Robert Gindafdbb3f22012-09-06 20:23:06 -07002851 if (node.nodeName != 'X-ROW') {
2852 // If the selection doesn't start on an x-row node, then it must be
2853 // somewhere inside the x-row. Add any characters from previous siblings
2854 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002855
2856 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2857 // If node is the text node in a styled span, move up to the span node.
2858 node = node.parentNode;
2859 }
2860
Robert Gindafdbb3f22012-09-06 20:23:06 -07002861 while (node.previousSibling) {
2862 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002863 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002864 }
rgindaa09e7332012-08-17 12:49:51 -07002865 }
2866
2867 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002868 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
2869 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05002870 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002871
Robert Gindafdbb3f22012-09-06 20:23:06 -07002872 if (node.nodeName != 'X-ROW') {
2873 // If the selection doesn't end on an x-row node, then it must be
2874 // somewhere inside the x-row. Add any characters from following siblings
2875 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002876
2877 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2878 // If node is the text node in a styled span, move up to the span node.
2879 node = node.parentNode;
2880 }
2881
Robert Gindafdbb3f22012-09-06 20:23:06 -07002882 while (node.nextSibling) {
2883 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002884 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002885 }
rgindaa09e7332012-08-17 12:49:51 -07002886 }
2887
2888 var rv = this.getRowsText(selection.startRow.rowIndex,
2889 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002890 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002891};
2892
rginda4bba5e12012-06-20 16:15:30 -07002893/**
2894 * Copy the current selection to the system clipboard, then clear it after a
2895 * short delay.
2896 */
2897hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002898 var text = this.getSelectionText();
2899 if (text != null)
2900 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002901};
2902
rgindaf0090c92012-02-10 14:58:52 -08002903hterm.Terminal.prototype.overlaySize = function() {
2904 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2905};
2906
rginda87b86462011-12-14 13:48:03 -08002907/**
2908 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2909 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07002910 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08002911 */
2912hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002913 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002914 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2915
Robert Ginda8cb7d902013-06-20 14:37:18 -07002916 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08002917};
2918
2919/**
Mike Frysinger70b94692017-01-26 18:57:50 -10002920 * Launches url in a new tab.
2921 *
2922 * @param {string} url URL to launch in a new tab.
2923 */
2924hterm.Terminal.prototype.openUrl = function(url) {
2925 var win = window.open(url, '_blank');
2926 win.focus();
2927}
2928
2929/**
2930 * Open the selected url.
2931 */
2932hterm.Terminal.prototype.openSelectedUrl_ = function() {
2933 var str = this.getSelectionText();
2934
2935 // If there is no selection, try and expand wherever they clicked.
2936 if (str == null) {
2937 this.screen_.expandSelection(this.document_.getSelection());
2938 str = this.getSelectionText();
2939 }
2940
2941 // Make sure URL is valid before opening.
2942 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
2943 return;
2944 // If the URL isn't anchored, it'll open relative to the extension.
2945 // We have no way of knowing the correct schema, so assume http.
2946 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0)
2947 str = 'http://' + str;
2948
2949 this.openUrl(str);
2950}
2951
2952
2953/**
rgindad5613292012-06-19 15:40:37 -07002954 * Add the terminalRow and terminalColumn properties to mouse events and
2955 * then forward on to onMouse().
2956 *
2957 * The terminalRow and terminalColumn properties contain the (row, column)
2958 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05002959 *
2960 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07002961 */
2962hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002963 if (e.processedByTerminalHandler_) {
2964 // We register our event handlers on the document, as well as the cursor
2965 // and the scroll blocker. Mouse events that occur on the cursor or
2966 // scroll blocker will also appear on the document, but we don't want to
2967 // process them twice.
2968 //
2969 // We can't just prevent bubbling because that has other side effects, so
2970 // we decorate the event object with this property instead.
2971 return;
2972 }
2973
Robert Ginda6aec7eb2015-06-16 10:31:30 -07002974 var reportMouseEvents = (!this.defeatMouseReports_ &&
2975 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
2976
rgindafaa74742012-08-21 13:34:03 -07002977 e.processedByTerminalHandler_ = true;
2978
Robert Gindaeda48db2014-07-17 09:25:30 -07002979 // One based row/column stored on the mouse event.
2980 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2981 this.scrollPort_.characterSize.height) + 1;
2982 e.terminalColumn = parseInt(e.clientX /
2983 this.scrollPort_.characterSize.width) + 1;
2984
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002985 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
2986 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07002987 return;
2988 }
2989
Robert Ginda6aec7eb2015-06-16 10:31:30 -07002990 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07002991 // If the cursor is visible and we're not sending mouse events to the
2992 // host app, then we want to hide the terminal cursor when the mouse
2993 // cursor is over top. This keeps the terminal cursor from interfering
2994 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07002995 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
2996 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
2997 this.cursorNode_.style.display = 'none';
2998 } else if (this.cursorNode_.style.display == 'none') {
2999 this.cursorNode_.style.display = '';
3000 }
3001 }
rgindad5613292012-06-19 15:40:37 -07003002
Robert Ginda928cf632014-03-05 15:07:41 -08003003 if (e.type == 'mousedown') {
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003004 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003005 // If VT mouse reporting is disabled, or has been defeated with
3006 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003007 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003008 this.setSelectionEnabled(true);
3009 } else {
3010 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003011 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003012 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003013 this.setSelectionEnabled(false);
3014 e.preventDefault();
3015 }
3016 }
3017
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003018 if (!reportMouseEvents) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003019 if (e.type == 'dblclick' && this.copyOnSelect) {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003020 this.screen_.expandSelection(this.document_.getSelection());
Robert Ginda15ed4902016-07-12 10:43:22 -07003021 this.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07003022 }
3023
Mike Frysinger70b94692017-01-26 18:57:50 -10003024 if (e.type == 'click' && !e.shiftKey && e.ctrlKey) {
3025 // Debounce this event with the dblclick event. If you try to doubleclick
3026 // a URL to open it, Chrome will fire click then dblclick, but we won't
3027 // have expanded the selection text at the first click event.
3028 clearTimeout(this.timeouts_.openUrl);
3029 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3030 500);
3031 return;
3032 }
3033
Mike Frysinger847577f2017-05-23 23:25:57 -04003034 if (e.type == 'mousedown') {
3035 if ((this.mouseRightClickPaste && e.button == 2 /* right button */) ||
3036 e.which == this.mousePasteButton) {
3037 this.paste();
3038 }
3039 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003040
3041 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
3042 !this.document_.getSelection().isCollapsed) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003043 this.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003044 }
3045
3046 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3047 this.scrollBlockerNode_.engaged) {
3048 // Disengage the scroll-blocker after one of these events.
3049 this.scrollBlockerNode_.engaged = false;
3050 this.scrollBlockerNode_.style.top = '-99px';
3051 }
3052
Robert Ginda928cf632014-03-05 15:07:41 -08003053 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003054 if (!this.scrollBlockerNode_.engaged) {
3055 if (e.type == 'mousedown') {
3056 // Move the scroll-blocker into place if we want to keep the scrollport
3057 // from scrolling.
3058 this.scrollBlockerNode_.engaged = true;
3059 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3060 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3061 } else if (e.type == 'mousemove') {
3062 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3063 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003064 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003065 e.preventDefault();
3066 }
3067 }
Robert Ginda928cf632014-03-05 15:07:41 -08003068
3069 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003070 }
3071
Robert Ginda928cf632014-03-05 15:07:41 -08003072 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3073 // Restore this on mouseup in case it was temporarily defeated with a
3074 // alt-mousedown. Only do this when the selection is empty so that
3075 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003076 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003077 }
rgindad5613292012-06-19 15:40:37 -07003078};
3079
3080/**
3081 * Clients should override this if they care to know about mouse events.
3082 *
3083 * The event parameter will be a normal DOM mouse click event with additional
3084 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003085 *
3086 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003087 */
3088hterm.Terminal.prototype.onMouse = function(e) { };
3089
3090/**
rginda8e92a692012-05-20 19:37:20 -07003091 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003092 *
3093 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003094 */
Rob Spies06533ba2014-04-24 11:20:37 -07003095hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3096 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003097 this.restyleCursor_();
Michael Kelly485ecd12014-06-09 11:41:56 -04003098 if (focused === true)
3099 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003100};
3101
3102/**
rginda8ba33642011-12-14 12:31:31 -08003103 * React when the ScrollPort is scrolled.
3104 */
3105hterm.Terminal.prototype.onScroll_ = function() {
3106 this.scheduleSyncCursorPosition_();
3107};
3108
3109/**
rginda9846e2f2012-01-27 13:53:33 -08003110 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003111 *
3112 * @param {Event} e The DOM paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003113 */
3114hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07003115 var data = e.text.replace(/\n/mg, '\r');
Connor Hegartyf525ceb2014-09-03 13:36:03 -07003116 data = this.keyboard.encode(data);
Robert Gindaa063b202014-07-21 11:08:25 -07003117 if (this.options_.bracketedPaste)
3118 data = '\x1b[200~' + data + '\x1b[201~';
3119
3120 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003121};
3122
3123/**
rgindaa09e7332012-08-17 12:49:51 -07003124 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003125 *
3126 * @param {Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003127 */
3128hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003129 if (!this.useDefaultWindowCopy) {
3130 e.preventDefault();
3131 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3132 }
rgindaa09e7332012-08-17 12:49:51 -07003133};
3134
3135/**
rginda8ba33642011-12-14 12:31:31 -08003136 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003137 *
3138 * Note: This function should not directly contain code that alters the internal
3139 * state of the terminal. That kind of code belongs in realizeWidth or
3140 * realizeHeight, so that it can be executed synchronously in the case of a
3141 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003142 */
3143hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003144 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003145 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003146 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003147 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003148
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003149 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003150 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003151 // gets removed from the document or during the initial load, and we can't
3152 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003153 // This can also happen if called before the scrollPort calculates the
3154 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003155 return;
3156 }
3157
rgindaa8ba17d2012-08-15 14:41:10 -07003158 var isNewSize = (columnCount != this.screenSize.width ||
3159 rowCount != this.screenSize.height);
3160
3161 // We do this even if the size didn't change, just to be sure everything is
3162 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003163 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003164 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003165
3166 if (isNewSize)
3167 this.overlaySize();
3168
Robert Gindafb1be6a2013-12-11 11:56:22 -08003169 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003170 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08003171};
3172
3173/**
3174 * Service the cursor blink timeout.
3175 */
3176hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003177 if (!this.options_.cursorBlink) {
3178 delete this.timeouts_.cursorBlink;
3179 return;
3180 }
3181
Robert Ginda830583c2013-08-07 13:20:46 -07003182 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3183 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003184 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003185 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3186 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003187 } else {
rginda87b86462011-12-14 13:48:03 -08003188 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003189 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3190 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003191 }
3192};
David Reveman8f552492012-03-28 12:18:41 -04003193
3194/**
3195 * Set the scrollbar-visible mode bit.
3196 *
3197 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3198 * Otherwise it will not.
3199 *
3200 * Defaults to on.
3201 *
3202 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3203 */
3204hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3205 this.scrollPort_.setScrollbarVisible(state);
3206};
Michael Kelly485ecd12014-06-09 11:41:56 -04003207
3208/**
Rob Spies49039e52014-12-17 13:40:04 -08003209 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003210 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003211 *
3212 * Defaults to 1.
3213 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003214 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003215 */
3216hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3217 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3218};
3219
3220/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003221 * Close all web notifications created by terminal bells.
3222 */
3223hterm.Terminal.prototype.closeBellNotifications_ = function() {
3224 this.bellNotificationList_.forEach(function(n) {
3225 n.close();
3226 });
3227 this.bellNotificationList_.length = 0;
3228};