blob: e0187e2a139ef7817a838a7eab35f209c2009500 [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;
rginda4bba5e12012-06-20 16:15:30 -0700138 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700139
Zhu Qunying30d40712017-03-14 16:27:00 -0700140 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700141 this.useDefaultWindowCopy = false;
142
143 this.clearSelectionAfterCopy = true;
144
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400145 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800146 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700147
148 this.setProfile(opt_profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500149 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800150};
151
152/**
Robert Ginda830583c2013-08-07 13:20:46 -0700153 * Possible cursor shapes.
154 */
155hterm.Terminal.cursorShape = {
156 BLOCK: 'BLOCK',
157 BEAM: 'BEAM',
158 UNDERLINE: 'UNDERLINE'
159};
160
161/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700162 * Clients should override this to be notified when the terminal is ready
163 * for use.
164 *
165 * The terminal initialization is asynchronous, and shouldn't be used before
166 * this method is called.
167 */
168hterm.Terminal.prototype.onTerminalReady = function() { };
169
170/**
rginda35c456b2012-02-09 17:29:05 -0800171 * Default tab with of 8 to match xterm.
172 */
173hterm.Terminal.prototype.tabWidth = 8;
174
175/**
rginda9f5222b2012-03-05 11:53:28 -0800176 * Select a preference profile.
177 *
178 * This will load the terminal preferences for the given profile name and
179 * associate subsequent preference changes with the new preference profile.
180 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500181 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800182 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700183 * @param {function} opt_callback Optional callback to invoke when the profile
184 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800185 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700186hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
187 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800188
Robert Ginda57f03b42012-09-13 11:02:48 -0700189 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800190
Robert Ginda57f03b42012-09-13 11:02:48 -0700191 if (this.prefs_)
192 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800193
Robert Ginda57f03b42012-09-13 11:02:48 -0700194 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
195 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800196 'alt-gr-mode': function(v) {
197 if (v == null) {
198 if (navigator.language.toLowerCase() == 'en-us') {
199 v = 'none';
200 } else {
201 v = 'right-alt';
202 }
203 } else if (typeof v == 'string') {
204 v = v.toLowerCase();
205 } else {
206 v = 'none';
207 }
208
209 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v))
210 v = 'none';
211
212 terminal.keyboard.altGrMode = v;
213 },
214
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700215 'alt-backspace-is-meta-backspace': function(v) {
216 terminal.keyboard.altBackspaceIsMetaBackspace = v;
217 },
218
Robert Ginda57f03b42012-09-13 11:02:48 -0700219 'alt-is-meta': function(v) {
220 terminal.keyboard.altIsMeta = v;
221 },
222
223 'alt-sends-what': function(v) {
224 if (!/^(escape|8-bit|browser-key)$/.test(v))
225 v = 'escape';
226
227 terminal.keyboard.altSendsWhat = v;
228 },
229
230 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800231 var ary = v.match(/^lib-resource:(\S+)/);
232 if (ary) {
233 terminal.bellAudio_.setAttribute('src',
234 lib.resource.getDataUrl(ary[1]));
235 } else {
236 terminal.bellAudio_.setAttribute('src', v);
237 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700238 },
239
Michael Kelly485ecd12014-06-09 11:41:56 -0400240 'desktop-notification-bell': function(v) {
241 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700242 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400243 Notification.permission === 'granted';
244 if (!terminal.desktopNotificationBell_) {
245 // Note: We don't call Notification.requestPermission here because
246 // Chrome requires the call be the result of a user action (such as an
247 // onclick handler), and pref listeners are run asynchronously.
248 //
249 // A way of working around this would be to display a dialog in the
250 // terminal with a "click-to-request-permission" button.
251 console.warn('desktop-notification-bell is true but we do not have ' +
252 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400253 }
254 } else {
255 terminal.desktopNotificationBell_ = false;
256 }
257 },
258
Robert Ginda57f03b42012-09-13 11:02:48 -0700259 'background-color': function(v) {
260 terminal.setBackgroundColor(v);
261 },
262
263 'background-image': function(v) {
264 terminal.scrollPort_.setBackgroundImage(v);
265 },
266
267 'background-size': function(v) {
268 terminal.scrollPort_.setBackgroundSize(v);
269 },
270
271 'background-position': function(v) {
272 terminal.scrollPort_.setBackgroundPosition(v);
273 },
274
275 'backspace-sends-backspace': function(v) {
276 terminal.keyboard.backspaceSendsBackspace = v;
277 },
278
Brad Town18654b62015-03-12 00:27:45 -0700279 'character-map-overrides': function(v) {
280 if (!(v == null || v instanceof Object)) {
281 console.warn('Preference character-map-modifications is not an ' +
282 'object: ' + v);
283 return;
284 }
285
286 for (var code in v) {
287 var glmap = hterm.VT.CharacterMap.maps[code].glmap;
288 for (var received in v[code]) {
289 glmap[received] = v[code][received];
290 }
291 hterm.VT.CharacterMap.maps[code].reset(glmap);
292 }
293 },
294
Robert Ginda57f03b42012-09-13 11:02:48 -0700295 'cursor-blink': function(v) {
296 terminal.setCursorBlink(!!v);
297 },
298
Robert Gindaea2183e2014-07-17 09:51:51 -0700299 'cursor-blink-cycle': function(v) {
300 if (v instanceof Array &&
301 typeof v[0] == 'number' &&
302 typeof v[1] == 'number') {
303 terminal.cursorBlinkCycle_ = v;
304 } else if (typeof v == 'number') {
305 terminal.cursorBlinkCycle_ = [v, v];
306 } else {
307 // Fast blink indicates an error.
308 terminal.cursorBlinkCycle_ = [100, 100];
309 }
310 },
311
Robert Ginda57f03b42012-09-13 11:02:48 -0700312 'cursor-color': function(v) {
313 terminal.setCursorColor(v);
314 },
315
316 'color-palette-overrides': function(v) {
317 if (!(v == null || v instanceof Object || v instanceof Array)) {
318 console.warn('Preference color-palette-overrides is not an array or ' +
319 'object: ' + v);
320 return;
rginda9f5222b2012-03-05 11:53:28 -0800321 }
rginda9f5222b2012-03-05 11:53:28 -0800322
Robert Ginda57f03b42012-09-13 11:02:48 -0700323 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700324
Robert Ginda57f03b42012-09-13 11:02:48 -0700325 if (v) {
326 for (var key in v) {
327 var i = parseInt(key);
328 if (isNaN(i) || i < 0 || i > 255) {
329 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
330 continue;
331 }
332
333 if (v[i]) {
334 var rgb = lib.colors.normalizeCSS(v[i]);
335 if (rgb)
336 lib.colors.colorPalette[i] = rgb;
337 }
338 }
rginda30f20f62012-04-05 16:36:19 -0700339 }
rginda30f20f62012-04-05 16:36:19 -0700340
Evan Jones5f9df812016-12-06 09:38:58 -0500341 terminal.primaryScreen_.textAttributes.resetColorPalette();
Robert Ginda57f03b42012-09-13 11:02:48 -0700342 terminal.alternateScreen_.textAttributes.resetColorPalette();
343 },
rginda30f20f62012-04-05 16:36:19 -0700344
Robert Ginda57f03b42012-09-13 11:02:48 -0700345 'copy-on-select': function(v) {
346 terminal.copyOnSelect = !!v;
347 },
rginda9f5222b2012-03-05 11:53:28 -0800348
Rob Spies0bec09b2014-06-06 15:58:09 -0700349 'use-default-window-copy': function(v) {
350 terminal.useDefaultWindowCopy = !!v;
351 },
352
353 'clear-selection-after-copy': function(v) {
354 terminal.clearSelectionAfterCopy = !!v;
355 },
356
Robert Ginda7e5e9522014-03-14 12:23:58 -0700357 'ctrl-plus-minus-zero-zoom': function(v) {
358 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
359 },
360
Robert Gindafb5a3f92014-05-13 14:12:00 -0700361 'ctrl-c-copy': function(v) {
362 terminal.keyboard.ctrlCCopy = v;
363 },
364
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100365 'ctrl-v-paste': function(v) {
366 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700367 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100368 },
369
Masaya Suzuki273aa982014-05-31 07:25:55 +0900370 'east-asian-ambiguous-as-two-column': function(v) {
371 lib.wc.regardCjkAmbiguous = v;
372 },
373
Robert Ginda57f03b42012-09-13 11:02:48 -0700374 'enable-8-bit-control': function(v) {
375 terminal.vt.enable8BitControl = !!v;
376 },
rginda30f20f62012-04-05 16:36:19 -0700377
Robert Ginda57f03b42012-09-13 11:02:48 -0700378 'enable-bold': function(v) {
379 terminal.syncBoldSafeState();
380 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400381
Robert Ginda3e278d72014-03-25 13:18:51 -0700382 'enable-bold-as-bright': function(v) {
383 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
384 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
385 },
386
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400387 'enable-blink': function(v) {
388 terminal.syncBlinkState();
389 },
390
Robert Ginda57f03b42012-09-13 11:02:48 -0700391 'enable-clipboard-write': function(v) {
392 terminal.vt.enableClipboardWrite = !!v;
393 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400394
Robert Ginda3755e752013-05-31 13:34:09 -0700395 'enable-dec12': function(v) {
396 terminal.vt.enableDec12 = !!v;
397 },
398
Robert Ginda57f03b42012-09-13 11:02:48 -0700399 'font-family': function(v) {
400 terminal.syncFontFamily();
401 },
rginda30f20f62012-04-05 16:36:19 -0700402
Robert Ginda57f03b42012-09-13 11:02:48 -0700403 'font-size': function(v) {
404 terminal.setFontSize(v);
405 },
rginda9875d902012-08-20 16:21:57 -0700406
Robert Ginda57f03b42012-09-13 11:02:48 -0700407 'font-smoothing': function(v) {
408 terminal.syncFontFamily();
409 },
rgindade84e382012-04-20 15:39:31 -0700410
Robert Ginda57f03b42012-09-13 11:02:48 -0700411 'foreground-color': function(v) {
412 terminal.setForegroundColor(v);
413 },
rginda30f20f62012-04-05 16:36:19 -0700414
Robert Ginda57f03b42012-09-13 11:02:48 -0700415 'home-keys-scroll': function(v) {
416 terminal.keyboard.homeKeysScroll = v;
417 },
rginda4bba5e12012-06-20 16:15:30 -0700418
Robert Gindaa8165692015-06-15 14:46:31 -0700419 'keybindings': function(v) {
420 terminal.keyboard.bindings.clear();
421
422 if (!v)
423 return;
424
425 if (!(v instanceof Object)) {
426 console.error('Error in keybindings preference: Expected object');
427 return;
428 }
429
430 try {
431 terminal.keyboard.bindings.addBindings(v);
432 } catch (ex) {
433 console.error('Error in keybindings preference: ' + ex);
434 }
435 },
436
Robert Ginda57f03b42012-09-13 11:02:48 -0700437 'max-string-sequence': function(v) {
438 terminal.vt.maxStringSequence = v;
439 },
rginda11057d52012-04-25 12:29:56 -0700440
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700441 'media-keys-are-fkeys': function(v) {
442 terminal.keyboard.mediaKeysAreFKeys = v;
443 },
444
Robert Ginda57f03b42012-09-13 11:02:48 -0700445 'meta-sends-escape': function(v) {
446 terminal.keyboard.metaSendsEscape = v;
447 },
rginda30f20f62012-04-05 16:36:19 -0700448
Robert Ginda57f03b42012-09-13 11:02:48 -0700449 'mouse-paste-button': function(v) {
450 terminal.syncMousePasteButton();
451 },
rgindaa8ba17d2012-08-15 14:41:10 -0700452
Robert Gindae76aa9f2014-03-14 12:29:12 -0700453 'page-keys-scroll': function(v) {
454 terminal.keyboard.pageKeysScroll = v;
455 },
456
Robert Ginda40932892012-12-10 17:26:40 -0800457 'pass-alt-number': function(v) {
458 if (v == null) {
459 var osx = window.navigator.userAgent.match(/Mac OS X/);
460
461 // Let Alt-1..9 pass to the browser (to control tab switching) on
462 // non-OS X systems, or if hterm is not opened in an app window.
463 v = (!osx && hterm.windowType != 'popup');
464 }
465
466 terminal.passAltNumber = v;
467 },
468
469 'pass-ctrl-number': function(v) {
470 if (v == null) {
471 var osx = window.navigator.userAgent.match(/Mac OS X/);
472
473 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
474 // non-OS X systems, or if hterm is not opened in an app window.
475 v = (!osx && hterm.windowType != 'popup');
476 }
477
478 terminal.passCtrlNumber = v;
479 },
480
481 'pass-meta-number': function(v) {
482 if (v == null) {
483 var osx = window.navigator.userAgent.match(/Mac OS X/);
484
485 // Let Meta-1..9 pass to the browser (to control tab switching) on
486 // OS X systems, or if hterm is not opened in an app window.
487 v = (osx && hterm.windowType != 'popup');
488 }
489
490 terminal.passMetaNumber = v;
491 },
492
Marius Schilder77857b32014-05-14 16:21:26 -0700493 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700494 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700495 },
496
Robert Ginda8cb7d902013-06-20 14:37:18 -0700497 'receive-encoding': function(v) {
498 if (!(/^(utf-8|raw)$/).test(v)) {
499 console.warn('Invalid value for "receive-encoding": ' + v);
500 v = 'utf-8';
501 }
502
503 terminal.vt.characterEncoding = v;
504 },
505
Robert Ginda57f03b42012-09-13 11:02:48 -0700506 'scroll-on-keystroke': function(v) {
507 terminal.scrollOnKeystroke_ = v;
508 },
rginda9f5222b2012-03-05 11:53:28 -0800509
Robert Ginda57f03b42012-09-13 11:02:48 -0700510 'scroll-on-output': function(v) {
511 terminal.scrollOnOutput_ = v;
512 },
rginda30f20f62012-04-05 16:36:19 -0700513
Robert Ginda57f03b42012-09-13 11:02:48 -0700514 'scrollbar-visible': function(v) {
515 terminal.setScrollbarVisible(v);
516 },
rginda9f5222b2012-03-05 11:53:28 -0800517
Rob Spies49039e52014-12-17 13:40:04 -0800518 'scroll-wheel-move-multiplier': function(v) {
519 terminal.setScrollWheelMoveMultipler(v);
520 },
521
Robert Ginda8cb7d902013-06-20 14:37:18 -0700522 'send-encoding': function(v) {
523 if (!(/^(utf-8|raw)$/).test(v)) {
524 console.warn('Invalid value for "send-encoding": ' + v);
525 v = 'utf-8';
526 }
527
528 terminal.keyboard.characterEncoding = v;
529 },
530
Robert Ginda57f03b42012-09-13 11:02:48 -0700531 'shift-insert-paste': function(v) {
532 terminal.keyboard.shiftInsertPaste = v;
533 },
rginda9f5222b2012-03-05 11:53:28 -0800534
Robert Gindae76aa9f2014-03-14 12:29:12 -0700535 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400536 terminal.scrollPort_.setUserCssUrl(v);
537 },
538
539 'user-css-text': function(v) {
540 terminal.scrollPort_.setUserCssText(v);
541 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700542 });
rginda30f20f62012-04-05 16:36:19 -0700543
Robert Ginda57f03b42012-09-13 11:02:48 -0700544 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800545 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700546
547 if (opt_callback)
548 opt_callback();
549 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800550};
551
Rob Spies56953412014-04-28 14:09:47 -0700552
553/**
554 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500555 *
556 * @return {hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700557 */
558hterm.Terminal.prototype.getPrefs = function() {
559 return this.prefs_;
560};
561
Robert Gindaa063b202014-07-21 11:08:25 -0700562/**
563 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500564 *
565 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700566 */
567hterm.Terminal.prototype.setBracketedPaste = function(state) {
568 this.options_.bracketedPaste = state;
569};
Rob Spies56953412014-04-28 14:09:47 -0700570
rginda8e92a692012-05-20 19:37:20 -0700571/**
572 * Set the color for the cursor.
573 *
574 * If you want this setting to persist, set it through prefs_, rather than
575 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500576 *
577 * @param {string} color The color to set.
rginda8e92a692012-05-20 19:37:20 -0700578 */
579hterm.Terminal.prototype.setCursorColor = function(color) {
Robert Ginda830583c2013-08-07 13:20:46 -0700580 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700581 this.cursorNode_.style.backgroundColor = color;
582 this.cursorNode_.style.borderColor = color;
583};
584
585/**
586 * Return the current cursor color as a string.
Evan Jones2600d4f2016-12-06 09:29:36 -0500587 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700588 */
589hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700590 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700591};
592
593/**
rgindad5613292012-06-19 15:40:37 -0700594 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500595 *
596 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700597 */
598hterm.Terminal.prototype.setSelectionEnabled = function(state) {
599 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700600};
601
602/**
rginda8e92a692012-05-20 19:37:20 -0700603 * Set the background color.
604 *
605 * If you want this setting to persist, set it through prefs_, rather than
606 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500607 *
608 * @param {string} color The color to set.
rginda8e92a692012-05-20 19:37:20 -0700609 */
610hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700611 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700612 this.primaryScreen_.textAttributes.setDefaults(
613 this.foregroundColor_, this.backgroundColor_);
614 this.alternateScreen_.textAttributes.setDefaults(
615 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700616 this.scrollPort_.setBackgroundColor(color);
617};
618
rginda9f5222b2012-03-05 11:53:28 -0800619/**
620 * Return the current terminal background color.
621 *
622 * Intended for use by other classes, so we don't have to expose the entire
623 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500624 *
625 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800626 */
627hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700628 return this.backgroundColor_;
629};
630
631/**
632 * Set the foreground color.
633 *
634 * If you want this setting to persist, set it through prefs_, rather than
635 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500636 *
637 * @param {string} color The color to set.
rginda8e92a692012-05-20 19:37:20 -0700638 */
639hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700640 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700641 this.primaryScreen_.textAttributes.setDefaults(
642 this.foregroundColor_, this.backgroundColor_);
643 this.alternateScreen_.textAttributes.setDefaults(
644 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700645 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800646};
647
648/**
649 * Return the current terminal foreground color.
650 *
651 * Intended for use by other classes, so we don't have to expose the entire
652 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500653 *
654 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800655 */
656hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700657 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800658};
659
660/**
rginda87b86462011-12-14 13:48:03 -0800661 * Create a new instance of a terminal command and run it with a given
662 * argument string.
663 *
664 * @param {function} commandClass The constructor for a terminal command.
665 * @param {string} argString The argument string to pass to the command.
666 */
667hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700668 var environment = this.prefs_.get('environment');
669 if (typeof environment != 'object' || environment == null)
670 environment = {};
671
rginda87b86462011-12-14 13:48:03 -0800672 var self = this;
673 this.command = new commandClass(
674 { argString: argString || '',
675 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700676 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800677 onExit: function(code) {
678 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800679 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700680 if (self.prefs_.get('close-on-exit'))
681 window.close();
rginda87b86462011-12-14 13:48:03 -0800682 }
683 });
684
rgindafeaf3142012-01-31 15:14:20 -0800685 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800686 this.command.run();
687};
688
689/**
rgindafeaf3142012-01-31 15:14:20 -0800690 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500691 *
692 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800693 */
694hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700695 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800696};
697
698/**
699 * Install the keyboard handler for this terminal.
700 *
701 * This will prevent the browser from seeing any keystrokes sent to the
702 * terminal.
703 */
704hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700705 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
rgindafeaf3142012-01-31 15:14:20 -0800706}
707
708/**
709 * Uninstall the keyboard handler for this terminal.
710 */
711hterm.Terminal.prototype.uninstallKeyboard = function() {
712 this.keyboard.installKeyboard(null);
713}
714
715/**
rginda35c456b2012-02-09 17:29:05 -0800716 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800717 *
718 * Call setFontSize(0) to reset to the default font size.
719 *
720 * This function does not modify the font-size preference.
721 *
722 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800723 */
724hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800725 if (px === 0)
726 px = this.prefs_.get('font-size');
727
rginda35c456b2012-02-09 17:29:05 -0800728 this.scrollPort_.setFontSize(px);
Ricky Liang48f05cb2013-12-31 23:35:29 +0800729 if (this.wcCssRule_) {
730 this.wcCssRule_.style.width = this.scrollPort_.characterSize.width * 2 +
731 'px';
732 }
rginda35c456b2012-02-09 17:29:05 -0800733};
734
735/**
736 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500737 *
738 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800739 */
740hterm.Terminal.prototype.getFontSize = function() {
741 return this.scrollPort_.getFontSize();
742};
743
744/**
rginda8e92a692012-05-20 19:37:20 -0700745 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500746 *
747 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700748 */
749hterm.Terminal.prototype.getFontFamily = function() {
750 return this.scrollPort_.getFontFamily();
751};
752
753/**
rginda35c456b2012-02-09 17:29:05 -0800754 * Set the CSS "font-family" for this terminal.
755 */
rginda9f5222b2012-03-05 11:53:28 -0800756hterm.Terminal.prototype.syncFontFamily = function() {
757 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
758 this.prefs_.get('font-smoothing'));
759 this.syncBoldSafeState();
760};
761
rginda4bba5e12012-06-20 16:15:30 -0700762/**
763 * Set this.mousePasteButton based on the mouse-paste-button pref,
764 * autodetecting if necessary.
765 */
766hterm.Terminal.prototype.syncMousePasteButton = function() {
767 var button = this.prefs_.get('mouse-paste-button');
768 if (typeof button == 'number') {
769 this.mousePasteButton = button;
770 return;
771 }
772
773 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
774 if (!ary || ary[2] == 'CrOS') {
775 this.mousePasteButton = 2;
776 } else {
777 this.mousePasteButton = 3;
778 }
779};
780
781/**
782 * Enable or disable bold based on the enable-bold pref, autodetecting if
783 * necessary.
784 */
rginda9f5222b2012-03-05 11:53:28 -0800785hterm.Terminal.prototype.syncBoldSafeState = function() {
786 var enableBold = this.prefs_.get('enable-bold');
787 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700788 this.primaryScreen_.textAttributes.enableBold = enableBold;
789 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800790 return;
791 }
792
rgindaf7521392012-02-28 17:20:34 -0800793 var normalSize = this.scrollPort_.measureCharacterSize();
794 var boldSize = this.scrollPort_.measureCharacterSize('bold');
795
796 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800797 if (!isBoldSafe) {
798 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700799 'from normal. Font family is: ' +
800 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800801 }
rginda9f5222b2012-03-05 11:53:28 -0800802
Robert Gindaed016262012-10-26 16:27:09 -0700803 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
804 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800805};
806
807/**
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400808 * Enable or disable blink based on the enable-blink pref.
809 */
810hterm.Terminal.prototype.syncBlinkState = function() {
811 this.document_.documentElement.style.setProperty(
812 '--hterm-blink-node-duration',
813 this.prefs_.get('enable-blink') ? '0.7s' : '0');
814};
815
816/**
rginda87b86462011-12-14 13:48:03 -0800817 * Return a copy of the current cursor position.
818 *
819 * @return {hterm.RowCol} The RowCol object representing the current position.
820 */
821hterm.Terminal.prototype.saveCursor = function() {
822 return this.screen_.cursorPosition.clone();
823};
824
Evan Jones2600d4f2016-12-06 09:29:36 -0500825/**
826 * Return the current text attributes.
827 *
828 * @return {string}
829 */
rgindaa19afe22012-01-25 15:40:22 -0800830hterm.Terminal.prototype.getTextAttributes = function() {
831 return this.screen_.textAttributes;
832};
833
Evan Jones2600d4f2016-12-06 09:29:36 -0500834/**
835 * Set the text attributes.
836 *
837 * @param {string} textAttributes The attributes to set.
838 */
rginda1a09aa02012-06-18 21:11:25 -0700839hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
840 this.screen_.textAttributes = textAttributes;
841};
842
rginda87b86462011-12-14 13:48:03 -0800843/**
rgindaf522ce02012-04-17 17:49:17 -0700844 * Return the current browser zoom factor applied to the terminal.
845 *
846 * @return {number} The current browser zoom factor.
847 */
848hterm.Terminal.prototype.getZoomFactor = function() {
849 return this.scrollPort_.characterSize.zoomFactor;
850};
851
852/**
rginda9846e2f2012-01-27 13:53:33 -0800853 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500854 *
855 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800856 */
857hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800858 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800859};
860
861/**
rginda87b86462011-12-14 13:48:03 -0800862 * Restore a previously saved cursor position.
863 *
864 * @param {hterm.RowCol} cursor The position to restore.
865 */
866hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700867 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
868 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800869 this.screen_.setCursorPosition(row, column);
870 if (cursor.column > column ||
871 cursor.column == column && cursor.overflow) {
872 this.screen_.cursorPosition.overflow = true;
873 }
rginda87b86462011-12-14 13:48:03 -0800874};
875
876/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400877 * Clear the cursor's overflow flag.
878 */
879hterm.Terminal.prototype.clearCursorOverflow = function() {
880 this.screen_.cursorPosition.overflow = false;
881};
882
883/**
Robert Ginda830583c2013-08-07 13:20:46 -0700884 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -0500885 *
886 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -0700887 */
888hterm.Terminal.prototype.setCursorShape = function(shape) {
889 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -0800890 this.restyleCursor_();
Robert Ginda830583c2013-08-07 13:20:46 -0700891}
892
893/**
894 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -0500895 *
896 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -0700897 */
898hterm.Terminal.prototype.getCursorShape = function() {
899 return this.cursorShape_;
900}
901
902/**
rginda87b86462011-12-14 13:48:03 -0800903 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -0500904 *
905 * @param {number} columnCount
rginda87b86462011-12-14 13:48:03 -0800906 */
907hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800908 if (columnCount == null) {
909 this.div_.style.width = '100%';
910 return;
911 }
912
Robert Ginda26806d12014-07-24 13:44:07 -0700913 this.div_.style.width = Math.ceil(
914 this.scrollPort_.characterSize.width *
915 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400916 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800917 this.scheduleSyncCursorPosition_();
918};
rginda87b86462011-12-14 13:48:03 -0800919
rgindac9bc5502012-01-18 11:48:44 -0800920/**
rginda35c456b2012-02-09 17:29:05 -0800921 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -0500922 *
923 * @param {number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -0800924 */
925hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800926 if (rowCount == null) {
927 this.div_.style.height = '100%';
928 return;
929 }
930
rginda35c456b2012-02-09 17:29:05 -0800931 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700932 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800933 this.realizeSize_(this.screenSize.width, rowCount);
934 this.scheduleSyncCursorPosition_();
935};
936
937/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400938 * Deal with terminal size changes.
939 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500940 * @param {number} columnCount The number of columns.
941 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400942 */
943hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
944 if (columnCount != this.screenSize.width)
945 this.realizeWidth_(columnCount);
946
947 if (rowCount != this.screenSize.height)
948 this.realizeHeight_(rowCount);
949
950 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -0700951 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400952};
953
954/**
rgindac9bc5502012-01-18 11:48:44 -0800955 * Deal with terminal width changes.
956 *
957 * This function does what needs to be done when the terminal width changes
958 * out from under us. It happens here rather than in onResize_() because this
959 * code may need to run synchronously to handle programmatic changes of
960 * terminal width.
961 *
962 * Relying on the browser to send us an async resize event means we may not be
963 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -0500964 *
965 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -0800966 */
967hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700968 if (columnCount <= 0)
969 throw new Error('Attempt to realize bad width: ' + columnCount);
970
rgindac9bc5502012-01-18 11:48:44 -0800971 var deltaColumns = columnCount - this.screen_.getWidth();
972
rginda87b86462011-12-14 13:48:03 -0800973 this.screenSize.width = columnCount;
974 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800975
976 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400977 if (this.defaultTabStops)
978 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800979 } else {
980 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400981 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800982 break;
983
984 this.tabStops_.pop();
985 }
986 }
987
988 this.screen_.setColumnCount(this.screenSize.width);
989};
990
991/**
992 * Deal with terminal height changes.
993 *
994 * This function does what needs to be done when the terminal height changes
995 * out from under us. It happens here rather than in onResize_() because this
996 * code may need to run synchronously to handle programmatic changes of
997 * terminal height.
998 *
999 * Relying on the browser to send us an async resize event means we may not be
1000 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001001 *
1002 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001003 */
1004hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001005 if (rowCount <= 0)
1006 throw new Error('Attempt to realize bad height: ' + rowCount);
1007
rgindac9bc5502012-01-18 11:48:44 -08001008 var deltaRows = rowCount - this.screen_.getHeight();
1009
1010 this.screenSize.height = rowCount;
1011
1012 var cursor = this.saveCursor();
1013
1014 if (deltaRows < 0) {
1015 // Screen got smaller.
1016 deltaRows *= -1;
1017 while (deltaRows) {
1018 var lastRow = this.getRowCount() - 1;
1019 if (lastRow - this.scrollbackRows_.length == cursor.row)
1020 break;
1021
1022 if (this.getRowText(lastRow))
1023 break;
1024
1025 this.screen_.popRow();
1026 deltaRows--;
1027 }
1028
1029 var ary = this.screen_.shiftRows(deltaRows);
1030 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1031
1032 // We just removed rows from the top of the screen, we need to update
1033 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001034 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001035 } else if (deltaRows > 0) {
1036 // Screen got larger.
1037
1038 if (deltaRows <= this.scrollbackRows_.length) {
1039 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1040 var rows = this.scrollbackRows_.splice(
1041 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1042 this.screen_.unshiftRows(rows);
1043 deltaRows -= scrollbackCount;
1044 cursor.row += scrollbackCount;
1045 }
1046
1047 if (deltaRows)
1048 this.appendRows_(deltaRows);
1049 }
1050
rginda35c456b2012-02-09 17:29:05 -08001051 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001052 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001053};
1054
1055/**
1056 * Scroll the terminal to the top of the scrollback buffer.
1057 */
1058hterm.Terminal.prototype.scrollHome = function() {
1059 this.scrollPort_.scrollRowToTop(0);
1060};
1061
1062/**
1063 * Scroll the terminal to the end.
1064 */
1065hterm.Terminal.prototype.scrollEnd = function() {
1066 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1067};
1068
1069/**
1070 * Scroll the terminal one page up (minus one line) relative to the current
1071 * position.
1072 */
1073hterm.Terminal.prototype.scrollPageUp = function() {
1074 var i = this.scrollPort_.getTopRowIndex();
1075 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
1076};
1077
1078/**
1079 * Scroll the terminal one page down (minus one line) relative to the current
1080 * position.
1081 */
1082hterm.Terminal.prototype.scrollPageDown = function() {
1083 var i = this.scrollPort_.getTopRowIndex();
1084 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -08001085};
1086
rgindac9bc5502012-01-18 11:48:44 -08001087/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001088 * Scroll the terminal one line up relative to the current position.
1089 */
1090hterm.Terminal.prototype.scrollLineUp = function() {
1091 var i = this.scrollPort_.getTopRowIndex();
1092 this.scrollPort_.scrollRowToTop(i - 1);
1093};
1094
1095/**
1096 * Scroll the terminal one line down relative to the current position.
1097 */
1098hterm.Terminal.prototype.scrollLineDown = function() {
1099 var i = this.scrollPort_.getTopRowIndex();
1100 this.scrollPort_.scrollRowToTop(i + 1);
1101};
1102
1103/**
Robert Ginda40932892012-12-10 17:26:40 -08001104 * Clear primary screen, secondary screen, and the scrollback buffer.
1105 */
1106hterm.Terminal.prototype.wipeContents = function() {
1107 this.scrollbackRows_.length = 0;
1108 this.scrollPort_.resetCache();
1109
1110 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
1111 var bottom = screen.getHeight();
1112 if (bottom > 0) {
1113 this.renumberRows_(0, bottom);
1114 this.clearHome(screen);
1115 }
1116 }.bind(this));
1117
1118 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001119 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001120};
1121
1122/**
rgindac9bc5502012-01-18 11:48:44 -08001123 * Full terminal reset.
1124 */
rginda87b86462011-12-14 13:48:03 -08001125hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -08001126 this.clearAllTabStops();
1127 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001128
1129 this.clearHome(this.primaryScreen_);
1130 this.primaryScreen_.textAttributes.reset();
1131
1132 this.clearHome(this.alternateScreen_);
1133 this.alternateScreen_.textAttributes.reset();
1134
rgindab8bc8932012-04-27 12:45:03 -07001135 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1136
Robert Ginda92e18102013-03-14 13:56:37 -07001137 this.vt.reset();
1138
rgindac9bc5502012-01-18 11:48:44 -08001139 this.softReset();
rginda87b86462011-12-14 13:48:03 -08001140};
1141
rgindac9bc5502012-01-18 11:48:44 -08001142/**
1143 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001144 *
1145 * Perform a soft reset to the default values listed in
1146 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001147 */
rginda0f5c0292012-01-13 11:00:13 -08001148hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -07001149 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001150 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001151
Brad Townb62dfdc2015-03-16 19:07:15 -07001152 // We show the cursor on soft reset but do not alter the blink state.
1153 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1154
rgindab8bc8932012-04-27 12:45:03 -07001155 // Xterm also resets the color palette on soft reset, even though it doesn't
1156 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -07001157 this.primaryScreen_.textAttributes.resetColorPalette();
1158 this.alternateScreen_.textAttributes.resetColorPalette();
1159
rgindab8bc8932012-04-27 12:45:03 -07001160 // The xterm man page explicitly says this will happen on soft reset.
1161 this.setVTScrollRegion(null, null);
1162
1163 // Xterm also shows the cursor on soft reset, but does not alter the blink
1164 // state.
rgindaa19afe22012-01-25 15:40:22 -08001165 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001166};
1167
rgindac9bc5502012-01-18 11:48:44 -08001168/**
1169 * Move the cursor forward to the next tab stop, or to the last column
1170 * if no more tab stops are set.
1171 */
1172hterm.Terminal.prototype.forwardTabStop = function() {
1173 var column = this.screen_.cursorPosition.column;
1174
1175 for (var i = 0; i < this.tabStops_.length; i++) {
1176 if (this.tabStops_[i] > column) {
1177 this.setCursorColumn(this.tabStops_[i]);
1178 return;
1179 }
1180 }
1181
David Benjamin66e954d2012-05-05 21:08:12 -04001182 // xterm does not clear the overflow flag on HT or CHT.
1183 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001184 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001185 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001186};
1187
rgindac9bc5502012-01-18 11:48:44 -08001188/**
1189 * Move the cursor backward to the previous tab stop, or to the first column
1190 * if no previous tab stops are set.
1191 */
1192hterm.Terminal.prototype.backwardTabStop = function() {
1193 var column = this.screen_.cursorPosition.column;
1194
1195 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1196 if (this.tabStops_[i] < column) {
1197 this.setCursorColumn(this.tabStops_[i]);
1198 return;
1199 }
1200 }
1201
1202 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001203};
1204
rgindac9bc5502012-01-18 11:48:44 -08001205/**
1206 * Set a tab stop at the given column.
1207 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001208 * @param {integer} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001209 */
1210hterm.Terminal.prototype.setTabStop = function(column) {
1211 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1212 if (this.tabStops_[i] == column)
1213 return;
1214
1215 if (this.tabStops_[i] < column) {
1216 this.tabStops_.splice(i + 1, 0, column);
1217 return;
1218 }
1219 }
1220
1221 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001222};
1223
rgindac9bc5502012-01-18 11:48:44 -08001224/**
1225 * Clear the tab stop at the current cursor position.
1226 *
1227 * No effect if there is no tab stop at the current cursor position.
1228 */
1229hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1230 var column = this.screen_.cursorPosition.column;
1231
1232 var i = this.tabStops_.indexOf(column);
1233 if (i == -1)
1234 return;
1235
1236 this.tabStops_.splice(i, 1);
1237};
1238
1239/**
1240 * Clear all tab stops.
1241 */
1242hterm.Terminal.prototype.clearAllTabStops = function() {
1243 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001244 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001245};
1246
1247/**
1248 * Set up the default tab stops, starting from a given column.
1249 *
1250 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001251 * from the specified column, or 0 if no column is provided. It also flags
1252 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001253 *
1254 * This does not clear the existing tab stops first, use clearAllTabStops
1255 * for that.
1256 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001257 * @param {integer} opt_start Optional starting zero based starting column, useful
rgindac9bc5502012-01-18 11:48:44 -08001258 * for filling out missing tab stops when the terminal is resized.
1259 */
1260hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1261 var start = opt_start || 0;
1262 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001263 // Round start up to a default tab stop.
1264 start = start - 1 - ((start - 1) % w) + w;
1265 for (var i = start; i < this.screenSize.width; i += w) {
1266 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001267 }
David Benjamin66e954d2012-05-05 21:08:12 -04001268
1269 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001270};
1271
rginda6d397402012-01-17 10:58:29 -08001272/**
rginda8ba33642011-12-14 12:31:31 -08001273 * Interpret a sequence of characters.
1274 *
1275 * Incomplete escape sequences are buffered until the next call.
1276 *
1277 * @param {string} str Sequence of characters to interpret or pass through.
1278 */
1279hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001280 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001281 this.scheduleSyncCursorPosition_();
1282};
1283
1284/**
1285 * Take over the given DIV for use as the terminal display.
1286 *
1287 * @param {HTMLDivElement} div The div to use as the terminal display.
1288 */
1289hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001290 this.div_ = div;
1291
rginda8ba33642011-12-14 12:31:31 -08001292 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001293 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001294 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1295 this.scrollPort_.setBackgroundPosition(
1296 this.prefs_.get('background-position'));
Mike Frysinger08bad432017-04-24 00:50:54 -04001297 this.scrollPort_.setUserCssUrl(this.prefs_.get('user-css'));
1298 this.scrollPort_.setUserCssText(this.prefs_.get('user-css-text'));
rginda30f20f62012-04-05 16:36:19 -07001299
rginda0918b652012-04-04 11:26:24 -07001300 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001301
rginda9f5222b2012-03-05 11:53:28 -08001302 this.setFontSize(this.prefs_.get('font-size'));
1303 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001304
David Reveman8f552492012-03-28 12:18:41 -04001305 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001306 this.setScrollWheelMoveMultipler(
1307 this.prefs_.get('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001308
rginda8ba33642011-12-14 12:31:31 -08001309 this.document_ = this.scrollPort_.getDocument();
1310
Evan Jones5f9df812016-12-06 09:38:58 -05001311 this.document_.body.oncontextmenu = function() { return false; };
rginda4bba5e12012-06-20 16:15:30 -07001312
1313 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001314 var screenNode = this.scrollPort_.getScreenNode();
1315 screenNode.addEventListener('mousedown', onMouse);
1316 screenNode.addEventListener('mouseup', onMouse);
1317 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001318 this.scrollPort_.onScrollWheel = onMouse;
1319
Toni Barzic0bfa8922013-11-22 11:18:35 -08001320 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001321 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001322 // Listen for mousedown events on the screenNode as in FF the focus
1323 // events don't bubble.
1324 screenNode.addEventListener('mousedown', function() {
1325 setTimeout(this.onFocusChange_.bind(this, true));
1326 }.bind(this));
1327
Toni Barzic0bfa8922013-11-22 11:18:35 -08001328 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001329 'blur', this.onFocusChange_.bind(this, false));
1330
1331 var style = this.document_.createElement('style');
1332 style.textContent =
1333 ('.cursor-node[focus="false"] {' +
1334 ' box-sizing: border-box;' +
1335 ' background-color: transparent !important;' +
1336 ' border-width: 2px;' +
1337 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001338 '}' +
1339 '.wc-node {' +
1340 ' display: inline-block;' +
1341 ' text-align: center;' +
1342 ' width: ' + this.scrollPort_.characterSize.width * 2 + 'px;' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001343 '}' +
1344 ':root {' +
1345 ' --hterm-blink-node-duration: 0.7s;' +
1346 '}' +
1347 '@keyframes blink {' +
1348 ' from { opacity: 1.0; }' +
1349 ' to { opacity: 0.0; }' +
1350 '}' +
1351 '.blink-node {' +
1352 ' animation-name: blink;' +
1353 ' animation-duration: var(--hterm-blink-node-duration);' +
1354 ' animation-iteration-count: infinite;' +
1355 ' animation-timing-function: ease-in-out;' +
1356 ' animation-direction: alternate;' +
rginda8e92a692012-05-20 19:37:20 -07001357 '}');
1358 this.document_.head.appendChild(style);
1359
Ricky Liang48f05cb2013-12-31 23:35:29 +08001360 var styleSheets = this.document_.styleSheets;
1361 var cssRules = styleSheets[styleSheets.length - 1].cssRules;
1362 this.wcCssRule_ = cssRules[cssRules.length - 1];
1363
rginda8ba33642011-12-14 12:31:31 -08001364 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001365 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001366 this.cursorNode_.style.cssText =
1367 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001368 'top: -99px;' +
1369 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001370 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1371 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
Rob Spies06533ba2014-04-24 11:20:37 -07001372 '-webkit-transition: opacity, background-color 100ms linear;' +
1373 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001374
rginda8e92a692012-05-20 19:37:20 -07001375 this.setCursorColor(this.prefs_.get('cursor-color'));
Robert Gindafb1be6a2013-12-11 11:56:22 -08001376 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1377 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001378
rginda8ba33642011-12-14 12:31:31 -08001379 this.document_.body.appendChild(this.cursorNode_);
1380
rgindad5613292012-06-19 15:40:37 -07001381 // When 'enableMouseDragScroll' is off we reposition this element directly
1382 // under the mouse cursor after a click. This makes Chrome associate
1383 // subsequent mousemove events with the scroll-blocker. Since the
1384 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1385 // events do not cause the scrollport to scroll.
1386 //
1387 // It's a hack, but it's the cleanest way I could find.
1388 this.scrollBlockerNode_ = this.document_.createElement('div');
1389 this.scrollBlockerNode_.style.cssText =
1390 ('position: absolute;' +
1391 'top: -99px;' +
1392 'display: block;' +
1393 'width: 10px;' +
1394 'height: 10px;');
1395 this.document_.body.appendChild(this.scrollBlockerNode_);
1396
rgindad5613292012-06-19 15:40:37 -07001397 this.scrollPort_.onScrollWheel = onMouse;
1398 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1399 ].forEach(function(event) {
1400 this.scrollBlockerNode_.addEventListener(event, onMouse);
1401 this.cursorNode_.addEventListener(event, onMouse);
1402 this.document_.addEventListener(event, onMouse);
1403 }.bind(this));
1404
1405 this.cursorNode_.addEventListener('mousedown', function() {
1406 setTimeout(this.focus.bind(this));
1407 }.bind(this));
1408
rginda8ba33642011-12-14 12:31:31 -08001409 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001410
rginda87b86462011-12-14 13:48:03 -08001411 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001412 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001413};
1414
rginda0918b652012-04-04 11:26:24 -07001415/**
1416 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001417 *
1418 * @return {HTMLDocument}
rginda0918b652012-04-04 11:26:24 -07001419 */
rginda87b86462011-12-14 13:48:03 -08001420hterm.Terminal.prototype.getDocument = function() {
1421 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001422};
1423
1424/**
rginda0918b652012-04-04 11:26:24 -07001425 * Focus the terminal.
1426 */
1427hterm.Terminal.prototype.focus = function() {
1428 this.scrollPort_.focus();
1429};
1430
1431/**
rginda8ba33642011-12-14 12:31:31 -08001432 * Return the HTML Element for a given row index.
1433 *
1434 * This is a method from the RowProvider interface. The ScrollPort uses
1435 * it to fetch rows on demand as they are scrolled into view.
1436 *
1437 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1438 * pairs to conserve memory.
1439 *
1440 * @param {integer} index The zero-based row index, measured relative to the
1441 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001442 * largest indices.
rginda8ba33642011-12-14 12:31:31 -08001443 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1444 */
1445hterm.Terminal.prototype.getRowNode = function(index) {
1446 if (index < this.scrollbackRows_.length)
1447 return this.scrollbackRows_[index];
1448
1449 var screenIndex = index - this.scrollbackRows_.length;
1450 return this.screen_.rowsArray[screenIndex];
1451};
1452
1453/**
1454 * Return the text content for a given range of rows.
1455 *
1456 * This is a method from the RowProvider interface. The ScrollPort uses
1457 * it to fetch text content on demand when the user attempts to copy their
1458 * selection to the clipboard.
1459 *
1460 * @param {integer} start The zero-based row index to start from, measured
1461 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001462 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001463 * @param {integer} end The zero-based row index to end on, measured
1464 * relative to the start of the scrollback buffer.
1465 * @return {string} A single string containing the text value of the range of
1466 * rows. Lines will be newline delimited, with no trailing newline.
1467 */
1468hterm.Terminal.prototype.getRowsText = function(start, end) {
1469 var ary = [];
1470 for (var i = start; i < end; i++) {
1471 var node = this.getRowNode(i);
1472 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001473 if (i < end - 1 && !node.getAttribute('line-overflow'))
1474 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001475 }
1476
rgindaa09e7332012-08-17 12:49:51 -07001477 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001478};
1479
1480/**
1481 * Return the text content for a given row.
1482 *
1483 * This is a method from the RowProvider interface. The ScrollPort uses
1484 * it to fetch text content on demand when the user attempts to copy their
1485 * selection to the clipboard.
1486 *
1487 * @param {integer} index The zero-based row index to return, measured
1488 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001489 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001490 * @return {string} A string containing the text value of the selected row.
1491 */
1492hterm.Terminal.prototype.getRowText = function(index) {
1493 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001494 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001495};
1496
1497/**
1498 * Return the total number of rows in the addressable screen and in the
1499 * scrollback buffer of this terminal.
1500 *
1501 * This is a method from the RowProvider interface. The ScrollPort uses
1502 * it to compute the size of the scrollbar.
1503 *
1504 * @return {integer} The number of rows in this terminal.
1505 */
1506hterm.Terminal.prototype.getRowCount = function() {
1507 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1508};
1509
1510/**
1511 * Create DOM nodes for new rows and append them to the end of the terminal.
1512 *
1513 * This is the only correct way to add a new DOM node for a row. Notice that
1514 * the new row is appended to the bottom of the list of rows, and does not
1515 * require renumbering (of the rowIndex property) of previous rows.
1516 *
1517 * If you think you want a new blank row somewhere in the middle of the
1518 * terminal, look into moveRows_().
1519 *
1520 * This method does not pay attention to vtScrollTop/Bottom, since you should
1521 * be using moveRows() in cases where they would matter.
1522 *
1523 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001524 *
1525 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001526 */
1527hterm.Terminal.prototype.appendRows_ = function(count) {
1528 var cursorRow = this.screen_.rowsArray.length;
1529 var offset = this.scrollbackRows_.length + cursorRow;
1530 for (var i = 0; i < count; i++) {
1531 var row = this.document_.createElement('x-row');
1532 row.appendChild(this.document_.createTextNode(''));
1533 row.rowIndex = offset + i;
1534 this.screen_.pushRow(row);
1535 }
1536
1537 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1538 if (extraRows > 0) {
1539 var ary = this.screen_.shiftRows(extraRows);
1540 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001541 if (this.scrollPort_.isScrolledEnd)
1542 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001543 }
1544
1545 if (cursorRow >= this.screen_.rowsArray.length)
1546 cursorRow = this.screen_.rowsArray.length - 1;
1547
rginda87b86462011-12-14 13:48:03 -08001548 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001549};
1550
1551/**
1552 * Relocate rows from one part of the addressable screen to another.
1553 *
1554 * This is used to recycle rows during VT scrolls (those which are driven
1555 * by VT commands, rather than by the user manipulating the scrollbar.)
1556 *
1557 * In this case, the blank lines scrolled into the scroll region are made of
1558 * the nodes we scrolled off. These have their rowIndex properties carefully
1559 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001560 *
1561 * @param {number} fromIndex The start index.
1562 * @param {number} count The number of rows to move.
1563 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001564 */
1565hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1566 var ary = this.screen_.removeRows(fromIndex, count);
1567 this.screen_.insertRows(toIndex, ary);
1568
1569 var start, end;
1570 if (fromIndex < toIndex) {
1571 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001572 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001573 } else {
1574 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001575 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001576 }
1577
1578 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001579 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001580};
1581
1582/**
1583 * Renumber the rowIndex property of the given range of rows.
1584 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001585 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001586 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001587 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001588 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001589 *
1590 * @param {number} start The start index.
1591 * @param {number} end The end index.
1592 * @param {hterm.Screen} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001593 */
Robert Ginda40932892012-12-10 17:26:40 -08001594hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1595 var screen = opt_screen || this.screen_;
1596
rginda8ba33642011-12-14 12:31:31 -08001597 var offset = this.scrollbackRows_.length;
1598 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001599 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001600 }
1601};
1602
1603/**
1604 * Print a string to the terminal.
1605 *
1606 * This respects the current insert and wraparound modes. It will add new lines
1607 * to the end of the terminal, scrolling off the top into the scrollback buffer
1608 * if necessary.
1609 *
1610 * The string is *not* parsed for escape codes. Use the interpret() method if
1611 * that's what you're after.
1612 *
1613 * @param{string} str The string to print.
1614 */
1615hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001616 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001617
Ricky Liang48f05cb2013-12-31 23:35:29 +08001618 var strWidth = lib.wc.strWidth(str);
1619
1620 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001621 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1622 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001623 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001624 }
rgindaa19afe22012-01-25 15:40:22 -08001625
Ricky Liang48f05cb2013-12-31 23:35:29 +08001626 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001627 var didOverflow = false;
1628 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001629
rgindaa9abdd82012-08-06 18:05:09 -07001630 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1631 didOverflow = true;
1632 count = this.screenSize.width - this.screen_.cursorPosition.column;
1633 }
rgindaa19afe22012-01-25 15:40:22 -08001634
rgindaa9abdd82012-08-06 18:05:09 -07001635 if (didOverflow && !this.options_.wraparound) {
1636 // If the string overflowed the line but wraparound is off, then the
1637 // last printed character should be the last of the string.
1638 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001639 substr = lib.wc.substr(str, startOffset, count - 1) +
1640 lib.wc.substr(str, strWidth - 1);
1641 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001642 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001643 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001644 }
rgindaa19afe22012-01-25 15:40:22 -08001645
Ricky Liang48f05cb2013-12-31 23:35:29 +08001646 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1647 for (var i = 0; i < tokens.length; i++) {
1648 if (tokens[i].wcNode)
1649 this.screen_.textAttributes.wcNode = true;
1650
1651 if (this.options_.insertMode) {
1652 this.screen_.insertString(tokens[i].str);
1653 } else {
1654 this.screen_.overwriteString(tokens[i].str);
1655 }
1656 this.screen_.textAttributes.wcNode = false;
rgindaa9abdd82012-08-06 18:05:09 -07001657 }
1658
1659 this.screen_.maybeClipCurrentRow();
1660 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001661 }
rginda8ba33642011-12-14 12:31:31 -08001662
1663 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001664
rginda9f5222b2012-03-05 11:53:28 -08001665 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001666 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001667};
1668
1669/**
rginda87b86462011-12-14 13:48:03 -08001670 * Set the VT scroll region.
1671 *
rginda87b86462011-12-14 13:48:03 -08001672 * This also resets the cursor position to the absolute (0, 0) position, since
1673 * that's what xterm appears to do.
1674 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001675 * Setting the scroll region to the full height of the terminal will clear
1676 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1677 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1678 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1679 * continue to work as most users would expect.
1680 *
rginda87b86462011-12-14 13:48:03 -08001681 * @param {integer} scrollTop The zero-based top of the scroll region.
1682 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1683 * inclusive.
1684 */
1685hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001686 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001687 this.vtScrollTop_ = null;
1688 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001689 } else {
1690 this.vtScrollTop_ = scrollTop;
1691 this.vtScrollBottom_ = scrollBottom;
1692 }
rginda87b86462011-12-14 13:48:03 -08001693};
1694
1695/**
rginda8ba33642011-12-14 12:31:31 -08001696 * Return the top row index according to the VT.
1697 *
1698 * This will return 0 unless the terminal has been told to restrict scrolling
1699 * to some lower row. It is used for some VT cursor positioning and scrolling
1700 * commands.
1701 *
1702 * @return {integer} The topmost row in the terminal's scroll region.
1703 */
1704hterm.Terminal.prototype.getVTScrollTop = function() {
1705 if (this.vtScrollTop_ != null)
1706 return this.vtScrollTop_;
1707
1708 return 0;
rginda87b86462011-12-14 13:48:03 -08001709};
rginda8ba33642011-12-14 12:31:31 -08001710
1711/**
1712 * Return the bottom row index according to the VT.
1713 *
1714 * This will return the height of the terminal unless the it has been told to
1715 * restrict scrolling to some higher row. It is used for some VT cursor
1716 * positioning and scrolling commands.
1717 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001718 * @return {integer} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001719 */
1720hterm.Terminal.prototype.getVTScrollBottom = function() {
1721 if (this.vtScrollBottom_ != null)
1722 return this.vtScrollBottom_;
1723
rginda87b86462011-12-14 13:48:03 -08001724 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001725}
1726
1727/**
1728 * Process a '\n' character.
1729 *
1730 * If the cursor is on the final row of the terminal this will append a new
1731 * blank row to the screen and scroll the topmost row into the scrollback
1732 * buffer.
1733 *
1734 * Otherwise, this moves the cursor to column zero of the next row.
1735 */
1736hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001737 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1738 this.screen_.rowsArray.length - 1);
1739
1740 if (this.vtScrollBottom_ != null) {
1741 // A VT Scroll region is active, we never append new rows.
1742 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1743 // We're at the end of the VT Scroll Region, perform a VT scroll.
1744 this.vtScrollUp(1);
1745 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1746 } else if (cursorAtEndOfScreen) {
1747 // We're at the end of the screen, the only thing to do is put the
1748 // cursor to column 0.
1749 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1750 } else {
1751 // Anywhere else, advance the cursor row, and reset the column.
1752 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1753 }
1754 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001755 // We're at the end of the screen. Append a new row to the terminal,
1756 // shifting the top row into the scrollback.
1757 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001758 } else {
rginda87b86462011-12-14 13:48:03 -08001759 // Anywhere else in the screen just moves the cursor.
1760 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001761 }
1762};
1763
1764/**
1765 * Like newLine(), except maintain the cursor column.
1766 */
1767hterm.Terminal.prototype.lineFeed = function() {
1768 var column = this.screen_.cursorPosition.column;
1769 this.newLine();
1770 this.setCursorColumn(column);
1771};
1772
1773/**
rginda87b86462011-12-14 13:48:03 -08001774 * If autoCarriageReturn is set then newLine(), else lineFeed().
1775 */
1776hterm.Terminal.prototype.formFeed = function() {
1777 if (this.options_.autoCarriageReturn) {
1778 this.newLine();
1779 } else {
1780 this.lineFeed();
1781 }
1782};
1783
1784/**
1785 * Move the cursor up one row, possibly inserting a blank line.
1786 *
1787 * The cursor column is not changed.
1788 */
1789hterm.Terminal.prototype.reverseLineFeed = function() {
1790 var scrollTop = this.getVTScrollTop();
1791 var currentRow = this.screen_.cursorPosition.row;
1792
1793 if (currentRow == scrollTop) {
1794 this.insertLines(1);
1795 } else {
1796 this.setAbsoluteCursorRow(currentRow - 1);
1797 }
1798};
1799
1800/**
rginda8ba33642011-12-14 12:31:31 -08001801 * Replace all characters to the left of the current cursor with the space
1802 * character.
1803 *
1804 * TODO(rginda): This should probably *remove* the characters (not just replace
1805 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001806 * position.
rginda8ba33642011-12-14 12:31:31 -08001807 */
1808hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001809 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001810 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001811 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001812 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001813};
1814
1815/**
David Benjamin684a9b72012-05-01 17:19:58 -04001816 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001817 *
1818 * The cursor position is unchanged.
1819 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001820 * If the current background color is not the default background color this
1821 * will insert spaces rather than delete. This is unfortunate because the
1822 * trailing space will affect text selection, but it's difficult to come up
1823 * with a way to style empty space that wouldn't trip up the hterm.Screen
1824 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001825 *
1826 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1827 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1828 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05001829 *
1830 * @param {number} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08001831 */
1832hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001833 if (this.screen_.cursorPosition.overflow)
1834 return;
1835
Robert Ginda7fd57082012-09-25 14:41:47 -07001836 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1837 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001838
1839 if (this.screen_.textAttributes.background ===
1840 this.screen_.textAttributes.DEFAULT_COLOR) {
1841 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08001842 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07001843 this.screen_.cursorPosition.column + count) {
1844 this.screen_.deleteChars(count);
1845 this.clearCursorOverflow();
1846 return;
1847 }
1848 }
1849
rginda87b86462011-12-14 13:48:03 -08001850 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001851 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001852 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001853 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001854};
1855
1856/**
1857 * Erase the current line.
1858 *
1859 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001860 */
1861hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001862 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001863 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001864 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001865 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001866};
1867
1868/**
David Benjamina08d78f2012-05-05 00:28:49 -04001869 * Erase all characters from the start of the screen to the current cursor
1870 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001871 *
1872 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001873 */
1874hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001875 var cursor = this.saveCursor();
1876
1877 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001878
David Benjamina08d78f2012-05-05 00:28:49 -04001879 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001880 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001881 this.screen_.clearCursorRow();
1882 }
1883
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/**
1889 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001890 * screen, 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.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001895 var cursor = this.saveCursor();
1896
1897 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001898
David Benjamina08d78f2012-05-05 00:28:49 -04001899 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001900 for (var i = cursor.row + 1; i <= bottom; i++) {
1901 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001902 this.screen_.clearCursorRow();
1903 }
1904
rginda87b86462011-12-14 13:48:03 -08001905 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001906 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001907};
1908
1909/**
1910 * Fill the terminal with a given character.
1911 *
1912 * This methods does not respect the VT scroll region.
1913 *
1914 * @param {string} ch The character to use for the fill.
1915 */
1916hterm.Terminal.prototype.fill = function(ch) {
1917 var cursor = this.saveCursor();
1918
1919 this.setAbsoluteCursorPosition(0, 0);
1920 for (var row = 0; row < this.screenSize.height; row++) {
1921 for (var col = 0; col < this.screenSize.width; col++) {
1922 this.setAbsoluteCursorPosition(row, col);
1923 this.screen_.overwriteString(ch);
1924 }
1925 }
1926
1927 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001928};
1929
1930/**
rginda9ea433c2012-03-16 11:57:00 -07001931 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001932 *
rginda9ea433c2012-03-16 11:57:00 -07001933 * This does not respect the scroll region.
1934 *
1935 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1936 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001937 */
rginda9ea433c2012-03-16 11:57:00 -07001938hterm.Terminal.prototype.clearHome = function(opt_screen) {
1939 var screen = opt_screen || this.screen_;
1940 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001941
rginda11057d52012-04-25 12:29:56 -07001942 if (bottom == 0) {
1943 // Empty screen, nothing to do.
1944 return;
1945 }
1946
rgindae4d29232012-01-19 10:47:13 -08001947 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001948 screen.setCursorPosition(i, 0);
1949 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001950 }
1951
rginda9ea433c2012-03-16 11:57:00 -07001952 screen.setCursorPosition(0, 0);
1953};
1954
1955/**
1956 * Erase the entire display without changing the cursor position.
1957 *
1958 * The cursor position is unchanged. This does not respect the scroll
1959 * region.
1960 *
1961 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1962 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001963 */
1964hterm.Terminal.prototype.clear = function(opt_screen) {
1965 var screen = opt_screen || this.screen_;
1966 var cursor = screen.cursorPosition.clone();
1967 this.clearHome(screen);
1968 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001969};
1970
1971/**
1972 * VT command to insert lines at the current cursor row.
1973 *
1974 * This respects the current scroll region. Rows pushed off the bottom are
1975 * lost (they won't show up in the scrollback buffer).
1976 *
rginda8ba33642011-12-14 12:31:31 -08001977 * @param {integer} count The number of lines to insert.
1978 */
1979hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07001980 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08001981
1982 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07001983 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08001984
Robert Ginda579186b2012-09-26 11:40:04 -07001985 // The moveCount is the number of rows we need to relocate to make room for
1986 // the new row(s). The count is the distance to move them.
1987 var moveCount = bottom - cursorRow - count + 1;
1988 if (moveCount)
1989 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08001990
Robert Ginda579186b2012-09-26 11:40:04 -07001991 for (var i = count - 1; i >= 0; i--) {
1992 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001993 this.screen_.clearCursorRow();
1994 }
rginda8ba33642011-12-14 12:31:31 -08001995};
1996
1997/**
1998 * VT command to delete lines at the current cursor row.
1999 *
2000 * New rows are added to the bottom of scroll region to take their place. New
2001 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002002 *
2003 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002004 */
2005hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002006 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002007
rginda87b86462011-12-14 13:48:03 -08002008 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002009 var bottom = this.getVTScrollBottom();
2010
rginda87b86462011-12-14 13:48:03 -08002011 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002012 count = Math.min(count, maxCount);
2013
rginda87b86462011-12-14 13:48:03 -08002014 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002015 if (count != maxCount)
2016 this.moveRows_(top, count, moveStart);
2017
2018 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002019 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002020 this.screen_.clearCursorRow();
2021 }
2022
rginda87b86462011-12-14 13:48:03 -08002023 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002024 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002025};
2026
2027/**
2028 * Inserts the given number of spaces at the current cursor position.
2029 *
rginda87b86462011-12-14 13:48:03 -08002030 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002031 *
2032 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002033 */
2034hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002035 var cursor = this.saveCursor();
2036
rgindacbbd7482012-06-13 15:06:16 -07002037 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08002038 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08002039 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002040
2041 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002042 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002043};
2044
2045/**
2046 * Forward-delete the specified number of characters starting at the cursor
2047 * position.
2048 *
2049 * @param {integer} count The number of characters to delete.
2050 */
2051hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002052 var deleted = this.screen_.deleteChars(count);
2053 if (deleted && !this.screen_.textAttributes.isDefault()) {
2054 var cursor = this.saveCursor();
2055 this.setCursorColumn(this.screenSize.width - deleted);
2056 this.screen_.insertString(lib.f.getWhitespace(deleted));
2057 this.restoreCursor(cursor);
2058 }
2059
David Benjamin54e8bf62012-06-01 22:31:40 -04002060 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002061};
2062
2063/**
2064 * Shift rows in the scroll region upwards by a given number of lines.
2065 *
2066 * New rows are inserted at the bottom of the scroll region to fill the
2067 * vacated rows. The new rows not filled out with the current text attributes.
2068 *
2069 * This function does not affect the scrollback rows at all. Rows shifted
2070 * off the top are lost.
2071 *
rginda87b86462011-12-14 13:48:03 -08002072 * The cursor position is not altered.
2073 *
rginda8ba33642011-12-14 12:31:31 -08002074 * @param {integer} count The number of rows to scroll.
2075 */
2076hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002077 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002078
rginda87b86462011-12-14 13:48:03 -08002079 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002080 this.deleteLines(count);
2081
rginda87b86462011-12-14 13:48:03 -08002082 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002083};
2084
2085/**
2086 * Shift rows below the cursor down by a given number of lines.
2087 *
2088 * This function respects the current scroll region.
2089 *
2090 * New rows are inserted at the top of the scroll region to fill the
2091 * vacated rows. The new rows not filled out with the current text attributes.
2092 *
2093 * This function does not affect the scrollback rows at all. Rows shifted
2094 * off the bottom are lost.
2095 *
2096 * @param {integer} count The number of rows to scroll.
2097 */
2098hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08002099 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002100
rginda87b86462011-12-14 13:48:03 -08002101 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08002102 this.insertLines(opt_count);
2103
rginda87b86462011-12-14 13:48:03 -08002104 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002105};
2106
rginda87b86462011-12-14 13:48:03 -08002107
rginda8ba33642011-12-14 12:31:31 -08002108/**
2109 * Set the cursor position.
2110 *
2111 * The cursor row is relative to the scroll region if the terminal has
2112 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2113 *
2114 * @param {integer} row The new zero-based cursor row.
2115 * @param {integer} row The new zero-based cursor column.
2116 */
2117hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2118 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002119 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002120 } else {
rginda87b86462011-12-14 13:48:03 -08002121 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002122 }
rginda87b86462011-12-14 13:48:03 -08002123};
rginda8ba33642011-12-14 12:31:31 -08002124
Evan Jones2600d4f2016-12-06 09:29:36 -05002125/**
2126 * Move the cursor relative to its current position.
2127 *
2128 * @param {number} row
2129 * @param {number} column
2130 */
rginda87b86462011-12-14 13:48:03 -08002131hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2132 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002133 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2134 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002135 this.screen_.setCursorPosition(row, column);
2136};
2137
Evan Jones2600d4f2016-12-06 09:29:36 -05002138/**
2139 * Move the cursor to the specified position.
2140 *
2141 * @param {number} row
2142 * @param {number} column
2143 */
rginda87b86462011-12-14 13:48:03 -08002144hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002145 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2146 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002147 this.screen_.setCursorPosition(row, column);
2148};
2149
2150/**
2151 * Set the cursor column.
2152 *
2153 * @param {integer} column The new zero-based cursor column.
2154 */
2155hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002156 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002157};
2158
2159/**
2160 * Return the cursor column.
2161 *
2162 * @return {integer} The zero-based cursor column.
2163 */
2164hterm.Terminal.prototype.getCursorColumn = function() {
2165 return this.screen_.cursorPosition.column;
2166};
2167
2168/**
2169 * Set the cursor row.
2170 *
2171 * The cursor row is relative to the scroll region if the terminal has
2172 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2173 *
2174 * @param {integer} row The new cursor row.
2175 */
rginda87b86462011-12-14 13:48:03 -08002176hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2177 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002178};
2179
2180/**
2181 * Return the cursor row.
2182 *
2183 * @return {integer} The zero-based cursor row.
2184 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002185hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002186 return this.screen_.cursorPosition.row;
2187};
2188
2189/**
2190 * Request that the ScrollPort redraw itself soon.
2191 *
2192 * The redraw will happen asynchronously, soon after the call stack winds down.
2193 * Multiple calls will be coalesced into a single redraw.
2194 */
2195hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002196 if (this.timeouts_.redraw)
2197 return;
rginda8ba33642011-12-14 12:31:31 -08002198
2199 var self = this;
rginda87b86462011-12-14 13:48:03 -08002200 this.timeouts_.redraw = setTimeout(function() {
2201 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002202 self.scrollPort_.redraw_();
2203 }, 0);
2204};
2205
2206/**
2207 * Request that the ScrollPort be scrolled to the bottom.
2208 *
2209 * The scroll will happen asynchronously, soon after the call stack winds down.
2210 * Multiple calls will be coalesced into a single scroll.
2211 *
2212 * This affects the scrollbar position of the ScrollPort, and has nothing to
2213 * do with the VT scroll commands.
2214 */
2215hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2216 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002217 return;
rginda8ba33642011-12-14 12:31:31 -08002218
2219 var self = this;
2220 this.timeouts_.scrollDown = setTimeout(function() {
2221 delete self.timeouts_.scrollDown;
2222 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2223 }, 10);
2224};
2225
2226/**
2227 * Move the cursor up a specified number of rows.
2228 *
2229 * @param {integer} count The number of rows to move the cursor.
2230 */
2231hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002232 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002233};
2234
2235/**
2236 * Move the cursor down a specified number of rows.
2237 *
2238 * @param {integer} count The number of rows to move the cursor.
2239 */
2240hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002241 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002242 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2243 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2244 this.screenSize.height - 1);
2245
rgindacbbd7482012-06-13 15:06:16 -07002246 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002247 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002248 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002249};
2250
2251/**
2252 * Move the cursor left a specified number of columns.
2253 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002254 * If reverse wraparound mode is enabled and the previous row wrapped into
2255 * the current row then we back up through the wraparound as well.
2256 *
rginda8ba33642011-12-14 12:31:31 -08002257 * @param {integer} count The number of columns to move the cursor.
2258 */
2259hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002260 count = count || 1;
2261
2262 if (count < 1)
2263 return;
2264
2265 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002266 if (this.options_.reverseWraparound) {
2267 if (this.screen_.cursorPosition.overflow) {
2268 // If this cursor is in the right margin, consume one count to get it
2269 // back to the last column. This only applies when we're in reverse
2270 // wraparound mode.
2271 count--;
2272 this.clearCursorOverflow();
2273
2274 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002275 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002276 }
2277
Robert Gindabfb32622014-07-17 13:20:27 -07002278 var newRow = this.screen_.cursorPosition.row;
2279 var newColumn = currentColumn - count;
2280 if (newColumn < 0) {
2281 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2282 if (newRow < 0) {
2283 // xterm also wraps from row 0 to the last row.
2284 newRow = this.screenSize.height + newRow % this.screenSize.height;
2285 }
2286 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2287 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002288
Robert Gindabfb32622014-07-17 13:20:27 -07002289 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2290
2291 } else {
2292 var newColumn = Math.max(currentColumn - count, 0);
2293 this.setCursorColumn(newColumn);
2294 }
rginda8ba33642011-12-14 12:31:31 -08002295};
2296
2297/**
2298 * Move the cursor right a specified number of columns.
2299 *
2300 * @param {integer} count The number of columns to move the cursor.
2301 */
2302hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002303 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002304
2305 if (count < 1)
2306 return;
2307
rgindacbbd7482012-06-13 15:06:16 -07002308 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002309 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002310 this.setCursorColumn(column);
2311};
2312
2313/**
2314 * Reverse the foreground and background colors of the terminal.
2315 *
2316 * This only affects text that was drawn with no attributes.
2317 *
2318 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2319 * been drawn with attributes that happen to coincide with the default
2320 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002321 *
2322 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002323 */
2324hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002325 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002326 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002327 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2328 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002329 } else {
rginda9f5222b2012-03-05 11:53:28 -08002330 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2331 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002332 }
2333};
2334
2335/**
rginda87b86462011-12-14 13:48:03 -08002336 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002337 *
2338 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002339 */
2340hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002341 this.cursorNode_.style.backgroundColor =
2342 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002343
2344 var self = this;
2345 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08002346 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08002347 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002348
Michael Kelly485ecd12014-06-09 11:41:56 -04002349 // bellSquelchTimeout_ affects both audio and notification bells.
2350 if (this.bellSquelchTimeout_)
2351 return;
2352
Robert Ginda92e18102013-03-14 13:56:37 -07002353 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002354 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002355 this.bellSequelchTimeout_ = setTimeout(function() {
2356 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002357 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002358 } else {
2359 delete this.bellSquelchTimeout_;
2360 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002361
2362 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
2363 var n = new Notification(
2364 lib.f.replaceVars(hterm.desktopNotificationTitle,
Robert Ginda348dc2b2014-06-24 14:42:23 -07002365 {'title': window.document.title || 'hterm'}));
Michael Kelly485ecd12014-06-09 11:41:56 -04002366 this.bellNotificationList_.push(n);
2367 // TODO: Should we try to raise the window here?
2368 n.onclick = function() { self.closeBellNotifications_(); };
2369 }
rginda87b86462011-12-14 13:48:03 -08002370};
2371
2372/**
rginda8ba33642011-12-14 12:31:31 -08002373 * Set the origin mode bit.
2374 *
2375 * If origin mode is on, certain VT cursor and scrolling commands measure their
2376 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2377 * to the top of the addressable screen.
2378 *
2379 * Defaults to off.
2380 *
2381 * @param {boolean} state True to set origin mode, false to unset.
2382 */
2383hterm.Terminal.prototype.setOriginMode = function(state) {
2384 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002385 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002386};
2387
2388/**
2389 * Set the insert mode bit.
2390 *
2391 * If insert mode is on, existing text beyond the cursor position will be
2392 * shifted right to make room for new text. Otherwise, new text overwrites
2393 * any existing text.
2394 *
2395 * Defaults to off.
2396 *
2397 * @param {boolean} state True to set insert mode, false to unset.
2398 */
2399hterm.Terminal.prototype.setInsertMode = function(state) {
2400 this.options_.insertMode = state;
2401};
2402
2403/**
rginda87b86462011-12-14 13:48:03 -08002404 * Set the auto carriage return bit.
2405 *
2406 * If auto carriage return is on then a formfeed character is interpreted
2407 * as a newline, otherwise it's the same as a linefeed. The difference boils
2408 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002409 *
2410 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002411 */
2412hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2413 this.options_.autoCarriageReturn = state;
2414};
2415
2416/**
rginda8ba33642011-12-14 12:31:31 -08002417 * Set the wraparound mode bit.
2418 *
2419 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2420 * to the start of the following row. Otherwise, the cursor is clamped to the
2421 * end of the screen and attempts to write past it are ignored.
2422 *
2423 * Defaults to on.
2424 *
2425 * @param {boolean} state True to set wraparound mode, false to unset.
2426 */
2427hterm.Terminal.prototype.setWraparound = function(state) {
2428 this.options_.wraparound = state;
2429};
2430
2431/**
2432 * Set the reverse-wraparound mode bit.
2433 *
2434 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2435 * to the end of the previous row. Otherwise, the cursor is clamped to column
2436 * 0.
2437 *
2438 * Defaults to off.
2439 *
2440 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2441 */
2442hterm.Terminal.prototype.setReverseWraparound = function(state) {
2443 this.options_.reverseWraparound = state;
2444};
2445
2446/**
2447 * Selects between the primary and alternate screens.
2448 *
2449 * If alternate mode is on, the alternate screen is active. Otherwise the
2450 * primary screen is active.
2451 *
2452 * Swapping screens has no effect on the scrollback buffer.
2453 *
2454 * Each screen maintains its own cursor position.
2455 *
2456 * Defaults to off.
2457 *
2458 * @param {boolean} state True to set alternate mode, false to unset.
2459 */
2460hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002461 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002462 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2463
rginda35c456b2012-02-09 17:29:05 -08002464 if (this.screen_.rowsArray.length &&
2465 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2466 // If the screen changed sizes while we were away, our rowIndexes may
2467 // be incorrect.
2468 var offset = this.scrollbackRows_.length;
2469 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002470 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002471 ary[i].rowIndex = offset + i;
2472 }
2473 }
rginda8ba33642011-12-14 12:31:31 -08002474
rginda35c456b2012-02-09 17:29:05 -08002475 this.realizeWidth_(this.screenSize.width);
2476 this.realizeHeight_(this.screenSize.height);
2477 this.scrollPort_.syncScrollHeight();
2478 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002479
rginda6d397402012-01-17 10:58:29 -08002480 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002481 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002482};
2483
2484/**
2485 * Set the cursor-blink mode bit.
2486 *
2487 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2488 * a visible cursor does not blink.
2489 *
2490 * You should make sure to turn blinking off if you're going to dispose of a
2491 * terminal, otherwise you'll leak a timeout.
2492 *
2493 * Defaults to on.
2494 *
2495 * @param {boolean} state True to set cursor-blink mode, false to unset.
2496 */
2497hterm.Terminal.prototype.setCursorBlink = function(state) {
2498 this.options_.cursorBlink = state;
2499
2500 if (!state && this.timeouts_.cursorBlink) {
2501 clearTimeout(this.timeouts_.cursorBlink);
2502 delete this.timeouts_.cursorBlink;
2503 }
2504
2505 if (this.options_.cursorVisible)
2506 this.setCursorVisible(true);
2507};
2508
2509/**
2510 * Set the cursor-visible mode bit.
2511 *
2512 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2513 *
2514 * Defaults to on.
2515 *
2516 * @param {boolean} state True to set cursor-visible mode, false to unset.
2517 */
2518hterm.Terminal.prototype.setCursorVisible = function(state) {
2519 this.options_.cursorVisible = state;
2520
2521 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002522 if (this.timeouts_.cursorBlink) {
2523 clearTimeout(this.timeouts_.cursorBlink);
2524 delete this.timeouts_.cursorBlink;
2525 }
rginda87b86462011-12-14 13:48:03 -08002526 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002527 return;
2528 }
2529
rginda87b86462011-12-14 13:48:03 -08002530 this.syncCursorPosition_();
2531
2532 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002533
2534 if (this.options_.cursorBlink) {
2535 if (this.timeouts_.cursorBlink)
2536 return;
2537
Robert Gindaea2183e2014-07-17 09:51:51 -07002538 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002539 } else {
2540 if (this.timeouts_.cursorBlink) {
2541 clearTimeout(this.timeouts_.cursorBlink);
2542 delete this.timeouts_.cursorBlink;
2543 }
2544 }
2545};
2546
2547/**
rginda87b86462011-12-14 13:48:03 -08002548 * Synchronizes the visible cursor and document selection with the current
2549 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002550 */
2551hterm.Terminal.prototype.syncCursorPosition_ = function() {
2552 var topRowIndex = this.scrollPort_.getTopRowIndex();
2553 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2554 var cursorRowIndex = this.scrollbackRows_.length +
2555 this.screen_.cursorPosition.row;
2556
2557 if (cursorRowIndex > bottomRowIndex) {
2558 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002559 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002560 return;
2561 }
2562
Robert Gindab837c052014-08-11 11:17:51 -07002563 if (this.options_.cursorVisible &&
2564 this.cursorNode_.style.display == 'none') {
2565 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2566 this.cursorNode_.style.display = '';
2567 }
2568
2569
rginda8ba33642011-12-14 12:31:31 -08002570 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002571 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2572 'px';
2573 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2574 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002575
2576 this.cursorNode_.setAttribute('title',
2577 '(' + this.screen_.cursorPosition.row +
2578 ', ' + this.screen_.cursorPosition.column +
2579 ')');
2580
2581 // Update the caret for a11y purposes.
2582 var selection = this.document_.getSelection();
2583 if (selection && selection.isCollapsed)
2584 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002585};
2586
Robert Gindafb1be6a2013-12-11 11:56:22 -08002587/**
2588 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2589 * and character cell dimensions.
2590 */
Robert Ginda830583c2013-08-07 13:20:46 -07002591hterm.Terminal.prototype.restyleCursor_ = function() {
2592 var shape = this.cursorShape_;
2593
2594 if (this.cursorNode_.getAttribute('focus') == 'false') {
2595 // Always show a block cursor when unfocused.
2596 shape = hterm.Terminal.cursorShape.BLOCK;
2597 }
2598
2599 var style = this.cursorNode_.style;
2600
Robert Gindafb1be6a2013-12-11 11:56:22 -08002601 style.width = this.scrollPort_.characterSize.width + 'px';
2602
Robert Ginda830583c2013-08-07 13:20:46 -07002603 switch (shape) {
2604 case hterm.Terminal.cursorShape.BEAM:
2605 style.height = this.scrollPort_.characterSize.height + 'px';
2606 style.backgroundColor = 'transparent';
2607 style.borderBottomStyle = null;
2608 style.borderLeftStyle = 'solid';
2609 break;
2610
2611 case hterm.Terminal.cursorShape.UNDERLINE:
2612 style.height = this.scrollPort_.characterSize.baseline + 'px';
2613 style.backgroundColor = 'transparent';
2614 style.borderBottomStyle = 'solid';
2615 // correct the size to put it exactly at the baseline
2616 style.borderLeftStyle = null;
2617 break;
2618
2619 default:
2620 style.height = this.scrollPort_.characterSize.height + 'px';
2621 style.backgroundColor = this.cursorColor_;
2622 style.borderBottomStyle = null;
2623 style.borderLeftStyle = null;
2624 break;
2625 }
2626};
2627
rginda8ba33642011-12-14 12:31:31 -08002628/**
2629 * Synchronizes the visible cursor with the current cursor coordinates.
2630 *
2631 * The sync will happen asynchronously, soon after the call stack winds down.
2632 * Multiple calls will be coalesced into a single sync.
2633 */
2634hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2635 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002636 return;
rginda8ba33642011-12-14 12:31:31 -08002637
2638 var self = this;
2639 this.timeouts_.syncCursor = setTimeout(function() {
2640 self.syncCursorPosition_();
2641 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002642 }, 0);
2643};
2644
rgindacc2996c2012-02-24 14:59:31 -08002645/**
rgindaf522ce02012-04-17 17:49:17 -07002646 * Show or hide the zoom warning.
2647 *
2648 * The zoom warning is a message warning the user that their browser zoom must
2649 * be set to 100% in order for hterm to function properly.
2650 *
2651 * @param {boolean} state True to show the message, false to hide it.
2652 */
2653hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2654 if (!this.zoomWarningNode_) {
2655 if (!state)
2656 return;
2657
2658 this.zoomWarningNode_ = this.document_.createElement('div');
2659 this.zoomWarningNode_.style.cssText = (
2660 'color: black;' +
2661 'background-color: #ff2222;' +
2662 'font-size: large;' +
2663 'border-radius: 8px;' +
2664 'opacity: 0.75;' +
2665 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2666 'top: 0.5em;' +
2667 'right: 1.2em;' +
2668 'position: absolute;' +
2669 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002670 '-webkit-user-select: none;' +
2671 '-moz-text-size-adjust: none;' +
2672 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002673
2674 this.zoomWarningNode_.addEventListener('click', function(e) {
2675 this.parentNode.removeChild(this);
2676 });
rgindaf522ce02012-04-17 17:49:17 -07002677 }
2678
Robert Gindab4839c22013-02-28 16:52:10 -08002679 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2680 hterm.zoomWarningMessage,
2681 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2682
rgindaf522ce02012-04-17 17:49:17 -07002683 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2684
2685 if (state) {
2686 if (!this.zoomWarningNode_.parentNode)
2687 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2688 } else if (this.zoomWarningNode_.parentNode) {
2689 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2690 }
2691};
2692
2693/**
rgindacc2996c2012-02-24 14:59:31 -08002694 * Show the terminal overlay for a given amount of time.
2695 *
2696 * The terminal overlay appears in inverse video in a large font, centered
2697 * over the terminal. You should probably keep the overlay message brief,
2698 * since it's in a large font and you probably aren't going to check the size
2699 * of the terminal first.
2700 *
2701 * @param {string} msg The text (not HTML) message to display in the overlay.
2702 * @param {number} opt_timeout The amount of time to wait before fading out
2703 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2704 * stay up forever (or until the next overlay).
2705 */
2706hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002707 if (!this.overlayNode_) {
2708 if (!this.div_)
2709 return;
2710
2711 this.overlayNode_ = this.document_.createElement('div');
2712 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002713 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002714 'font-size: xx-large;' +
2715 'opacity: 0.75;' +
2716 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2717 'position: absolute;' +
2718 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002719 '-webkit-transition: opacity 180ms ease-in;' +
2720 '-moz-user-select: none;' +
2721 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002722
2723 this.overlayNode_.addEventListener('mousedown', function(e) {
2724 e.preventDefault();
2725 e.stopPropagation();
2726 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002727 }
2728
rginda9f5222b2012-03-05 11:53:28 -08002729 this.overlayNode_.style.color = this.prefs_.get('background-color');
2730 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2731 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2732
rgindaf0090c92012-02-10 14:58:52 -08002733 this.overlayNode_.textContent = msg;
2734 this.overlayNode_.style.opacity = '0.75';
2735
2736 if (!this.overlayNode_.parentNode)
2737 this.div_.appendChild(this.overlayNode_);
2738
Robert Ginda97769282013-02-01 15:30:30 -08002739 var divSize = hterm.getClientSize(this.div_);
2740 var overlaySize = hterm.getClientSize(this.overlayNode_);
2741
Robert Ginda8a59f762014-07-23 11:29:55 -07002742 this.overlayNode_.style.top =
2743 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08002744 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07002745 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08002746
2747 var self = this;
2748
2749 if (this.overlayTimeout_)
2750 clearTimeout(this.overlayTimeout_);
2751
rgindacc2996c2012-02-24 14:59:31 -08002752 if (opt_timeout === null)
2753 return;
2754
rgindaf0090c92012-02-10 14:58:52 -08002755 this.overlayTimeout_ = setTimeout(function() {
2756 self.overlayNode_.style.opacity = '0';
Robert Ginda70926e42013-11-25 14:56:36 -08002757 self.overlayTimeout_ = setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002758 if (self.overlayNode_.parentNode)
2759 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002760 self.overlayTimeout_ = null;
2761 self.overlayNode_.style.opacity = '0.75';
2762 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002763 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002764};
2765
rginda4bba5e12012-06-20 16:15:30 -07002766/**
2767 * Paste from the system clipboard to the terminal.
2768 */
2769hterm.Terminal.prototype.paste = function() {
2770 hterm.pasteFromClipboard(this.document_);
2771};
2772
2773/**
2774 * Copy a string to the system clipboard.
2775 *
2776 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05002777 *
2778 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07002779 */
2780hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002781 if (this.prefs_.get('enable-clipboard-notice'))
2782 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2783
rgindaa09e7332012-08-17 12:49:51 -07002784 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002785 copySource.textContent = str;
2786 copySource.style.cssText = (
2787 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002788 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07002789 'position: absolute;' +
2790 'top: -99px');
2791
2792 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002793
rginda4bba5e12012-06-20 16:15:30 -07002794 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002795 var anchorNode = selection.anchorNode;
2796 var anchorOffset = selection.anchorOffset;
2797 var focusNode = selection.focusNode;
2798 var focusOffset = selection.focusOffset;
2799
rginda4bba5e12012-06-20 16:15:30 -07002800 selection.selectAllChildren(copySource);
2801
rgindaa09e7332012-08-17 12:49:51 -07002802 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002803
Rob Spies56953412014-04-28 14:09:47 -07002804 // IE doesn't support selection.extend. This means that the selection
2805 // won't return on IE.
Robert Ginda6f4f0aa2014-07-28 10:21:39 -07002806 if (selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07002807 selection.collapse(anchorNode, anchorOffset);
2808 selection.extend(focusNode, focusOffset);
2809 }
rgindafaa74742012-08-21 13:34:03 -07002810
rginda4bba5e12012-06-20 16:15:30 -07002811 copySource.parentNode.removeChild(copySource);
2812};
2813
Evan Jones2600d4f2016-12-06 09:29:36 -05002814/**
2815 * Returns the selected text, or null if no text is selected.
2816 *
2817 * @return {string|null}
2818 */
rgindaa09e7332012-08-17 12:49:51 -07002819hterm.Terminal.prototype.getSelectionText = function() {
2820 var selection = this.scrollPort_.selection;
2821 selection.sync();
2822
2823 if (selection.isCollapsed)
2824 return null;
2825
2826
2827 // Start offset measures from the beginning of the line.
2828 var startOffset = selection.startOffset;
2829 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002830
Robert Gindafdbb3f22012-09-06 20:23:06 -07002831 if (node.nodeName != 'X-ROW') {
2832 // If the selection doesn't start on an x-row node, then it must be
2833 // somewhere inside the x-row. Add any characters from previous siblings
2834 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002835
2836 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2837 // If node is the text node in a styled span, move up to the span node.
2838 node = node.parentNode;
2839 }
2840
Robert Gindafdbb3f22012-09-06 20:23:06 -07002841 while (node.previousSibling) {
2842 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002843 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002844 }
rgindaa09e7332012-08-17 12:49:51 -07002845 }
2846
2847 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002848 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
2849 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05002850 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002851
Robert Gindafdbb3f22012-09-06 20:23:06 -07002852 if (node.nodeName != 'X-ROW') {
2853 // If the selection doesn't end on an x-row node, then it must be
2854 // somewhere inside the x-row. Add any characters from following siblings
2855 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002856
2857 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2858 // If node is the text node in a styled span, move up to the span node.
2859 node = node.parentNode;
2860 }
2861
Robert Gindafdbb3f22012-09-06 20:23:06 -07002862 while (node.nextSibling) {
2863 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002864 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002865 }
rgindaa09e7332012-08-17 12:49:51 -07002866 }
2867
2868 var rv = this.getRowsText(selection.startRow.rowIndex,
2869 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002870 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002871};
2872
rginda4bba5e12012-06-20 16:15:30 -07002873/**
2874 * Copy the current selection to the system clipboard, then clear it after a
2875 * short delay.
2876 */
2877hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002878 var text = this.getSelectionText();
2879 if (text != null)
2880 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002881};
2882
rgindaf0090c92012-02-10 14:58:52 -08002883hterm.Terminal.prototype.overlaySize = function() {
2884 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2885};
2886
rginda87b86462011-12-14 13:48:03 -08002887/**
2888 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2889 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07002890 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08002891 */
2892hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002893 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002894 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2895
Robert Ginda8cb7d902013-06-20 14:37:18 -07002896 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08002897};
2898
2899/**
Mike Frysinger70b94692017-01-26 18:57:50 -10002900 * Launches url in a new tab.
2901 *
2902 * @param {string} url URL to launch in a new tab.
2903 */
2904hterm.Terminal.prototype.openUrl = function(url) {
2905 var win = window.open(url, '_blank');
2906 win.focus();
2907}
2908
2909/**
2910 * Open the selected url.
2911 */
2912hterm.Terminal.prototype.openSelectedUrl_ = function() {
2913 var str = this.getSelectionText();
2914
2915 // If there is no selection, try and expand wherever they clicked.
2916 if (str == null) {
2917 this.screen_.expandSelection(this.document_.getSelection());
2918 str = this.getSelectionText();
2919 }
2920
2921 // Make sure URL is valid before opening.
2922 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
2923 return;
2924 // If the URL isn't anchored, it'll open relative to the extension.
2925 // We have no way of knowing the correct schema, so assume http.
2926 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0)
2927 str = 'http://' + str;
2928
2929 this.openUrl(str);
2930}
2931
2932
2933/**
rgindad5613292012-06-19 15:40:37 -07002934 * Add the terminalRow and terminalColumn properties to mouse events and
2935 * then forward on to onMouse().
2936 *
2937 * The terminalRow and terminalColumn properties contain the (row, column)
2938 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05002939 *
2940 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07002941 */
2942hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002943 if (e.processedByTerminalHandler_) {
2944 // We register our event handlers on the document, as well as the cursor
2945 // and the scroll blocker. Mouse events that occur on the cursor or
2946 // scroll blocker will also appear on the document, but we don't want to
2947 // process them twice.
2948 //
2949 // We can't just prevent bubbling because that has other side effects, so
2950 // we decorate the event object with this property instead.
2951 return;
2952 }
2953
Robert Ginda6aec7eb2015-06-16 10:31:30 -07002954 var reportMouseEvents = (!this.defeatMouseReports_ &&
2955 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
2956
rgindafaa74742012-08-21 13:34:03 -07002957 e.processedByTerminalHandler_ = true;
2958
Robert Gindaeda48db2014-07-17 09:25:30 -07002959 // One based row/column stored on the mouse event.
2960 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2961 this.scrollPort_.characterSize.height) + 1;
2962 e.terminalColumn = parseInt(e.clientX /
2963 this.scrollPort_.characterSize.width) + 1;
2964
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002965 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
2966 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07002967 return;
2968 }
2969
Robert Ginda6aec7eb2015-06-16 10:31:30 -07002970 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07002971 // If the cursor is visible and we're not sending mouse events to the
2972 // host app, then we want to hide the terminal cursor when the mouse
2973 // cursor is over top. This keeps the terminal cursor from interfering
2974 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07002975 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
2976 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
2977 this.cursorNode_.style.display = 'none';
2978 } else if (this.cursorNode_.style.display == 'none') {
2979 this.cursorNode_.style.display = '';
2980 }
2981 }
rgindad5613292012-06-19 15:40:37 -07002982
Robert Ginda928cf632014-03-05 15:07:41 -08002983 if (e.type == 'mousedown') {
Robert Ginda6aec7eb2015-06-16 10:31:30 -07002984 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08002985 // If VT mouse reporting is disabled, or has been defeated with
2986 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07002987 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08002988 this.setSelectionEnabled(true);
2989 } else {
2990 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07002991 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07002992 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08002993 this.setSelectionEnabled(false);
2994 e.preventDefault();
2995 }
2996 }
2997
Robert Ginda6aec7eb2015-06-16 10:31:30 -07002998 if (!reportMouseEvents) {
Robert Ginda15ed4902016-07-12 10:43:22 -07002999 if (e.type == 'dblclick' && this.copyOnSelect) {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003000 this.screen_.expandSelection(this.document_.getSelection());
Robert Ginda15ed4902016-07-12 10:43:22 -07003001 this.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07003002 }
3003
Mike Frysinger70b94692017-01-26 18:57:50 -10003004 if (e.type == 'click' && !e.shiftKey && e.ctrlKey) {
3005 // Debounce this event with the dblclick event. If you try to doubleclick
3006 // a URL to open it, Chrome will fire click then dblclick, but we won't
3007 // have expanded the selection text at the first click event.
3008 clearTimeout(this.timeouts_.openUrl);
3009 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3010 500);
3011 return;
3012 }
3013
Robert Ginda928cf632014-03-05 15:07:41 -08003014 if (e.type == 'mousedown' && e.which == this.mousePasteButton)
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003015 this.paste();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003016
3017 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
3018 !this.document_.getSelection().isCollapsed) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003019 this.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003020 }
3021
3022 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3023 this.scrollBlockerNode_.engaged) {
3024 // Disengage the scroll-blocker after one of these events.
3025 this.scrollBlockerNode_.engaged = false;
3026 this.scrollBlockerNode_.style.top = '-99px';
3027 }
3028
Robert Ginda928cf632014-03-05 15:07:41 -08003029 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003030 if (!this.scrollBlockerNode_.engaged) {
3031 if (e.type == 'mousedown') {
3032 // Move the scroll-blocker into place if we want to keep the scrollport
3033 // from scrolling.
3034 this.scrollBlockerNode_.engaged = true;
3035 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3036 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3037 } else if (e.type == 'mousemove') {
3038 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3039 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003040 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003041 e.preventDefault();
3042 }
3043 }
Robert Ginda928cf632014-03-05 15:07:41 -08003044
3045 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003046 }
3047
Robert Ginda928cf632014-03-05 15:07:41 -08003048 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3049 // Restore this on mouseup in case it was temporarily defeated with a
3050 // alt-mousedown. Only do this when the selection is empty so that
3051 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003052 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003053 }
rgindad5613292012-06-19 15:40:37 -07003054};
3055
3056/**
3057 * Clients should override this if they care to know about mouse events.
3058 *
3059 * The event parameter will be a normal DOM mouse click event with additional
3060 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003061 *
3062 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003063 */
3064hterm.Terminal.prototype.onMouse = function(e) { };
3065
3066/**
rginda8e92a692012-05-20 19:37:20 -07003067 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003068 *
3069 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003070 */
Rob Spies06533ba2014-04-24 11:20:37 -07003071hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3072 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003073 this.restyleCursor_();
Michael Kelly485ecd12014-06-09 11:41:56 -04003074 if (focused === true)
3075 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003076};
3077
3078/**
rginda8ba33642011-12-14 12:31:31 -08003079 * React when the ScrollPort is scrolled.
3080 */
3081hterm.Terminal.prototype.onScroll_ = function() {
3082 this.scheduleSyncCursorPosition_();
3083};
3084
3085/**
rginda9846e2f2012-01-27 13:53:33 -08003086 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003087 *
3088 * @param {Event} e The DOM paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003089 */
3090hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07003091 var data = e.text.replace(/\n/mg, '\r');
Connor Hegartyf525ceb2014-09-03 13:36:03 -07003092 data = this.keyboard.encode(data);
Robert Gindaa063b202014-07-21 11:08:25 -07003093 if (this.options_.bracketedPaste)
3094 data = '\x1b[200~' + data + '\x1b[201~';
3095
3096 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003097};
3098
3099/**
rgindaa09e7332012-08-17 12:49:51 -07003100 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003101 *
3102 * @param {Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003103 */
3104hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003105 if (!this.useDefaultWindowCopy) {
3106 e.preventDefault();
3107 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3108 }
rgindaa09e7332012-08-17 12:49:51 -07003109};
3110
3111/**
rginda8ba33642011-12-14 12:31:31 -08003112 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003113 *
3114 * Note: This function should not directly contain code that alters the internal
3115 * state of the terminal. That kind of code belongs in realizeWidth or
3116 * realizeHeight, so that it can be executed synchronously in the case of a
3117 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003118 */
3119hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003120 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003121 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003122 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003123 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003124
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003125 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003126 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003127 // gets removed from the document or during the initial load, and we can't
3128 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003129 // This can also happen if called before the scrollPort calculates the
3130 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003131 return;
3132 }
3133
rgindaa8ba17d2012-08-15 14:41:10 -07003134 var isNewSize = (columnCount != this.screenSize.width ||
3135 rowCount != this.screenSize.height);
3136
3137 // We do this even if the size didn't change, just to be sure everything is
3138 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003139 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003140 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003141
3142 if (isNewSize)
3143 this.overlaySize();
3144
Robert Gindafb1be6a2013-12-11 11:56:22 -08003145 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003146 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08003147};
3148
3149/**
3150 * Service the cursor blink timeout.
3151 */
3152hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003153 if (!this.options_.cursorBlink) {
3154 delete this.timeouts_.cursorBlink;
3155 return;
3156 }
3157
Robert Ginda830583c2013-08-07 13:20:46 -07003158 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3159 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003160 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003161 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3162 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003163 } else {
rginda87b86462011-12-14 13:48:03 -08003164 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003165 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3166 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003167 }
3168};
David Reveman8f552492012-03-28 12:18:41 -04003169
3170/**
3171 * Set the scrollbar-visible mode bit.
3172 *
3173 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3174 * Otherwise it will not.
3175 *
3176 * Defaults to on.
3177 *
3178 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3179 */
3180hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3181 this.scrollPort_.setScrollbarVisible(state);
3182};
Michael Kelly485ecd12014-06-09 11:41:56 -04003183
3184/**
Rob Spies49039e52014-12-17 13:40:04 -08003185 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003186 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003187 *
3188 * Defaults to 1.
3189 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003190 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003191 */
3192hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3193 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3194};
3195
3196/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003197 * Close all web notifications created by terminal bells.
3198 */
3199hterm.Terminal.prototype.closeBellNotifications_ = function() {
3200 this.bellNotificationList_.forEach(function(n) {
3201 n.close();
3202 });
3203 this.bellNotificationList_.length = 0;
3204};