blob: 2119290e58a683d680c3684277f3883c7e7858be [file] [log] [blame]
rginda87b86462011-12-14 13:48:03 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rginda8ba33642011-12-14 12:31:31 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rgindacbbd7482012-06-13 15:06:16 -07005'use strict';
6
Masaya Suzuki273aa982014-05-31 07:25:55 +09007lib.rtdep('lib.colors', 'lib.PreferenceManager', 'lib.resource', 'lib.wc',
Rob Spiesf4e90e82015-01-28 12:10:13 -08008 'lib.f', 'hterm.Keyboard', 'hterm.Options', 'hterm.PreferenceManager',
Ricky Liang48f05cb2013-12-31 23:35:29 +08009 'hterm.Screen', 'hterm.ScrollPort', 'hterm.Size',
10 'hterm.TextAttributes', 'hterm.VT');
rgindacbbd7482012-06-13 15:06:16 -070011
rginda8ba33642011-12-14 12:31:31 -080012/**
13 * Constructor for the Terminal class.
14 *
15 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
16 * classes to provide the complete terminal functionality.
17 *
18 * There are a number of lower-level Terminal methods that can be called
19 * directly to manipulate the cursor, text, scroll region, and other terminal
20 * attributes. However, the primary method is interpret(), which parses VT
21 * escape sequences and invokes the appropriate Terminal methods.
22 *
23 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
24 *
25 * TODO(rginda): Eventually we're going to need to support characters which are
26 * displayed twice as wide as standard latin characters. This is to support
27 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080028 *
Robert Ginda57f03b42012-09-13 11:02:48 -070029 * @param {string} opt_profileId Optional preference profile name. If not
rginda9f5222b2012-03-05 11:53:28 -080030 * provided, defaults to 'default'.
rginda8ba33642011-12-14 12:31:31 -080031 */
Robert Ginda57f03b42012-09-13 11:02:48 -070032hterm.Terminal = function(opt_profileId) {
33 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080034
rginda8ba33642011-12-14 12:31:31 -080035 // Two screen instances.
36 this.primaryScreen_ = new hterm.Screen();
37 this.alternateScreen_ = new hterm.Screen();
38
39 // The "current" screen.
40 this.screen_ = this.primaryScreen_;
41
rginda8ba33642011-12-14 12:31:31 -080042 // The local notion of the screen size. ScreenBuffers also have a size which
43 // indicates their present size. During size changes, the two may disagree.
44 // Also, the inactive screen's size is not altered until it is made the active
45 // screen.
46 this.screenSize = new hterm.Size(0, 0);
47
rginda8ba33642011-12-14 12:31:31 -080048 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080049 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080050 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
51 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080052 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070053 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080054
rginda87b86462011-12-14 13:48:03 -080055 // The div that contains this terminal.
56 this.div_ = null;
57
rgindac9bc5502012-01-18 11:48:44 -080058 // The document that contains the scrollPort. Defaulted to the global
59 // document here so that the terminal is functional even if it hasn't been
60 // inserted into a document yet, but re-set in decorate().
61 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080062
rginda8ba33642011-12-14 12:31:31 -080063 // The rows that have scrolled off screen and are no longer addressable.
64 this.scrollbackRows_ = [];
65
rgindac9bc5502012-01-18 11:48:44 -080066 // Saved tab stops.
67 this.tabStops_ = [];
68
David Benjamin66e954d2012-05-05 21:08:12 -040069 // Keep track of whether default tab stops have been erased; after a TBC
70 // clears all tab stops, defaults aren't restored on resize until a reset.
71 this.defaultTabStops = true;
72
rginda8ba33642011-12-14 12:31:31 -080073 // The VT's notion of the top and bottom rows. Used during some VT
74 // cursor positioning and scrolling commands.
75 this.vtScrollTop_ = null;
76 this.vtScrollBottom_ = null;
77
78 // The DIV element for the visible cursor.
79 this.cursorNode_ = null;
80
Robert Ginda830583c2013-08-07 13:20:46 -070081 // The current cursor shape of the terminal.
82 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
83
84 // The current color of the cursor.
85 this.cursorColor_ = null;
86
Robert Gindaea2183e2014-07-17 09:51:51 -070087 // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded.
88 this.cursorBlinkCycle_ = [100, 100];
89
90 // Pre-bound onCursorBlink_ handler, so we don't have to do this for each
91 // cursor on/off servicing.
92 this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
93
rginda9f5222b2012-03-05 11:53:28 -080094 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070095 // each output and keystroke. They are initialized by the preference manager.
Robert Ginda8cb7d902013-06-20 14:37:18 -070096 this.backgroundColor_ = null;
97 this.foregroundColor_ = null;
Robert Ginda57f03b42012-09-13 11:02:48 -070098 this.scrollOnOutput_ = null;
99 this.scrollOnKeystroke_ = null;
rginda9f5222b2012-03-05 11:53:28 -0800100
Robert Ginda6aec7eb2015-06-16 10:31:30 -0700101 // True if we should override mouse event reporting to allow local selection.
102 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -0800103
rgindaf0090c92012-02-10 14:58:52 -0800104 // Terminal bell sound.
105 this.bellAudio_ = this.document_.createElement('audio');
rgindaf0090c92012-02-10 14:58:52 -0800106 this.bellAudio_.setAttribute('preload', 'auto');
107
Michael Kelly485ecd12014-06-09 11:41:56 -0400108 // All terminal bell notifications that have been generated (not necessarily
109 // shown).
110 this.bellNotificationList_ = [];
111
112 // Whether we have permission to display notifications.
113 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400114
rginda6d397402012-01-17 10:58:29 -0800115 // Cursor position and attributes saved with DECSC.
116 this.savedOptions_ = {};
117
rginda8ba33642011-12-14 12:31:31 -0800118 // The current mode bits for the terminal.
119 this.options_ = new hterm.Options();
120
121 // Timeouts we might need to clear.
122 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800123
124 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800125 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800126
Zhu Qunying30d40712017-03-14 16:27:00 -0700127 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800128 this.keyboard = new hterm.Keyboard(this);
129
rginda87b86462011-12-14 13:48:03 -0800130 // General IO interface that can be given to third parties without exposing
131 // the entire terminal object.
132 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800133
rgindad5613292012-06-19 15:40:37 -0700134 // True if mouse-click-drag should scroll the terminal.
135 this.enableMouseDragScroll = true;
136
Robert Ginda57f03b42012-09-13 11:02:48 -0700137 this.copyOnSelect = null;
rginda4bba5e12012-06-20 16:15:30 -0700138 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700139
Zhu Qunying30d40712017-03-14 16:27:00 -0700140 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700141 this.useDefaultWindowCopy = false;
142
143 this.clearSelectionAfterCopy = true;
144
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400145 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800146 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700147
148 this.setProfile(opt_profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500149 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800150};
151
152/**
Robert Ginda830583c2013-08-07 13:20:46 -0700153 * Possible cursor shapes.
154 */
155hterm.Terminal.cursorShape = {
156 BLOCK: 'BLOCK',
157 BEAM: 'BEAM',
158 UNDERLINE: 'UNDERLINE'
159};
160
161/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700162 * Clients should override this to be notified when the terminal is ready
163 * for use.
164 *
165 * The terminal initialization is asynchronous, and shouldn't be used before
166 * this method is called.
167 */
168hterm.Terminal.prototype.onTerminalReady = function() { };
169
170/**
rginda35c456b2012-02-09 17:29:05 -0800171 * Default tab with of 8 to match xterm.
172 */
173hterm.Terminal.prototype.tabWidth = 8;
174
175/**
rginda9f5222b2012-03-05 11:53:28 -0800176 * Select a preference profile.
177 *
178 * This will load the terminal preferences for the given profile name and
179 * associate subsequent preference changes with the new preference profile.
180 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500181 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800182 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700183 * @param {function} opt_callback Optional callback to invoke when the profile
184 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800185 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700186hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
187 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800188
Robert Ginda57f03b42012-09-13 11:02:48 -0700189 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800190
Robert Ginda57f03b42012-09-13 11:02:48 -0700191 if (this.prefs_)
192 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800193
Robert Ginda57f03b42012-09-13 11:02:48 -0700194 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
195 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800196 'alt-gr-mode': function(v) {
197 if (v == null) {
198 if (navigator.language.toLowerCase() == 'en-us') {
199 v = 'none';
200 } else {
201 v = 'right-alt';
202 }
203 } else if (typeof v == 'string') {
204 v = v.toLowerCase();
205 } else {
206 v = 'none';
207 }
208
209 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v))
210 v = 'none';
211
212 terminal.keyboard.altGrMode = v;
213 },
214
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700215 'alt-backspace-is-meta-backspace': function(v) {
216 terminal.keyboard.altBackspaceIsMetaBackspace = v;
217 },
218
Robert Ginda57f03b42012-09-13 11:02:48 -0700219 'alt-is-meta': function(v) {
220 terminal.keyboard.altIsMeta = v;
221 },
222
223 'alt-sends-what': function(v) {
224 if (!/^(escape|8-bit|browser-key)$/.test(v))
225 v = 'escape';
226
227 terminal.keyboard.altSendsWhat = v;
228 },
229
230 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800231 var ary = v.match(/^lib-resource:(\S+)/);
232 if (ary) {
233 terminal.bellAudio_.setAttribute('src',
234 lib.resource.getDataUrl(ary[1]));
235 } else {
236 terminal.bellAudio_.setAttribute('src', v);
237 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700238 },
239
Michael Kelly485ecd12014-06-09 11:41:56 -0400240 'desktop-notification-bell': function(v) {
241 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700242 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400243 Notification.permission === 'granted';
244 if (!terminal.desktopNotificationBell_) {
245 // Note: We don't call Notification.requestPermission here because
246 // Chrome requires the call be the result of a user action (such as an
247 // onclick handler), and pref listeners are run asynchronously.
248 //
249 // A way of working around this would be to display a dialog in the
250 // terminal with a "click-to-request-permission" button.
251 console.warn('desktop-notification-bell is true but we do not have ' +
252 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400253 }
254 } else {
255 terminal.desktopNotificationBell_ = false;
256 }
257 },
258
Robert Ginda57f03b42012-09-13 11:02:48 -0700259 'background-color': function(v) {
260 terminal.setBackgroundColor(v);
261 },
262
263 'background-image': function(v) {
264 terminal.scrollPort_.setBackgroundImage(v);
265 },
266
267 'background-size': function(v) {
268 terminal.scrollPort_.setBackgroundSize(v);
269 },
270
271 'background-position': function(v) {
272 terminal.scrollPort_.setBackgroundPosition(v);
273 },
274
275 'backspace-sends-backspace': function(v) {
276 terminal.keyboard.backspaceSendsBackspace = v;
277 },
278
Brad Town18654b62015-03-12 00:27:45 -0700279 'character-map-overrides': function(v) {
280 if (!(v == null || v instanceof Object)) {
281 console.warn('Preference character-map-modifications is not an ' +
282 'object: ' + v);
283 return;
284 }
285
286 for (var code in v) {
287 var glmap = hterm.VT.CharacterMap.maps[code].glmap;
288 for (var received in v[code]) {
289 glmap[received] = v[code][received];
290 }
291 hterm.VT.CharacterMap.maps[code].reset(glmap);
292 }
293 },
294
Robert Ginda57f03b42012-09-13 11:02:48 -0700295 'cursor-blink': function(v) {
296 terminal.setCursorBlink(!!v);
297 },
298
Robert Gindaea2183e2014-07-17 09:51:51 -0700299 'cursor-blink-cycle': function(v) {
300 if (v instanceof Array &&
301 typeof v[0] == 'number' &&
302 typeof v[1] == 'number') {
303 terminal.cursorBlinkCycle_ = v;
304 } else if (typeof v == 'number') {
305 terminal.cursorBlinkCycle_ = [v, v];
306 } else {
307 // Fast blink indicates an error.
308 terminal.cursorBlinkCycle_ = [100, 100];
309 }
310 },
311
Robert Ginda57f03b42012-09-13 11:02:48 -0700312 'cursor-color': function(v) {
313 terminal.setCursorColor(v);
314 },
315
316 'color-palette-overrides': function(v) {
317 if (!(v == null || v instanceof Object || v instanceof Array)) {
318 console.warn('Preference color-palette-overrides is not an array or ' +
319 'object: ' + v);
320 return;
rginda9f5222b2012-03-05 11:53:28 -0800321 }
rginda9f5222b2012-03-05 11:53:28 -0800322
Robert Ginda57f03b42012-09-13 11:02:48 -0700323 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700324
Robert Ginda57f03b42012-09-13 11:02:48 -0700325 if (v) {
326 for (var key in v) {
327 var i = parseInt(key);
328 if (isNaN(i) || i < 0 || i > 255) {
329 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
330 continue;
331 }
332
333 if (v[i]) {
334 var rgb = lib.colors.normalizeCSS(v[i]);
335 if (rgb)
336 lib.colors.colorPalette[i] = rgb;
337 }
338 }
rginda30f20f62012-04-05 16:36:19 -0700339 }
rginda30f20f62012-04-05 16:36:19 -0700340
Evan Jones5f9df812016-12-06 09:38:58 -0500341 terminal.primaryScreen_.textAttributes.resetColorPalette();
Robert Ginda57f03b42012-09-13 11:02:48 -0700342 terminal.alternateScreen_.textAttributes.resetColorPalette();
343 },
rginda30f20f62012-04-05 16:36:19 -0700344
Robert Ginda57f03b42012-09-13 11:02:48 -0700345 'copy-on-select': function(v) {
346 terminal.copyOnSelect = !!v;
347 },
rginda9f5222b2012-03-05 11:53:28 -0800348
Rob Spies0bec09b2014-06-06 15:58:09 -0700349 'use-default-window-copy': function(v) {
350 terminal.useDefaultWindowCopy = !!v;
351 },
352
353 'clear-selection-after-copy': function(v) {
354 terminal.clearSelectionAfterCopy = !!v;
355 },
356
Robert Ginda7e5e9522014-03-14 12:23:58 -0700357 'ctrl-plus-minus-zero-zoom': function(v) {
358 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
359 },
360
Robert Gindafb5a3f92014-05-13 14:12:00 -0700361 'ctrl-c-copy': function(v) {
362 terminal.keyboard.ctrlCCopy = v;
363 },
364
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100365 'ctrl-v-paste': function(v) {
366 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700367 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100368 },
369
Masaya Suzuki273aa982014-05-31 07:25:55 +0900370 'east-asian-ambiguous-as-two-column': function(v) {
371 lib.wc.regardCjkAmbiguous = v;
372 },
373
Robert Ginda57f03b42012-09-13 11:02:48 -0700374 'enable-8-bit-control': function(v) {
375 terminal.vt.enable8BitControl = !!v;
376 },
rginda30f20f62012-04-05 16:36:19 -0700377
Robert Ginda57f03b42012-09-13 11:02:48 -0700378 'enable-bold': function(v) {
379 terminal.syncBoldSafeState();
380 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400381
Robert Ginda3e278d72014-03-25 13:18:51 -0700382 'enable-bold-as-bright': function(v) {
383 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
384 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
385 },
386
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400387 'enable-blink': function(v) {
388 terminal.syncBlinkState();
389 },
390
Robert Ginda57f03b42012-09-13 11:02:48 -0700391 'enable-clipboard-write': function(v) {
392 terminal.vt.enableClipboardWrite = !!v;
393 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400394
Robert Ginda3755e752013-05-31 13:34:09 -0700395 'enable-dec12': function(v) {
396 terminal.vt.enableDec12 = !!v;
397 },
398
Robert Ginda57f03b42012-09-13 11:02:48 -0700399 'font-family': function(v) {
400 terminal.syncFontFamily();
401 },
rginda30f20f62012-04-05 16:36:19 -0700402
Robert Ginda57f03b42012-09-13 11:02:48 -0700403 'font-size': function(v) {
404 terminal.setFontSize(v);
405 },
rginda9875d902012-08-20 16:21:57 -0700406
Robert Ginda57f03b42012-09-13 11:02:48 -0700407 'font-smoothing': function(v) {
408 terminal.syncFontFamily();
409 },
rgindade84e382012-04-20 15:39:31 -0700410
Robert Ginda57f03b42012-09-13 11:02:48 -0700411 'foreground-color': function(v) {
412 terminal.setForegroundColor(v);
413 },
rginda30f20f62012-04-05 16:36:19 -0700414
Robert Ginda57f03b42012-09-13 11:02:48 -0700415 'home-keys-scroll': function(v) {
416 terminal.keyboard.homeKeysScroll = v;
417 },
rginda4bba5e12012-06-20 16:15:30 -0700418
Robert Gindaa8165692015-06-15 14:46:31 -0700419 'keybindings': function(v) {
420 terminal.keyboard.bindings.clear();
421
422 if (!v)
423 return;
424
425 if (!(v instanceof Object)) {
426 console.error('Error in keybindings preference: Expected object');
427 return;
428 }
429
430 try {
431 terminal.keyboard.bindings.addBindings(v);
432 } catch (ex) {
433 console.error('Error in keybindings preference: ' + ex);
434 }
435 },
436
Robert Ginda57f03b42012-09-13 11:02:48 -0700437 'max-string-sequence': function(v) {
438 terminal.vt.maxStringSequence = v;
439 },
rginda11057d52012-04-25 12:29:56 -0700440
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700441 'media-keys-are-fkeys': function(v) {
442 terminal.keyboard.mediaKeysAreFKeys = v;
443 },
444
Robert Ginda57f03b42012-09-13 11:02:48 -0700445 'meta-sends-escape': function(v) {
446 terminal.keyboard.metaSendsEscape = v;
447 },
rginda30f20f62012-04-05 16:36:19 -0700448
Robert Ginda57f03b42012-09-13 11:02:48 -0700449 'mouse-paste-button': function(v) {
450 terminal.syncMousePasteButton();
451 },
rgindaa8ba17d2012-08-15 14:41:10 -0700452
Robert Gindae76aa9f2014-03-14 12:29:12 -0700453 'page-keys-scroll': function(v) {
454 terminal.keyboard.pageKeysScroll = v;
455 },
456
Robert Ginda40932892012-12-10 17:26:40 -0800457 'pass-alt-number': function(v) {
458 if (v == null) {
459 var osx = window.navigator.userAgent.match(/Mac OS X/);
460
461 // Let Alt-1..9 pass to the browser (to control tab switching) on
462 // non-OS X systems, or if hterm is not opened in an app window.
463 v = (!osx && hterm.windowType != 'popup');
464 }
465
466 terminal.passAltNumber = v;
467 },
468
469 'pass-ctrl-number': function(v) {
470 if (v == null) {
471 var osx = window.navigator.userAgent.match(/Mac OS X/);
472
473 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
474 // non-OS X systems, or if hterm is not opened in an app window.
475 v = (!osx && hterm.windowType != 'popup');
476 }
477
478 terminal.passCtrlNumber = v;
479 },
480
481 'pass-meta-number': function(v) {
482 if (v == null) {
483 var osx = window.navigator.userAgent.match(/Mac OS X/);
484
485 // Let Meta-1..9 pass to the browser (to control tab switching) on
486 // OS X systems, or if hterm is not opened in an app window.
487 v = (osx && hterm.windowType != 'popup');
488 }
489
490 terminal.passMetaNumber = v;
491 },
492
Marius Schilder77857b32014-05-14 16:21:26 -0700493 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700494 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700495 },
496
Robert Ginda8cb7d902013-06-20 14:37:18 -0700497 'receive-encoding': function(v) {
498 if (!(/^(utf-8|raw)$/).test(v)) {
499 console.warn('Invalid value for "receive-encoding": ' + v);
500 v = 'utf-8';
501 }
502
503 terminal.vt.characterEncoding = v;
504 },
505
Robert Ginda57f03b42012-09-13 11:02:48 -0700506 'scroll-on-keystroke': function(v) {
507 terminal.scrollOnKeystroke_ = v;
508 },
rginda9f5222b2012-03-05 11:53:28 -0800509
Robert Ginda57f03b42012-09-13 11:02:48 -0700510 'scroll-on-output': function(v) {
511 terminal.scrollOnOutput_ = v;
512 },
rginda30f20f62012-04-05 16:36:19 -0700513
Robert Ginda57f03b42012-09-13 11:02:48 -0700514 'scrollbar-visible': function(v) {
515 terminal.setScrollbarVisible(v);
516 },
rginda9f5222b2012-03-05 11:53:28 -0800517
Rob Spies49039e52014-12-17 13:40:04 -0800518 'scroll-wheel-move-multiplier': function(v) {
519 terminal.setScrollWheelMoveMultipler(v);
520 },
521
Robert Ginda8cb7d902013-06-20 14:37:18 -0700522 'send-encoding': function(v) {
523 if (!(/^(utf-8|raw)$/).test(v)) {
524 console.warn('Invalid value for "send-encoding": ' + v);
525 v = 'utf-8';
526 }
527
528 terminal.keyboard.characterEncoding = v;
529 },
530
Robert Ginda57f03b42012-09-13 11:02:48 -0700531 'shift-insert-paste': function(v) {
532 terminal.keyboard.shiftInsertPaste = v;
533 },
rginda9f5222b2012-03-05 11:53:28 -0800534
Robert Gindae76aa9f2014-03-14 12:29:12 -0700535 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400536 terminal.scrollPort_.setUserCssUrl(v);
537 },
538
539 'user-css-text': function(v) {
540 terminal.scrollPort_.setUserCssText(v);
541 },
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);
Ricky Liang48f05cb2013-12-31 23:35:29 +0800744 if (this.wcCssRule_) {
745 this.wcCssRule_.style.width = this.scrollPort_.characterSize.width * 2 +
746 'px';
747 }
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') {
rginda4bba5e12012-06-20 16:15:30 -0700790 this.mousePasteButton = 2;
791 } else {
792 this.mousePasteButton = 3;
793 }
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/**
rginda87b86462011-12-14 13:48:03 -0800832 * Return a copy of the current cursor position.
833 *
834 * @return {hterm.RowCol} The RowCol object representing the current position.
835 */
836hterm.Terminal.prototype.saveCursor = function() {
837 return this.screen_.cursorPosition.clone();
838};
839
Evan Jones2600d4f2016-12-06 09:29:36 -0500840/**
841 * Return the current text attributes.
842 *
843 * @return {string}
844 */
rgindaa19afe22012-01-25 15:40:22 -0800845hterm.Terminal.prototype.getTextAttributes = function() {
846 return this.screen_.textAttributes;
847};
848
Evan Jones2600d4f2016-12-06 09:29:36 -0500849/**
850 * Set the text attributes.
851 *
852 * @param {string} textAttributes The attributes to set.
853 */
rginda1a09aa02012-06-18 21:11:25 -0700854hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
855 this.screen_.textAttributes = textAttributes;
856};
857
rginda87b86462011-12-14 13:48:03 -0800858/**
rgindaf522ce02012-04-17 17:49:17 -0700859 * Return the current browser zoom factor applied to the terminal.
860 *
861 * @return {number} The current browser zoom factor.
862 */
863hterm.Terminal.prototype.getZoomFactor = function() {
864 return this.scrollPort_.characterSize.zoomFactor;
865};
866
867/**
rginda9846e2f2012-01-27 13:53:33 -0800868 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500869 *
870 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800871 */
872hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800873 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800874};
875
876/**
rginda87b86462011-12-14 13:48:03 -0800877 * Restore a previously saved cursor position.
878 *
879 * @param {hterm.RowCol} cursor The position to restore.
880 */
881hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700882 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
883 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800884 this.screen_.setCursorPosition(row, column);
885 if (cursor.column > column ||
886 cursor.column == column && cursor.overflow) {
887 this.screen_.cursorPosition.overflow = true;
888 }
rginda87b86462011-12-14 13:48:03 -0800889};
890
891/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400892 * Clear the cursor's overflow flag.
893 */
894hterm.Terminal.prototype.clearCursorOverflow = function() {
895 this.screen_.cursorPosition.overflow = false;
896};
897
898/**
Robert Ginda830583c2013-08-07 13:20:46 -0700899 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -0500900 *
901 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -0700902 */
903hterm.Terminal.prototype.setCursorShape = function(shape) {
904 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -0800905 this.restyleCursor_();
Robert Ginda830583c2013-08-07 13:20:46 -0700906}
907
908/**
909 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -0500910 *
911 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -0700912 */
913hterm.Terminal.prototype.getCursorShape = function() {
914 return this.cursorShape_;
915}
916
917/**
rginda87b86462011-12-14 13:48:03 -0800918 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -0500919 *
920 * @param {number} columnCount
rginda87b86462011-12-14 13:48:03 -0800921 */
922hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800923 if (columnCount == null) {
924 this.div_.style.width = '100%';
925 return;
926 }
927
Robert Ginda26806d12014-07-24 13:44:07 -0700928 this.div_.style.width = Math.ceil(
929 this.scrollPort_.characterSize.width *
930 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400931 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800932 this.scheduleSyncCursorPosition_();
933};
rginda87b86462011-12-14 13:48:03 -0800934
rgindac9bc5502012-01-18 11:48:44 -0800935/**
rginda35c456b2012-02-09 17:29:05 -0800936 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -0500937 *
938 * @param {number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -0800939 */
940hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800941 if (rowCount == null) {
942 this.div_.style.height = '100%';
943 return;
944 }
945
rginda35c456b2012-02-09 17:29:05 -0800946 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700947 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800948 this.realizeSize_(this.screenSize.width, rowCount);
949 this.scheduleSyncCursorPosition_();
950};
951
952/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400953 * Deal with terminal size changes.
954 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500955 * @param {number} columnCount The number of columns.
956 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400957 */
958hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
959 if (columnCount != this.screenSize.width)
960 this.realizeWidth_(columnCount);
961
962 if (rowCount != this.screenSize.height)
963 this.realizeHeight_(rowCount);
964
965 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -0700966 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400967};
968
969/**
rgindac9bc5502012-01-18 11:48:44 -0800970 * Deal with terminal width changes.
971 *
972 * This function does what needs to be done when the terminal width changes
973 * out from under us. It happens here rather than in onResize_() because this
974 * code may need to run synchronously to handle programmatic changes of
975 * terminal width.
976 *
977 * Relying on the browser to send us an async resize event means we may not be
978 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -0500979 *
980 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -0800981 */
982hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700983 if (columnCount <= 0)
984 throw new Error('Attempt to realize bad width: ' + columnCount);
985
rgindac9bc5502012-01-18 11:48:44 -0800986 var deltaColumns = columnCount - this.screen_.getWidth();
987
rginda87b86462011-12-14 13:48:03 -0800988 this.screenSize.width = columnCount;
989 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800990
991 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400992 if (this.defaultTabStops)
993 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800994 } else {
995 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400996 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800997 break;
998
999 this.tabStops_.pop();
1000 }
1001 }
1002
1003 this.screen_.setColumnCount(this.screenSize.width);
1004};
1005
1006/**
1007 * Deal with terminal height changes.
1008 *
1009 * This function does what needs to be done when the terminal height changes
1010 * out from under us. It happens here rather than in onResize_() because this
1011 * code may need to run synchronously to handle programmatic changes of
1012 * terminal height.
1013 *
1014 * Relying on the browser to send us an async resize event means we may not be
1015 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001016 *
1017 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001018 */
1019hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001020 if (rowCount <= 0)
1021 throw new Error('Attempt to realize bad height: ' + rowCount);
1022
rgindac9bc5502012-01-18 11:48:44 -08001023 var deltaRows = rowCount - this.screen_.getHeight();
1024
1025 this.screenSize.height = rowCount;
1026
1027 var cursor = this.saveCursor();
1028
1029 if (deltaRows < 0) {
1030 // Screen got smaller.
1031 deltaRows *= -1;
1032 while (deltaRows) {
1033 var lastRow = this.getRowCount() - 1;
1034 if (lastRow - this.scrollbackRows_.length == cursor.row)
1035 break;
1036
1037 if (this.getRowText(lastRow))
1038 break;
1039
1040 this.screen_.popRow();
1041 deltaRows--;
1042 }
1043
1044 var ary = this.screen_.shiftRows(deltaRows);
1045 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1046
1047 // We just removed rows from the top of the screen, we need to update
1048 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001049 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001050 } else if (deltaRows > 0) {
1051 // Screen got larger.
1052
1053 if (deltaRows <= this.scrollbackRows_.length) {
1054 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1055 var rows = this.scrollbackRows_.splice(
1056 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1057 this.screen_.unshiftRows(rows);
1058 deltaRows -= scrollbackCount;
1059 cursor.row += scrollbackCount;
1060 }
1061
1062 if (deltaRows)
1063 this.appendRows_(deltaRows);
1064 }
1065
rginda35c456b2012-02-09 17:29:05 -08001066 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001067 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001068};
1069
1070/**
1071 * Scroll the terminal to the top of the scrollback buffer.
1072 */
1073hterm.Terminal.prototype.scrollHome = function() {
1074 this.scrollPort_.scrollRowToTop(0);
1075};
1076
1077/**
1078 * Scroll the terminal to the end.
1079 */
1080hterm.Terminal.prototype.scrollEnd = function() {
1081 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1082};
1083
1084/**
1085 * Scroll the terminal one page up (minus one line) relative to the current
1086 * position.
1087 */
1088hterm.Terminal.prototype.scrollPageUp = function() {
1089 var i = this.scrollPort_.getTopRowIndex();
1090 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
1091};
1092
1093/**
1094 * Scroll the terminal one page down (minus one line) relative to the current
1095 * position.
1096 */
1097hterm.Terminal.prototype.scrollPageDown = function() {
1098 var i = this.scrollPort_.getTopRowIndex();
1099 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -08001100};
1101
rgindac9bc5502012-01-18 11:48:44 -08001102/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001103 * Scroll the terminal one line up relative to the current position.
1104 */
1105hterm.Terminal.prototype.scrollLineUp = function() {
1106 var i = this.scrollPort_.getTopRowIndex();
1107 this.scrollPort_.scrollRowToTop(i - 1);
1108};
1109
1110/**
1111 * Scroll the terminal one line down relative to the current position.
1112 */
1113hterm.Terminal.prototype.scrollLineDown = function() {
1114 var i = this.scrollPort_.getTopRowIndex();
1115 this.scrollPort_.scrollRowToTop(i + 1);
1116};
1117
1118/**
Robert Ginda40932892012-12-10 17:26:40 -08001119 * Clear primary screen, secondary screen, and the scrollback buffer.
1120 */
1121hterm.Terminal.prototype.wipeContents = function() {
1122 this.scrollbackRows_.length = 0;
1123 this.scrollPort_.resetCache();
1124
1125 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
1126 var bottom = screen.getHeight();
1127 if (bottom > 0) {
1128 this.renumberRows_(0, bottom);
1129 this.clearHome(screen);
1130 }
1131 }.bind(this));
1132
1133 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001134 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001135};
1136
1137/**
rgindac9bc5502012-01-18 11:48:44 -08001138 * Full terminal reset.
1139 */
rginda87b86462011-12-14 13:48:03 -08001140hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -08001141 this.clearAllTabStops();
1142 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001143
1144 this.clearHome(this.primaryScreen_);
1145 this.primaryScreen_.textAttributes.reset();
1146
1147 this.clearHome(this.alternateScreen_);
1148 this.alternateScreen_.textAttributes.reset();
1149
rgindab8bc8932012-04-27 12:45:03 -07001150 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1151
Robert Ginda92e18102013-03-14 13:56:37 -07001152 this.vt.reset();
1153
rgindac9bc5502012-01-18 11:48:44 -08001154 this.softReset();
rginda87b86462011-12-14 13:48:03 -08001155};
1156
rgindac9bc5502012-01-18 11:48:44 -08001157/**
1158 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001159 *
1160 * Perform a soft reset to the default values listed in
1161 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001162 */
rginda0f5c0292012-01-13 11:00:13 -08001163hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -07001164 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001165 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001166
Brad Townb62dfdc2015-03-16 19:07:15 -07001167 // We show the cursor on soft reset but do not alter the blink state.
1168 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1169
rgindab8bc8932012-04-27 12:45:03 -07001170 // Xterm also resets the color palette on soft reset, even though it doesn't
1171 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -07001172 this.primaryScreen_.textAttributes.resetColorPalette();
1173 this.alternateScreen_.textAttributes.resetColorPalette();
1174
rgindab8bc8932012-04-27 12:45:03 -07001175 // The xterm man page explicitly says this will happen on soft reset.
1176 this.setVTScrollRegion(null, null);
1177
1178 // Xterm also shows the cursor on soft reset, but does not alter the blink
1179 // state.
rgindaa19afe22012-01-25 15:40:22 -08001180 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001181};
1182
rgindac9bc5502012-01-18 11:48:44 -08001183/**
1184 * Move the cursor forward to the next tab stop, or to the last column
1185 * if no more tab stops are set.
1186 */
1187hterm.Terminal.prototype.forwardTabStop = function() {
1188 var column = this.screen_.cursorPosition.column;
1189
1190 for (var i = 0; i < this.tabStops_.length; i++) {
1191 if (this.tabStops_[i] > column) {
1192 this.setCursorColumn(this.tabStops_[i]);
1193 return;
1194 }
1195 }
1196
David Benjamin66e954d2012-05-05 21:08:12 -04001197 // xterm does not clear the overflow flag on HT or CHT.
1198 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001199 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001200 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001201};
1202
rgindac9bc5502012-01-18 11:48:44 -08001203/**
1204 * Move the cursor backward to the previous tab stop, or to the first column
1205 * if no previous tab stops are set.
1206 */
1207hterm.Terminal.prototype.backwardTabStop = function() {
1208 var column = this.screen_.cursorPosition.column;
1209
1210 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1211 if (this.tabStops_[i] < column) {
1212 this.setCursorColumn(this.tabStops_[i]);
1213 return;
1214 }
1215 }
1216
1217 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001218};
1219
rgindac9bc5502012-01-18 11:48:44 -08001220/**
1221 * Set a tab stop at the given column.
1222 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001223 * @param {integer} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001224 */
1225hterm.Terminal.prototype.setTabStop = function(column) {
1226 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1227 if (this.tabStops_[i] == column)
1228 return;
1229
1230 if (this.tabStops_[i] < column) {
1231 this.tabStops_.splice(i + 1, 0, column);
1232 return;
1233 }
1234 }
1235
1236 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001237};
1238
rgindac9bc5502012-01-18 11:48:44 -08001239/**
1240 * Clear the tab stop at the current cursor position.
1241 *
1242 * No effect if there is no tab stop at the current cursor position.
1243 */
1244hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1245 var column = this.screen_.cursorPosition.column;
1246
1247 var i = this.tabStops_.indexOf(column);
1248 if (i == -1)
1249 return;
1250
1251 this.tabStops_.splice(i, 1);
1252};
1253
1254/**
1255 * Clear all tab stops.
1256 */
1257hterm.Terminal.prototype.clearAllTabStops = function() {
1258 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001259 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001260};
1261
1262/**
1263 * Set up the default tab stops, starting from a given column.
1264 *
1265 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001266 * from the specified column, or 0 if no column is provided. It also flags
1267 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001268 *
1269 * This does not clear the existing tab stops first, use clearAllTabStops
1270 * for that.
1271 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001272 * @param {integer} opt_start Optional starting zero based starting column, useful
rgindac9bc5502012-01-18 11:48:44 -08001273 * for filling out missing tab stops when the terminal is resized.
1274 */
1275hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1276 var start = opt_start || 0;
1277 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001278 // Round start up to a default tab stop.
1279 start = start - 1 - ((start - 1) % w) + w;
1280 for (var i = start; i < this.screenSize.width; i += w) {
1281 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001282 }
David Benjamin66e954d2012-05-05 21:08:12 -04001283
1284 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001285};
1286
rginda6d397402012-01-17 10:58:29 -08001287/**
rginda8ba33642011-12-14 12:31:31 -08001288 * Interpret a sequence of characters.
1289 *
1290 * Incomplete escape sequences are buffered until the next call.
1291 *
1292 * @param {string} str Sequence of characters to interpret or pass through.
1293 */
1294hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001295 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001296 this.scheduleSyncCursorPosition_();
1297};
1298
1299/**
1300 * Take over the given DIV for use as the terminal display.
1301 *
1302 * @param {HTMLDivElement} div The div to use as the terminal display.
1303 */
1304hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001305 this.div_ = div;
1306
rginda8ba33642011-12-14 12:31:31 -08001307 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001308 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001309 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1310 this.scrollPort_.setBackgroundPosition(
1311 this.prefs_.get('background-position'));
Mike Frysinger08bad432017-04-24 00:50:54 -04001312 this.scrollPort_.setUserCssUrl(this.prefs_.get('user-css'));
1313 this.scrollPort_.setUserCssText(this.prefs_.get('user-css-text'));
rginda30f20f62012-04-05 16:36:19 -07001314
rginda0918b652012-04-04 11:26:24 -07001315 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001316
rginda9f5222b2012-03-05 11:53:28 -08001317 this.setFontSize(this.prefs_.get('font-size'));
1318 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001319
David Reveman8f552492012-03-28 12:18:41 -04001320 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001321 this.setScrollWheelMoveMultipler(
1322 this.prefs_.get('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001323
rginda8ba33642011-12-14 12:31:31 -08001324 this.document_ = this.scrollPort_.getDocument();
1325
Evan Jones5f9df812016-12-06 09:38:58 -05001326 this.document_.body.oncontextmenu = function() { return false; };
rginda4bba5e12012-06-20 16:15:30 -07001327
1328 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001329 var screenNode = this.scrollPort_.getScreenNode();
1330 screenNode.addEventListener('mousedown', onMouse);
1331 screenNode.addEventListener('mouseup', onMouse);
1332 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001333 this.scrollPort_.onScrollWheel = onMouse;
1334
Toni Barzic0bfa8922013-11-22 11:18:35 -08001335 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001336 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001337 // Listen for mousedown events on the screenNode as in FF the focus
1338 // events don't bubble.
1339 screenNode.addEventListener('mousedown', function() {
1340 setTimeout(this.onFocusChange_.bind(this, true));
1341 }.bind(this));
1342
Toni Barzic0bfa8922013-11-22 11:18:35 -08001343 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001344 'blur', this.onFocusChange_.bind(this, false));
1345
1346 var style = this.document_.createElement('style');
1347 style.textContent =
1348 ('.cursor-node[focus="false"] {' +
1349 ' box-sizing: border-box;' +
1350 ' background-color: transparent !important;' +
1351 ' border-width: 2px;' +
1352 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001353 '}' +
1354 '.wc-node {' +
1355 ' display: inline-block;' +
1356 ' text-align: center;' +
1357 ' width: ' + this.scrollPort_.characterSize.width * 2 + 'px;' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001358 '}' +
1359 ':root {' +
1360 ' --hterm-blink-node-duration: 0.7s;' +
1361 '}' +
1362 '@keyframes blink {' +
1363 ' from { opacity: 1.0; }' +
1364 ' to { opacity: 0.0; }' +
1365 '}' +
1366 '.blink-node {' +
1367 ' animation-name: blink;' +
1368 ' animation-duration: var(--hterm-blink-node-duration);' +
1369 ' animation-iteration-count: infinite;' +
1370 ' animation-timing-function: ease-in-out;' +
1371 ' animation-direction: alternate;' +
rginda8e92a692012-05-20 19:37:20 -07001372 '}');
1373 this.document_.head.appendChild(style);
1374
Ricky Liang48f05cb2013-12-31 23:35:29 +08001375 var styleSheets = this.document_.styleSheets;
1376 var cssRules = styleSheets[styleSheets.length - 1].cssRules;
1377 this.wcCssRule_ = cssRules[cssRules.length - 1];
1378
rginda8ba33642011-12-14 12:31:31 -08001379 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001380 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001381 this.cursorNode_.style.cssText =
1382 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001383 'top: -99px;' +
1384 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001385 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1386 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
Rob Spies06533ba2014-04-24 11:20:37 -07001387 '-webkit-transition: opacity, background-color 100ms linear;' +
1388 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001389
rginda8e92a692012-05-20 19:37:20 -07001390 this.setCursorColor(this.prefs_.get('cursor-color'));
Robert Gindafb1be6a2013-12-11 11:56:22 -08001391 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1392 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001393
rginda8ba33642011-12-14 12:31:31 -08001394 this.document_.body.appendChild(this.cursorNode_);
1395
rgindad5613292012-06-19 15:40:37 -07001396 // When 'enableMouseDragScroll' is off we reposition this element directly
1397 // under the mouse cursor after a click. This makes Chrome associate
1398 // subsequent mousemove events with the scroll-blocker. Since the
1399 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1400 // events do not cause the scrollport to scroll.
1401 //
1402 // It's a hack, but it's the cleanest way I could find.
1403 this.scrollBlockerNode_ = this.document_.createElement('div');
1404 this.scrollBlockerNode_.style.cssText =
1405 ('position: absolute;' +
1406 'top: -99px;' +
1407 'display: block;' +
1408 'width: 10px;' +
1409 'height: 10px;');
1410 this.document_.body.appendChild(this.scrollBlockerNode_);
1411
rgindad5613292012-06-19 15:40:37 -07001412 this.scrollPort_.onScrollWheel = onMouse;
1413 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1414 ].forEach(function(event) {
1415 this.scrollBlockerNode_.addEventListener(event, onMouse);
1416 this.cursorNode_.addEventListener(event, onMouse);
1417 this.document_.addEventListener(event, onMouse);
1418 }.bind(this));
1419
1420 this.cursorNode_.addEventListener('mousedown', function() {
1421 setTimeout(this.focus.bind(this));
1422 }.bind(this));
1423
rginda8ba33642011-12-14 12:31:31 -08001424 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001425
rginda87b86462011-12-14 13:48:03 -08001426 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001427 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001428};
1429
rginda0918b652012-04-04 11:26:24 -07001430/**
1431 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001432 *
1433 * @return {HTMLDocument}
rginda0918b652012-04-04 11:26:24 -07001434 */
rginda87b86462011-12-14 13:48:03 -08001435hterm.Terminal.prototype.getDocument = function() {
1436 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001437};
1438
1439/**
rginda0918b652012-04-04 11:26:24 -07001440 * Focus the terminal.
1441 */
1442hterm.Terminal.prototype.focus = function() {
1443 this.scrollPort_.focus();
1444};
1445
1446/**
rginda8ba33642011-12-14 12:31:31 -08001447 * Return the HTML Element for a given row index.
1448 *
1449 * This is a method from the RowProvider interface. The ScrollPort uses
1450 * it to fetch rows on demand as they are scrolled into view.
1451 *
1452 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1453 * pairs to conserve memory.
1454 *
1455 * @param {integer} index The zero-based row index, measured relative to the
1456 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001457 * largest indices.
rginda8ba33642011-12-14 12:31:31 -08001458 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1459 */
1460hterm.Terminal.prototype.getRowNode = function(index) {
1461 if (index < this.scrollbackRows_.length)
1462 return this.scrollbackRows_[index];
1463
1464 var screenIndex = index - this.scrollbackRows_.length;
1465 return this.screen_.rowsArray[screenIndex];
1466};
1467
1468/**
1469 * Return the text content for a given range of rows.
1470 *
1471 * This is a method from the RowProvider interface. The ScrollPort uses
1472 * it to fetch text content on demand when the user attempts to copy their
1473 * selection to the clipboard.
1474 *
1475 * @param {integer} start The zero-based row index to start from, measured
1476 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001477 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001478 * @param {integer} end The zero-based row index to end on, measured
1479 * relative to the start of the scrollback buffer.
1480 * @return {string} A single string containing the text value of the range of
1481 * rows. Lines will be newline delimited, with no trailing newline.
1482 */
1483hterm.Terminal.prototype.getRowsText = function(start, end) {
1484 var ary = [];
1485 for (var i = start; i < end; i++) {
1486 var node = this.getRowNode(i);
1487 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001488 if (i < end - 1 && !node.getAttribute('line-overflow'))
1489 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001490 }
1491
rgindaa09e7332012-08-17 12:49:51 -07001492 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001493};
1494
1495/**
1496 * Return the text content for a given row.
1497 *
1498 * This is a method from the RowProvider interface. The ScrollPort uses
1499 * it to fetch text content on demand when the user attempts to copy their
1500 * selection to the clipboard.
1501 *
1502 * @param {integer} index The zero-based row index to return, measured
1503 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001504 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001505 * @return {string} A string containing the text value of the selected row.
1506 */
1507hterm.Terminal.prototype.getRowText = function(index) {
1508 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001509 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001510};
1511
1512/**
1513 * Return the total number of rows in the addressable screen and in the
1514 * scrollback buffer of this terminal.
1515 *
1516 * This is a method from the RowProvider interface. The ScrollPort uses
1517 * it to compute the size of the scrollbar.
1518 *
1519 * @return {integer} The number of rows in this terminal.
1520 */
1521hterm.Terminal.prototype.getRowCount = function() {
1522 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1523};
1524
1525/**
1526 * Create DOM nodes for new rows and append them to the end of the terminal.
1527 *
1528 * This is the only correct way to add a new DOM node for a row. Notice that
1529 * the new row is appended to the bottom of the list of rows, and does not
1530 * require renumbering (of the rowIndex property) of previous rows.
1531 *
1532 * If you think you want a new blank row somewhere in the middle of the
1533 * terminal, look into moveRows_().
1534 *
1535 * This method does not pay attention to vtScrollTop/Bottom, since you should
1536 * be using moveRows() in cases where they would matter.
1537 *
1538 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001539 *
1540 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001541 */
1542hterm.Terminal.prototype.appendRows_ = function(count) {
1543 var cursorRow = this.screen_.rowsArray.length;
1544 var offset = this.scrollbackRows_.length + cursorRow;
1545 for (var i = 0; i < count; i++) {
1546 var row = this.document_.createElement('x-row');
1547 row.appendChild(this.document_.createTextNode(''));
1548 row.rowIndex = offset + i;
1549 this.screen_.pushRow(row);
1550 }
1551
1552 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1553 if (extraRows > 0) {
1554 var ary = this.screen_.shiftRows(extraRows);
1555 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001556 if (this.scrollPort_.isScrolledEnd)
1557 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001558 }
1559
1560 if (cursorRow >= this.screen_.rowsArray.length)
1561 cursorRow = this.screen_.rowsArray.length - 1;
1562
rginda87b86462011-12-14 13:48:03 -08001563 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001564};
1565
1566/**
1567 * Relocate rows from one part of the addressable screen to another.
1568 *
1569 * This is used to recycle rows during VT scrolls (those which are driven
1570 * by VT commands, rather than by the user manipulating the scrollbar.)
1571 *
1572 * In this case, the blank lines scrolled into the scroll region are made of
1573 * the nodes we scrolled off. These have their rowIndex properties carefully
1574 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001575 *
1576 * @param {number} fromIndex The start index.
1577 * @param {number} count The number of rows to move.
1578 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001579 */
1580hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1581 var ary = this.screen_.removeRows(fromIndex, count);
1582 this.screen_.insertRows(toIndex, ary);
1583
1584 var start, end;
1585 if (fromIndex < toIndex) {
1586 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001587 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001588 } else {
1589 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001590 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001591 }
1592
1593 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001594 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001595};
1596
1597/**
1598 * Renumber the rowIndex property of the given range of rows.
1599 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001600 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001601 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001602 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001603 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001604 *
1605 * @param {number} start The start index.
1606 * @param {number} end The end index.
1607 * @param {hterm.Screen} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001608 */
Robert Ginda40932892012-12-10 17:26:40 -08001609hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1610 var screen = opt_screen || this.screen_;
1611
rginda8ba33642011-12-14 12:31:31 -08001612 var offset = this.scrollbackRows_.length;
1613 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001614 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001615 }
1616};
1617
1618/**
1619 * Print a string to the terminal.
1620 *
1621 * This respects the current insert and wraparound modes. It will add new lines
1622 * to the end of the terminal, scrolling off the top into the scrollback buffer
1623 * if necessary.
1624 *
1625 * The string is *not* parsed for escape codes. Use the interpret() method if
1626 * that's what you're after.
1627 *
1628 * @param{string} str The string to print.
1629 */
1630hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001631 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001632
Ricky Liang48f05cb2013-12-31 23:35:29 +08001633 var strWidth = lib.wc.strWidth(str);
1634
1635 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001636 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1637 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001638 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001639 }
rgindaa19afe22012-01-25 15:40:22 -08001640
Ricky Liang48f05cb2013-12-31 23:35:29 +08001641 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001642 var didOverflow = false;
1643 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001644
rgindaa9abdd82012-08-06 18:05:09 -07001645 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1646 didOverflow = true;
1647 count = this.screenSize.width - this.screen_.cursorPosition.column;
1648 }
rgindaa19afe22012-01-25 15:40:22 -08001649
rgindaa9abdd82012-08-06 18:05:09 -07001650 if (didOverflow && !this.options_.wraparound) {
1651 // If the string overflowed the line but wraparound is off, then the
1652 // last printed character should be the last of the string.
1653 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001654 substr = lib.wc.substr(str, startOffset, count - 1) +
1655 lib.wc.substr(str, strWidth - 1);
1656 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001657 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001658 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001659 }
rgindaa19afe22012-01-25 15:40:22 -08001660
Ricky Liang48f05cb2013-12-31 23:35:29 +08001661 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1662 for (var i = 0; i < tokens.length; i++) {
1663 if (tokens[i].wcNode)
1664 this.screen_.textAttributes.wcNode = true;
1665
1666 if (this.options_.insertMode) {
1667 this.screen_.insertString(tokens[i].str);
1668 } else {
1669 this.screen_.overwriteString(tokens[i].str);
1670 }
1671 this.screen_.textAttributes.wcNode = false;
rgindaa9abdd82012-08-06 18:05:09 -07001672 }
1673
1674 this.screen_.maybeClipCurrentRow();
1675 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001676 }
rginda8ba33642011-12-14 12:31:31 -08001677
1678 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001679
rginda9f5222b2012-03-05 11:53:28 -08001680 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001681 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001682};
1683
1684/**
rginda87b86462011-12-14 13:48:03 -08001685 * Set the VT scroll region.
1686 *
rginda87b86462011-12-14 13:48:03 -08001687 * This also resets the cursor position to the absolute (0, 0) position, since
1688 * that's what xterm appears to do.
1689 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001690 * Setting the scroll region to the full height of the terminal will clear
1691 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1692 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1693 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1694 * continue to work as most users would expect.
1695 *
rginda87b86462011-12-14 13:48:03 -08001696 * @param {integer} scrollTop The zero-based top of the scroll region.
1697 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1698 * inclusive.
1699 */
1700hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001701 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001702 this.vtScrollTop_ = null;
1703 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001704 } else {
1705 this.vtScrollTop_ = scrollTop;
1706 this.vtScrollBottom_ = scrollBottom;
1707 }
rginda87b86462011-12-14 13:48:03 -08001708};
1709
1710/**
rginda8ba33642011-12-14 12:31:31 -08001711 * Return the top row index according to the VT.
1712 *
1713 * This will return 0 unless the terminal has been told to restrict scrolling
1714 * to some lower row. It is used for some VT cursor positioning and scrolling
1715 * commands.
1716 *
1717 * @return {integer} The topmost row in the terminal's scroll region.
1718 */
1719hterm.Terminal.prototype.getVTScrollTop = function() {
1720 if (this.vtScrollTop_ != null)
1721 return this.vtScrollTop_;
1722
1723 return 0;
rginda87b86462011-12-14 13:48:03 -08001724};
rginda8ba33642011-12-14 12:31:31 -08001725
1726/**
1727 * Return the bottom row index according to the VT.
1728 *
1729 * This will return the height of the terminal unless the it has been told to
1730 * restrict scrolling to some higher row. It is used for some VT cursor
1731 * positioning and scrolling commands.
1732 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001733 * @return {integer} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001734 */
1735hterm.Terminal.prototype.getVTScrollBottom = function() {
1736 if (this.vtScrollBottom_ != null)
1737 return this.vtScrollBottom_;
1738
rginda87b86462011-12-14 13:48:03 -08001739 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001740}
1741
1742/**
1743 * Process a '\n' character.
1744 *
1745 * If the cursor is on the final row of the terminal this will append a new
1746 * blank row to the screen and scroll the topmost row into the scrollback
1747 * buffer.
1748 *
1749 * Otherwise, this moves the cursor to column zero of the next row.
1750 */
1751hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001752 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1753 this.screen_.rowsArray.length - 1);
1754
1755 if (this.vtScrollBottom_ != null) {
1756 // A VT Scroll region is active, we never append new rows.
1757 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1758 // We're at the end of the VT Scroll Region, perform a VT scroll.
1759 this.vtScrollUp(1);
1760 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1761 } else if (cursorAtEndOfScreen) {
1762 // We're at the end of the screen, the only thing to do is put the
1763 // cursor to column 0.
1764 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1765 } else {
1766 // Anywhere else, advance the cursor row, and reset the column.
1767 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1768 }
1769 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001770 // We're at the end of the screen. Append a new row to the terminal,
1771 // shifting the top row into the scrollback.
1772 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001773 } else {
rginda87b86462011-12-14 13:48:03 -08001774 // Anywhere else in the screen just moves the cursor.
1775 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001776 }
1777};
1778
1779/**
1780 * Like newLine(), except maintain the cursor column.
1781 */
1782hterm.Terminal.prototype.lineFeed = function() {
1783 var column = this.screen_.cursorPosition.column;
1784 this.newLine();
1785 this.setCursorColumn(column);
1786};
1787
1788/**
rginda87b86462011-12-14 13:48:03 -08001789 * If autoCarriageReturn is set then newLine(), else lineFeed().
1790 */
1791hterm.Terminal.prototype.formFeed = function() {
1792 if (this.options_.autoCarriageReturn) {
1793 this.newLine();
1794 } else {
1795 this.lineFeed();
1796 }
1797};
1798
1799/**
1800 * Move the cursor up one row, possibly inserting a blank line.
1801 *
1802 * The cursor column is not changed.
1803 */
1804hterm.Terminal.prototype.reverseLineFeed = function() {
1805 var scrollTop = this.getVTScrollTop();
1806 var currentRow = this.screen_.cursorPosition.row;
1807
1808 if (currentRow == scrollTop) {
1809 this.insertLines(1);
1810 } else {
1811 this.setAbsoluteCursorRow(currentRow - 1);
1812 }
1813};
1814
1815/**
rginda8ba33642011-12-14 12:31:31 -08001816 * Replace all characters to the left of the current cursor with the space
1817 * character.
1818 *
1819 * TODO(rginda): This should probably *remove* the characters (not just replace
1820 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001821 * position.
rginda8ba33642011-12-14 12:31:31 -08001822 */
1823hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001824 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001825 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001826 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001827 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001828};
1829
1830/**
David Benjamin684a9b72012-05-01 17:19:58 -04001831 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001832 *
1833 * The cursor position is unchanged.
1834 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001835 * If the current background color is not the default background color this
1836 * will insert spaces rather than delete. This is unfortunate because the
1837 * trailing space will affect text selection, but it's difficult to come up
1838 * with a way to style empty space that wouldn't trip up the hterm.Screen
1839 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001840 *
1841 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1842 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1843 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05001844 *
1845 * @param {number} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08001846 */
1847hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001848 if (this.screen_.cursorPosition.overflow)
1849 return;
1850
Robert Ginda7fd57082012-09-25 14:41:47 -07001851 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1852 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001853
1854 if (this.screen_.textAttributes.background ===
1855 this.screen_.textAttributes.DEFAULT_COLOR) {
1856 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08001857 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07001858 this.screen_.cursorPosition.column + count) {
1859 this.screen_.deleteChars(count);
1860 this.clearCursorOverflow();
1861 return;
1862 }
1863 }
1864
rginda87b86462011-12-14 13:48:03 -08001865 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001866 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001867 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001868 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001869};
1870
1871/**
1872 * Erase the current line.
1873 *
1874 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001875 */
1876hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001877 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001878 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001879 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001880 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001881};
1882
1883/**
David Benjamina08d78f2012-05-05 00:28:49 -04001884 * Erase all characters from the start of the screen to the current cursor
1885 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001886 *
1887 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001888 */
1889hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001890 var cursor = this.saveCursor();
1891
1892 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001893
David Benjamina08d78f2012-05-05 00:28:49 -04001894 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001895 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001896 this.screen_.clearCursorRow();
1897 }
1898
rginda87b86462011-12-14 13:48:03 -08001899 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001900 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001901};
1902
1903/**
1904 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001905 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001906 *
1907 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001908 */
1909hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001910 var cursor = this.saveCursor();
1911
1912 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001913
David Benjamina08d78f2012-05-05 00:28:49 -04001914 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001915 for (var i = cursor.row + 1; i <= bottom; i++) {
1916 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001917 this.screen_.clearCursorRow();
1918 }
1919
rginda87b86462011-12-14 13:48:03 -08001920 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001921 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001922};
1923
1924/**
1925 * Fill the terminal with a given character.
1926 *
1927 * This methods does not respect the VT scroll region.
1928 *
1929 * @param {string} ch The character to use for the fill.
1930 */
1931hterm.Terminal.prototype.fill = function(ch) {
1932 var cursor = this.saveCursor();
1933
1934 this.setAbsoluteCursorPosition(0, 0);
1935 for (var row = 0; row < this.screenSize.height; row++) {
1936 for (var col = 0; col < this.screenSize.width; col++) {
1937 this.setAbsoluteCursorPosition(row, col);
1938 this.screen_.overwriteString(ch);
1939 }
1940 }
1941
1942 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001943};
1944
1945/**
rginda9ea433c2012-03-16 11:57:00 -07001946 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001947 *
rginda9ea433c2012-03-16 11:57:00 -07001948 * This does not respect the scroll region.
1949 *
1950 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1951 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001952 */
rginda9ea433c2012-03-16 11:57:00 -07001953hterm.Terminal.prototype.clearHome = function(opt_screen) {
1954 var screen = opt_screen || this.screen_;
1955 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001956
rginda11057d52012-04-25 12:29:56 -07001957 if (bottom == 0) {
1958 // Empty screen, nothing to do.
1959 return;
1960 }
1961
rgindae4d29232012-01-19 10:47:13 -08001962 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001963 screen.setCursorPosition(i, 0);
1964 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001965 }
1966
rginda9ea433c2012-03-16 11:57:00 -07001967 screen.setCursorPosition(0, 0);
1968};
1969
1970/**
1971 * Erase the entire display without changing the cursor position.
1972 *
1973 * The cursor position is unchanged. This does not respect the scroll
1974 * region.
1975 *
1976 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1977 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001978 */
1979hterm.Terminal.prototype.clear = function(opt_screen) {
1980 var screen = opt_screen || this.screen_;
1981 var cursor = screen.cursorPosition.clone();
1982 this.clearHome(screen);
1983 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001984};
1985
1986/**
1987 * VT command to insert lines at the current cursor row.
1988 *
1989 * This respects the current scroll region. Rows pushed off the bottom are
1990 * lost (they won't show up in the scrollback buffer).
1991 *
rginda8ba33642011-12-14 12:31:31 -08001992 * @param {integer} count The number of lines to insert.
1993 */
1994hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07001995 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08001996
1997 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07001998 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08001999
Robert Ginda579186b2012-09-26 11:40:04 -07002000 // The moveCount is the number of rows we need to relocate to make room for
2001 // the new row(s). The count is the distance to move them.
2002 var moveCount = bottom - cursorRow - count + 1;
2003 if (moveCount)
2004 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002005
Robert Ginda579186b2012-09-26 11:40:04 -07002006 for (var i = count - 1; i >= 0; i--) {
2007 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002008 this.screen_.clearCursorRow();
2009 }
rginda8ba33642011-12-14 12:31:31 -08002010};
2011
2012/**
2013 * VT command to delete lines at the current cursor row.
2014 *
2015 * New rows are added to the bottom of scroll region to take their place. New
2016 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002017 *
2018 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002019 */
2020hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002021 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002022
rginda87b86462011-12-14 13:48:03 -08002023 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002024 var bottom = this.getVTScrollBottom();
2025
rginda87b86462011-12-14 13:48:03 -08002026 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002027 count = Math.min(count, maxCount);
2028
rginda87b86462011-12-14 13:48:03 -08002029 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002030 if (count != maxCount)
2031 this.moveRows_(top, count, moveStart);
2032
2033 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002034 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002035 this.screen_.clearCursorRow();
2036 }
2037
rginda87b86462011-12-14 13:48:03 -08002038 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002039 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002040};
2041
2042/**
2043 * Inserts the given number of spaces at the current cursor position.
2044 *
rginda87b86462011-12-14 13:48:03 -08002045 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002046 *
2047 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002048 */
2049hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002050 var cursor = this.saveCursor();
2051
rgindacbbd7482012-06-13 15:06:16 -07002052 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08002053 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08002054 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002055
2056 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002057 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002058};
2059
2060/**
2061 * Forward-delete the specified number of characters starting at the cursor
2062 * position.
2063 *
2064 * @param {integer} count The number of characters to delete.
2065 */
2066hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002067 var deleted = this.screen_.deleteChars(count);
2068 if (deleted && !this.screen_.textAttributes.isDefault()) {
2069 var cursor = this.saveCursor();
2070 this.setCursorColumn(this.screenSize.width - deleted);
2071 this.screen_.insertString(lib.f.getWhitespace(deleted));
2072 this.restoreCursor(cursor);
2073 }
2074
David Benjamin54e8bf62012-06-01 22:31:40 -04002075 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002076};
2077
2078/**
2079 * Shift rows in the scroll region upwards by a given number of lines.
2080 *
2081 * New rows are inserted at the bottom of the scroll region to fill the
2082 * vacated rows. The new rows not filled out with the current text attributes.
2083 *
2084 * This function does not affect the scrollback rows at all. Rows shifted
2085 * off the top are lost.
2086 *
rginda87b86462011-12-14 13:48:03 -08002087 * The cursor position is not altered.
2088 *
rginda8ba33642011-12-14 12:31:31 -08002089 * @param {integer} count The number of rows to scroll.
2090 */
2091hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002092 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002093
rginda87b86462011-12-14 13:48:03 -08002094 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002095 this.deleteLines(count);
2096
rginda87b86462011-12-14 13:48:03 -08002097 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002098};
2099
2100/**
2101 * Shift rows below the cursor down by a given number of lines.
2102 *
2103 * This function respects the current scroll region.
2104 *
2105 * New rows are inserted at the top of the scroll region to fill the
2106 * vacated rows. The new rows not filled out with the current text attributes.
2107 *
2108 * This function does not affect the scrollback rows at all. Rows shifted
2109 * off the bottom are lost.
2110 *
2111 * @param {integer} count The number of rows to scroll.
2112 */
2113hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08002114 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002115
rginda87b86462011-12-14 13:48:03 -08002116 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08002117 this.insertLines(opt_count);
2118
rginda87b86462011-12-14 13:48:03 -08002119 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002120};
2121
rginda87b86462011-12-14 13:48:03 -08002122
rginda8ba33642011-12-14 12:31:31 -08002123/**
2124 * Set the cursor position.
2125 *
2126 * The cursor row is relative to the scroll region if the terminal has
2127 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2128 *
2129 * @param {integer} row The new zero-based cursor row.
2130 * @param {integer} row The new zero-based cursor column.
2131 */
2132hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2133 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002134 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002135 } else {
rginda87b86462011-12-14 13:48:03 -08002136 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002137 }
rginda87b86462011-12-14 13:48:03 -08002138};
rginda8ba33642011-12-14 12:31:31 -08002139
Evan Jones2600d4f2016-12-06 09:29:36 -05002140/**
2141 * Move the cursor relative to its current position.
2142 *
2143 * @param {number} row
2144 * @param {number} column
2145 */
rginda87b86462011-12-14 13:48:03 -08002146hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2147 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002148 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2149 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002150 this.screen_.setCursorPosition(row, column);
2151};
2152
Evan Jones2600d4f2016-12-06 09:29:36 -05002153/**
2154 * Move the cursor to the specified position.
2155 *
2156 * @param {number} row
2157 * @param {number} column
2158 */
rginda87b86462011-12-14 13:48:03 -08002159hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002160 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2161 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002162 this.screen_.setCursorPosition(row, column);
2163};
2164
2165/**
2166 * Set the cursor column.
2167 *
2168 * @param {integer} column The new zero-based cursor column.
2169 */
2170hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002171 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002172};
2173
2174/**
2175 * Return the cursor column.
2176 *
2177 * @return {integer} The zero-based cursor column.
2178 */
2179hterm.Terminal.prototype.getCursorColumn = function() {
2180 return this.screen_.cursorPosition.column;
2181};
2182
2183/**
2184 * Set the cursor row.
2185 *
2186 * The cursor row is relative to the scroll region if the terminal has
2187 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2188 *
2189 * @param {integer} row The new cursor row.
2190 */
rginda87b86462011-12-14 13:48:03 -08002191hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2192 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002193};
2194
2195/**
2196 * Return the cursor row.
2197 *
2198 * @return {integer} The zero-based cursor row.
2199 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002200hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002201 return this.screen_.cursorPosition.row;
2202};
2203
2204/**
2205 * Request that the ScrollPort redraw itself soon.
2206 *
2207 * The redraw will happen asynchronously, soon after the call stack winds down.
2208 * Multiple calls will be coalesced into a single redraw.
2209 */
2210hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002211 if (this.timeouts_.redraw)
2212 return;
rginda8ba33642011-12-14 12:31:31 -08002213
2214 var self = this;
rginda87b86462011-12-14 13:48:03 -08002215 this.timeouts_.redraw = setTimeout(function() {
2216 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002217 self.scrollPort_.redraw_();
2218 }, 0);
2219};
2220
2221/**
2222 * Request that the ScrollPort be scrolled to the bottom.
2223 *
2224 * The scroll will happen asynchronously, soon after the call stack winds down.
2225 * Multiple calls will be coalesced into a single scroll.
2226 *
2227 * This affects the scrollbar position of the ScrollPort, and has nothing to
2228 * do with the VT scroll commands.
2229 */
2230hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2231 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002232 return;
rginda8ba33642011-12-14 12:31:31 -08002233
2234 var self = this;
2235 this.timeouts_.scrollDown = setTimeout(function() {
2236 delete self.timeouts_.scrollDown;
2237 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2238 }, 10);
2239};
2240
2241/**
2242 * Move the cursor up a specified number of rows.
2243 *
2244 * @param {integer} count The number of rows to move the cursor.
2245 */
2246hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002247 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002248};
2249
2250/**
2251 * Move the cursor down a specified number of rows.
2252 *
2253 * @param {integer} count The number of rows to move the cursor.
2254 */
2255hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002256 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002257 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2258 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2259 this.screenSize.height - 1);
2260
rgindacbbd7482012-06-13 15:06:16 -07002261 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002262 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002263 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002264};
2265
2266/**
2267 * Move the cursor left a specified number of columns.
2268 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002269 * If reverse wraparound mode is enabled and the previous row wrapped into
2270 * the current row then we back up through the wraparound as well.
2271 *
rginda8ba33642011-12-14 12:31:31 -08002272 * @param {integer} count The number of columns to move the cursor.
2273 */
2274hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002275 count = count || 1;
2276
2277 if (count < 1)
2278 return;
2279
2280 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002281 if (this.options_.reverseWraparound) {
2282 if (this.screen_.cursorPosition.overflow) {
2283 // If this cursor is in the right margin, consume one count to get it
2284 // back to the last column. This only applies when we're in reverse
2285 // wraparound mode.
2286 count--;
2287 this.clearCursorOverflow();
2288
2289 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002290 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002291 }
2292
Robert Gindabfb32622014-07-17 13:20:27 -07002293 var newRow = this.screen_.cursorPosition.row;
2294 var newColumn = currentColumn - count;
2295 if (newColumn < 0) {
2296 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2297 if (newRow < 0) {
2298 // xterm also wraps from row 0 to the last row.
2299 newRow = this.screenSize.height + newRow % this.screenSize.height;
2300 }
2301 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2302 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002303
Robert Gindabfb32622014-07-17 13:20:27 -07002304 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2305
2306 } else {
2307 var newColumn = Math.max(currentColumn - count, 0);
2308 this.setCursorColumn(newColumn);
2309 }
rginda8ba33642011-12-14 12:31:31 -08002310};
2311
2312/**
2313 * Move the cursor right a specified number of columns.
2314 *
2315 * @param {integer} count The number of columns to move the cursor.
2316 */
2317hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002318 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002319
2320 if (count < 1)
2321 return;
2322
rgindacbbd7482012-06-13 15:06:16 -07002323 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002324 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002325 this.setCursorColumn(column);
2326};
2327
2328/**
2329 * Reverse the foreground and background colors of the terminal.
2330 *
2331 * This only affects text that was drawn with no attributes.
2332 *
2333 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2334 * been drawn with attributes that happen to coincide with the default
2335 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002336 *
2337 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002338 */
2339hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002340 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002341 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002342 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2343 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002344 } else {
rginda9f5222b2012-03-05 11:53:28 -08002345 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2346 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002347 }
2348};
2349
2350/**
rginda87b86462011-12-14 13:48:03 -08002351 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002352 *
2353 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002354 */
2355hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002356 this.cursorNode_.style.backgroundColor =
2357 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002358
2359 var self = this;
2360 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08002361 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08002362 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002363
Michael Kelly485ecd12014-06-09 11:41:56 -04002364 // bellSquelchTimeout_ affects both audio and notification bells.
2365 if (this.bellSquelchTimeout_)
2366 return;
2367
Robert Ginda92e18102013-03-14 13:56:37 -07002368 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002369 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002370 this.bellSequelchTimeout_ = setTimeout(function() {
2371 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002372 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002373 } else {
2374 delete this.bellSquelchTimeout_;
2375 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002376
2377 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
2378 var n = new Notification(
2379 lib.f.replaceVars(hterm.desktopNotificationTitle,
Robert Ginda348dc2b2014-06-24 14:42:23 -07002380 {'title': window.document.title || 'hterm'}));
Michael Kelly485ecd12014-06-09 11:41:56 -04002381 this.bellNotificationList_.push(n);
2382 // TODO: Should we try to raise the window here?
2383 n.onclick = function() { self.closeBellNotifications_(); };
2384 }
rginda87b86462011-12-14 13:48:03 -08002385};
2386
2387/**
rginda8ba33642011-12-14 12:31:31 -08002388 * Set the origin mode bit.
2389 *
2390 * If origin mode is on, certain VT cursor and scrolling commands measure their
2391 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2392 * to the top of the addressable screen.
2393 *
2394 * Defaults to off.
2395 *
2396 * @param {boolean} state True to set origin mode, false to unset.
2397 */
2398hterm.Terminal.prototype.setOriginMode = function(state) {
2399 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002400 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002401};
2402
2403/**
2404 * Set the insert mode bit.
2405 *
2406 * If insert mode is on, existing text beyond the cursor position will be
2407 * shifted right to make room for new text. Otherwise, new text overwrites
2408 * any existing text.
2409 *
2410 * Defaults to off.
2411 *
2412 * @param {boolean} state True to set insert mode, false to unset.
2413 */
2414hterm.Terminal.prototype.setInsertMode = function(state) {
2415 this.options_.insertMode = state;
2416};
2417
2418/**
rginda87b86462011-12-14 13:48:03 -08002419 * Set the auto carriage return bit.
2420 *
2421 * If auto carriage return is on then a formfeed character is interpreted
2422 * as a newline, otherwise it's the same as a linefeed. The difference boils
2423 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002424 *
2425 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002426 */
2427hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2428 this.options_.autoCarriageReturn = state;
2429};
2430
2431/**
rginda8ba33642011-12-14 12:31:31 -08002432 * Set the wraparound mode bit.
2433 *
2434 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2435 * to the start of the following row. Otherwise, the cursor is clamped to the
2436 * end of the screen and attempts to write past it are ignored.
2437 *
2438 * Defaults to on.
2439 *
2440 * @param {boolean} state True to set wraparound mode, false to unset.
2441 */
2442hterm.Terminal.prototype.setWraparound = function(state) {
2443 this.options_.wraparound = state;
2444};
2445
2446/**
2447 * Set the reverse-wraparound mode bit.
2448 *
2449 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2450 * to the end of the previous row. Otherwise, the cursor is clamped to column
2451 * 0.
2452 *
2453 * Defaults to off.
2454 *
2455 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2456 */
2457hterm.Terminal.prototype.setReverseWraparound = function(state) {
2458 this.options_.reverseWraparound = state;
2459};
2460
2461/**
2462 * Selects between the primary and alternate screens.
2463 *
2464 * If alternate mode is on, the alternate screen is active. Otherwise the
2465 * primary screen is active.
2466 *
2467 * Swapping screens has no effect on the scrollback buffer.
2468 *
2469 * Each screen maintains its own cursor position.
2470 *
2471 * Defaults to off.
2472 *
2473 * @param {boolean} state True to set alternate mode, false to unset.
2474 */
2475hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002476 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002477 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2478
rginda35c456b2012-02-09 17:29:05 -08002479 if (this.screen_.rowsArray.length &&
2480 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2481 // If the screen changed sizes while we were away, our rowIndexes may
2482 // be incorrect.
2483 var offset = this.scrollbackRows_.length;
2484 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002485 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002486 ary[i].rowIndex = offset + i;
2487 }
2488 }
rginda8ba33642011-12-14 12:31:31 -08002489
rginda35c456b2012-02-09 17:29:05 -08002490 this.realizeWidth_(this.screenSize.width);
2491 this.realizeHeight_(this.screenSize.height);
2492 this.scrollPort_.syncScrollHeight();
2493 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002494
rginda6d397402012-01-17 10:58:29 -08002495 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002496 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002497};
2498
2499/**
2500 * Set the cursor-blink mode bit.
2501 *
2502 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2503 * a visible cursor does not blink.
2504 *
2505 * You should make sure to turn blinking off if you're going to dispose of a
2506 * terminal, otherwise you'll leak a timeout.
2507 *
2508 * Defaults to on.
2509 *
2510 * @param {boolean} state True to set cursor-blink mode, false to unset.
2511 */
2512hterm.Terminal.prototype.setCursorBlink = function(state) {
2513 this.options_.cursorBlink = state;
2514
2515 if (!state && this.timeouts_.cursorBlink) {
2516 clearTimeout(this.timeouts_.cursorBlink);
2517 delete this.timeouts_.cursorBlink;
2518 }
2519
2520 if (this.options_.cursorVisible)
2521 this.setCursorVisible(true);
2522};
2523
2524/**
2525 * Set the cursor-visible mode bit.
2526 *
2527 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2528 *
2529 * Defaults to on.
2530 *
2531 * @param {boolean} state True to set cursor-visible mode, false to unset.
2532 */
2533hterm.Terminal.prototype.setCursorVisible = function(state) {
2534 this.options_.cursorVisible = state;
2535
2536 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002537 if (this.timeouts_.cursorBlink) {
2538 clearTimeout(this.timeouts_.cursorBlink);
2539 delete this.timeouts_.cursorBlink;
2540 }
rginda87b86462011-12-14 13:48:03 -08002541 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002542 return;
2543 }
2544
rginda87b86462011-12-14 13:48:03 -08002545 this.syncCursorPosition_();
2546
2547 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002548
2549 if (this.options_.cursorBlink) {
2550 if (this.timeouts_.cursorBlink)
2551 return;
2552
Robert Gindaea2183e2014-07-17 09:51:51 -07002553 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002554 } else {
2555 if (this.timeouts_.cursorBlink) {
2556 clearTimeout(this.timeouts_.cursorBlink);
2557 delete this.timeouts_.cursorBlink;
2558 }
2559 }
2560};
2561
2562/**
rginda87b86462011-12-14 13:48:03 -08002563 * Synchronizes the visible cursor and document selection with the current
2564 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002565 */
2566hterm.Terminal.prototype.syncCursorPosition_ = function() {
2567 var topRowIndex = this.scrollPort_.getTopRowIndex();
2568 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2569 var cursorRowIndex = this.scrollbackRows_.length +
2570 this.screen_.cursorPosition.row;
2571
2572 if (cursorRowIndex > bottomRowIndex) {
2573 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002574 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002575 return;
2576 }
2577
Robert Gindab837c052014-08-11 11:17:51 -07002578 if (this.options_.cursorVisible &&
2579 this.cursorNode_.style.display == 'none') {
2580 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2581 this.cursorNode_.style.display = '';
2582 }
2583
2584
rginda8ba33642011-12-14 12:31:31 -08002585 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002586 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2587 'px';
2588 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2589 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002590
2591 this.cursorNode_.setAttribute('title',
2592 '(' + this.screen_.cursorPosition.row +
2593 ', ' + this.screen_.cursorPosition.column +
2594 ')');
2595
2596 // Update the caret for a11y purposes.
2597 var selection = this.document_.getSelection();
2598 if (selection && selection.isCollapsed)
2599 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002600};
2601
Robert Gindafb1be6a2013-12-11 11:56:22 -08002602/**
2603 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2604 * and character cell dimensions.
2605 */
Robert Ginda830583c2013-08-07 13:20:46 -07002606hterm.Terminal.prototype.restyleCursor_ = function() {
2607 var shape = this.cursorShape_;
2608
2609 if (this.cursorNode_.getAttribute('focus') == 'false') {
2610 // Always show a block cursor when unfocused.
2611 shape = hterm.Terminal.cursorShape.BLOCK;
2612 }
2613
2614 var style = this.cursorNode_.style;
2615
Robert Gindafb1be6a2013-12-11 11:56:22 -08002616 style.width = this.scrollPort_.characterSize.width + 'px';
2617
Robert Ginda830583c2013-08-07 13:20:46 -07002618 switch (shape) {
2619 case hterm.Terminal.cursorShape.BEAM:
2620 style.height = this.scrollPort_.characterSize.height + 'px';
2621 style.backgroundColor = 'transparent';
2622 style.borderBottomStyle = null;
2623 style.borderLeftStyle = 'solid';
2624 break;
2625
2626 case hterm.Terminal.cursorShape.UNDERLINE:
2627 style.height = this.scrollPort_.characterSize.baseline + 'px';
2628 style.backgroundColor = 'transparent';
2629 style.borderBottomStyle = 'solid';
2630 // correct the size to put it exactly at the baseline
2631 style.borderLeftStyle = null;
2632 break;
2633
2634 default:
2635 style.height = this.scrollPort_.characterSize.height + 'px';
2636 style.backgroundColor = this.cursorColor_;
2637 style.borderBottomStyle = null;
2638 style.borderLeftStyle = null;
2639 break;
2640 }
2641};
2642
rginda8ba33642011-12-14 12:31:31 -08002643/**
2644 * Synchronizes the visible cursor with the current cursor coordinates.
2645 *
2646 * The sync will happen asynchronously, soon after the call stack winds down.
2647 * Multiple calls will be coalesced into a single sync.
2648 */
2649hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2650 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002651 return;
rginda8ba33642011-12-14 12:31:31 -08002652
2653 var self = this;
2654 this.timeouts_.syncCursor = setTimeout(function() {
2655 self.syncCursorPosition_();
2656 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002657 }, 0);
2658};
2659
rgindacc2996c2012-02-24 14:59:31 -08002660/**
rgindaf522ce02012-04-17 17:49:17 -07002661 * Show or hide the zoom warning.
2662 *
2663 * The zoom warning is a message warning the user that their browser zoom must
2664 * be set to 100% in order for hterm to function properly.
2665 *
2666 * @param {boolean} state True to show the message, false to hide it.
2667 */
2668hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2669 if (!this.zoomWarningNode_) {
2670 if (!state)
2671 return;
2672
2673 this.zoomWarningNode_ = this.document_.createElement('div');
2674 this.zoomWarningNode_.style.cssText = (
2675 'color: black;' +
2676 'background-color: #ff2222;' +
2677 'font-size: large;' +
2678 'border-radius: 8px;' +
2679 'opacity: 0.75;' +
2680 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2681 'top: 0.5em;' +
2682 'right: 1.2em;' +
2683 'position: absolute;' +
2684 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002685 '-webkit-user-select: none;' +
2686 '-moz-text-size-adjust: none;' +
2687 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002688
2689 this.zoomWarningNode_.addEventListener('click', function(e) {
2690 this.parentNode.removeChild(this);
2691 });
rgindaf522ce02012-04-17 17:49:17 -07002692 }
2693
Robert Gindab4839c22013-02-28 16:52:10 -08002694 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2695 hterm.zoomWarningMessage,
2696 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2697
rgindaf522ce02012-04-17 17:49:17 -07002698 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2699
2700 if (state) {
2701 if (!this.zoomWarningNode_.parentNode)
2702 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2703 } else if (this.zoomWarningNode_.parentNode) {
2704 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2705 }
2706};
2707
2708/**
rgindacc2996c2012-02-24 14:59:31 -08002709 * Show the terminal overlay for a given amount of time.
2710 *
2711 * The terminal overlay appears in inverse video in a large font, centered
2712 * over the terminal. You should probably keep the overlay message brief,
2713 * since it's in a large font and you probably aren't going to check the size
2714 * of the terminal first.
2715 *
2716 * @param {string} msg The text (not HTML) message to display in the overlay.
2717 * @param {number} opt_timeout The amount of time to wait before fading out
2718 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2719 * stay up forever (or until the next overlay).
2720 */
2721hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002722 if (!this.overlayNode_) {
2723 if (!this.div_)
2724 return;
2725
2726 this.overlayNode_ = this.document_.createElement('div');
2727 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002728 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002729 'font-size: xx-large;' +
2730 'opacity: 0.75;' +
2731 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2732 'position: absolute;' +
2733 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002734 '-webkit-transition: opacity 180ms ease-in;' +
2735 '-moz-user-select: none;' +
2736 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002737
2738 this.overlayNode_.addEventListener('mousedown', function(e) {
2739 e.preventDefault();
2740 e.stopPropagation();
2741 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002742 }
2743
rginda9f5222b2012-03-05 11:53:28 -08002744 this.overlayNode_.style.color = this.prefs_.get('background-color');
2745 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2746 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2747
rgindaf0090c92012-02-10 14:58:52 -08002748 this.overlayNode_.textContent = msg;
2749 this.overlayNode_.style.opacity = '0.75';
2750
2751 if (!this.overlayNode_.parentNode)
2752 this.div_.appendChild(this.overlayNode_);
2753
Robert Ginda97769282013-02-01 15:30:30 -08002754 var divSize = hterm.getClientSize(this.div_);
2755 var overlaySize = hterm.getClientSize(this.overlayNode_);
2756
Robert Ginda8a59f762014-07-23 11:29:55 -07002757 this.overlayNode_.style.top =
2758 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08002759 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07002760 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08002761
2762 var self = this;
2763
2764 if (this.overlayTimeout_)
2765 clearTimeout(this.overlayTimeout_);
2766
rgindacc2996c2012-02-24 14:59:31 -08002767 if (opt_timeout === null)
2768 return;
2769
rgindaf0090c92012-02-10 14:58:52 -08002770 this.overlayTimeout_ = setTimeout(function() {
2771 self.overlayNode_.style.opacity = '0';
Robert Ginda70926e42013-11-25 14:56:36 -08002772 self.overlayTimeout_ = setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002773 if (self.overlayNode_.parentNode)
2774 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002775 self.overlayTimeout_ = null;
2776 self.overlayNode_.style.opacity = '0.75';
2777 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002778 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002779};
2780
rginda4bba5e12012-06-20 16:15:30 -07002781/**
2782 * Paste from the system clipboard to the terminal.
2783 */
2784hterm.Terminal.prototype.paste = function() {
2785 hterm.pasteFromClipboard(this.document_);
2786};
2787
2788/**
2789 * Copy a string to the system clipboard.
2790 *
2791 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05002792 *
2793 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07002794 */
2795hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002796 if (this.prefs_.get('enable-clipboard-notice'))
2797 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2798
rgindaa09e7332012-08-17 12:49:51 -07002799 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002800 copySource.textContent = str;
2801 copySource.style.cssText = (
2802 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002803 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07002804 'position: absolute;' +
2805 'top: -99px');
2806
2807 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002808
rginda4bba5e12012-06-20 16:15:30 -07002809 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002810 var anchorNode = selection.anchorNode;
2811 var anchorOffset = selection.anchorOffset;
2812 var focusNode = selection.focusNode;
2813 var focusOffset = selection.focusOffset;
2814
rginda4bba5e12012-06-20 16:15:30 -07002815 selection.selectAllChildren(copySource);
2816
rgindaa09e7332012-08-17 12:49:51 -07002817 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002818
Rob Spies56953412014-04-28 14:09:47 -07002819 // IE doesn't support selection.extend. This means that the selection
2820 // won't return on IE.
Robert Ginda6f4f0aa2014-07-28 10:21:39 -07002821 if (selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07002822 selection.collapse(anchorNode, anchorOffset);
2823 selection.extend(focusNode, focusOffset);
2824 }
rgindafaa74742012-08-21 13:34:03 -07002825
rginda4bba5e12012-06-20 16:15:30 -07002826 copySource.parentNode.removeChild(copySource);
2827};
2828
Evan Jones2600d4f2016-12-06 09:29:36 -05002829/**
2830 * Returns the selected text, or null if no text is selected.
2831 *
2832 * @return {string|null}
2833 */
rgindaa09e7332012-08-17 12:49:51 -07002834hterm.Terminal.prototype.getSelectionText = function() {
2835 var selection = this.scrollPort_.selection;
2836 selection.sync();
2837
2838 if (selection.isCollapsed)
2839 return null;
2840
2841
2842 // Start offset measures from the beginning of the line.
2843 var startOffset = selection.startOffset;
2844 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002845
Robert Gindafdbb3f22012-09-06 20:23:06 -07002846 if (node.nodeName != 'X-ROW') {
2847 // If the selection doesn't start on an x-row node, then it must be
2848 // somewhere inside the x-row. Add any characters from previous siblings
2849 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002850
2851 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2852 // If node is the text node in a styled span, move up to the span node.
2853 node = node.parentNode;
2854 }
2855
Robert Gindafdbb3f22012-09-06 20:23:06 -07002856 while (node.previousSibling) {
2857 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002858 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002859 }
rgindaa09e7332012-08-17 12:49:51 -07002860 }
2861
2862 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002863 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
2864 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05002865 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002866
Robert Gindafdbb3f22012-09-06 20:23:06 -07002867 if (node.nodeName != 'X-ROW') {
2868 // If the selection doesn't end on an x-row node, then it must be
2869 // somewhere inside the x-row. Add any characters from following siblings
2870 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002871
2872 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2873 // If node is the text node in a styled span, move up to the span node.
2874 node = node.parentNode;
2875 }
2876
Robert Gindafdbb3f22012-09-06 20:23:06 -07002877 while (node.nextSibling) {
2878 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002879 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002880 }
rgindaa09e7332012-08-17 12:49:51 -07002881 }
2882
2883 var rv = this.getRowsText(selection.startRow.rowIndex,
2884 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002885 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002886};
2887
rginda4bba5e12012-06-20 16:15:30 -07002888/**
2889 * Copy the current selection to the system clipboard, then clear it after a
2890 * short delay.
2891 */
2892hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002893 var text = this.getSelectionText();
2894 if (text != null)
2895 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002896};
2897
rgindaf0090c92012-02-10 14:58:52 -08002898hterm.Terminal.prototype.overlaySize = function() {
2899 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2900};
2901
rginda87b86462011-12-14 13:48:03 -08002902/**
2903 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2904 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07002905 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08002906 */
2907hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002908 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002909 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2910
Robert Ginda8cb7d902013-06-20 14:37:18 -07002911 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08002912};
2913
2914/**
Mike Frysinger70b94692017-01-26 18:57:50 -10002915 * Launches url in a new tab.
2916 *
2917 * @param {string} url URL to launch in a new tab.
2918 */
2919hterm.Terminal.prototype.openUrl = function(url) {
2920 var win = window.open(url, '_blank');
2921 win.focus();
2922}
2923
2924/**
2925 * Open the selected url.
2926 */
2927hterm.Terminal.prototype.openSelectedUrl_ = function() {
2928 var str = this.getSelectionText();
2929
2930 // If there is no selection, try and expand wherever they clicked.
2931 if (str == null) {
2932 this.screen_.expandSelection(this.document_.getSelection());
2933 str = this.getSelectionText();
2934 }
2935
2936 // Make sure URL is valid before opening.
2937 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
2938 return;
2939 // If the URL isn't anchored, it'll open relative to the extension.
2940 // We have no way of knowing the correct schema, so assume http.
2941 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0)
2942 str = 'http://' + str;
2943
2944 this.openUrl(str);
2945}
2946
2947
2948/**
rgindad5613292012-06-19 15:40:37 -07002949 * Add the terminalRow and terminalColumn properties to mouse events and
2950 * then forward on to onMouse().
2951 *
2952 * The terminalRow and terminalColumn properties contain the (row, column)
2953 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05002954 *
2955 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07002956 */
2957hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002958 if (e.processedByTerminalHandler_) {
2959 // We register our event handlers on the document, as well as the cursor
2960 // and the scroll blocker. Mouse events that occur on the cursor or
2961 // scroll blocker will also appear on the document, but we don't want to
2962 // process them twice.
2963 //
2964 // We can't just prevent bubbling because that has other side effects, so
2965 // we decorate the event object with this property instead.
2966 return;
2967 }
2968
Robert Ginda6aec7eb2015-06-16 10:31:30 -07002969 var reportMouseEvents = (!this.defeatMouseReports_ &&
2970 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
2971
rgindafaa74742012-08-21 13:34:03 -07002972 e.processedByTerminalHandler_ = true;
2973
Robert Gindaeda48db2014-07-17 09:25:30 -07002974 // One based row/column stored on the mouse event.
2975 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2976 this.scrollPort_.characterSize.height) + 1;
2977 e.terminalColumn = parseInt(e.clientX /
2978 this.scrollPort_.characterSize.width) + 1;
2979
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002980 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
2981 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07002982 return;
2983 }
2984
Robert Ginda6aec7eb2015-06-16 10:31:30 -07002985 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07002986 // If the cursor is visible and we're not sending mouse events to the
2987 // host app, then we want to hide the terminal cursor when the mouse
2988 // cursor is over top. This keeps the terminal cursor from interfering
2989 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07002990 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
2991 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
2992 this.cursorNode_.style.display = 'none';
2993 } else if (this.cursorNode_.style.display == 'none') {
2994 this.cursorNode_.style.display = '';
2995 }
2996 }
rgindad5613292012-06-19 15:40:37 -07002997
Robert Ginda928cf632014-03-05 15:07:41 -08002998 if (e.type == 'mousedown') {
Robert Ginda6aec7eb2015-06-16 10:31:30 -07002999 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003000 // If VT mouse reporting is disabled, or has been defeated with
3001 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003002 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003003 this.setSelectionEnabled(true);
3004 } else {
3005 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003006 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003007 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003008 this.setSelectionEnabled(false);
3009 e.preventDefault();
3010 }
3011 }
3012
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003013 if (!reportMouseEvents) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003014 if (e.type == 'dblclick' && this.copyOnSelect) {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003015 this.screen_.expandSelection(this.document_.getSelection());
Robert Ginda15ed4902016-07-12 10:43:22 -07003016 this.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07003017 }
3018
Mike Frysinger70b94692017-01-26 18:57:50 -10003019 if (e.type == 'click' && !e.shiftKey && e.ctrlKey) {
3020 // Debounce this event with the dblclick event. If you try to doubleclick
3021 // a URL to open it, Chrome will fire click then dblclick, but we won't
3022 // have expanded the selection text at the first click event.
3023 clearTimeout(this.timeouts_.openUrl);
3024 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3025 500);
3026 return;
3027 }
3028
Robert Ginda928cf632014-03-05 15:07:41 -08003029 if (e.type == 'mousedown' && e.which == this.mousePasteButton)
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003030 this.paste();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003031
3032 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
3033 !this.document_.getSelection().isCollapsed) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003034 this.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003035 }
3036
3037 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3038 this.scrollBlockerNode_.engaged) {
3039 // Disengage the scroll-blocker after one of these events.
3040 this.scrollBlockerNode_.engaged = false;
3041 this.scrollBlockerNode_.style.top = '-99px';
3042 }
3043
Robert Ginda928cf632014-03-05 15:07:41 -08003044 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003045 if (!this.scrollBlockerNode_.engaged) {
3046 if (e.type == 'mousedown') {
3047 // Move the scroll-blocker into place if we want to keep the scrollport
3048 // from scrolling.
3049 this.scrollBlockerNode_.engaged = true;
3050 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3051 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3052 } else if (e.type == 'mousemove') {
3053 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3054 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003055 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003056 e.preventDefault();
3057 }
3058 }
Robert Ginda928cf632014-03-05 15:07:41 -08003059
3060 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003061 }
3062
Robert Ginda928cf632014-03-05 15:07:41 -08003063 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3064 // Restore this on mouseup in case it was temporarily defeated with a
3065 // alt-mousedown. Only do this when the selection is empty so that
3066 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003067 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003068 }
rgindad5613292012-06-19 15:40:37 -07003069};
3070
3071/**
3072 * Clients should override this if they care to know about mouse events.
3073 *
3074 * The event parameter will be a normal DOM mouse click event with additional
3075 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003076 *
3077 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003078 */
3079hterm.Terminal.prototype.onMouse = function(e) { };
3080
3081/**
rginda8e92a692012-05-20 19:37:20 -07003082 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003083 *
3084 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003085 */
Rob Spies06533ba2014-04-24 11:20:37 -07003086hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3087 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003088 this.restyleCursor_();
Michael Kelly485ecd12014-06-09 11:41:56 -04003089 if (focused === true)
3090 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003091};
3092
3093/**
rginda8ba33642011-12-14 12:31:31 -08003094 * React when the ScrollPort is scrolled.
3095 */
3096hterm.Terminal.prototype.onScroll_ = function() {
3097 this.scheduleSyncCursorPosition_();
3098};
3099
3100/**
rginda9846e2f2012-01-27 13:53:33 -08003101 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003102 *
3103 * @param {Event} e The DOM paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003104 */
3105hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07003106 var data = e.text.replace(/\n/mg, '\r');
Connor Hegartyf525ceb2014-09-03 13:36:03 -07003107 data = this.keyboard.encode(data);
Robert Gindaa063b202014-07-21 11:08:25 -07003108 if (this.options_.bracketedPaste)
3109 data = '\x1b[200~' + data + '\x1b[201~';
3110
3111 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003112};
3113
3114/**
rgindaa09e7332012-08-17 12:49:51 -07003115 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003116 *
3117 * @param {Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003118 */
3119hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003120 if (!this.useDefaultWindowCopy) {
3121 e.preventDefault();
3122 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3123 }
rgindaa09e7332012-08-17 12:49:51 -07003124};
3125
3126/**
rginda8ba33642011-12-14 12:31:31 -08003127 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003128 *
3129 * Note: This function should not directly contain code that alters the internal
3130 * state of the terminal. That kind of code belongs in realizeWidth or
3131 * realizeHeight, so that it can be executed synchronously in the case of a
3132 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003133 */
3134hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003135 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003136 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003137 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003138 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003139
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003140 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003141 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003142 // gets removed from the document or during the initial load, and we can't
3143 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003144 // This can also happen if called before the scrollPort calculates the
3145 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003146 return;
3147 }
3148
rgindaa8ba17d2012-08-15 14:41:10 -07003149 var isNewSize = (columnCount != this.screenSize.width ||
3150 rowCount != this.screenSize.height);
3151
3152 // We do this even if the size didn't change, just to be sure everything is
3153 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003154 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003155 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003156
3157 if (isNewSize)
3158 this.overlaySize();
3159
Robert Gindafb1be6a2013-12-11 11:56:22 -08003160 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003161 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08003162};
3163
3164/**
3165 * Service the cursor blink timeout.
3166 */
3167hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003168 if (!this.options_.cursorBlink) {
3169 delete this.timeouts_.cursorBlink;
3170 return;
3171 }
3172
Robert Ginda830583c2013-08-07 13:20:46 -07003173 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3174 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003175 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003176 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3177 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003178 } else {
rginda87b86462011-12-14 13:48:03 -08003179 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003180 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3181 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003182 }
3183};
David Reveman8f552492012-03-28 12:18:41 -04003184
3185/**
3186 * Set the scrollbar-visible mode bit.
3187 *
3188 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3189 * Otherwise it will not.
3190 *
3191 * Defaults to on.
3192 *
3193 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3194 */
3195hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3196 this.scrollPort_.setScrollbarVisible(state);
3197};
Michael Kelly485ecd12014-06-09 11:41:56 -04003198
3199/**
Rob Spies49039e52014-12-17 13:40:04 -08003200 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003201 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003202 *
3203 * Defaults to 1.
3204 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003205 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003206 */
3207hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3208 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3209};
3210
3211/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003212 * Close all web notifications created by terminal bells.
3213 */
3214hterm.Terminal.prototype.closeBellNotifications_ = function() {
3215 this.bellNotificationList_.forEach(function(n) {
3216 n.close();
3217 });
3218 this.bellNotificationList_.length = 0;
3219};