blob: 7a75e7a132feaf29afcc4196890735e193cc3e60 [file] [log] [blame]
rginda87b86462011-12-14 13:48:03 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rginda8ba33642011-12-14 12:31:31 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rgindacbbd7482012-06-13 15:06:16 -07005'use strict';
6
Masaya Suzuki273aa982014-05-31 07:25:55 +09007lib.rtdep('lib.colors', 'lib.PreferenceManager', 'lib.resource', 'lib.wc',
Rob Spiesf4e90e82015-01-28 12:10:13 -08008 'lib.f', 'hterm.Keyboard', 'hterm.Options', 'hterm.PreferenceManager',
Ricky Liang48f05cb2013-12-31 23:35:29 +08009 'hterm.Screen', 'hterm.ScrollPort', 'hterm.Size',
10 'hterm.TextAttributes', 'hterm.VT');
rgindacbbd7482012-06-13 15:06:16 -070011
rginda8ba33642011-12-14 12:31:31 -080012/**
13 * Constructor for the Terminal class.
14 *
15 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
16 * classes to provide the complete terminal functionality.
17 *
18 * There are a number of lower-level Terminal methods that can be called
19 * directly to manipulate the cursor, text, scroll region, and other terminal
20 * attributes. However, the primary method is interpret(), which parses VT
21 * escape sequences and invokes the appropriate Terminal methods.
22 *
23 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
24 *
25 * TODO(rginda): Eventually we're going to need to support characters which are
26 * displayed twice as wide as standard latin characters. This is to support
27 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080028 *
Robert Ginda57f03b42012-09-13 11:02:48 -070029 * @param {string} opt_profileId Optional preference profile name. If not
rginda9f5222b2012-03-05 11:53:28 -080030 * provided, defaults to 'default'.
rginda8ba33642011-12-14 12:31:31 -080031 */
Robert Ginda57f03b42012-09-13 11:02:48 -070032hterm.Terminal = function(opt_profileId) {
33 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080034
rginda8ba33642011-12-14 12:31:31 -080035 // Two screen instances.
36 this.primaryScreen_ = new hterm.Screen();
37 this.alternateScreen_ = new hterm.Screen();
38
39 // The "current" screen.
40 this.screen_ = this.primaryScreen_;
41
rginda8ba33642011-12-14 12:31:31 -080042 // The local notion of the screen size. ScreenBuffers also have a size which
43 // indicates their present size. During size changes, the two may disagree.
44 // Also, the inactive screen's size is not altered until it is made the active
45 // screen.
46 this.screenSize = new hterm.Size(0, 0);
47
rginda8ba33642011-12-14 12:31:31 -080048 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080049 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080050 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
51 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080052 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070053 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080054
rginda87b86462011-12-14 13:48:03 -080055 // The div that contains this terminal.
56 this.div_ = null;
57
rgindac9bc5502012-01-18 11:48:44 -080058 // The document that contains the scrollPort. Defaulted to the global
59 // document here so that the terminal is functional even if it hasn't been
60 // inserted into a document yet, but re-set in decorate().
61 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080062
rginda8ba33642011-12-14 12:31:31 -080063 // The rows that have scrolled off screen and are no longer addressable.
64 this.scrollbackRows_ = [];
65
rgindac9bc5502012-01-18 11:48:44 -080066 // Saved tab stops.
67 this.tabStops_ = [];
68
David Benjamin66e954d2012-05-05 21:08:12 -040069 // Keep track of whether default tab stops have been erased; after a TBC
70 // clears all tab stops, defaults aren't restored on resize until a reset.
71 this.defaultTabStops = true;
72
rginda8ba33642011-12-14 12:31:31 -080073 // The VT's notion of the top and bottom rows. Used during some VT
74 // cursor positioning and scrolling commands.
75 this.vtScrollTop_ = null;
76 this.vtScrollBottom_ = null;
77
78 // The DIV element for the visible cursor.
79 this.cursorNode_ = null;
80
Robert Ginda830583c2013-08-07 13:20:46 -070081 // The current cursor shape of the terminal.
82 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
83
84 // The current color of the cursor.
85 this.cursorColor_ = null;
86
Robert Gindaea2183e2014-07-17 09:51:51 -070087 // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded.
88 this.cursorBlinkCycle_ = [100, 100];
89
90 // Pre-bound onCursorBlink_ handler, so we don't have to do this for each
91 // cursor on/off servicing.
92 this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
93
rginda9f5222b2012-03-05 11:53:28 -080094 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070095 // each output and keystroke. They are initialized by the preference manager.
Robert Ginda8cb7d902013-06-20 14:37:18 -070096 this.backgroundColor_ = null;
97 this.foregroundColor_ = null;
Robert Ginda57f03b42012-09-13 11:02:48 -070098 this.scrollOnOutput_ = null;
99 this.scrollOnKeystroke_ = null;
rginda9f5222b2012-03-05 11:53:28 -0800100
Robert Ginda6aec7eb2015-06-16 10:31:30 -0700101 // True if we should override mouse event reporting to allow local selection.
102 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -0800103
rgindaf0090c92012-02-10 14:58:52 -0800104 // Terminal bell sound.
105 this.bellAudio_ = this.document_.createElement('audio');
rgindaf0090c92012-02-10 14:58:52 -0800106 this.bellAudio_.setAttribute('preload', 'auto');
107
Michael Kelly485ecd12014-06-09 11:41:56 -0400108 // All terminal bell notifications that have been generated (not necessarily
109 // shown).
110 this.bellNotificationList_ = [];
111
112 // Whether we have permission to display notifications.
113 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400114
rginda6d397402012-01-17 10:58:29 -0800115 // Cursor position and attributes saved with DECSC.
116 this.savedOptions_ = {};
117
rginda8ba33642011-12-14 12:31:31 -0800118 // The current mode bits for the terminal.
119 this.options_ = new hterm.Options();
120
121 // Timeouts we might need to clear.
122 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800123
124 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800125 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800126
Zhu Qunying30d40712017-03-14 16:27:00 -0700127 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800128 this.keyboard = new hterm.Keyboard(this);
129
rginda87b86462011-12-14 13:48:03 -0800130 // General IO interface that can be given to third parties without exposing
131 // the entire terminal object.
132 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800133
rgindad5613292012-06-19 15:40:37 -0700134 // True if mouse-click-drag should scroll the terminal.
135 this.enableMouseDragScroll = true;
136
Robert Ginda57f03b42012-09-13 11:02:48 -0700137 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400138 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700139 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700140
Zhu Qunying30d40712017-03-14 16:27:00 -0700141 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700142 this.useDefaultWindowCopy = false;
143
144 this.clearSelectionAfterCopy = true;
145
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400146 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800147 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700148
149 this.setProfile(opt_profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500150 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800151};
152
153/**
Robert Ginda830583c2013-08-07 13:20:46 -0700154 * Possible cursor shapes.
155 */
156hterm.Terminal.cursorShape = {
157 BLOCK: 'BLOCK',
158 BEAM: 'BEAM',
159 UNDERLINE: 'UNDERLINE'
160};
161
162/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700163 * Clients should override this to be notified when the terminal is ready
164 * for use.
165 *
166 * The terminal initialization is asynchronous, and shouldn't be used before
167 * this method is called.
168 */
169hterm.Terminal.prototype.onTerminalReady = function() { };
170
171/**
rginda35c456b2012-02-09 17:29:05 -0800172 * Default tab with of 8 to match xterm.
173 */
174hterm.Terminal.prototype.tabWidth = 8;
175
176/**
rginda9f5222b2012-03-05 11:53:28 -0800177 * Select a preference profile.
178 *
179 * This will load the terminal preferences for the given profile name and
180 * associate subsequent preference changes with the new preference profile.
181 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500182 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800183 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700184 * @param {function} opt_callback Optional callback to invoke when the profile
185 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800186 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700187hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
188 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800189
Robert Ginda57f03b42012-09-13 11:02:48 -0700190 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800191
Robert Ginda57f03b42012-09-13 11:02:48 -0700192 if (this.prefs_)
193 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800194
Robert Ginda57f03b42012-09-13 11:02:48 -0700195 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
196 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800197 'alt-gr-mode': function(v) {
198 if (v == null) {
199 if (navigator.language.toLowerCase() == 'en-us') {
200 v = 'none';
201 } else {
202 v = 'right-alt';
203 }
204 } else if (typeof v == 'string') {
205 v = v.toLowerCase();
206 } else {
207 v = 'none';
208 }
209
210 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v))
211 v = 'none';
212
213 terminal.keyboard.altGrMode = v;
214 },
215
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700216 'alt-backspace-is-meta-backspace': function(v) {
217 terminal.keyboard.altBackspaceIsMetaBackspace = v;
218 },
219
Robert Ginda57f03b42012-09-13 11:02:48 -0700220 'alt-is-meta': function(v) {
221 terminal.keyboard.altIsMeta = v;
222 },
223
224 'alt-sends-what': function(v) {
225 if (!/^(escape|8-bit|browser-key)$/.test(v))
226 v = 'escape';
227
228 terminal.keyboard.altSendsWhat = v;
229 },
230
231 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800232 var ary = v.match(/^lib-resource:(\S+)/);
233 if (ary) {
234 terminal.bellAudio_.setAttribute('src',
235 lib.resource.getDataUrl(ary[1]));
236 } else {
237 terminal.bellAudio_.setAttribute('src', v);
238 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700239 },
240
Michael Kelly485ecd12014-06-09 11:41:56 -0400241 'desktop-notification-bell': function(v) {
242 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700243 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400244 Notification.permission === 'granted';
245 if (!terminal.desktopNotificationBell_) {
246 // Note: We don't call Notification.requestPermission here because
247 // Chrome requires the call be the result of a user action (such as an
248 // onclick handler), and pref listeners are run asynchronously.
249 //
250 // A way of working around this would be to display a dialog in the
251 // terminal with a "click-to-request-permission" button.
252 console.warn('desktop-notification-bell is true but we do not have ' +
253 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400254 }
255 } else {
256 terminal.desktopNotificationBell_ = false;
257 }
258 },
259
Robert Ginda57f03b42012-09-13 11:02:48 -0700260 'background-color': function(v) {
261 terminal.setBackgroundColor(v);
262 },
263
264 'background-image': function(v) {
265 terminal.scrollPort_.setBackgroundImage(v);
266 },
267
268 'background-size': function(v) {
269 terminal.scrollPort_.setBackgroundSize(v);
270 },
271
272 'background-position': function(v) {
273 terminal.scrollPort_.setBackgroundPosition(v);
274 },
275
276 'backspace-sends-backspace': function(v) {
277 terminal.keyboard.backspaceSendsBackspace = v;
278 },
279
Brad Town18654b62015-03-12 00:27:45 -0700280 'character-map-overrides': function(v) {
281 if (!(v == null || v instanceof Object)) {
282 console.warn('Preference character-map-modifications is not an ' +
283 'object: ' + v);
284 return;
285 }
286
Mike Frysinger095d4062017-06-14 00:29:48 -0700287 terminal.vt.characterMaps.reset();
288 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700289 },
290
Robert Ginda57f03b42012-09-13 11:02:48 -0700291 'cursor-blink': function(v) {
292 terminal.setCursorBlink(!!v);
293 },
294
Robert Gindaea2183e2014-07-17 09:51:51 -0700295 'cursor-blink-cycle': function(v) {
296 if (v instanceof Array &&
297 typeof v[0] == 'number' &&
298 typeof v[1] == 'number') {
299 terminal.cursorBlinkCycle_ = v;
300 } else if (typeof v == 'number') {
301 terminal.cursorBlinkCycle_ = [v, v];
302 } else {
303 // Fast blink indicates an error.
304 terminal.cursorBlinkCycle_ = [100, 100];
305 }
306 },
307
Robert Ginda57f03b42012-09-13 11:02:48 -0700308 'cursor-color': function(v) {
309 terminal.setCursorColor(v);
310 },
311
312 'color-palette-overrides': function(v) {
313 if (!(v == null || v instanceof Object || v instanceof Array)) {
314 console.warn('Preference color-palette-overrides is not an array or ' +
315 'object: ' + v);
316 return;
rginda9f5222b2012-03-05 11:53:28 -0800317 }
rginda9f5222b2012-03-05 11:53:28 -0800318
Robert Ginda57f03b42012-09-13 11:02:48 -0700319 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700320
Robert Ginda57f03b42012-09-13 11:02:48 -0700321 if (v) {
322 for (var key in v) {
323 var i = parseInt(key);
324 if (isNaN(i) || i < 0 || i > 255) {
325 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
326 continue;
327 }
328
329 if (v[i]) {
330 var rgb = lib.colors.normalizeCSS(v[i]);
331 if (rgb)
332 lib.colors.colorPalette[i] = rgb;
333 }
334 }
rginda30f20f62012-04-05 16:36:19 -0700335 }
rginda30f20f62012-04-05 16:36:19 -0700336
Evan Jones5f9df812016-12-06 09:38:58 -0500337 terminal.primaryScreen_.textAttributes.resetColorPalette();
Robert Ginda57f03b42012-09-13 11:02:48 -0700338 terminal.alternateScreen_.textAttributes.resetColorPalette();
339 },
rginda30f20f62012-04-05 16:36:19 -0700340
Robert Ginda57f03b42012-09-13 11:02:48 -0700341 'copy-on-select': function(v) {
342 terminal.copyOnSelect = !!v;
343 },
rginda9f5222b2012-03-05 11:53:28 -0800344
Rob Spies0bec09b2014-06-06 15:58:09 -0700345 'use-default-window-copy': function(v) {
346 terminal.useDefaultWindowCopy = !!v;
347 },
348
349 'clear-selection-after-copy': function(v) {
350 terminal.clearSelectionAfterCopy = !!v;
351 },
352
Robert Ginda7e5e9522014-03-14 12:23:58 -0700353 'ctrl-plus-minus-zero-zoom': function(v) {
354 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
355 },
356
Robert Gindafb5a3f92014-05-13 14:12:00 -0700357 'ctrl-c-copy': function(v) {
358 terminal.keyboard.ctrlCCopy = v;
359 },
360
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100361 'ctrl-v-paste': function(v) {
362 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700363 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100364 },
365
Masaya Suzuki273aa982014-05-31 07:25:55 +0900366 'east-asian-ambiguous-as-two-column': function(v) {
367 lib.wc.regardCjkAmbiguous = v;
368 },
369
Robert Ginda57f03b42012-09-13 11:02:48 -0700370 'enable-8-bit-control': function(v) {
371 terminal.vt.enable8BitControl = !!v;
372 },
rginda30f20f62012-04-05 16:36:19 -0700373
Robert Ginda57f03b42012-09-13 11:02:48 -0700374 'enable-bold': function(v) {
375 terminal.syncBoldSafeState();
376 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400377
Robert Ginda3e278d72014-03-25 13:18:51 -0700378 'enable-bold-as-bright': function(v) {
379 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
380 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
381 },
382
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400383 'enable-blink': function(v) {
384 terminal.syncBlinkState();
385 },
386
Robert Ginda57f03b42012-09-13 11:02:48 -0700387 'enable-clipboard-write': function(v) {
388 terminal.vt.enableClipboardWrite = !!v;
389 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400390
Robert Ginda3755e752013-05-31 13:34:09 -0700391 'enable-dec12': function(v) {
392 terminal.vt.enableDec12 = !!v;
393 },
394
Robert Ginda57f03b42012-09-13 11:02:48 -0700395 'font-family': function(v) {
396 terminal.syncFontFamily();
397 },
rginda30f20f62012-04-05 16:36:19 -0700398
Robert Ginda57f03b42012-09-13 11:02:48 -0700399 'font-size': function(v) {
400 terminal.setFontSize(v);
401 },
rginda9875d902012-08-20 16:21:57 -0700402
Robert Ginda57f03b42012-09-13 11:02:48 -0700403 'font-smoothing': function(v) {
404 terminal.syncFontFamily();
405 },
rgindade84e382012-04-20 15:39:31 -0700406
Robert Ginda57f03b42012-09-13 11:02:48 -0700407 'foreground-color': function(v) {
408 terminal.setForegroundColor(v);
409 },
rginda30f20f62012-04-05 16:36:19 -0700410
Robert Ginda57f03b42012-09-13 11:02:48 -0700411 'home-keys-scroll': function(v) {
412 terminal.keyboard.homeKeysScroll = v;
413 },
rginda4bba5e12012-06-20 16:15:30 -0700414
Robert Gindaa8165692015-06-15 14:46:31 -0700415 'keybindings': function(v) {
416 terminal.keyboard.bindings.clear();
417
418 if (!v)
419 return;
420
421 if (!(v instanceof Object)) {
422 console.error('Error in keybindings preference: Expected object');
423 return;
424 }
425
426 try {
427 terminal.keyboard.bindings.addBindings(v);
428 } catch (ex) {
429 console.error('Error in keybindings preference: ' + ex);
430 }
431 },
432
Robert Ginda57f03b42012-09-13 11:02:48 -0700433 'max-string-sequence': function(v) {
434 terminal.vt.maxStringSequence = v;
435 },
rginda11057d52012-04-25 12:29:56 -0700436
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700437 'media-keys-are-fkeys': function(v) {
438 terminal.keyboard.mediaKeysAreFKeys = v;
439 },
440
Robert Ginda57f03b42012-09-13 11:02:48 -0700441 'meta-sends-escape': function(v) {
442 terminal.keyboard.metaSendsEscape = v;
443 },
rginda30f20f62012-04-05 16:36:19 -0700444
Mike Frysinger847577f2017-05-23 23:25:57 -0400445 'mouse-right-click-paste': function(v) {
446 terminal.mouseRightClickPaste = v;
447 },
448
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 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400542
543 'word-break-match-left': function(v) {
544 terminal.primaryScreen_.wordBreakMatchLeft = v;
545 terminal.alternateScreen_.wordBreakMatchLeft = v;
546 },
547
548 'word-break-match-right': function(v) {
549 terminal.primaryScreen_.wordBreakMatchRight = v;
550 terminal.alternateScreen_.wordBreakMatchRight = v;
551 },
552
553 'word-break-match-middle': function(v) {
554 terminal.primaryScreen_.wordBreakMatchMiddle = v;
555 terminal.alternateScreen_.wordBreakMatchMiddle = v;
556 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700557 });
rginda30f20f62012-04-05 16:36:19 -0700558
Robert Ginda57f03b42012-09-13 11:02:48 -0700559 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800560 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700561
562 if (opt_callback)
563 opt_callback();
564 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800565};
566
Rob Spies56953412014-04-28 14:09:47 -0700567
568/**
569 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500570 *
571 * @return {hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700572 */
573hterm.Terminal.prototype.getPrefs = function() {
574 return this.prefs_;
575};
576
Robert Gindaa063b202014-07-21 11:08:25 -0700577/**
578 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500579 *
580 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700581 */
582hterm.Terminal.prototype.setBracketedPaste = function(state) {
583 this.options_.bracketedPaste = state;
584};
Rob Spies56953412014-04-28 14:09:47 -0700585
rginda8e92a692012-05-20 19:37:20 -0700586/**
587 * Set the color for the cursor.
588 *
589 * If you want this setting to persist, set it through prefs_, rather than
590 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500591 *
592 * @param {string} color The color to set.
rginda8e92a692012-05-20 19:37:20 -0700593 */
594hterm.Terminal.prototype.setCursorColor = function(color) {
Robert Ginda830583c2013-08-07 13:20:46 -0700595 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700596 this.cursorNode_.style.backgroundColor = color;
597 this.cursorNode_.style.borderColor = color;
598};
599
600/**
601 * Return the current cursor color as a string.
Evan Jones2600d4f2016-12-06 09:29:36 -0500602 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700603 */
604hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700605 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700606};
607
608/**
rgindad5613292012-06-19 15:40:37 -0700609 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500610 *
611 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700612 */
613hterm.Terminal.prototype.setSelectionEnabled = function(state) {
614 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700615};
616
617/**
rginda8e92a692012-05-20 19:37:20 -0700618 * Set the background color.
619 *
620 * If you want this setting to persist, set it through prefs_, rather than
621 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500622 *
623 * @param {string} color The color to set.
rginda8e92a692012-05-20 19:37:20 -0700624 */
625hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700626 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700627 this.primaryScreen_.textAttributes.setDefaults(
628 this.foregroundColor_, this.backgroundColor_);
629 this.alternateScreen_.textAttributes.setDefaults(
630 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700631 this.scrollPort_.setBackgroundColor(color);
632};
633
rginda9f5222b2012-03-05 11:53:28 -0800634/**
635 * Return the current terminal background color.
636 *
637 * Intended for use by other classes, so we don't have to expose the entire
638 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500639 *
640 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800641 */
642hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700643 return this.backgroundColor_;
644};
645
646/**
647 * Set the foreground color.
648 *
649 * If you want this setting to persist, set it through prefs_, rather than
650 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500651 *
652 * @param {string} color The color to set.
rginda8e92a692012-05-20 19:37:20 -0700653 */
654hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700655 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700656 this.primaryScreen_.textAttributes.setDefaults(
657 this.foregroundColor_, this.backgroundColor_);
658 this.alternateScreen_.textAttributes.setDefaults(
659 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700660 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800661};
662
663/**
664 * Return the current terminal foreground color.
665 *
666 * Intended for use by other classes, so we don't have to expose the entire
667 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500668 *
669 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800670 */
671hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700672 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800673};
674
675/**
rginda87b86462011-12-14 13:48:03 -0800676 * Create a new instance of a terminal command and run it with a given
677 * argument string.
678 *
679 * @param {function} commandClass The constructor for a terminal command.
680 * @param {string} argString The argument string to pass to the command.
681 */
682hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700683 var environment = this.prefs_.get('environment');
684 if (typeof environment != 'object' || environment == null)
685 environment = {};
686
rginda87b86462011-12-14 13:48:03 -0800687 var self = this;
688 this.command = new commandClass(
689 { argString: argString || '',
690 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700691 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800692 onExit: function(code) {
693 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800694 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700695 if (self.prefs_.get('close-on-exit'))
696 window.close();
rginda87b86462011-12-14 13:48:03 -0800697 }
698 });
699
rgindafeaf3142012-01-31 15:14:20 -0800700 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800701 this.command.run();
702};
703
704/**
rgindafeaf3142012-01-31 15:14:20 -0800705 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500706 *
707 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800708 */
709hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700710 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800711};
712
713/**
714 * Install the keyboard handler for this terminal.
715 *
716 * This will prevent the browser from seeing any keystrokes sent to the
717 * terminal.
718 */
719hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700720 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
rgindafeaf3142012-01-31 15:14:20 -0800721}
722
723/**
724 * Uninstall the keyboard handler for this terminal.
725 */
726hterm.Terminal.prototype.uninstallKeyboard = function() {
727 this.keyboard.installKeyboard(null);
728}
729
730/**
rginda35c456b2012-02-09 17:29:05 -0800731 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800732 *
733 * Call setFontSize(0) to reset to the default font size.
734 *
735 * This function does not modify the font-size preference.
736 *
737 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800738 */
739hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800740 if (px === 0)
741 px = this.prefs_.get('font-size');
742
rginda35c456b2012-02-09 17:29:05 -0800743 this.scrollPort_.setFontSize(px);
Mike Frysinger66beb0b2017-05-30 19:44:51 -0400744 this.document_.documentElement.style.setProperty(
745 '--hterm-charsize-width', this.scrollPort_.characterSize.width + 'px');
746 this.document_.documentElement.style.setProperty(
747 '--hterm-charsize-height', this.scrollPort_.characterSize.height + 'px');
rginda35c456b2012-02-09 17:29:05 -0800748};
749
750/**
751 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500752 *
753 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800754 */
755hterm.Terminal.prototype.getFontSize = function() {
756 return this.scrollPort_.getFontSize();
757};
758
759/**
rginda8e92a692012-05-20 19:37:20 -0700760 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500761 *
762 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700763 */
764hterm.Terminal.prototype.getFontFamily = function() {
765 return this.scrollPort_.getFontFamily();
766};
767
768/**
rginda35c456b2012-02-09 17:29:05 -0800769 * Set the CSS "font-family" for this terminal.
770 */
rginda9f5222b2012-03-05 11:53:28 -0800771hterm.Terminal.prototype.syncFontFamily = function() {
772 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
773 this.prefs_.get('font-smoothing'));
774 this.syncBoldSafeState();
775};
776
rginda4bba5e12012-06-20 16:15:30 -0700777/**
778 * Set this.mousePasteButton based on the mouse-paste-button pref,
779 * autodetecting if necessary.
780 */
781hterm.Terminal.prototype.syncMousePasteButton = function() {
782 var button = this.prefs_.get('mouse-paste-button');
783 if (typeof button == 'number') {
784 this.mousePasteButton = button;
785 return;
786 }
787
788 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
Mike Frysinger98dd15b2017-05-18 22:52:23 -0400789 if (!ary || ary[1] == 'CrOS') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400790 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700791 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400792 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700793 }
794};
795
796/**
797 * Enable or disable bold based on the enable-bold pref, autodetecting if
798 * necessary.
799 */
rginda9f5222b2012-03-05 11:53:28 -0800800hterm.Terminal.prototype.syncBoldSafeState = function() {
801 var enableBold = this.prefs_.get('enable-bold');
802 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700803 this.primaryScreen_.textAttributes.enableBold = enableBold;
804 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800805 return;
806 }
807
rgindaf7521392012-02-28 17:20:34 -0800808 var normalSize = this.scrollPort_.measureCharacterSize();
809 var boldSize = this.scrollPort_.measureCharacterSize('bold');
810
811 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800812 if (!isBoldSafe) {
813 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700814 'from normal. Font family is: ' +
815 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800816 }
rginda9f5222b2012-03-05 11:53:28 -0800817
Robert Gindaed016262012-10-26 16:27:09 -0700818 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
819 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800820};
821
822/**
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400823 * Enable or disable blink based on the enable-blink pref.
824 */
825hterm.Terminal.prototype.syncBlinkState = function() {
826 this.document_.documentElement.style.setProperty(
827 '--hterm-blink-node-duration',
828 this.prefs_.get('enable-blink') ? '0.7s' : '0');
829};
830
831/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400832 * Set the mouse cursor style based on the current terminal mode.
833 */
834hterm.Terminal.prototype.syncMouseStyle = function() {
835 this.document_.documentElement.style.setProperty(
836 '--hterm-mouse-cursor-style',
837 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
838 'var(--hterm-mouse-cursor-text)' :
839 'var(--hterm-mouse-cursor-pointer)');
840};
841
842/**
rginda87b86462011-12-14 13:48:03 -0800843 * Return a copy of the current cursor position.
844 *
845 * @return {hterm.RowCol} The RowCol object representing the current position.
846 */
847hterm.Terminal.prototype.saveCursor = function() {
848 return this.screen_.cursorPosition.clone();
849};
850
Evan Jones2600d4f2016-12-06 09:29:36 -0500851/**
852 * Return the current text attributes.
853 *
854 * @return {string}
855 */
rgindaa19afe22012-01-25 15:40:22 -0800856hterm.Terminal.prototype.getTextAttributes = function() {
857 return this.screen_.textAttributes;
858};
859
Evan Jones2600d4f2016-12-06 09:29:36 -0500860/**
861 * Set the text attributes.
862 *
863 * @param {string} textAttributes The attributes to set.
864 */
rginda1a09aa02012-06-18 21:11:25 -0700865hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
866 this.screen_.textAttributes = textAttributes;
867};
868
rginda87b86462011-12-14 13:48:03 -0800869/**
rgindaf522ce02012-04-17 17:49:17 -0700870 * Return the current browser zoom factor applied to the terminal.
871 *
872 * @return {number} The current browser zoom factor.
873 */
874hterm.Terminal.prototype.getZoomFactor = function() {
875 return this.scrollPort_.characterSize.zoomFactor;
876};
877
878/**
rginda9846e2f2012-01-27 13:53:33 -0800879 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500880 *
881 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800882 */
883hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800884 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800885};
886
887/**
rginda87b86462011-12-14 13:48:03 -0800888 * Restore a previously saved cursor position.
889 *
890 * @param {hterm.RowCol} cursor The position to restore.
891 */
892hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700893 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
894 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800895 this.screen_.setCursorPosition(row, column);
896 if (cursor.column > column ||
897 cursor.column == column && cursor.overflow) {
898 this.screen_.cursorPosition.overflow = true;
899 }
rginda87b86462011-12-14 13:48:03 -0800900};
901
902/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400903 * Clear the cursor's overflow flag.
904 */
905hterm.Terminal.prototype.clearCursorOverflow = function() {
906 this.screen_.cursorPosition.overflow = false;
907};
908
909/**
Robert Ginda830583c2013-08-07 13:20:46 -0700910 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -0500911 *
912 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -0700913 */
914hterm.Terminal.prototype.setCursorShape = function(shape) {
915 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -0800916 this.restyleCursor_();
Robert Ginda830583c2013-08-07 13:20:46 -0700917}
918
919/**
920 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -0500921 *
922 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -0700923 */
924hterm.Terminal.prototype.getCursorShape = function() {
925 return this.cursorShape_;
926}
927
928/**
rginda87b86462011-12-14 13:48:03 -0800929 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -0500930 *
931 * @param {number} columnCount
rginda87b86462011-12-14 13:48:03 -0800932 */
933hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800934 if (columnCount == null) {
935 this.div_.style.width = '100%';
936 return;
937 }
938
Robert Ginda26806d12014-07-24 13:44:07 -0700939 this.div_.style.width = Math.ceil(
940 this.scrollPort_.characterSize.width *
941 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400942 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800943 this.scheduleSyncCursorPosition_();
944};
rginda87b86462011-12-14 13:48:03 -0800945
rgindac9bc5502012-01-18 11:48:44 -0800946/**
rginda35c456b2012-02-09 17:29:05 -0800947 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -0500948 *
949 * @param {number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -0800950 */
951hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800952 if (rowCount == null) {
953 this.div_.style.height = '100%';
954 return;
955 }
956
rginda35c456b2012-02-09 17:29:05 -0800957 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700958 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800959 this.realizeSize_(this.screenSize.width, rowCount);
960 this.scheduleSyncCursorPosition_();
961};
962
963/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400964 * Deal with terminal size changes.
965 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500966 * @param {number} columnCount The number of columns.
967 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400968 */
969hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
970 if (columnCount != this.screenSize.width)
971 this.realizeWidth_(columnCount);
972
973 if (rowCount != this.screenSize.height)
974 this.realizeHeight_(rowCount);
975
976 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -0700977 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400978};
979
980/**
rgindac9bc5502012-01-18 11:48:44 -0800981 * Deal with terminal width changes.
982 *
983 * This function does what needs to be done when the terminal width changes
984 * out from under us. It happens here rather than in onResize_() because this
985 * code may need to run synchronously to handle programmatic changes of
986 * terminal width.
987 *
988 * Relying on the browser to send us an async resize event means we may not be
989 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -0500990 *
991 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -0800992 */
993hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700994 if (columnCount <= 0)
995 throw new Error('Attempt to realize bad width: ' + columnCount);
996
rgindac9bc5502012-01-18 11:48:44 -0800997 var deltaColumns = columnCount - this.screen_.getWidth();
998
rginda87b86462011-12-14 13:48:03 -0800999 this.screenSize.width = columnCount;
1000 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001001
1002 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -04001003 if (this.defaultTabStops)
1004 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -08001005 } else {
1006 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -04001007 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -08001008 break;
1009
1010 this.tabStops_.pop();
1011 }
1012 }
1013
1014 this.screen_.setColumnCount(this.screenSize.width);
1015};
1016
1017/**
1018 * Deal with terminal height changes.
1019 *
1020 * This function does what needs to be done when the terminal height changes
1021 * out from under us. It happens here rather than in onResize_() because this
1022 * code may need to run synchronously to handle programmatic changes of
1023 * terminal height.
1024 *
1025 * Relying on the browser to send us an async resize event means we may not be
1026 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001027 *
1028 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001029 */
1030hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001031 if (rowCount <= 0)
1032 throw new Error('Attempt to realize bad height: ' + rowCount);
1033
rgindac9bc5502012-01-18 11:48:44 -08001034 var deltaRows = rowCount - this.screen_.getHeight();
1035
1036 this.screenSize.height = rowCount;
1037
1038 var cursor = this.saveCursor();
1039
1040 if (deltaRows < 0) {
1041 // Screen got smaller.
1042 deltaRows *= -1;
1043 while (deltaRows) {
1044 var lastRow = this.getRowCount() - 1;
1045 if (lastRow - this.scrollbackRows_.length == cursor.row)
1046 break;
1047
1048 if (this.getRowText(lastRow))
1049 break;
1050
1051 this.screen_.popRow();
1052 deltaRows--;
1053 }
1054
1055 var ary = this.screen_.shiftRows(deltaRows);
1056 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1057
1058 // We just removed rows from the top of the screen, we need to update
1059 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001060 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001061 } else if (deltaRows > 0) {
1062 // Screen got larger.
1063
1064 if (deltaRows <= this.scrollbackRows_.length) {
1065 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1066 var rows = this.scrollbackRows_.splice(
1067 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1068 this.screen_.unshiftRows(rows);
1069 deltaRows -= scrollbackCount;
1070 cursor.row += scrollbackCount;
1071 }
1072
1073 if (deltaRows)
1074 this.appendRows_(deltaRows);
1075 }
1076
rginda35c456b2012-02-09 17:29:05 -08001077 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001078 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001079};
1080
1081/**
1082 * Scroll the terminal to the top of the scrollback buffer.
1083 */
1084hterm.Terminal.prototype.scrollHome = function() {
1085 this.scrollPort_.scrollRowToTop(0);
1086};
1087
1088/**
1089 * Scroll the terminal to the end.
1090 */
1091hterm.Terminal.prototype.scrollEnd = function() {
1092 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1093};
1094
1095/**
1096 * Scroll the terminal one page up (minus one line) relative to the current
1097 * position.
1098 */
1099hterm.Terminal.prototype.scrollPageUp = function() {
1100 var i = this.scrollPort_.getTopRowIndex();
1101 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
1102};
1103
1104/**
1105 * Scroll the terminal one page down (minus one line) relative to the current
1106 * position.
1107 */
1108hterm.Terminal.prototype.scrollPageDown = function() {
1109 var i = this.scrollPort_.getTopRowIndex();
1110 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -08001111};
1112
rgindac9bc5502012-01-18 11:48:44 -08001113/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001114 * Scroll the terminal one line up relative to the current position.
1115 */
1116hterm.Terminal.prototype.scrollLineUp = function() {
1117 var i = this.scrollPort_.getTopRowIndex();
1118 this.scrollPort_.scrollRowToTop(i - 1);
1119};
1120
1121/**
1122 * Scroll the terminal one line down relative to the current position.
1123 */
1124hterm.Terminal.prototype.scrollLineDown = function() {
1125 var i = this.scrollPort_.getTopRowIndex();
1126 this.scrollPort_.scrollRowToTop(i + 1);
1127};
1128
1129/**
Robert Ginda40932892012-12-10 17:26:40 -08001130 * Clear primary screen, secondary screen, and the scrollback buffer.
1131 */
1132hterm.Terminal.prototype.wipeContents = function() {
1133 this.scrollbackRows_.length = 0;
1134 this.scrollPort_.resetCache();
1135
1136 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
1137 var bottom = screen.getHeight();
1138 if (bottom > 0) {
1139 this.renumberRows_(0, bottom);
1140 this.clearHome(screen);
1141 }
1142 }.bind(this));
1143
1144 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001145 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001146};
1147
1148/**
rgindac9bc5502012-01-18 11:48:44 -08001149 * Full terminal reset.
1150 */
rginda87b86462011-12-14 13:48:03 -08001151hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -08001152 this.clearAllTabStops();
1153 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001154
1155 this.clearHome(this.primaryScreen_);
1156 this.primaryScreen_.textAttributes.reset();
1157
1158 this.clearHome(this.alternateScreen_);
1159 this.alternateScreen_.textAttributes.reset();
1160
rgindab8bc8932012-04-27 12:45:03 -07001161 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1162
Robert Ginda92e18102013-03-14 13:56:37 -07001163 this.vt.reset();
1164
rgindac9bc5502012-01-18 11:48:44 -08001165 this.softReset();
rginda87b86462011-12-14 13:48:03 -08001166};
1167
rgindac9bc5502012-01-18 11:48:44 -08001168/**
1169 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001170 *
1171 * Perform a soft reset to the default values listed in
1172 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001173 */
rginda0f5c0292012-01-13 11:00:13 -08001174hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -07001175 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001176 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001177
Brad Townb62dfdc2015-03-16 19:07:15 -07001178 // We show the cursor on soft reset but do not alter the blink state.
1179 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1180
rgindab8bc8932012-04-27 12:45:03 -07001181 // Xterm also resets the color palette on soft reset, even though it doesn't
1182 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -07001183 this.primaryScreen_.textAttributes.resetColorPalette();
1184 this.alternateScreen_.textAttributes.resetColorPalette();
1185
rgindab8bc8932012-04-27 12:45:03 -07001186 // The xterm man page explicitly says this will happen on soft reset.
1187 this.setVTScrollRegion(null, null);
1188
1189 // Xterm also shows the cursor on soft reset, but does not alter the blink
1190 // state.
rgindaa19afe22012-01-25 15:40:22 -08001191 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001192};
1193
rgindac9bc5502012-01-18 11:48:44 -08001194/**
1195 * Move the cursor forward to the next tab stop, or to the last column
1196 * if no more tab stops are set.
1197 */
1198hterm.Terminal.prototype.forwardTabStop = function() {
1199 var column = this.screen_.cursorPosition.column;
1200
1201 for (var i = 0; i < this.tabStops_.length; i++) {
1202 if (this.tabStops_[i] > column) {
1203 this.setCursorColumn(this.tabStops_[i]);
1204 return;
1205 }
1206 }
1207
David Benjamin66e954d2012-05-05 21:08:12 -04001208 // xterm does not clear the overflow flag on HT or CHT.
1209 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001210 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001211 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001212};
1213
rgindac9bc5502012-01-18 11:48:44 -08001214/**
1215 * Move the cursor backward to the previous tab stop, or to the first column
1216 * if no previous tab stops are set.
1217 */
1218hterm.Terminal.prototype.backwardTabStop = function() {
1219 var column = this.screen_.cursorPosition.column;
1220
1221 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1222 if (this.tabStops_[i] < column) {
1223 this.setCursorColumn(this.tabStops_[i]);
1224 return;
1225 }
1226 }
1227
1228 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001229};
1230
rgindac9bc5502012-01-18 11:48:44 -08001231/**
1232 * Set a tab stop at the given column.
1233 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001234 * @param {integer} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001235 */
1236hterm.Terminal.prototype.setTabStop = function(column) {
1237 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1238 if (this.tabStops_[i] == column)
1239 return;
1240
1241 if (this.tabStops_[i] < column) {
1242 this.tabStops_.splice(i + 1, 0, column);
1243 return;
1244 }
1245 }
1246
1247 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001248};
1249
rgindac9bc5502012-01-18 11:48:44 -08001250/**
1251 * Clear the tab stop at the current cursor position.
1252 *
1253 * No effect if there is no tab stop at the current cursor position.
1254 */
1255hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1256 var column = this.screen_.cursorPosition.column;
1257
1258 var i = this.tabStops_.indexOf(column);
1259 if (i == -1)
1260 return;
1261
1262 this.tabStops_.splice(i, 1);
1263};
1264
1265/**
1266 * Clear all tab stops.
1267 */
1268hterm.Terminal.prototype.clearAllTabStops = function() {
1269 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001270 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001271};
1272
1273/**
1274 * Set up the default tab stops, starting from a given column.
1275 *
1276 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001277 * from the specified column, or 0 if no column is provided. It also flags
1278 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001279 *
1280 * This does not clear the existing tab stops first, use clearAllTabStops
1281 * for that.
1282 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001283 * @param {integer} opt_start Optional starting zero based starting column, useful
rgindac9bc5502012-01-18 11:48:44 -08001284 * for filling out missing tab stops when the terminal is resized.
1285 */
1286hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1287 var start = opt_start || 0;
1288 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001289 // Round start up to a default tab stop.
1290 start = start - 1 - ((start - 1) % w) + w;
1291 for (var i = start; i < this.screenSize.width; i += w) {
1292 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001293 }
David Benjamin66e954d2012-05-05 21:08:12 -04001294
1295 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001296};
1297
rginda6d397402012-01-17 10:58:29 -08001298/**
rginda8ba33642011-12-14 12:31:31 -08001299 * Interpret a sequence of characters.
1300 *
1301 * Incomplete escape sequences are buffered until the next call.
1302 *
1303 * @param {string} str Sequence of characters to interpret or pass through.
1304 */
1305hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001306 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001307 this.scheduleSyncCursorPosition_();
1308};
1309
1310/**
1311 * Take over the given DIV for use as the terminal display.
1312 *
1313 * @param {HTMLDivElement} div The div to use as the terminal display.
1314 */
1315hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001316 this.div_ = div;
1317
rginda8ba33642011-12-14 12:31:31 -08001318 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001319 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001320 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1321 this.scrollPort_.setBackgroundPosition(
1322 this.prefs_.get('background-position'));
Mike Frysinger08bad432017-04-24 00:50:54 -04001323 this.scrollPort_.setUserCssUrl(this.prefs_.get('user-css'));
1324 this.scrollPort_.setUserCssText(this.prefs_.get('user-css-text'));
rginda30f20f62012-04-05 16:36:19 -07001325
rginda0918b652012-04-04 11:26:24 -07001326 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001327
rginda9f5222b2012-03-05 11:53:28 -08001328 this.setFontSize(this.prefs_.get('font-size'));
1329 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001330
David Reveman8f552492012-03-28 12:18:41 -04001331 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001332 this.setScrollWheelMoveMultipler(
1333 this.prefs_.get('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001334
rginda8ba33642011-12-14 12:31:31 -08001335 this.document_ = this.scrollPort_.getDocument();
1336
Evan Jones5f9df812016-12-06 09:38:58 -05001337 this.document_.body.oncontextmenu = function() { return false; };
rginda4bba5e12012-06-20 16:15:30 -07001338
1339 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001340 var screenNode = this.scrollPort_.getScreenNode();
1341 screenNode.addEventListener('mousedown', onMouse);
1342 screenNode.addEventListener('mouseup', onMouse);
1343 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001344 this.scrollPort_.onScrollWheel = onMouse;
1345
Toni Barzic0bfa8922013-11-22 11:18:35 -08001346 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001347 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001348 // Listen for mousedown events on the screenNode as in FF the focus
1349 // events don't bubble.
1350 screenNode.addEventListener('mousedown', function() {
1351 setTimeout(this.onFocusChange_.bind(this, true));
1352 }.bind(this));
1353
Toni Barzic0bfa8922013-11-22 11:18:35 -08001354 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001355 'blur', this.onFocusChange_.bind(this, false));
1356
1357 var style = this.document_.createElement('style');
1358 style.textContent =
1359 ('.cursor-node[focus="false"] {' +
1360 ' box-sizing: border-box;' +
1361 ' background-color: transparent !important;' +
1362 ' border-width: 2px;' +
1363 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001364 '}' +
1365 '.wc-node {' +
1366 ' display: inline-block;' +
1367 ' text-align: center;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001368 ' width: calc(var(--hterm-charsize-width) * 2);' +
Mike Frysinger4036f6e2017-05-31 14:02:55 -04001369 ' line-height: var(--hterm-charsize-height);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001370 '}' +
1371 ':root {' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001372 ' --hterm-charsize-width: ' + this.scrollPort_.characterSize.width + 'px;' +
1373 ' --hterm-charsize-height: ' + this.scrollPort_.characterSize.height + 'px;' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001374 ' --hterm-blink-node-duration: 0.7s;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001375 ' --hterm-mouse-cursor-text: text;' +
1376 ' --hterm-mouse-cursor-pointer: default;' +
1377 ' --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001378 '}' +
1379 '@keyframes blink {' +
1380 ' from { opacity: 1.0; }' +
1381 ' to { opacity: 0.0; }' +
1382 '}' +
1383 '.blink-node {' +
1384 ' animation-name: blink;' +
1385 ' animation-duration: var(--hterm-blink-node-duration);' +
1386 ' animation-iteration-count: infinite;' +
1387 ' animation-timing-function: ease-in-out;' +
1388 ' animation-direction: alternate;' +
rginda8e92a692012-05-20 19:37:20 -07001389 '}');
1390 this.document_.head.appendChild(style);
1391
rginda8ba33642011-12-14 12:31:31 -08001392 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001393 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001394 this.cursorNode_.style.cssText =
1395 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001396 'top: -99px;' +
1397 'display: block;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001398 'width: var(--hterm-charsize-width);' +
1399 'height: var(--hterm-charsize-height);' +
Rob Spies06533ba2014-04-24 11:20:37 -07001400 '-webkit-transition: opacity, background-color 100ms linear;' +
1401 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001402
rginda8e92a692012-05-20 19:37:20 -07001403 this.setCursorColor(this.prefs_.get('cursor-color'));
Robert Gindafb1be6a2013-12-11 11:56:22 -08001404 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1405 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001406
rginda8ba33642011-12-14 12:31:31 -08001407 this.document_.body.appendChild(this.cursorNode_);
1408
rgindad5613292012-06-19 15:40:37 -07001409 // When 'enableMouseDragScroll' is off we reposition this element directly
1410 // under the mouse cursor after a click. This makes Chrome associate
1411 // subsequent mousemove events with the scroll-blocker. Since the
1412 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1413 // events do not cause the scrollport to scroll.
1414 //
1415 // It's a hack, but it's the cleanest way I could find.
1416 this.scrollBlockerNode_ = this.document_.createElement('div');
1417 this.scrollBlockerNode_.style.cssText =
1418 ('position: absolute;' +
1419 'top: -99px;' +
1420 'display: block;' +
1421 'width: 10px;' +
1422 'height: 10px;');
1423 this.document_.body.appendChild(this.scrollBlockerNode_);
1424
rgindad5613292012-06-19 15:40:37 -07001425 this.scrollPort_.onScrollWheel = onMouse;
1426 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1427 ].forEach(function(event) {
1428 this.scrollBlockerNode_.addEventListener(event, onMouse);
1429 this.cursorNode_.addEventListener(event, onMouse);
1430 this.document_.addEventListener(event, onMouse);
1431 }.bind(this));
1432
1433 this.cursorNode_.addEventListener('mousedown', function() {
1434 setTimeout(this.focus.bind(this));
1435 }.bind(this));
1436
rginda8ba33642011-12-14 12:31:31 -08001437 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001438
rginda87b86462011-12-14 13:48:03 -08001439 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001440 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001441};
1442
rginda0918b652012-04-04 11:26:24 -07001443/**
1444 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001445 *
1446 * @return {HTMLDocument}
rginda0918b652012-04-04 11:26:24 -07001447 */
rginda87b86462011-12-14 13:48:03 -08001448hterm.Terminal.prototype.getDocument = function() {
1449 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001450};
1451
1452/**
rginda0918b652012-04-04 11:26:24 -07001453 * Focus the terminal.
1454 */
1455hterm.Terminal.prototype.focus = function() {
1456 this.scrollPort_.focus();
1457};
1458
1459/**
rginda8ba33642011-12-14 12:31:31 -08001460 * Return the HTML Element for a given row index.
1461 *
1462 * This is a method from the RowProvider interface. The ScrollPort uses
1463 * it to fetch rows on demand as they are scrolled into view.
1464 *
1465 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1466 * pairs to conserve memory.
1467 *
1468 * @param {integer} index The zero-based row index, measured relative to the
1469 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001470 * largest indices.
rginda8ba33642011-12-14 12:31:31 -08001471 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1472 */
1473hterm.Terminal.prototype.getRowNode = function(index) {
1474 if (index < this.scrollbackRows_.length)
1475 return this.scrollbackRows_[index];
1476
1477 var screenIndex = index - this.scrollbackRows_.length;
1478 return this.screen_.rowsArray[screenIndex];
1479};
1480
1481/**
1482 * Return the text content for a given range of rows.
1483 *
1484 * This is a method from the RowProvider interface. The ScrollPort uses
1485 * it to fetch text content on demand when the user attempts to copy their
1486 * selection to the clipboard.
1487 *
1488 * @param {integer} start The zero-based row index to start from, measured
1489 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001490 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001491 * @param {integer} end The zero-based row index to end on, measured
1492 * relative to the start of the scrollback buffer.
1493 * @return {string} A single string containing the text value of the range of
1494 * rows. Lines will be newline delimited, with no trailing newline.
1495 */
1496hterm.Terminal.prototype.getRowsText = function(start, end) {
1497 var ary = [];
1498 for (var i = start; i < end; i++) {
1499 var node = this.getRowNode(i);
1500 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001501 if (i < end - 1 && !node.getAttribute('line-overflow'))
1502 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001503 }
1504
rgindaa09e7332012-08-17 12:49:51 -07001505 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001506};
1507
1508/**
1509 * Return the text content for a given row.
1510 *
1511 * This is a method from the RowProvider interface. The ScrollPort uses
1512 * it to fetch text content on demand when the user attempts to copy their
1513 * selection to the clipboard.
1514 *
1515 * @param {integer} index The zero-based row index to return, measured
1516 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001517 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001518 * @return {string} A string containing the text value of the selected row.
1519 */
1520hterm.Terminal.prototype.getRowText = function(index) {
1521 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001522 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001523};
1524
1525/**
1526 * Return the total number of rows in the addressable screen and in the
1527 * scrollback buffer of this terminal.
1528 *
1529 * This is a method from the RowProvider interface. The ScrollPort uses
1530 * it to compute the size of the scrollbar.
1531 *
1532 * @return {integer} The number of rows in this terminal.
1533 */
1534hterm.Terminal.prototype.getRowCount = function() {
1535 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1536};
1537
1538/**
1539 * Create DOM nodes for new rows and append them to the end of the terminal.
1540 *
1541 * This is the only correct way to add a new DOM node for a row. Notice that
1542 * the new row is appended to the bottom of the list of rows, and does not
1543 * require renumbering (of the rowIndex property) of previous rows.
1544 *
1545 * If you think you want a new blank row somewhere in the middle of the
1546 * terminal, look into moveRows_().
1547 *
1548 * This method does not pay attention to vtScrollTop/Bottom, since you should
1549 * be using moveRows() in cases where they would matter.
1550 *
1551 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001552 *
1553 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001554 */
1555hterm.Terminal.prototype.appendRows_ = function(count) {
1556 var cursorRow = this.screen_.rowsArray.length;
1557 var offset = this.scrollbackRows_.length + cursorRow;
1558 for (var i = 0; i < count; i++) {
1559 var row = this.document_.createElement('x-row');
1560 row.appendChild(this.document_.createTextNode(''));
1561 row.rowIndex = offset + i;
1562 this.screen_.pushRow(row);
1563 }
1564
1565 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1566 if (extraRows > 0) {
1567 var ary = this.screen_.shiftRows(extraRows);
1568 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001569 if (this.scrollPort_.isScrolledEnd)
1570 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001571 }
1572
1573 if (cursorRow >= this.screen_.rowsArray.length)
1574 cursorRow = this.screen_.rowsArray.length - 1;
1575
rginda87b86462011-12-14 13:48:03 -08001576 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001577};
1578
1579/**
1580 * Relocate rows from one part of the addressable screen to another.
1581 *
1582 * This is used to recycle rows during VT scrolls (those which are driven
1583 * by VT commands, rather than by the user manipulating the scrollbar.)
1584 *
1585 * In this case, the blank lines scrolled into the scroll region are made of
1586 * the nodes we scrolled off. These have their rowIndex properties carefully
1587 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001588 *
1589 * @param {number} fromIndex The start index.
1590 * @param {number} count The number of rows to move.
1591 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001592 */
1593hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1594 var ary = this.screen_.removeRows(fromIndex, count);
1595 this.screen_.insertRows(toIndex, ary);
1596
1597 var start, end;
1598 if (fromIndex < toIndex) {
1599 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001600 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001601 } else {
1602 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001603 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001604 }
1605
1606 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001607 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001608};
1609
1610/**
1611 * Renumber the rowIndex property of the given range of rows.
1612 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001613 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001614 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001615 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001616 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001617 *
1618 * @param {number} start The start index.
1619 * @param {number} end The end index.
1620 * @param {hterm.Screen} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001621 */
Robert Ginda40932892012-12-10 17:26:40 -08001622hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1623 var screen = opt_screen || this.screen_;
1624
rginda8ba33642011-12-14 12:31:31 -08001625 var offset = this.scrollbackRows_.length;
1626 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001627 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001628 }
1629};
1630
1631/**
1632 * Print a string to the terminal.
1633 *
1634 * This respects the current insert and wraparound modes. It will add new lines
1635 * to the end of the terminal, scrolling off the top into the scrollback buffer
1636 * if necessary.
1637 *
1638 * The string is *not* parsed for escape codes. Use the interpret() method if
1639 * that's what you're after.
1640 *
1641 * @param{string} str The string to print.
1642 */
1643hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001644 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001645
Ricky Liang48f05cb2013-12-31 23:35:29 +08001646 var strWidth = lib.wc.strWidth(str);
1647
1648 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001649 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1650 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001651 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001652 }
rgindaa19afe22012-01-25 15:40:22 -08001653
Ricky Liang48f05cb2013-12-31 23:35:29 +08001654 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001655 var didOverflow = false;
1656 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001657
rgindaa9abdd82012-08-06 18:05:09 -07001658 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1659 didOverflow = true;
1660 count = this.screenSize.width - this.screen_.cursorPosition.column;
1661 }
rgindaa19afe22012-01-25 15:40:22 -08001662
rgindaa9abdd82012-08-06 18:05:09 -07001663 if (didOverflow && !this.options_.wraparound) {
1664 // If the string overflowed the line but wraparound is off, then the
1665 // last printed character should be the last of the string.
1666 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001667 substr = lib.wc.substr(str, startOffset, count - 1) +
1668 lib.wc.substr(str, strWidth - 1);
1669 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001670 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001671 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001672 }
rgindaa19afe22012-01-25 15:40:22 -08001673
Ricky Liang48f05cb2013-12-31 23:35:29 +08001674 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1675 for (var i = 0; i < tokens.length; i++) {
1676 if (tokens[i].wcNode)
1677 this.screen_.textAttributes.wcNode = true;
1678
1679 if (this.options_.insertMode) {
1680 this.screen_.insertString(tokens[i].str);
1681 } else {
1682 this.screen_.overwriteString(tokens[i].str);
1683 }
1684 this.screen_.textAttributes.wcNode = false;
rgindaa9abdd82012-08-06 18:05:09 -07001685 }
1686
1687 this.screen_.maybeClipCurrentRow();
1688 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001689 }
rginda8ba33642011-12-14 12:31:31 -08001690
1691 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001692
rginda9f5222b2012-03-05 11:53:28 -08001693 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001694 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001695};
1696
1697/**
rginda87b86462011-12-14 13:48:03 -08001698 * Set the VT scroll region.
1699 *
rginda87b86462011-12-14 13:48:03 -08001700 * This also resets the cursor position to the absolute (0, 0) position, since
1701 * that's what xterm appears to do.
1702 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001703 * Setting the scroll region to the full height of the terminal will clear
1704 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1705 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1706 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1707 * continue to work as most users would expect.
1708 *
rginda87b86462011-12-14 13:48:03 -08001709 * @param {integer} scrollTop The zero-based top of the scroll region.
1710 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1711 * inclusive.
1712 */
1713hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001714 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001715 this.vtScrollTop_ = null;
1716 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001717 } else {
1718 this.vtScrollTop_ = scrollTop;
1719 this.vtScrollBottom_ = scrollBottom;
1720 }
rginda87b86462011-12-14 13:48:03 -08001721};
1722
1723/**
rginda8ba33642011-12-14 12:31:31 -08001724 * Return the top row index according to the VT.
1725 *
1726 * This will return 0 unless the terminal has been told to restrict scrolling
1727 * to some lower row. It is used for some VT cursor positioning and scrolling
1728 * commands.
1729 *
1730 * @return {integer} The topmost row in the terminal's scroll region.
1731 */
1732hterm.Terminal.prototype.getVTScrollTop = function() {
1733 if (this.vtScrollTop_ != null)
1734 return this.vtScrollTop_;
1735
1736 return 0;
rginda87b86462011-12-14 13:48:03 -08001737};
rginda8ba33642011-12-14 12:31:31 -08001738
1739/**
1740 * Return the bottom row index according to the VT.
1741 *
1742 * This will return the height of the terminal unless the it has been told to
1743 * restrict scrolling to some higher row. It is used for some VT cursor
1744 * positioning and scrolling commands.
1745 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001746 * @return {integer} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001747 */
1748hterm.Terminal.prototype.getVTScrollBottom = function() {
1749 if (this.vtScrollBottom_ != null)
1750 return this.vtScrollBottom_;
1751
rginda87b86462011-12-14 13:48:03 -08001752 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001753}
1754
1755/**
1756 * Process a '\n' character.
1757 *
1758 * If the cursor is on the final row of the terminal this will append a new
1759 * blank row to the screen and scroll the topmost row into the scrollback
1760 * buffer.
1761 *
1762 * Otherwise, this moves the cursor to column zero of the next row.
1763 */
1764hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001765 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1766 this.screen_.rowsArray.length - 1);
1767
1768 if (this.vtScrollBottom_ != null) {
1769 // A VT Scroll region is active, we never append new rows.
1770 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1771 // We're at the end of the VT Scroll Region, perform a VT scroll.
1772 this.vtScrollUp(1);
1773 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1774 } else if (cursorAtEndOfScreen) {
1775 // We're at the end of the screen, the only thing to do is put the
1776 // cursor to column 0.
1777 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1778 } else {
1779 // Anywhere else, advance the cursor row, and reset the column.
1780 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1781 }
1782 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001783 // We're at the end of the screen. Append a new row to the terminal,
1784 // shifting the top row into the scrollback.
1785 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001786 } else {
rginda87b86462011-12-14 13:48:03 -08001787 // Anywhere else in the screen just moves the cursor.
1788 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001789 }
1790};
1791
1792/**
1793 * Like newLine(), except maintain the cursor column.
1794 */
1795hterm.Terminal.prototype.lineFeed = function() {
1796 var column = this.screen_.cursorPosition.column;
1797 this.newLine();
1798 this.setCursorColumn(column);
1799};
1800
1801/**
rginda87b86462011-12-14 13:48:03 -08001802 * If autoCarriageReturn is set then newLine(), else lineFeed().
1803 */
1804hterm.Terminal.prototype.formFeed = function() {
1805 if (this.options_.autoCarriageReturn) {
1806 this.newLine();
1807 } else {
1808 this.lineFeed();
1809 }
1810};
1811
1812/**
1813 * Move the cursor up one row, possibly inserting a blank line.
1814 *
1815 * The cursor column is not changed.
1816 */
1817hterm.Terminal.prototype.reverseLineFeed = function() {
1818 var scrollTop = this.getVTScrollTop();
1819 var currentRow = this.screen_.cursorPosition.row;
1820
1821 if (currentRow == scrollTop) {
1822 this.insertLines(1);
1823 } else {
1824 this.setAbsoluteCursorRow(currentRow - 1);
1825 }
1826};
1827
1828/**
rginda8ba33642011-12-14 12:31:31 -08001829 * Replace all characters to the left of the current cursor with the space
1830 * character.
1831 *
1832 * TODO(rginda): This should probably *remove* the characters (not just replace
1833 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001834 * position.
rginda8ba33642011-12-14 12:31:31 -08001835 */
1836hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001837 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001838 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001839 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001840 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001841};
1842
1843/**
David Benjamin684a9b72012-05-01 17:19:58 -04001844 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001845 *
1846 * The cursor position is unchanged.
1847 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001848 * If the current background color is not the default background color this
1849 * will insert spaces rather than delete. This is unfortunate because the
1850 * trailing space will affect text selection, but it's difficult to come up
1851 * with a way to style empty space that wouldn't trip up the hterm.Screen
1852 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001853 *
1854 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1855 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1856 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05001857 *
1858 * @param {number} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08001859 */
1860hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001861 if (this.screen_.cursorPosition.overflow)
1862 return;
1863
Robert Ginda7fd57082012-09-25 14:41:47 -07001864 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1865 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001866
1867 if (this.screen_.textAttributes.background ===
1868 this.screen_.textAttributes.DEFAULT_COLOR) {
1869 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08001870 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07001871 this.screen_.cursorPosition.column + count) {
1872 this.screen_.deleteChars(count);
1873 this.clearCursorOverflow();
1874 return;
1875 }
1876 }
1877
rginda87b86462011-12-14 13:48:03 -08001878 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001879 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001880 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001881 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001882};
1883
1884/**
1885 * Erase the current line.
1886 *
1887 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001888 */
1889hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001890 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001891 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001892 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001893 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001894};
1895
1896/**
David Benjamina08d78f2012-05-05 00:28:49 -04001897 * Erase all characters from the start of the screen to the current cursor
1898 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001899 *
1900 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001901 */
1902hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001903 var cursor = this.saveCursor();
1904
1905 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001906
David Benjamina08d78f2012-05-05 00:28:49 -04001907 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001908 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001909 this.screen_.clearCursorRow();
1910 }
1911
rginda87b86462011-12-14 13:48:03 -08001912 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001913 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001914};
1915
1916/**
1917 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001918 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001919 *
1920 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001921 */
1922hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001923 var cursor = this.saveCursor();
1924
1925 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001926
David Benjamina08d78f2012-05-05 00:28:49 -04001927 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001928 for (var i = cursor.row + 1; i <= bottom; i++) {
1929 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001930 this.screen_.clearCursorRow();
1931 }
1932
rginda87b86462011-12-14 13:48:03 -08001933 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001934 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001935};
1936
1937/**
1938 * Fill the terminal with a given character.
1939 *
1940 * This methods does not respect the VT scroll region.
1941 *
1942 * @param {string} ch The character to use for the fill.
1943 */
1944hterm.Terminal.prototype.fill = function(ch) {
1945 var cursor = this.saveCursor();
1946
1947 this.setAbsoluteCursorPosition(0, 0);
1948 for (var row = 0; row < this.screenSize.height; row++) {
1949 for (var col = 0; col < this.screenSize.width; col++) {
1950 this.setAbsoluteCursorPosition(row, col);
1951 this.screen_.overwriteString(ch);
1952 }
1953 }
1954
1955 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001956};
1957
1958/**
rginda9ea433c2012-03-16 11:57:00 -07001959 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001960 *
rginda9ea433c2012-03-16 11:57:00 -07001961 * This does not respect the scroll region.
1962 *
1963 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1964 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001965 */
rginda9ea433c2012-03-16 11:57:00 -07001966hterm.Terminal.prototype.clearHome = function(opt_screen) {
1967 var screen = opt_screen || this.screen_;
1968 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001969
rginda11057d52012-04-25 12:29:56 -07001970 if (bottom == 0) {
1971 // Empty screen, nothing to do.
1972 return;
1973 }
1974
rgindae4d29232012-01-19 10:47:13 -08001975 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001976 screen.setCursorPosition(i, 0);
1977 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001978 }
1979
rginda9ea433c2012-03-16 11:57:00 -07001980 screen.setCursorPosition(0, 0);
1981};
1982
1983/**
1984 * Erase the entire display without changing the cursor position.
1985 *
1986 * The cursor position is unchanged. This does not respect the scroll
1987 * region.
1988 *
1989 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1990 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001991 */
1992hterm.Terminal.prototype.clear = function(opt_screen) {
1993 var screen = opt_screen || this.screen_;
1994 var cursor = screen.cursorPosition.clone();
1995 this.clearHome(screen);
1996 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001997};
1998
1999/**
2000 * VT command to insert lines at the current cursor row.
2001 *
2002 * This respects the current scroll region. Rows pushed off the bottom are
2003 * lost (they won't show up in the scrollback buffer).
2004 *
rginda8ba33642011-12-14 12:31:31 -08002005 * @param {integer} count The number of lines to insert.
2006 */
2007hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002008 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002009
2010 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002011 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002012
Robert Ginda579186b2012-09-26 11:40:04 -07002013 // The moveCount is the number of rows we need to relocate to make room for
2014 // the new row(s). The count is the distance to move them.
2015 var moveCount = bottom - cursorRow - count + 1;
2016 if (moveCount)
2017 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002018
Robert Ginda579186b2012-09-26 11:40:04 -07002019 for (var i = count - 1; i >= 0; i--) {
2020 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002021 this.screen_.clearCursorRow();
2022 }
rginda8ba33642011-12-14 12:31:31 -08002023};
2024
2025/**
2026 * VT command to delete lines at the current cursor row.
2027 *
2028 * New rows are added to the bottom of scroll region to take their place. New
2029 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002030 *
2031 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002032 */
2033hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002034 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002035
rginda87b86462011-12-14 13:48:03 -08002036 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002037 var bottom = this.getVTScrollBottom();
2038
rginda87b86462011-12-14 13:48:03 -08002039 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002040 count = Math.min(count, maxCount);
2041
rginda87b86462011-12-14 13:48:03 -08002042 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002043 if (count != maxCount)
2044 this.moveRows_(top, count, moveStart);
2045
2046 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002047 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002048 this.screen_.clearCursorRow();
2049 }
2050
rginda87b86462011-12-14 13:48:03 -08002051 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002052 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002053};
2054
2055/**
2056 * Inserts the given number of spaces at the current cursor position.
2057 *
rginda87b86462011-12-14 13:48:03 -08002058 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002059 *
2060 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002061 */
2062hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002063 var cursor = this.saveCursor();
2064
rgindacbbd7482012-06-13 15:06:16 -07002065 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08002066 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08002067 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002068
2069 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002070 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002071};
2072
2073/**
2074 * Forward-delete the specified number of characters starting at the cursor
2075 * position.
2076 *
2077 * @param {integer} count The number of characters to delete.
2078 */
2079hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002080 var deleted = this.screen_.deleteChars(count);
2081 if (deleted && !this.screen_.textAttributes.isDefault()) {
2082 var cursor = this.saveCursor();
2083 this.setCursorColumn(this.screenSize.width - deleted);
2084 this.screen_.insertString(lib.f.getWhitespace(deleted));
2085 this.restoreCursor(cursor);
2086 }
2087
David Benjamin54e8bf62012-06-01 22:31:40 -04002088 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002089};
2090
2091/**
2092 * Shift rows in the scroll region upwards by a given number of lines.
2093 *
2094 * New rows are inserted at the bottom of the scroll region to fill the
2095 * vacated rows. The new rows not filled out with the current text attributes.
2096 *
2097 * This function does not affect the scrollback rows at all. Rows shifted
2098 * off the top are lost.
2099 *
rginda87b86462011-12-14 13:48:03 -08002100 * The cursor position is not altered.
2101 *
rginda8ba33642011-12-14 12:31:31 -08002102 * @param {integer} count The number of rows to scroll.
2103 */
2104hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002105 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002106
rginda87b86462011-12-14 13:48:03 -08002107 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002108 this.deleteLines(count);
2109
rginda87b86462011-12-14 13:48:03 -08002110 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002111};
2112
2113/**
2114 * Shift rows below the cursor down by a given number of lines.
2115 *
2116 * This function respects the current scroll region.
2117 *
2118 * New rows are inserted at the top of the scroll region to fill the
2119 * vacated rows. The new rows not filled out with the current text attributes.
2120 *
2121 * This function does not affect the scrollback rows at all. Rows shifted
2122 * off the bottom are lost.
2123 *
2124 * @param {integer} count The number of rows to scroll.
2125 */
2126hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08002127 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002128
rginda87b86462011-12-14 13:48:03 -08002129 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08002130 this.insertLines(opt_count);
2131
rginda87b86462011-12-14 13:48:03 -08002132 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002133};
2134
rginda87b86462011-12-14 13:48:03 -08002135
rginda8ba33642011-12-14 12:31:31 -08002136/**
2137 * Set the cursor position.
2138 *
2139 * The cursor row is relative to the scroll region if the terminal has
2140 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2141 *
2142 * @param {integer} row The new zero-based cursor row.
2143 * @param {integer} row The new zero-based cursor column.
2144 */
2145hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2146 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002147 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002148 } else {
rginda87b86462011-12-14 13:48:03 -08002149 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002150 }
rginda87b86462011-12-14 13:48:03 -08002151};
rginda8ba33642011-12-14 12:31:31 -08002152
Evan Jones2600d4f2016-12-06 09:29:36 -05002153/**
2154 * Move the cursor relative to its current position.
2155 *
2156 * @param {number} row
2157 * @param {number} column
2158 */
rginda87b86462011-12-14 13:48:03 -08002159hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2160 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002161 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2162 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002163 this.screen_.setCursorPosition(row, column);
2164};
2165
Evan Jones2600d4f2016-12-06 09:29:36 -05002166/**
2167 * Move the cursor to the specified position.
2168 *
2169 * @param {number} row
2170 * @param {number} column
2171 */
rginda87b86462011-12-14 13:48:03 -08002172hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002173 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2174 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002175 this.screen_.setCursorPosition(row, column);
2176};
2177
2178/**
2179 * Set the cursor column.
2180 *
2181 * @param {integer} column The new zero-based cursor column.
2182 */
2183hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002184 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002185};
2186
2187/**
2188 * Return the cursor column.
2189 *
2190 * @return {integer} The zero-based cursor column.
2191 */
2192hterm.Terminal.prototype.getCursorColumn = function() {
2193 return this.screen_.cursorPosition.column;
2194};
2195
2196/**
2197 * Set the cursor row.
2198 *
2199 * The cursor row is relative to the scroll region if the terminal has
2200 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2201 *
2202 * @param {integer} row The new cursor row.
2203 */
rginda87b86462011-12-14 13:48:03 -08002204hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2205 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002206};
2207
2208/**
2209 * Return the cursor row.
2210 *
2211 * @return {integer} The zero-based cursor row.
2212 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002213hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002214 return this.screen_.cursorPosition.row;
2215};
2216
2217/**
2218 * Request that the ScrollPort redraw itself soon.
2219 *
2220 * The redraw will happen asynchronously, soon after the call stack winds down.
2221 * Multiple calls will be coalesced into a single redraw.
2222 */
2223hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002224 if (this.timeouts_.redraw)
2225 return;
rginda8ba33642011-12-14 12:31:31 -08002226
2227 var self = this;
rginda87b86462011-12-14 13:48:03 -08002228 this.timeouts_.redraw = setTimeout(function() {
2229 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002230 self.scrollPort_.redraw_();
2231 }, 0);
2232};
2233
2234/**
2235 * Request that the ScrollPort be scrolled to the bottom.
2236 *
2237 * The scroll will happen asynchronously, soon after the call stack winds down.
2238 * Multiple calls will be coalesced into a single scroll.
2239 *
2240 * This affects the scrollbar position of the ScrollPort, and has nothing to
2241 * do with the VT scroll commands.
2242 */
2243hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2244 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002245 return;
rginda8ba33642011-12-14 12:31:31 -08002246
2247 var self = this;
2248 this.timeouts_.scrollDown = setTimeout(function() {
2249 delete self.timeouts_.scrollDown;
2250 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2251 }, 10);
2252};
2253
2254/**
2255 * Move the cursor up a specified number of rows.
2256 *
2257 * @param {integer} count The number of rows to move the cursor.
2258 */
2259hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002260 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002261};
2262
2263/**
2264 * Move the cursor down a specified number of rows.
2265 *
2266 * @param {integer} count The number of rows to move the cursor.
2267 */
2268hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002269 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002270 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2271 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2272 this.screenSize.height - 1);
2273
rgindacbbd7482012-06-13 15:06:16 -07002274 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002275 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002276 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002277};
2278
2279/**
2280 * Move the cursor left a specified number of columns.
2281 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002282 * If reverse wraparound mode is enabled and the previous row wrapped into
2283 * the current row then we back up through the wraparound as well.
2284 *
rginda8ba33642011-12-14 12:31:31 -08002285 * @param {integer} count The number of columns to move the cursor.
2286 */
2287hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002288 count = count || 1;
2289
2290 if (count < 1)
2291 return;
2292
2293 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002294 if (this.options_.reverseWraparound) {
2295 if (this.screen_.cursorPosition.overflow) {
2296 // If this cursor is in the right margin, consume one count to get it
2297 // back to the last column. This only applies when we're in reverse
2298 // wraparound mode.
2299 count--;
2300 this.clearCursorOverflow();
2301
2302 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002303 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002304 }
2305
Robert Gindabfb32622014-07-17 13:20:27 -07002306 var newRow = this.screen_.cursorPosition.row;
2307 var newColumn = currentColumn - count;
2308 if (newColumn < 0) {
2309 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2310 if (newRow < 0) {
2311 // xterm also wraps from row 0 to the last row.
2312 newRow = this.screenSize.height + newRow % this.screenSize.height;
2313 }
2314 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2315 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002316
Robert Gindabfb32622014-07-17 13:20:27 -07002317 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2318
2319 } else {
2320 var newColumn = Math.max(currentColumn - count, 0);
2321 this.setCursorColumn(newColumn);
2322 }
rginda8ba33642011-12-14 12:31:31 -08002323};
2324
2325/**
2326 * Move the cursor right a specified number of columns.
2327 *
2328 * @param {integer} count The number of columns to move the cursor.
2329 */
2330hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002331 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002332
2333 if (count < 1)
2334 return;
2335
rgindacbbd7482012-06-13 15:06:16 -07002336 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002337 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002338 this.setCursorColumn(column);
2339};
2340
2341/**
2342 * Reverse the foreground and background colors of the terminal.
2343 *
2344 * This only affects text that was drawn with no attributes.
2345 *
2346 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2347 * been drawn with attributes that happen to coincide with the default
2348 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002349 *
2350 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002351 */
2352hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002353 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002354 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002355 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2356 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002357 } else {
rginda9f5222b2012-03-05 11:53:28 -08002358 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2359 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002360 }
2361};
2362
2363/**
rginda87b86462011-12-14 13:48:03 -08002364 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002365 *
2366 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002367 */
2368hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002369 this.cursorNode_.style.backgroundColor =
2370 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002371
2372 var self = this;
2373 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08002374 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08002375 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002376
Michael Kelly485ecd12014-06-09 11:41:56 -04002377 // bellSquelchTimeout_ affects both audio and notification bells.
2378 if (this.bellSquelchTimeout_)
2379 return;
2380
Robert Ginda92e18102013-03-14 13:56:37 -07002381 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002382 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002383 this.bellSequelchTimeout_ = setTimeout(function() {
2384 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002385 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002386 } else {
2387 delete this.bellSquelchTimeout_;
2388 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002389
2390 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
2391 var n = new Notification(
2392 lib.f.replaceVars(hterm.desktopNotificationTitle,
Robert Ginda348dc2b2014-06-24 14:42:23 -07002393 {'title': window.document.title || 'hterm'}));
Michael Kelly485ecd12014-06-09 11:41:56 -04002394 this.bellNotificationList_.push(n);
2395 // TODO: Should we try to raise the window here?
2396 n.onclick = function() { self.closeBellNotifications_(); };
2397 }
rginda87b86462011-12-14 13:48:03 -08002398};
2399
2400/**
rginda8ba33642011-12-14 12:31:31 -08002401 * Set the origin mode bit.
2402 *
2403 * If origin mode is on, certain VT cursor and scrolling commands measure their
2404 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2405 * to the top of the addressable screen.
2406 *
2407 * Defaults to off.
2408 *
2409 * @param {boolean} state True to set origin mode, false to unset.
2410 */
2411hterm.Terminal.prototype.setOriginMode = function(state) {
2412 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002413 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002414};
2415
2416/**
2417 * Set the insert mode bit.
2418 *
2419 * If insert mode is on, existing text beyond the cursor position will be
2420 * shifted right to make room for new text. Otherwise, new text overwrites
2421 * any existing text.
2422 *
2423 * Defaults to off.
2424 *
2425 * @param {boolean} state True to set insert mode, false to unset.
2426 */
2427hterm.Terminal.prototype.setInsertMode = function(state) {
2428 this.options_.insertMode = state;
2429};
2430
2431/**
rginda87b86462011-12-14 13:48:03 -08002432 * Set the auto carriage return bit.
2433 *
2434 * If auto carriage return is on then a formfeed character is interpreted
2435 * as a newline, otherwise it's the same as a linefeed. The difference boils
2436 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002437 *
2438 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002439 */
2440hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2441 this.options_.autoCarriageReturn = state;
2442};
2443
2444/**
rginda8ba33642011-12-14 12:31:31 -08002445 * Set the wraparound mode bit.
2446 *
2447 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2448 * to the start of the following row. Otherwise, the cursor is clamped to the
2449 * end of the screen and attempts to write past it are ignored.
2450 *
2451 * Defaults to on.
2452 *
2453 * @param {boolean} state True to set wraparound mode, false to unset.
2454 */
2455hterm.Terminal.prototype.setWraparound = function(state) {
2456 this.options_.wraparound = state;
2457};
2458
2459/**
2460 * Set the reverse-wraparound mode bit.
2461 *
2462 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2463 * to the end of the previous row. Otherwise, the cursor is clamped to column
2464 * 0.
2465 *
2466 * Defaults to off.
2467 *
2468 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2469 */
2470hterm.Terminal.prototype.setReverseWraparound = function(state) {
2471 this.options_.reverseWraparound = state;
2472};
2473
2474/**
2475 * Selects between the primary and alternate screens.
2476 *
2477 * If alternate mode is on, the alternate screen is active. Otherwise the
2478 * primary screen is active.
2479 *
2480 * Swapping screens has no effect on the scrollback buffer.
2481 *
2482 * Each screen maintains its own cursor position.
2483 *
2484 * Defaults to off.
2485 *
2486 * @param {boolean} state True to set alternate mode, false to unset.
2487 */
2488hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002489 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002490 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2491
rginda35c456b2012-02-09 17:29:05 -08002492 if (this.screen_.rowsArray.length &&
2493 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2494 // If the screen changed sizes while we were away, our rowIndexes may
2495 // be incorrect.
2496 var offset = this.scrollbackRows_.length;
2497 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002498 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002499 ary[i].rowIndex = offset + i;
2500 }
2501 }
rginda8ba33642011-12-14 12:31:31 -08002502
rginda35c456b2012-02-09 17:29:05 -08002503 this.realizeWidth_(this.screenSize.width);
2504 this.realizeHeight_(this.screenSize.height);
2505 this.scrollPort_.syncScrollHeight();
2506 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002507
rginda6d397402012-01-17 10:58:29 -08002508 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002509 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002510};
2511
2512/**
2513 * Set the cursor-blink mode bit.
2514 *
2515 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2516 * a visible cursor does not blink.
2517 *
2518 * You should make sure to turn blinking off if you're going to dispose of a
2519 * terminal, otherwise you'll leak a timeout.
2520 *
2521 * Defaults to on.
2522 *
2523 * @param {boolean} state True to set cursor-blink mode, false to unset.
2524 */
2525hterm.Terminal.prototype.setCursorBlink = function(state) {
2526 this.options_.cursorBlink = state;
2527
2528 if (!state && this.timeouts_.cursorBlink) {
2529 clearTimeout(this.timeouts_.cursorBlink);
2530 delete this.timeouts_.cursorBlink;
2531 }
2532
2533 if (this.options_.cursorVisible)
2534 this.setCursorVisible(true);
2535};
2536
2537/**
2538 * Set the cursor-visible mode bit.
2539 *
2540 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2541 *
2542 * Defaults to on.
2543 *
2544 * @param {boolean} state True to set cursor-visible mode, false to unset.
2545 */
2546hterm.Terminal.prototype.setCursorVisible = function(state) {
2547 this.options_.cursorVisible = state;
2548
2549 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002550 if (this.timeouts_.cursorBlink) {
2551 clearTimeout(this.timeouts_.cursorBlink);
2552 delete this.timeouts_.cursorBlink;
2553 }
rginda87b86462011-12-14 13:48:03 -08002554 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002555 return;
2556 }
2557
rginda87b86462011-12-14 13:48:03 -08002558 this.syncCursorPosition_();
2559
2560 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002561
2562 if (this.options_.cursorBlink) {
2563 if (this.timeouts_.cursorBlink)
2564 return;
2565
Robert Gindaea2183e2014-07-17 09:51:51 -07002566 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002567 } else {
2568 if (this.timeouts_.cursorBlink) {
2569 clearTimeout(this.timeouts_.cursorBlink);
2570 delete this.timeouts_.cursorBlink;
2571 }
2572 }
2573};
2574
2575/**
rginda87b86462011-12-14 13:48:03 -08002576 * Synchronizes the visible cursor and document selection with the current
2577 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002578 */
2579hterm.Terminal.prototype.syncCursorPosition_ = function() {
2580 var topRowIndex = this.scrollPort_.getTopRowIndex();
2581 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2582 var cursorRowIndex = this.scrollbackRows_.length +
2583 this.screen_.cursorPosition.row;
2584
2585 if (cursorRowIndex > bottomRowIndex) {
2586 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002587 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002588 return;
2589 }
2590
Robert Gindab837c052014-08-11 11:17:51 -07002591 if (this.options_.cursorVisible &&
2592 this.cursorNode_.style.display == 'none') {
2593 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2594 this.cursorNode_.style.display = '';
2595 }
2596
2597
rginda8ba33642011-12-14 12:31:31 -08002598 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002599 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2600 'px';
2601 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2602 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002603
2604 this.cursorNode_.setAttribute('title',
2605 '(' + this.screen_.cursorPosition.row +
2606 ', ' + this.screen_.cursorPosition.column +
2607 ')');
2608
2609 // Update the caret for a11y purposes.
2610 var selection = this.document_.getSelection();
2611 if (selection && selection.isCollapsed)
2612 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002613};
2614
Robert Gindafb1be6a2013-12-11 11:56:22 -08002615/**
2616 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2617 * and character cell dimensions.
2618 */
Robert Ginda830583c2013-08-07 13:20:46 -07002619hterm.Terminal.prototype.restyleCursor_ = function() {
2620 var shape = this.cursorShape_;
2621
2622 if (this.cursorNode_.getAttribute('focus') == 'false') {
2623 // Always show a block cursor when unfocused.
2624 shape = hterm.Terminal.cursorShape.BLOCK;
2625 }
2626
2627 var style = this.cursorNode_.style;
2628
2629 switch (shape) {
2630 case hterm.Terminal.cursorShape.BEAM:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002631 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002632 style.backgroundColor = 'transparent';
2633 style.borderBottomStyle = null;
2634 style.borderLeftStyle = 'solid';
2635 break;
2636
2637 case hterm.Terminal.cursorShape.UNDERLINE:
2638 style.height = this.scrollPort_.characterSize.baseline + 'px';
2639 style.backgroundColor = 'transparent';
2640 style.borderBottomStyle = 'solid';
2641 // correct the size to put it exactly at the baseline
2642 style.borderLeftStyle = null;
2643 break;
2644
2645 default:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002646 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002647 style.backgroundColor = this.cursorColor_;
2648 style.borderBottomStyle = null;
2649 style.borderLeftStyle = null;
2650 break;
2651 }
2652};
2653
rginda8ba33642011-12-14 12:31:31 -08002654/**
2655 * Synchronizes the visible cursor with the current cursor coordinates.
2656 *
2657 * The sync will happen asynchronously, soon after the call stack winds down.
2658 * Multiple calls will be coalesced into a single sync.
2659 */
2660hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2661 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002662 return;
rginda8ba33642011-12-14 12:31:31 -08002663
2664 var self = this;
2665 this.timeouts_.syncCursor = setTimeout(function() {
2666 self.syncCursorPosition_();
2667 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002668 }, 0);
2669};
2670
rgindacc2996c2012-02-24 14:59:31 -08002671/**
rgindaf522ce02012-04-17 17:49:17 -07002672 * Show or hide the zoom warning.
2673 *
2674 * The zoom warning is a message warning the user that their browser zoom must
2675 * be set to 100% in order for hterm to function properly.
2676 *
2677 * @param {boolean} state True to show the message, false to hide it.
2678 */
2679hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2680 if (!this.zoomWarningNode_) {
2681 if (!state)
2682 return;
2683
2684 this.zoomWarningNode_ = this.document_.createElement('div');
2685 this.zoomWarningNode_.style.cssText = (
2686 'color: black;' +
2687 'background-color: #ff2222;' +
2688 'font-size: large;' +
2689 'border-radius: 8px;' +
2690 'opacity: 0.75;' +
2691 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2692 'top: 0.5em;' +
2693 'right: 1.2em;' +
2694 'position: absolute;' +
2695 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002696 '-webkit-user-select: none;' +
2697 '-moz-text-size-adjust: none;' +
2698 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002699
2700 this.zoomWarningNode_.addEventListener('click', function(e) {
2701 this.parentNode.removeChild(this);
2702 });
rgindaf522ce02012-04-17 17:49:17 -07002703 }
2704
Robert Gindab4839c22013-02-28 16:52:10 -08002705 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2706 hterm.zoomWarningMessage,
2707 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2708
rgindaf522ce02012-04-17 17:49:17 -07002709 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2710
2711 if (state) {
2712 if (!this.zoomWarningNode_.parentNode)
2713 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2714 } else if (this.zoomWarningNode_.parentNode) {
2715 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2716 }
2717};
2718
2719/**
rgindacc2996c2012-02-24 14:59:31 -08002720 * Show the terminal overlay for a given amount of time.
2721 *
2722 * The terminal overlay appears in inverse video in a large font, centered
2723 * over the terminal. You should probably keep the overlay message brief,
2724 * since it's in a large font and you probably aren't going to check the size
2725 * of the terminal first.
2726 *
2727 * @param {string} msg The text (not HTML) message to display in the overlay.
2728 * @param {number} opt_timeout The amount of time to wait before fading out
2729 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2730 * stay up forever (or until the next overlay).
2731 */
2732hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002733 if (!this.overlayNode_) {
2734 if (!this.div_)
2735 return;
2736
2737 this.overlayNode_ = this.document_.createElement('div');
2738 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002739 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002740 'font-size: xx-large;' +
2741 'opacity: 0.75;' +
2742 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2743 'position: absolute;' +
2744 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002745 '-webkit-transition: opacity 180ms ease-in;' +
2746 '-moz-user-select: none;' +
2747 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002748
2749 this.overlayNode_.addEventListener('mousedown', function(e) {
2750 e.preventDefault();
2751 e.stopPropagation();
2752 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002753 }
2754
rginda9f5222b2012-03-05 11:53:28 -08002755 this.overlayNode_.style.color = this.prefs_.get('background-color');
2756 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2757 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2758
rgindaf0090c92012-02-10 14:58:52 -08002759 this.overlayNode_.textContent = msg;
2760 this.overlayNode_.style.opacity = '0.75';
2761
2762 if (!this.overlayNode_.parentNode)
2763 this.div_.appendChild(this.overlayNode_);
2764
Robert Ginda97769282013-02-01 15:30:30 -08002765 var divSize = hterm.getClientSize(this.div_);
2766 var overlaySize = hterm.getClientSize(this.overlayNode_);
2767
Robert Ginda8a59f762014-07-23 11:29:55 -07002768 this.overlayNode_.style.top =
2769 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08002770 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07002771 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08002772
2773 var self = this;
2774
2775 if (this.overlayTimeout_)
2776 clearTimeout(this.overlayTimeout_);
2777
rgindacc2996c2012-02-24 14:59:31 -08002778 if (opt_timeout === null)
2779 return;
2780
rgindaf0090c92012-02-10 14:58:52 -08002781 this.overlayTimeout_ = setTimeout(function() {
2782 self.overlayNode_.style.opacity = '0';
Robert Ginda70926e42013-11-25 14:56:36 -08002783 self.overlayTimeout_ = setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002784 if (self.overlayNode_.parentNode)
2785 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002786 self.overlayTimeout_ = null;
2787 self.overlayNode_.style.opacity = '0.75';
2788 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002789 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002790};
2791
rginda4bba5e12012-06-20 16:15:30 -07002792/**
2793 * Paste from the system clipboard to the terminal.
2794 */
2795hterm.Terminal.prototype.paste = function() {
2796 hterm.pasteFromClipboard(this.document_);
2797};
2798
2799/**
2800 * Copy a string to the system clipboard.
2801 *
2802 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05002803 *
2804 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07002805 */
2806hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002807 if (this.prefs_.get('enable-clipboard-notice'))
2808 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2809
rgindaa09e7332012-08-17 12:49:51 -07002810 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002811 copySource.textContent = str;
2812 copySource.style.cssText = (
2813 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002814 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07002815 'position: absolute;' +
2816 'top: -99px');
2817
2818 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002819
rginda4bba5e12012-06-20 16:15:30 -07002820 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002821 var anchorNode = selection.anchorNode;
2822 var anchorOffset = selection.anchorOffset;
2823 var focusNode = selection.focusNode;
2824 var focusOffset = selection.focusOffset;
2825
rginda4bba5e12012-06-20 16:15:30 -07002826 selection.selectAllChildren(copySource);
2827
rgindaa09e7332012-08-17 12:49:51 -07002828 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002829
Rob Spies56953412014-04-28 14:09:47 -07002830 // IE doesn't support selection.extend. This means that the selection
2831 // won't return on IE.
Robert Ginda6f4f0aa2014-07-28 10:21:39 -07002832 if (selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07002833 selection.collapse(anchorNode, anchorOffset);
2834 selection.extend(focusNode, focusOffset);
2835 }
rgindafaa74742012-08-21 13:34:03 -07002836
rginda4bba5e12012-06-20 16:15:30 -07002837 copySource.parentNode.removeChild(copySource);
2838};
2839
Evan Jones2600d4f2016-12-06 09:29:36 -05002840/**
2841 * Returns the selected text, or null if no text is selected.
2842 *
2843 * @return {string|null}
2844 */
rgindaa09e7332012-08-17 12:49:51 -07002845hterm.Terminal.prototype.getSelectionText = function() {
2846 var selection = this.scrollPort_.selection;
2847 selection.sync();
2848
2849 if (selection.isCollapsed)
2850 return null;
2851
2852
2853 // Start offset measures from the beginning of the line.
2854 var startOffset = selection.startOffset;
2855 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002856
Robert Gindafdbb3f22012-09-06 20:23:06 -07002857 if (node.nodeName != 'X-ROW') {
2858 // If the selection doesn't start on an x-row node, then it must be
2859 // somewhere inside the x-row. Add any characters from previous siblings
2860 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002861
2862 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2863 // If node is the text node in a styled span, move up to the span node.
2864 node = node.parentNode;
2865 }
2866
Robert Gindafdbb3f22012-09-06 20:23:06 -07002867 while (node.previousSibling) {
2868 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002869 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002870 }
rgindaa09e7332012-08-17 12:49:51 -07002871 }
2872
2873 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002874 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
2875 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05002876 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002877
Robert Gindafdbb3f22012-09-06 20:23:06 -07002878 if (node.nodeName != 'X-ROW') {
2879 // If the selection doesn't end on an x-row node, then it must be
2880 // somewhere inside the x-row. Add any characters from following siblings
2881 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002882
2883 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2884 // If node is the text node in a styled span, move up to the span node.
2885 node = node.parentNode;
2886 }
2887
Robert Gindafdbb3f22012-09-06 20:23:06 -07002888 while (node.nextSibling) {
2889 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002890 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002891 }
rgindaa09e7332012-08-17 12:49:51 -07002892 }
2893
2894 var rv = this.getRowsText(selection.startRow.rowIndex,
2895 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002896 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002897};
2898
rginda4bba5e12012-06-20 16:15:30 -07002899/**
2900 * Copy the current selection to the system clipboard, then clear it after a
2901 * short delay.
2902 */
2903hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002904 var text = this.getSelectionText();
2905 if (text != null)
2906 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002907};
2908
rgindaf0090c92012-02-10 14:58:52 -08002909hterm.Terminal.prototype.overlaySize = function() {
2910 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2911};
2912
rginda87b86462011-12-14 13:48:03 -08002913/**
2914 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2915 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07002916 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08002917 */
2918hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002919 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002920 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2921
Robert Ginda8cb7d902013-06-20 14:37:18 -07002922 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08002923};
2924
2925/**
Mike Frysinger70b94692017-01-26 18:57:50 -10002926 * Launches url in a new tab.
2927 *
2928 * @param {string} url URL to launch in a new tab.
2929 */
2930hterm.Terminal.prototype.openUrl = function(url) {
2931 var win = window.open(url, '_blank');
2932 win.focus();
2933}
2934
2935/**
2936 * Open the selected url.
2937 */
2938hterm.Terminal.prototype.openSelectedUrl_ = function() {
2939 var str = this.getSelectionText();
2940
2941 // If there is no selection, try and expand wherever they clicked.
2942 if (str == null) {
2943 this.screen_.expandSelection(this.document_.getSelection());
2944 str = this.getSelectionText();
2945 }
2946
2947 // Make sure URL is valid before opening.
2948 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
2949 return;
2950 // If the URL isn't anchored, it'll open relative to the extension.
2951 // We have no way of knowing the correct schema, so assume http.
2952 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0)
2953 str = 'http://' + str;
2954
2955 this.openUrl(str);
2956}
2957
2958
2959/**
rgindad5613292012-06-19 15:40:37 -07002960 * Add the terminalRow and terminalColumn properties to mouse events and
2961 * then forward on to onMouse().
2962 *
2963 * The terminalRow and terminalColumn properties contain the (row, column)
2964 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05002965 *
2966 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07002967 */
2968hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002969 if (e.processedByTerminalHandler_) {
2970 // We register our event handlers on the document, as well as the cursor
2971 // and the scroll blocker. Mouse events that occur on the cursor or
2972 // scroll blocker will also appear on the document, but we don't want to
2973 // process them twice.
2974 //
2975 // We can't just prevent bubbling because that has other side effects, so
2976 // we decorate the event object with this property instead.
2977 return;
2978 }
2979
Robert Ginda6aec7eb2015-06-16 10:31:30 -07002980 var reportMouseEvents = (!this.defeatMouseReports_ &&
2981 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
2982
rgindafaa74742012-08-21 13:34:03 -07002983 e.processedByTerminalHandler_ = true;
2984
Robert Gindaeda48db2014-07-17 09:25:30 -07002985 // One based row/column stored on the mouse event.
2986 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2987 this.scrollPort_.characterSize.height) + 1;
2988 e.terminalColumn = parseInt(e.clientX /
2989 this.scrollPort_.characterSize.width) + 1;
2990
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002991 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
2992 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07002993 return;
2994 }
2995
Robert Ginda6aec7eb2015-06-16 10:31:30 -07002996 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07002997 // If the cursor is visible and we're not sending mouse events to the
2998 // host app, then we want to hide the terminal cursor when the mouse
2999 // cursor is over top. This keeps the terminal cursor from interfering
3000 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003001 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3002 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3003 this.cursorNode_.style.display = 'none';
3004 } else if (this.cursorNode_.style.display == 'none') {
3005 this.cursorNode_.style.display = '';
3006 }
3007 }
rgindad5613292012-06-19 15:40:37 -07003008
Robert Ginda928cf632014-03-05 15:07:41 -08003009 if (e.type == 'mousedown') {
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003010 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003011 // If VT mouse reporting is disabled, or has been defeated with
3012 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003013 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003014 this.setSelectionEnabled(true);
3015 } else {
3016 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003017 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003018 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003019 this.setSelectionEnabled(false);
3020 e.preventDefault();
3021 }
3022 }
3023
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003024 if (!reportMouseEvents) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003025 if (e.type == 'dblclick' && this.copyOnSelect) {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003026 this.screen_.expandSelection(this.document_.getSelection());
Robert Ginda15ed4902016-07-12 10:43:22 -07003027 this.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07003028 }
3029
Mike Frysinger70b94692017-01-26 18:57:50 -10003030 if (e.type == 'click' && !e.shiftKey && e.ctrlKey) {
3031 // Debounce this event with the dblclick event. If you try to doubleclick
3032 // a URL to open it, Chrome will fire click then dblclick, but we won't
3033 // have expanded the selection text at the first click event.
3034 clearTimeout(this.timeouts_.openUrl);
3035 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3036 500);
3037 return;
3038 }
3039
Mike Frysinger847577f2017-05-23 23:25:57 -04003040 if (e.type == 'mousedown') {
3041 if ((this.mouseRightClickPaste && e.button == 2 /* right button */) ||
Mike Frysinger2edd3612017-05-24 00:54:39 -04003042 e.button == this.mousePasteButton) {
Mike Frysinger847577f2017-05-23 23:25:57 -04003043 this.paste();
3044 }
3045 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003046
Mike Frysinger2edd3612017-05-24 00:54:39 -04003047 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003048 !this.document_.getSelection().isCollapsed) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003049 this.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003050 }
3051
3052 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3053 this.scrollBlockerNode_.engaged) {
3054 // Disengage the scroll-blocker after one of these events.
3055 this.scrollBlockerNode_.engaged = false;
3056 this.scrollBlockerNode_.style.top = '-99px';
3057 }
3058
Mike Frysingerc3204502017-06-22 14:09:01 -07003059 if (this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003060 if (e.type == 'wheel') {
3061 var delta = this.scrollPort_.scrollWheelDelta(e);
3062 var lines = lib.f.smartFloorDivide(
3063 Math.abs(delta), this.scrollPort_.characterSize.height);
3064
3065 var data = '\x1bO' + (delta < 0 ? 'B' : 'A');
3066 this.io.sendString(data.repeat(lines));
3067
3068 e.preventDefault();
3069 }
3070 }
Robert Ginda928cf632014-03-05 15:07:41 -08003071 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003072 if (!this.scrollBlockerNode_.engaged) {
3073 if (e.type == 'mousedown') {
3074 // Move the scroll-blocker into place if we want to keep the scrollport
3075 // from scrolling.
3076 this.scrollBlockerNode_.engaged = true;
3077 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3078 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3079 } else if (e.type == 'mousemove') {
3080 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3081 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003082 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003083 e.preventDefault();
3084 }
3085 }
Robert Ginda928cf632014-03-05 15:07:41 -08003086
3087 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003088 }
3089
Robert Ginda928cf632014-03-05 15:07:41 -08003090 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3091 // Restore this on mouseup in case it was temporarily defeated with a
3092 // alt-mousedown. Only do this when the selection is empty so that
3093 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003094 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003095 }
rgindad5613292012-06-19 15:40:37 -07003096};
3097
3098/**
3099 * Clients should override this if they care to know about mouse events.
3100 *
3101 * The event parameter will be a normal DOM mouse click event with additional
3102 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003103 *
3104 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003105 */
3106hterm.Terminal.prototype.onMouse = function(e) { };
3107
3108/**
rginda8e92a692012-05-20 19:37:20 -07003109 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003110 *
3111 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003112 */
Rob Spies06533ba2014-04-24 11:20:37 -07003113hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3114 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003115 this.restyleCursor_();
Michael Kelly485ecd12014-06-09 11:41:56 -04003116 if (focused === true)
3117 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003118};
3119
3120/**
rginda8ba33642011-12-14 12:31:31 -08003121 * React when the ScrollPort is scrolled.
3122 */
3123hterm.Terminal.prototype.onScroll_ = function() {
3124 this.scheduleSyncCursorPosition_();
3125};
3126
3127/**
rginda9846e2f2012-01-27 13:53:33 -08003128 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003129 *
3130 * @param {Event} e The DOM paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003131 */
3132hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07003133 var data = e.text.replace(/\n/mg, '\r');
Connor Hegartyf525ceb2014-09-03 13:36:03 -07003134 data = this.keyboard.encode(data);
Robert Gindaa063b202014-07-21 11:08:25 -07003135 if (this.options_.bracketedPaste)
3136 data = '\x1b[200~' + data + '\x1b[201~';
3137
3138 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003139};
3140
3141/**
rgindaa09e7332012-08-17 12:49:51 -07003142 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003143 *
3144 * @param {Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003145 */
3146hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003147 if (!this.useDefaultWindowCopy) {
3148 e.preventDefault();
3149 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3150 }
rgindaa09e7332012-08-17 12:49:51 -07003151};
3152
3153/**
rginda8ba33642011-12-14 12:31:31 -08003154 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003155 *
3156 * Note: This function should not directly contain code that alters the internal
3157 * state of the terminal. That kind of code belongs in realizeWidth or
3158 * realizeHeight, so that it can be executed synchronously in the case of a
3159 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003160 */
3161hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003162 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003163 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003164 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003165 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003166
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003167 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003168 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003169 // gets removed from the document or during the initial load, and we can't
3170 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003171 // This can also happen if called before the scrollPort calculates the
3172 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003173 return;
3174 }
3175
rgindaa8ba17d2012-08-15 14:41:10 -07003176 var isNewSize = (columnCount != this.screenSize.width ||
3177 rowCount != this.screenSize.height);
3178
3179 // We do this even if the size didn't change, just to be sure everything is
3180 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003181 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003182 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003183
3184 if (isNewSize)
3185 this.overlaySize();
3186
Robert Gindafb1be6a2013-12-11 11:56:22 -08003187 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003188 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08003189};
3190
3191/**
3192 * Service the cursor blink timeout.
3193 */
3194hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003195 if (!this.options_.cursorBlink) {
3196 delete this.timeouts_.cursorBlink;
3197 return;
3198 }
3199
Robert Ginda830583c2013-08-07 13:20:46 -07003200 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3201 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003202 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003203 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3204 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003205 } else {
rginda87b86462011-12-14 13:48:03 -08003206 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003207 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3208 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003209 }
3210};
David Reveman8f552492012-03-28 12:18:41 -04003211
3212/**
3213 * Set the scrollbar-visible mode bit.
3214 *
3215 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3216 * Otherwise it will not.
3217 *
3218 * Defaults to on.
3219 *
3220 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3221 */
3222hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3223 this.scrollPort_.setScrollbarVisible(state);
3224};
Michael Kelly485ecd12014-06-09 11:41:56 -04003225
3226/**
Rob Spies49039e52014-12-17 13:40:04 -08003227 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003228 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003229 *
3230 * Defaults to 1.
3231 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003232 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003233 */
3234hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3235 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3236};
3237
3238/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003239 * Close all web notifications created by terminal bells.
3240 */
3241hterm.Terminal.prototype.closeBellNotifications_ = function() {
3242 this.bellNotificationList_.forEach(function(n) {
3243 n.close();
3244 });
3245 this.bellNotificationList_.length = 0;
3246};