blob: d47bc117a747491bd08c1c4477c57161c773d536 [file] [log] [blame]
rginda87b86462011-12-14 13:48:03 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rginda8ba33642011-12-14 12:31:31 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rgindacbbd7482012-06-13 15:06:16 -07005'use strict';
6
Masaya Suzuki273aa982014-05-31 07:25:55 +09007lib.rtdep('lib.colors', 'lib.PreferenceManager', 'lib.resource', 'lib.wc',
Rob Spiesf4e90e82015-01-28 12:10:13 -08008 'lib.f', 'hterm.Keyboard', 'hterm.Options', 'hterm.PreferenceManager',
Ricky Liang48f05cb2013-12-31 23:35:29 +08009 'hterm.Screen', 'hterm.ScrollPort', 'hterm.Size',
10 'hterm.TextAttributes', 'hterm.VT');
rgindacbbd7482012-06-13 15:06:16 -070011
rginda8ba33642011-12-14 12:31:31 -080012/**
13 * Constructor for the Terminal class.
14 *
15 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
16 * classes to provide the complete terminal functionality.
17 *
18 * There are a number of lower-level Terminal methods that can be called
19 * directly to manipulate the cursor, text, scroll region, and other terminal
20 * attributes. However, the primary method is interpret(), which parses VT
21 * escape sequences and invokes the appropriate Terminal methods.
22 *
23 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
24 *
25 * TODO(rginda): Eventually we're going to need to support characters which are
26 * displayed twice as wide as standard latin characters. This is to support
27 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080028 *
Robert Ginda57f03b42012-09-13 11:02:48 -070029 * @param {string} opt_profileId Optional preference profile name. If not
rginda9f5222b2012-03-05 11:53:28 -080030 * provided, defaults to 'default'.
rginda8ba33642011-12-14 12:31:31 -080031 */
Robert Ginda57f03b42012-09-13 11:02:48 -070032hterm.Terminal = function(opt_profileId) {
33 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080034
rginda8ba33642011-12-14 12:31:31 -080035 // Two screen instances.
36 this.primaryScreen_ = new hterm.Screen();
37 this.alternateScreen_ = new hterm.Screen();
38
39 // The "current" screen.
40 this.screen_ = this.primaryScreen_;
41
rginda8ba33642011-12-14 12:31:31 -080042 // The local notion of the screen size. ScreenBuffers also have a size which
43 // indicates their present size. During size changes, the two may disagree.
44 // Also, the inactive screen's size is not altered until it is made the active
45 // screen.
46 this.screenSize = new hterm.Size(0, 0);
47
rginda8ba33642011-12-14 12:31:31 -080048 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080049 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080050 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
51 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080052 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070053 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080054
rginda87b86462011-12-14 13:48:03 -080055 // The div that contains this terminal.
56 this.div_ = null;
57
rgindac9bc5502012-01-18 11:48:44 -080058 // The document that contains the scrollPort. Defaulted to the global
59 // document here so that the terminal is functional even if it hasn't been
60 // inserted into a document yet, but re-set in decorate().
61 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080062
rginda8ba33642011-12-14 12:31:31 -080063 // The rows that have scrolled off screen and are no longer addressable.
64 this.scrollbackRows_ = [];
65
rgindac9bc5502012-01-18 11:48:44 -080066 // Saved tab stops.
67 this.tabStops_ = [];
68
David Benjamin66e954d2012-05-05 21:08:12 -040069 // Keep track of whether default tab stops have been erased; after a TBC
70 // clears all tab stops, defaults aren't restored on resize until a reset.
71 this.defaultTabStops = true;
72
rginda8ba33642011-12-14 12:31:31 -080073 // The VT's notion of the top and bottom rows. Used during some VT
74 // cursor positioning and scrolling commands.
75 this.vtScrollTop_ = null;
76 this.vtScrollBottom_ = null;
77
78 // The DIV element for the visible cursor.
79 this.cursorNode_ = null;
80
Robert Ginda830583c2013-08-07 13:20:46 -070081 // The current cursor shape of the terminal.
82 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
83
84 // The current color of the cursor.
85 this.cursorColor_ = null;
86
Robert Gindaea2183e2014-07-17 09:51:51 -070087 // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded.
88 this.cursorBlinkCycle_ = [100, 100];
89
90 // Pre-bound onCursorBlink_ handler, so we don't have to do this for each
91 // cursor on/off servicing.
92 this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
93
rginda9f5222b2012-03-05 11:53:28 -080094 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070095 // each output and keystroke. They are initialized by the preference manager.
Robert Ginda8cb7d902013-06-20 14:37:18 -070096 this.backgroundColor_ = null;
97 this.foregroundColor_ = null;
Robert Ginda57f03b42012-09-13 11:02:48 -070098 this.scrollOnOutput_ = null;
99 this.scrollOnKeystroke_ = null;
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400100 this.scrollWheelArrowKeys_ = null;
rginda9f5222b2012-03-05 11:53:28 -0800101
Robert Ginda6aec7eb2015-06-16 10:31:30 -0700102 // True if we should override mouse event reporting to allow local selection.
103 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -0800104
rgindaf0090c92012-02-10 14:58:52 -0800105 // Terminal bell sound.
106 this.bellAudio_ = this.document_.createElement('audio');
Mike Frysingerd826f1a2017-07-06 16:20:06 -0400107 this.bellAudio_.id = 'hterm:bell-audio';
rgindaf0090c92012-02-10 14:58:52 -0800108 this.bellAudio_.setAttribute('preload', 'auto');
109
Michael Kelly485ecd12014-06-09 11:41:56 -0400110 // All terminal bell notifications that have been generated (not necessarily
111 // shown).
112 this.bellNotificationList_ = [];
113
114 // Whether we have permission to display notifications.
115 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400116
rginda6d397402012-01-17 10:58:29 -0800117 // Cursor position and attributes saved with DECSC.
118 this.savedOptions_ = {};
119
rginda8ba33642011-12-14 12:31:31 -0800120 // The current mode bits for the terminal.
121 this.options_ = new hterm.Options();
122
123 // Timeouts we might need to clear.
124 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800125
126 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800127 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800128
Zhu Qunying30d40712017-03-14 16:27:00 -0700129 // The keyboard handler.
rgindafeaf3142012-01-31 15:14:20 -0800130 this.keyboard = new hterm.Keyboard(this);
131
rginda87b86462011-12-14 13:48:03 -0800132 // General IO interface that can be given to third parties without exposing
133 // the entire terminal object.
134 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800135
rgindad5613292012-06-19 15:40:37 -0700136 // True if mouse-click-drag should scroll the terminal.
137 this.enableMouseDragScroll = true;
138
Robert Ginda57f03b42012-09-13 11:02:48 -0700139 this.copyOnSelect = null;
Mike Frysinger847577f2017-05-23 23:25:57 -0400140 this.mouseRightClickPaste = null;
rginda4bba5e12012-06-20 16:15:30 -0700141 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700142
Zhu Qunying30d40712017-03-14 16:27:00 -0700143 // Whether to use the default window copy behavior.
Rob Spies0bec09b2014-06-06 15:58:09 -0700144 this.useDefaultWindowCopy = false;
145
146 this.clearSelectionAfterCopy = true;
147
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400148 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800149 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700150
151 this.setProfile(opt_profileId || 'default',
Evan Jones5f9df812016-12-06 09:38:58 -0500152 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800153};
154
155/**
Robert Ginda830583c2013-08-07 13:20:46 -0700156 * Possible cursor shapes.
157 */
158hterm.Terminal.cursorShape = {
159 BLOCK: 'BLOCK',
160 BEAM: 'BEAM',
161 UNDERLINE: 'UNDERLINE'
162};
163
164/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700165 * Clients should override this to be notified when the terminal is ready
166 * for use.
167 *
168 * The terminal initialization is asynchronous, and shouldn't be used before
169 * this method is called.
170 */
171hterm.Terminal.prototype.onTerminalReady = function() { };
172
173/**
rginda35c456b2012-02-09 17:29:05 -0800174 * Default tab with of 8 to match xterm.
175 */
176hterm.Terminal.prototype.tabWidth = 8;
177
178/**
rginda9f5222b2012-03-05 11:53:28 -0800179 * Select a preference profile.
180 *
181 * This will load the terminal preferences for the given profile name and
182 * associate subsequent preference changes with the new preference profile.
183 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500184 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800185 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700186 * @param {function} opt_callback Optional callback to invoke when the profile
187 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800188 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700189hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
190 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800191
Robert Ginda57f03b42012-09-13 11:02:48 -0700192 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800193
Robert Ginda57f03b42012-09-13 11:02:48 -0700194 if (this.prefs_)
195 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800196
Robert Ginda57f03b42012-09-13 11:02:48 -0700197 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
198 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800199 'alt-gr-mode': function(v) {
200 if (v == null) {
201 if (navigator.language.toLowerCase() == 'en-us') {
202 v = 'none';
203 } else {
204 v = 'right-alt';
205 }
206 } else if (typeof v == 'string') {
207 v = v.toLowerCase();
208 } else {
209 v = 'none';
210 }
211
212 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v))
213 v = 'none';
214
215 terminal.keyboard.altGrMode = v;
216 },
217
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700218 'alt-backspace-is-meta-backspace': function(v) {
219 terminal.keyboard.altBackspaceIsMetaBackspace = v;
220 },
221
Robert Ginda57f03b42012-09-13 11:02:48 -0700222 'alt-is-meta': function(v) {
223 terminal.keyboard.altIsMeta = v;
224 },
225
226 'alt-sends-what': function(v) {
227 if (!/^(escape|8-bit|browser-key)$/.test(v))
228 v = 'escape';
229
230 terminal.keyboard.altSendsWhat = v;
231 },
232
233 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800234 var ary = v.match(/^lib-resource:(\S+)/);
235 if (ary) {
236 terminal.bellAudio_.setAttribute('src',
237 lib.resource.getDataUrl(ary[1]));
238 } else {
239 terminal.bellAudio_.setAttribute('src', v);
240 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700241 },
242
Michael Kelly485ecd12014-06-09 11:41:56 -0400243 'desktop-notification-bell': function(v) {
244 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700245 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400246 Notification.permission === 'granted';
247 if (!terminal.desktopNotificationBell_) {
248 // Note: We don't call Notification.requestPermission here because
249 // Chrome requires the call be the result of a user action (such as an
250 // onclick handler), and pref listeners are run asynchronously.
251 //
252 // A way of working around this would be to display a dialog in the
253 // terminal with a "click-to-request-permission" button.
254 console.warn('desktop-notification-bell is true but we do not have ' +
255 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400256 }
257 } else {
258 terminal.desktopNotificationBell_ = false;
259 }
260 },
261
Robert Ginda57f03b42012-09-13 11:02:48 -0700262 'background-color': function(v) {
263 terminal.setBackgroundColor(v);
264 },
265
266 'background-image': function(v) {
267 terminal.scrollPort_.setBackgroundImage(v);
268 },
269
270 'background-size': function(v) {
271 terminal.scrollPort_.setBackgroundSize(v);
272 },
273
274 'background-position': function(v) {
275 terminal.scrollPort_.setBackgroundPosition(v);
276 },
277
278 'backspace-sends-backspace': function(v) {
279 terminal.keyboard.backspaceSendsBackspace = v;
280 },
281
Brad Town18654b62015-03-12 00:27:45 -0700282 'character-map-overrides': function(v) {
283 if (!(v == null || v instanceof Object)) {
284 console.warn('Preference character-map-modifications is not an ' +
285 'object: ' + v);
286 return;
287 }
288
Mike Frysinger095d4062017-06-14 00:29:48 -0700289 terminal.vt.characterMaps.reset();
290 terminal.vt.characterMaps.setOverrides(v);
Brad Town18654b62015-03-12 00:27:45 -0700291 },
292
Robert Ginda57f03b42012-09-13 11:02:48 -0700293 'cursor-blink': function(v) {
294 terminal.setCursorBlink(!!v);
295 },
296
Robert Gindaea2183e2014-07-17 09:51:51 -0700297 'cursor-blink-cycle': function(v) {
298 if (v instanceof Array &&
299 typeof v[0] == 'number' &&
300 typeof v[1] == 'number') {
301 terminal.cursorBlinkCycle_ = v;
302 } else if (typeof v == 'number') {
303 terminal.cursorBlinkCycle_ = [v, v];
304 } else {
305 // Fast blink indicates an error.
306 terminal.cursorBlinkCycle_ = [100, 100];
307 }
308 },
309
Robert Ginda57f03b42012-09-13 11:02:48 -0700310 'cursor-color': function(v) {
311 terminal.setCursorColor(v);
312 },
313
314 'color-palette-overrides': function(v) {
315 if (!(v == null || v instanceof Object || v instanceof Array)) {
316 console.warn('Preference color-palette-overrides is not an array or ' +
317 'object: ' + v);
318 return;
rginda9f5222b2012-03-05 11:53:28 -0800319 }
rginda9f5222b2012-03-05 11:53:28 -0800320
Robert Ginda57f03b42012-09-13 11:02:48 -0700321 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700322
Robert Ginda57f03b42012-09-13 11:02:48 -0700323 if (v) {
324 for (var key in v) {
325 var i = parseInt(key);
326 if (isNaN(i) || i < 0 || i > 255) {
327 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
328 continue;
329 }
330
331 if (v[i]) {
332 var rgb = lib.colors.normalizeCSS(v[i]);
333 if (rgb)
334 lib.colors.colorPalette[i] = rgb;
335 }
336 }
rginda30f20f62012-04-05 16:36:19 -0700337 }
rginda30f20f62012-04-05 16:36:19 -0700338
Evan Jones5f9df812016-12-06 09:38:58 -0500339 terminal.primaryScreen_.textAttributes.resetColorPalette();
Robert Ginda57f03b42012-09-13 11:02:48 -0700340 terminal.alternateScreen_.textAttributes.resetColorPalette();
341 },
rginda30f20f62012-04-05 16:36:19 -0700342
Robert Ginda57f03b42012-09-13 11:02:48 -0700343 'copy-on-select': function(v) {
344 terminal.copyOnSelect = !!v;
345 },
rginda9f5222b2012-03-05 11:53:28 -0800346
Rob Spies0bec09b2014-06-06 15:58:09 -0700347 'use-default-window-copy': function(v) {
348 terminal.useDefaultWindowCopy = !!v;
349 },
350
351 'clear-selection-after-copy': function(v) {
352 terminal.clearSelectionAfterCopy = !!v;
353 },
354
Robert Ginda7e5e9522014-03-14 12:23:58 -0700355 'ctrl-plus-minus-zero-zoom': function(v) {
356 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
357 },
358
Robert Gindafb5a3f92014-05-13 14:12:00 -0700359 'ctrl-c-copy': function(v) {
360 terminal.keyboard.ctrlCCopy = v;
361 },
362
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100363 'ctrl-v-paste': function(v) {
364 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700365 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100366 },
367
Masaya Suzuki273aa982014-05-31 07:25:55 +0900368 'east-asian-ambiguous-as-two-column': function(v) {
369 lib.wc.regardCjkAmbiguous = v;
370 },
371
Robert Ginda57f03b42012-09-13 11:02:48 -0700372 'enable-8-bit-control': function(v) {
373 terminal.vt.enable8BitControl = !!v;
374 },
rginda30f20f62012-04-05 16:36:19 -0700375
Robert Ginda57f03b42012-09-13 11:02:48 -0700376 'enable-bold': function(v) {
377 terminal.syncBoldSafeState();
378 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400379
Robert Ginda3e278d72014-03-25 13:18:51 -0700380 'enable-bold-as-bright': function(v) {
381 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
382 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
383 },
384
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400385 'enable-blink': function(v) {
386 terminal.syncBlinkState();
387 },
388
Robert Ginda57f03b42012-09-13 11:02:48 -0700389 'enable-clipboard-write': function(v) {
390 terminal.vt.enableClipboardWrite = !!v;
391 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400392
Robert Ginda3755e752013-05-31 13:34:09 -0700393 'enable-dec12': function(v) {
394 terminal.vt.enableDec12 = !!v;
395 },
396
Robert Ginda57f03b42012-09-13 11:02:48 -0700397 'font-family': function(v) {
398 terminal.syncFontFamily();
399 },
rginda30f20f62012-04-05 16:36:19 -0700400
Robert Ginda57f03b42012-09-13 11:02:48 -0700401 'font-size': function(v) {
402 terminal.setFontSize(v);
403 },
rginda9875d902012-08-20 16:21:57 -0700404
Robert Ginda57f03b42012-09-13 11:02:48 -0700405 'font-smoothing': function(v) {
406 terminal.syncFontFamily();
407 },
rgindade84e382012-04-20 15:39:31 -0700408
Robert Ginda57f03b42012-09-13 11:02:48 -0700409 'foreground-color': function(v) {
410 terminal.setForegroundColor(v);
411 },
rginda30f20f62012-04-05 16:36:19 -0700412
Robert Ginda57f03b42012-09-13 11:02:48 -0700413 'home-keys-scroll': function(v) {
414 terminal.keyboard.homeKeysScroll = v;
415 },
rginda4bba5e12012-06-20 16:15:30 -0700416
Robert Gindaa8165692015-06-15 14:46:31 -0700417 'keybindings': function(v) {
418 terminal.keyboard.bindings.clear();
419
420 if (!v)
421 return;
422
423 if (!(v instanceof Object)) {
424 console.error('Error in keybindings preference: Expected object');
425 return;
426 }
427
428 try {
429 terminal.keyboard.bindings.addBindings(v);
430 } catch (ex) {
431 console.error('Error in keybindings preference: ' + ex);
432 }
433 },
434
Robert Ginda57f03b42012-09-13 11:02:48 -0700435 'max-string-sequence': function(v) {
436 terminal.vt.maxStringSequence = v;
437 },
rginda11057d52012-04-25 12:29:56 -0700438
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700439 'media-keys-are-fkeys': function(v) {
440 terminal.keyboard.mediaKeysAreFKeys = v;
441 },
442
Robert Ginda57f03b42012-09-13 11:02:48 -0700443 'meta-sends-escape': function(v) {
444 terminal.keyboard.metaSendsEscape = v;
445 },
rginda30f20f62012-04-05 16:36:19 -0700446
Mike Frysinger847577f2017-05-23 23:25:57 -0400447 'mouse-right-click-paste': function(v) {
448 terminal.mouseRightClickPaste = v;
449 },
450
Robert Ginda57f03b42012-09-13 11:02:48 -0700451 'mouse-paste-button': function(v) {
452 terminal.syncMousePasteButton();
453 },
rgindaa8ba17d2012-08-15 14:41:10 -0700454
Robert Gindae76aa9f2014-03-14 12:29:12 -0700455 'page-keys-scroll': function(v) {
456 terminal.keyboard.pageKeysScroll = v;
457 },
458
Robert Ginda40932892012-12-10 17:26:40 -0800459 'pass-alt-number': function(v) {
460 if (v == null) {
461 var osx = window.navigator.userAgent.match(/Mac OS X/);
462
463 // Let Alt-1..9 pass to the browser (to control tab switching) on
464 // non-OS X systems, or if hterm is not opened in an app window.
465 v = (!osx && hterm.windowType != 'popup');
466 }
467
468 terminal.passAltNumber = v;
469 },
470
471 'pass-ctrl-number': function(v) {
472 if (v == null) {
473 var osx = window.navigator.userAgent.match(/Mac OS X/);
474
475 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
476 // non-OS X systems, or if hterm is not opened in an app window.
477 v = (!osx && hterm.windowType != 'popup');
478 }
479
480 terminal.passCtrlNumber = v;
481 },
482
483 'pass-meta-number': function(v) {
484 if (v == null) {
485 var osx = window.navigator.userAgent.match(/Mac OS X/);
486
487 // Let Meta-1..9 pass to the browser (to control tab switching) on
488 // OS X systems, or if hterm is not opened in an app window.
489 v = (osx && hterm.windowType != 'popup');
490 }
491
492 terminal.passMetaNumber = v;
493 },
494
Marius Schilder77857b32014-05-14 16:21:26 -0700495 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700496 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700497 },
498
Robert Ginda8cb7d902013-06-20 14:37:18 -0700499 'receive-encoding': function(v) {
500 if (!(/^(utf-8|raw)$/).test(v)) {
501 console.warn('Invalid value for "receive-encoding": ' + v);
502 v = 'utf-8';
503 }
504
505 terminal.vt.characterEncoding = v;
506 },
507
Robert Ginda57f03b42012-09-13 11:02:48 -0700508 'scroll-on-keystroke': function(v) {
509 terminal.scrollOnKeystroke_ = v;
510 },
rginda9f5222b2012-03-05 11:53:28 -0800511
Robert Ginda57f03b42012-09-13 11:02:48 -0700512 'scroll-on-output': function(v) {
513 terminal.scrollOnOutput_ = v;
514 },
rginda30f20f62012-04-05 16:36:19 -0700515
Robert Ginda57f03b42012-09-13 11:02:48 -0700516 'scrollbar-visible': function(v) {
517 terminal.setScrollbarVisible(v);
518 },
rginda9f5222b2012-03-05 11:53:28 -0800519
Mike Frysinger3c9fa072017-07-13 10:21:13 -0400520 'scroll-wheel-may-send-arrow-keys': function(v) {
521 terminal.scrollWheelArrowKeys_ = v;
522 },
523
Rob Spies49039e52014-12-17 13:40:04 -0800524 'scroll-wheel-move-multiplier': function(v) {
525 terminal.setScrollWheelMoveMultipler(v);
526 },
527
Robert Ginda8cb7d902013-06-20 14:37:18 -0700528 'send-encoding': function(v) {
529 if (!(/^(utf-8|raw)$/).test(v)) {
530 console.warn('Invalid value for "send-encoding": ' + v);
531 v = 'utf-8';
532 }
533
534 terminal.keyboard.characterEncoding = v;
535 },
536
Robert Ginda57f03b42012-09-13 11:02:48 -0700537 'shift-insert-paste': function(v) {
538 terminal.keyboard.shiftInsertPaste = v;
539 },
rginda9f5222b2012-03-05 11:53:28 -0800540
Mike Frysingera7768922017-07-28 15:00:12 -0400541 'terminal-encoding': function(v) {
542 switch (v) {
543 default:
544 console.warn('Invalid value for "terminal-encoding": ' + v);
545 // Fall through.
546 case 'iso-2022':
547 terminal.vt.codingSystemUtf8 = false;
548 terminal.vt.codingSystemLocked = false;
549 break;
550 case 'utf-8-locked':
551 terminal.vt.codingSystemUtf8 = true;
552 terminal.vt.codingSystemLocked = true;
553 break;
554 case 'utf-8':
555 terminal.vt.codingSystemUtf8 = true;
556 terminal.vt.codingSystemLocked = false;
557 break;
558 }
559 },
560
Robert Gindae76aa9f2014-03-14 12:29:12 -0700561 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400562 terminal.scrollPort_.setUserCssUrl(v);
563 },
564
565 'user-css-text': function(v) {
566 terminal.scrollPort_.setUserCssText(v);
567 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400568
569 'word-break-match-left': function(v) {
570 terminal.primaryScreen_.wordBreakMatchLeft = v;
571 terminal.alternateScreen_.wordBreakMatchLeft = v;
572 },
573
574 'word-break-match-right': function(v) {
575 terminal.primaryScreen_.wordBreakMatchRight = v;
576 terminal.alternateScreen_.wordBreakMatchRight = v;
577 },
578
579 'word-break-match-middle': function(v) {
580 terminal.primaryScreen_.wordBreakMatchMiddle = v;
581 terminal.alternateScreen_.wordBreakMatchMiddle = v;
582 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700583 });
rginda30f20f62012-04-05 16:36:19 -0700584
Robert Ginda57f03b42012-09-13 11:02:48 -0700585 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800586 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700587
588 if (opt_callback)
589 opt_callback();
590 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800591};
592
Rob Spies56953412014-04-28 14:09:47 -0700593
594/**
595 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500596 *
597 * @return {hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700598 */
599hterm.Terminal.prototype.getPrefs = function() {
600 return this.prefs_;
601};
602
Robert Gindaa063b202014-07-21 11:08:25 -0700603/**
604 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500605 *
606 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700607 */
608hterm.Terminal.prototype.setBracketedPaste = function(state) {
609 this.options_.bracketedPaste = state;
610};
Rob Spies56953412014-04-28 14:09:47 -0700611
rginda8e92a692012-05-20 19:37:20 -0700612/**
613 * Set the color for the cursor.
614 *
615 * If you want this setting to persist, set it through prefs_, rather than
616 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500617 *
618 * @param {string} color The color to set.
rginda8e92a692012-05-20 19:37:20 -0700619 */
620hterm.Terminal.prototype.setCursorColor = function(color) {
Robert Ginda830583c2013-08-07 13:20:46 -0700621 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700622 this.cursorNode_.style.backgroundColor = color;
623 this.cursorNode_.style.borderColor = color;
624};
625
626/**
627 * Return the current cursor color as a string.
Evan Jones2600d4f2016-12-06 09:29:36 -0500628 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700629 */
630hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700631 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700632};
633
634/**
rgindad5613292012-06-19 15:40:37 -0700635 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500636 *
637 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700638 */
639hterm.Terminal.prototype.setSelectionEnabled = function(state) {
640 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700641};
642
643/**
rginda8e92a692012-05-20 19:37:20 -0700644 * Set the background color.
645 *
646 * If you want this setting to persist, set it through prefs_, rather than
647 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500648 *
649 * @param {string} color The color to set.
rginda8e92a692012-05-20 19:37:20 -0700650 */
651hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700652 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700653 this.primaryScreen_.textAttributes.setDefaults(
654 this.foregroundColor_, this.backgroundColor_);
655 this.alternateScreen_.textAttributes.setDefaults(
656 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700657 this.scrollPort_.setBackgroundColor(color);
658};
659
rginda9f5222b2012-03-05 11:53:28 -0800660/**
661 * Return the current terminal background color.
662 *
663 * Intended for use by other classes, so we don't have to expose the entire
664 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500665 *
666 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800667 */
668hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700669 return this.backgroundColor_;
670};
671
672/**
673 * Set the foreground color.
674 *
675 * If you want this setting to persist, set it through prefs_, rather than
676 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500677 *
678 * @param {string} color The color to set.
rginda8e92a692012-05-20 19:37:20 -0700679 */
680hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700681 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700682 this.primaryScreen_.textAttributes.setDefaults(
683 this.foregroundColor_, this.backgroundColor_);
684 this.alternateScreen_.textAttributes.setDefaults(
685 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700686 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800687};
688
689/**
690 * Return the current terminal foreground color.
691 *
692 * Intended for use by other classes, so we don't have to expose the entire
693 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500694 *
695 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800696 */
697hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700698 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800699};
700
701/**
rginda87b86462011-12-14 13:48:03 -0800702 * Create a new instance of a terminal command and run it with a given
703 * argument string.
704 *
705 * @param {function} commandClass The constructor for a terminal command.
706 * @param {string} argString The argument string to pass to the command.
707 */
708hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700709 var environment = this.prefs_.get('environment');
710 if (typeof environment != 'object' || environment == null)
711 environment = {};
712
rginda87b86462011-12-14 13:48:03 -0800713 var self = this;
714 this.command = new commandClass(
715 { argString: argString || '',
716 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700717 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800718 onExit: function(code) {
719 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800720 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700721 if (self.prefs_.get('close-on-exit'))
722 window.close();
rginda87b86462011-12-14 13:48:03 -0800723 }
724 });
725
rgindafeaf3142012-01-31 15:14:20 -0800726 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800727 this.command.run();
728};
729
730/**
rgindafeaf3142012-01-31 15:14:20 -0800731 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500732 *
733 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800734 */
735hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700736 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800737};
738
739/**
740 * Install the keyboard handler for this terminal.
741 *
742 * This will prevent the browser from seeing any keystrokes sent to the
743 * terminal.
744 */
745hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700746 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
rgindafeaf3142012-01-31 15:14:20 -0800747}
748
749/**
750 * Uninstall the keyboard handler for this terminal.
751 */
752hterm.Terminal.prototype.uninstallKeyboard = function() {
753 this.keyboard.installKeyboard(null);
754}
755
756/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400757 * Set a CSS variable.
758 *
759 * Normally this is used to set variables in the hterm namespace.
760 *
761 * @param {string} name The variable to set.
762 * @param {string} value The value to assign to the variable.
763 * @param {string?} opt_prefix The variable namespace/prefix to use.
764 */
765hterm.Terminal.prototype.setCssVar = function(name, value,
766 opt_prefix='--hterm-') {
767 this.document_.documentElement.style.setProperty(
768 `${opt_prefix}${name}`, value);
769};
770
771/**
rginda35c456b2012-02-09 17:29:05 -0800772 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800773 *
774 * Call setFontSize(0) to reset to the default font size.
775 *
776 * This function does not modify the font-size preference.
777 *
778 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800779 */
780hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800781 if (px === 0)
782 px = this.prefs_.get('font-size');
783
rginda35c456b2012-02-09 17:29:05 -0800784 this.scrollPort_.setFontSize(px);
Mike Frysingercce97c42017-08-05 01:11:22 -0400785 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
786 this.setCssVar('charsize-height',
787 this.scrollPort_.characterSize.height + 'px');
rginda35c456b2012-02-09 17:29:05 -0800788};
789
790/**
791 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500792 *
793 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800794 */
795hterm.Terminal.prototype.getFontSize = function() {
796 return this.scrollPort_.getFontSize();
797};
798
799/**
rginda8e92a692012-05-20 19:37:20 -0700800 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500801 *
802 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700803 */
804hterm.Terminal.prototype.getFontFamily = function() {
805 return this.scrollPort_.getFontFamily();
806};
807
808/**
rginda35c456b2012-02-09 17:29:05 -0800809 * Set the CSS "font-family" for this terminal.
810 */
rginda9f5222b2012-03-05 11:53:28 -0800811hterm.Terminal.prototype.syncFontFamily = function() {
812 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
813 this.prefs_.get('font-smoothing'));
814 this.syncBoldSafeState();
815};
816
rginda4bba5e12012-06-20 16:15:30 -0700817/**
818 * Set this.mousePasteButton based on the mouse-paste-button pref,
819 * autodetecting if necessary.
820 */
821hterm.Terminal.prototype.syncMousePasteButton = function() {
822 var button = this.prefs_.get('mouse-paste-button');
823 if (typeof button == 'number') {
824 this.mousePasteButton = button;
825 return;
826 }
827
828 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
Mike Frysinger98dd15b2017-05-18 22:52:23 -0400829 if (!ary || ary[1] == 'CrOS') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400830 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700831 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400832 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700833 }
834};
835
836/**
837 * Enable or disable bold based on the enable-bold pref, autodetecting if
838 * necessary.
839 */
rginda9f5222b2012-03-05 11:53:28 -0800840hterm.Terminal.prototype.syncBoldSafeState = function() {
841 var enableBold = this.prefs_.get('enable-bold');
842 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700843 this.primaryScreen_.textAttributes.enableBold = enableBold;
844 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800845 return;
846 }
847
rgindaf7521392012-02-28 17:20:34 -0800848 var normalSize = this.scrollPort_.measureCharacterSize();
849 var boldSize = this.scrollPort_.measureCharacterSize('bold');
850
851 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800852 if (!isBoldSafe) {
853 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700854 'from normal. Font family is: ' +
855 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800856 }
rginda9f5222b2012-03-05 11:53:28 -0800857
Robert Gindaed016262012-10-26 16:27:09 -0700858 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
859 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800860};
861
862/**
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400863 * Enable or disable blink based on the enable-blink pref.
864 */
865hterm.Terminal.prototype.syncBlinkState = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400866 this.setCssVar('node-duration',
867 this.prefs_.get('enable-blink') ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400868};
869
870/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400871 * Set the mouse cursor style based on the current terminal mode.
872 */
873hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400874 this.setCssVar('mouse-cursor-style',
875 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
876 'var(--hterm-mouse-cursor-text)' :
877 'var(--hterm-mouse-cursor-pointer)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400878};
879
880/**
rginda87b86462011-12-14 13:48:03 -0800881 * Return a copy of the current cursor position.
882 *
883 * @return {hterm.RowCol} The RowCol object representing the current position.
884 */
885hterm.Terminal.prototype.saveCursor = function() {
886 return this.screen_.cursorPosition.clone();
887};
888
Evan Jones2600d4f2016-12-06 09:29:36 -0500889/**
890 * Return the current text attributes.
891 *
892 * @return {string}
893 */
rgindaa19afe22012-01-25 15:40:22 -0800894hterm.Terminal.prototype.getTextAttributes = function() {
895 return this.screen_.textAttributes;
896};
897
Evan Jones2600d4f2016-12-06 09:29:36 -0500898/**
899 * Set the text attributes.
900 *
901 * @param {string} textAttributes The attributes to set.
902 */
rginda1a09aa02012-06-18 21:11:25 -0700903hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
904 this.screen_.textAttributes = textAttributes;
905};
906
rginda87b86462011-12-14 13:48:03 -0800907/**
rgindaf522ce02012-04-17 17:49:17 -0700908 * Return the current browser zoom factor applied to the terminal.
909 *
910 * @return {number} The current browser zoom factor.
911 */
912hterm.Terminal.prototype.getZoomFactor = function() {
913 return this.scrollPort_.characterSize.zoomFactor;
914};
915
916/**
rginda9846e2f2012-01-27 13:53:33 -0800917 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500918 *
919 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800920 */
921hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800922 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800923};
924
925/**
rginda87b86462011-12-14 13:48:03 -0800926 * Restore a previously saved cursor position.
927 *
928 * @param {hterm.RowCol} cursor The position to restore.
929 */
930hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700931 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
932 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800933 this.screen_.setCursorPosition(row, column);
934 if (cursor.column > column ||
935 cursor.column == column && cursor.overflow) {
936 this.screen_.cursorPosition.overflow = true;
937 }
rginda87b86462011-12-14 13:48:03 -0800938};
939
940/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400941 * Clear the cursor's overflow flag.
942 */
943hterm.Terminal.prototype.clearCursorOverflow = function() {
944 this.screen_.cursorPosition.overflow = false;
945};
946
947/**
Robert Ginda830583c2013-08-07 13:20:46 -0700948 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -0500949 *
950 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -0700951 */
952hterm.Terminal.prototype.setCursorShape = function(shape) {
953 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -0800954 this.restyleCursor_();
Robert Ginda830583c2013-08-07 13:20:46 -0700955}
956
957/**
958 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -0500959 *
960 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -0700961 */
962hterm.Terminal.prototype.getCursorShape = function() {
963 return this.cursorShape_;
964}
965
966/**
rginda87b86462011-12-14 13:48:03 -0800967 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -0500968 *
969 * @param {number} columnCount
rginda87b86462011-12-14 13:48:03 -0800970 */
971hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800972 if (columnCount == null) {
973 this.div_.style.width = '100%';
974 return;
975 }
976
Robert Ginda26806d12014-07-24 13:44:07 -0700977 this.div_.style.width = Math.ceil(
978 this.scrollPort_.characterSize.width *
979 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400980 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800981 this.scheduleSyncCursorPosition_();
982};
rginda87b86462011-12-14 13:48:03 -0800983
rgindac9bc5502012-01-18 11:48:44 -0800984/**
rginda35c456b2012-02-09 17:29:05 -0800985 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -0500986 *
987 * @param {number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -0800988 */
989hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800990 if (rowCount == null) {
991 this.div_.style.height = '100%';
992 return;
993 }
994
rginda35c456b2012-02-09 17:29:05 -0800995 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700996 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800997 this.realizeSize_(this.screenSize.width, rowCount);
998 this.scheduleSyncCursorPosition_();
999};
1000
1001/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001002 * Deal with terminal size changes.
1003 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001004 * @param {number} columnCount The number of columns.
1005 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001006 */
1007hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
1008 if (columnCount != this.screenSize.width)
1009 this.realizeWidth_(columnCount);
1010
1011 if (rowCount != this.screenSize.height)
1012 this.realizeHeight_(rowCount);
1013
1014 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -07001015 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001016};
1017
1018/**
rgindac9bc5502012-01-18 11:48:44 -08001019 * Deal with terminal width changes.
1020 *
1021 * This function does what needs to be done when the terminal width changes
1022 * out from under us. It happens here rather than in onResize_() because this
1023 * code may need to run synchronously to handle programmatic changes of
1024 * terminal width.
1025 *
1026 * Relying on the browser to send us an async resize event means we may not be
1027 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001028 *
1029 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001030 */
1031hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001032 if (columnCount <= 0)
1033 throw new Error('Attempt to realize bad width: ' + columnCount);
1034
rgindac9bc5502012-01-18 11:48:44 -08001035 var deltaColumns = columnCount - this.screen_.getWidth();
1036
rginda87b86462011-12-14 13:48:03 -08001037 this.screenSize.width = columnCount;
1038 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001039
1040 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -04001041 if (this.defaultTabStops)
1042 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -08001043 } else {
1044 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -04001045 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -08001046 break;
1047
1048 this.tabStops_.pop();
1049 }
1050 }
1051
1052 this.screen_.setColumnCount(this.screenSize.width);
1053};
1054
1055/**
1056 * Deal with terminal height changes.
1057 *
1058 * This function does what needs to be done when the terminal height changes
1059 * out from under us. It happens here rather than in onResize_() because this
1060 * code may need to run synchronously to handle programmatic changes of
1061 * terminal height.
1062 *
1063 * Relying on the browser to send us an async resize event means we may not be
1064 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001065 *
1066 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001067 */
1068hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001069 if (rowCount <= 0)
1070 throw new Error('Attempt to realize bad height: ' + rowCount);
1071
rgindac9bc5502012-01-18 11:48:44 -08001072 var deltaRows = rowCount - this.screen_.getHeight();
1073
1074 this.screenSize.height = rowCount;
1075
1076 var cursor = this.saveCursor();
1077
1078 if (deltaRows < 0) {
1079 // Screen got smaller.
1080 deltaRows *= -1;
1081 while (deltaRows) {
1082 var lastRow = this.getRowCount() - 1;
1083 if (lastRow - this.scrollbackRows_.length == cursor.row)
1084 break;
1085
1086 if (this.getRowText(lastRow))
1087 break;
1088
1089 this.screen_.popRow();
1090 deltaRows--;
1091 }
1092
1093 var ary = this.screen_.shiftRows(deltaRows);
1094 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1095
1096 // We just removed rows from the top of the screen, we need to update
1097 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001098 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001099 } else if (deltaRows > 0) {
1100 // Screen got larger.
1101
1102 if (deltaRows <= this.scrollbackRows_.length) {
1103 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1104 var rows = this.scrollbackRows_.splice(
1105 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1106 this.screen_.unshiftRows(rows);
1107 deltaRows -= scrollbackCount;
1108 cursor.row += scrollbackCount;
1109 }
1110
1111 if (deltaRows)
1112 this.appendRows_(deltaRows);
1113 }
1114
rginda35c456b2012-02-09 17:29:05 -08001115 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001116 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001117};
1118
1119/**
1120 * Scroll the terminal to the top of the scrollback buffer.
1121 */
1122hterm.Terminal.prototype.scrollHome = function() {
1123 this.scrollPort_.scrollRowToTop(0);
1124};
1125
1126/**
1127 * Scroll the terminal to the end.
1128 */
1129hterm.Terminal.prototype.scrollEnd = function() {
1130 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1131};
1132
1133/**
1134 * Scroll the terminal one page up (minus one line) relative to the current
1135 * position.
1136 */
1137hterm.Terminal.prototype.scrollPageUp = function() {
1138 var i = this.scrollPort_.getTopRowIndex();
1139 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
1140};
1141
1142/**
1143 * Scroll the terminal one page down (minus one line) relative to the current
1144 * position.
1145 */
1146hterm.Terminal.prototype.scrollPageDown = function() {
1147 var i = this.scrollPort_.getTopRowIndex();
1148 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -08001149};
1150
rgindac9bc5502012-01-18 11:48:44 -08001151/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001152 * Scroll the terminal one line up relative to the current position.
1153 */
1154hterm.Terminal.prototype.scrollLineUp = function() {
1155 var i = this.scrollPort_.getTopRowIndex();
1156 this.scrollPort_.scrollRowToTop(i - 1);
1157};
1158
1159/**
1160 * Scroll the terminal one line down relative to the current position.
1161 */
1162hterm.Terminal.prototype.scrollLineDown = function() {
1163 var i = this.scrollPort_.getTopRowIndex();
1164 this.scrollPort_.scrollRowToTop(i + 1);
1165};
1166
1167/**
Robert Ginda40932892012-12-10 17:26:40 -08001168 * Clear primary screen, secondary screen, and the scrollback buffer.
1169 */
1170hterm.Terminal.prototype.wipeContents = function() {
1171 this.scrollbackRows_.length = 0;
1172 this.scrollPort_.resetCache();
1173
1174 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
1175 var bottom = screen.getHeight();
1176 if (bottom > 0) {
1177 this.renumberRows_(0, bottom);
1178 this.clearHome(screen);
1179 }
1180 }.bind(this));
1181
1182 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001183 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001184};
1185
1186/**
rgindac9bc5502012-01-18 11:48:44 -08001187 * Full terminal reset.
1188 */
rginda87b86462011-12-14 13:48:03 -08001189hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -08001190 this.clearAllTabStops();
1191 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001192
1193 this.clearHome(this.primaryScreen_);
1194 this.primaryScreen_.textAttributes.reset();
1195
1196 this.clearHome(this.alternateScreen_);
1197 this.alternateScreen_.textAttributes.reset();
1198
rgindab8bc8932012-04-27 12:45:03 -07001199 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1200
Robert Ginda92e18102013-03-14 13:56:37 -07001201 this.vt.reset();
1202
rgindac9bc5502012-01-18 11:48:44 -08001203 this.softReset();
rginda87b86462011-12-14 13:48:03 -08001204};
1205
rgindac9bc5502012-01-18 11:48:44 -08001206/**
1207 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001208 *
1209 * Perform a soft reset to the default values listed in
1210 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001211 */
rginda0f5c0292012-01-13 11:00:13 -08001212hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -07001213 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001214 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001215
Brad Townb62dfdc2015-03-16 19:07:15 -07001216 // We show the cursor on soft reset but do not alter the blink state.
1217 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1218
rgindab8bc8932012-04-27 12:45:03 -07001219 // Xterm also resets the color palette on soft reset, even though it doesn't
1220 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -07001221 this.primaryScreen_.textAttributes.resetColorPalette();
1222 this.alternateScreen_.textAttributes.resetColorPalette();
1223
rgindab8bc8932012-04-27 12:45:03 -07001224 // The xterm man page explicitly says this will happen on soft reset.
1225 this.setVTScrollRegion(null, null);
1226
1227 // Xterm also shows the cursor on soft reset, but does not alter the blink
1228 // state.
rgindaa19afe22012-01-25 15:40:22 -08001229 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001230};
1231
rgindac9bc5502012-01-18 11:48:44 -08001232/**
1233 * Move the cursor forward to the next tab stop, or to the last column
1234 * if no more tab stops are set.
1235 */
1236hterm.Terminal.prototype.forwardTabStop = function() {
1237 var column = this.screen_.cursorPosition.column;
1238
1239 for (var i = 0; i < this.tabStops_.length; i++) {
1240 if (this.tabStops_[i] > column) {
1241 this.setCursorColumn(this.tabStops_[i]);
1242 return;
1243 }
1244 }
1245
David Benjamin66e954d2012-05-05 21:08:12 -04001246 // xterm does not clear the overflow flag on HT or CHT.
1247 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001248 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001249 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001250};
1251
rgindac9bc5502012-01-18 11:48:44 -08001252/**
1253 * Move the cursor backward to the previous tab stop, or to the first column
1254 * if no previous tab stops are set.
1255 */
1256hterm.Terminal.prototype.backwardTabStop = function() {
1257 var column = this.screen_.cursorPosition.column;
1258
1259 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1260 if (this.tabStops_[i] < column) {
1261 this.setCursorColumn(this.tabStops_[i]);
1262 return;
1263 }
1264 }
1265
1266 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001267};
1268
rgindac9bc5502012-01-18 11:48:44 -08001269/**
1270 * Set a tab stop at the given column.
1271 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001272 * @param {integer} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001273 */
1274hterm.Terminal.prototype.setTabStop = function(column) {
1275 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1276 if (this.tabStops_[i] == column)
1277 return;
1278
1279 if (this.tabStops_[i] < column) {
1280 this.tabStops_.splice(i + 1, 0, column);
1281 return;
1282 }
1283 }
1284
1285 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001286};
1287
rgindac9bc5502012-01-18 11:48:44 -08001288/**
1289 * Clear the tab stop at the current cursor position.
1290 *
1291 * No effect if there is no tab stop at the current cursor position.
1292 */
1293hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1294 var column = this.screen_.cursorPosition.column;
1295
1296 var i = this.tabStops_.indexOf(column);
1297 if (i == -1)
1298 return;
1299
1300 this.tabStops_.splice(i, 1);
1301};
1302
1303/**
1304 * Clear all tab stops.
1305 */
1306hterm.Terminal.prototype.clearAllTabStops = function() {
1307 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001308 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001309};
1310
1311/**
1312 * Set up the default tab stops, starting from a given column.
1313 *
1314 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001315 * from the specified column, or 0 if no column is provided. It also flags
1316 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001317 *
1318 * This does not clear the existing tab stops first, use clearAllTabStops
1319 * for that.
1320 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001321 * @param {integer} opt_start Optional starting zero based starting column, useful
rgindac9bc5502012-01-18 11:48:44 -08001322 * for filling out missing tab stops when the terminal is resized.
1323 */
1324hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1325 var start = opt_start || 0;
1326 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001327 // Round start up to a default tab stop.
1328 start = start - 1 - ((start - 1) % w) + w;
1329 for (var i = start; i < this.screenSize.width; i += w) {
1330 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001331 }
David Benjamin66e954d2012-05-05 21:08:12 -04001332
1333 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001334};
1335
rginda6d397402012-01-17 10:58:29 -08001336/**
rginda8ba33642011-12-14 12:31:31 -08001337 * Interpret a sequence of characters.
1338 *
1339 * Incomplete escape sequences are buffered until the next call.
1340 *
1341 * @param {string} str Sequence of characters to interpret or pass through.
1342 */
1343hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001344 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001345 this.scheduleSyncCursorPosition_();
1346};
1347
1348/**
1349 * Take over the given DIV for use as the terminal display.
1350 *
1351 * @param {HTMLDivElement} div The div to use as the terminal display.
1352 */
1353hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001354 this.div_ = div;
1355
rginda8ba33642011-12-14 12:31:31 -08001356 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001357 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001358 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1359 this.scrollPort_.setBackgroundPosition(
1360 this.prefs_.get('background-position'));
Mike Frysinger08bad432017-04-24 00:50:54 -04001361 this.scrollPort_.setUserCssUrl(this.prefs_.get('user-css'));
1362 this.scrollPort_.setUserCssText(this.prefs_.get('user-css-text'));
rginda30f20f62012-04-05 16:36:19 -07001363
rginda0918b652012-04-04 11:26:24 -07001364 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001365
rginda9f5222b2012-03-05 11:53:28 -08001366 this.setFontSize(this.prefs_.get('font-size'));
1367 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001368
David Reveman8f552492012-03-28 12:18:41 -04001369 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001370 this.setScrollWheelMoveMultipler(
1371 this.prefs_.get('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001372
rginda8ba33642011-12-14 12:31:31 -08001373 this.document_ = this.scrollPort_.getDocument();
1374
Evan Jones5f9df812016-12-06 09:38:58 -05001375 this.document_.body.oncontextmenu = function() { return false; };
rginda4bba5e12012-06-20 16:15:30 -07001376
1377 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001378 var screenNode = this.scrollPort_.getScreenNode();
1379 screenNode.addEventListener('mousedown', onMouse);
1380 screenNode.addEventListener('mouseup', onMouse);
1381 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001382 this.scrollPort_.onScrollWheel = onMouse;
1383
Toni Barzic0bfa8922013-11-22 11:18:35 -08001384 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001385 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001386 // Listen for mousedown events on the screenNode as in FF the focus
1387 // events don't bubble.
1388 screenNode.addEventListener('mousedown', function() {
1389 setTimeout(this.onFocusChange_.bind(this, true));
1390 }.bind(this));
1391
Toni Barzic0bfa8922013-11-22 11:18:35 -08001392 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001393 'blur', this.onFocusChange_.bind(this, false));
1394
1395 var style = this.document_.createElement('style');
1396 style.textContent =
1397 ('.cursor-node[focus="false"] {' +
1398 ' box-sizing: border-box;' +
1399 ' background-color: transparent !important;' +
1400 ' border-width: 2px;' +
1401 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001402 '}' +
1403 '.wc-node {' +
1404 ' display: inline-block;' +
1405 ' text-align: center;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001406 ' width: calc(var(--hterm-charsize-width) * 2);' +
Mike Frysinger4036f6e2017-05-31 14:02:55 -04001407 ' line-height: var(--hterm-charsize-height);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001408 '}' +
1409 ':root {' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001410 ' --hterm-charsize-width: ' + this.scrollPort_.characterSize.width + 'px;' +
1411 ' --hterm-charsize-height: ' + this.scrollPort_.characterSize.height + 'px;' +
Mike Frysinger44c32202017-08-05 01:13:09 -04001412 ' --hterm-cursor-offset-col: 0;' +
1413 ' --hterm-cursor-offset-row: 0;' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001414 ' --hterm-blink-node-duration: 0.7s;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001415 ' --hterm-mouse-cursor-text: text;' +
1416 ' --hterm-mouse-cursor-pointer: default;' +
1417 ' --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001418 '}' +
1419 '@keyframes blink {' +
1420 ' from { opacity: 1.0; }' +
1421 ' to { opacity: 0.0; }' +
1422 '}' +
1423 '.blink-node {' +
1424 ' animation-name: blink;' +
1425 ' animation-duration: var(--hterm-blink-node-duration);' +
1426 ' animation-iteration-count: infinite;' +
1427 ' animation-timing-function: ease-in-out;' +
1428 ' animation-direction: alternate;' +
rginda8e92a692012-05-20 19:37:20 -07001429 '}');
1430 this.document_.head.appendChild(style);
1431
rginda8ba33642011-12-14 12:31:31 -08001432 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001433 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001434 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001435 this.cursorNode_.style.cssText =
1436 ('position: absolute;' +
Mike Frysinger44c32202017-08-05 01:13:09 -04001437 'left: calc(var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));' +
1438 'top: calc(var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));' +
rginda87b86462011-12-14 13:48:03 -08001439 'display: block;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001440 'width: var(--hterm-charsize-width);' +
1441 'height: var(--hterm-charsize-height);' +
Rob Spies06533ba2014-04-24 11:20:37 -07001442 '-webkit-transition: opacity, background-color 100ms linear;' +
1443 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001444
rginda8e92a692012-05-20 19:37:20 -07001445 this.setCursorColor(this.prefs_.get('cursor-color'));
Robert Gindafb1be6a2013-12-11 11:56:22 -08001446 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1447 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001448
rginda8ba33642011-12-14 12:31:31 -08001449 this.document_.body.appendChild(this.cursorNode_);
1450
rgindad5613292012-06-19 15:40:37 -07001451 // When 'enableMouseDragScroll' is off we reposition this element directly
1452 // under the mouse cursor after a click. This makes Chrome associate
1453 // subsequent mousemove events with the scroll-blocker. Since the
1454 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1455 // events do not cause the scrollport to scroll.
1456 //
1457 // It's a hack, but it's the cleanest way I could find.
1458 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001459 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
rgindad5613292012-06-19 15:40:37 -07001460 this.scrollBlockerNode_.style.cssText =
1461 ('position: absolute;' +
1462 'top: -99px;' +
1463 'display: block;' +
1464 'width: 10px;' +
1465 'height: 10px;');
1466 this.document_.body.appendChild(this.scrollBlockerNode_);
1467
rgindad5613292012-06-19 15:40:37 -07001468 this.scrollPort_.onScrollWheel = onMouse;
1469 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1470 ].forEach(function(event) {
1471 this.scrollBlockerNode_.addEventListener(event, onMouse);
1472 this.cursorNode_.addEventListener(event, onMouse);
1473 this.document_.addEventListener(event, onMouse);
1474 }.bind(this));
1475
1476 this.cursorNode_.addEventListener('mousedown', function() {
1477 setTimeout(this.focus.bind(this));
1478 }.bind(this));
1479
rginda8ba33642011-12-14 12:31:31 -08001480 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001481
rginda87b86462011-12-14 13:48:03 -08001482 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001483 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001484};
1485
rginda0918b652012-04-04 11:26:24 -07001486/**
1487 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001488 *
1489 * @return {HTMLDocument}
rginda0918b652012-04-04 11:26:24 -07001490 */
rginda87b86462011-12-14 13:48:03 -08001491hterm.Terminal.prototype.getDocument = function() {
1492 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001493};
1494
1495/**
rginda0918b652012-04-04 11:26:24 -07001496 * Focus the terminal.
1497 */
1498hterm.Terminal.prototype.focus = function() {
1499 this.scrollPort_.focus();
1500};
1501
1502/**
rginda8ba33642011-12-14 12:31:31 -08001503 * Return the HTML Element for a given row index.
1504 *
1505 * This is a method from the RowProvider interface. The ScrollPort uses
1506 * it to fetch rows on demand as they are scrolled into view.
1507 *
1508 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1509 * pairs to conserve memory.
1510 *
1511 * @param {integer} index The zero-based row index, measured relative to the
1512 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001513 * largest indices.
rginda8ba33642011-12-14 12:31:31 -08001514 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1515 */
1516hterm.Terminal.prototype.getRowNode = function(index) {
1517 if (index < this.scrollbackRows_.length)
1518 return this.scrollbackRows_[index];
1519
1520 var screenIndex = index - this.scrollbackRows_.length;
1521 return this.screen_.rowsArray[screenIndex];
1522};
1523
1524/**
1525 * Return the text content for a given range of rows.
1526 *
1527 * This is a method from the RowProvider interface. The ScrollPort uses
1528 * it to fetch text content on demand when the user attempts to copy their
1529 * selection to the clipboard.
1530 *
1531 * @param {integer} start The zero-based row index to start from, measured
1532 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001533 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001534 * @param {integer} end The zero-based row index to end on, measured
1535 * relative to the start of the scrollback buffer.
1536 * @return {string} A single string containing the text value of the range of
1537 * rows. Lines will be newline delimited, with no trailing newline.
1538 */
1539hterm.Terminal.prototype.getRowsText = function(start, end) {
1540 var ary = [];
1541 for (var i = start; i < end; i++) {
1542 var node = this.getRowNode(i);
1543 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001544 if (i < end - 1 && !node.getAttribute('line-overflow'))
1545 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001546 }
1547
rgindaa09e7332012-08-17 12:49:51 -07001548 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001549};
1550
1551/**
1552 * Return the text content for a given row.
1553 *
1554 * This is a method from the RowProvider interface. The ScrollPort uses
1555 * it to fetch text content on demand when the user attempts to copy their
1556 * selection to the clipboard.
1557 *
1558 * @param {integer} index The zero-based row index to return, measured
1559 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001560 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001561 * @return {string} A string containing the text value of the selected row.
1562 */
1563hterm.Terminal.prototype.getRowText = function(index) {
1564 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001565 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001566};
1567
1568/**
1569 * Return the total number of rows in the addressable screen and in the
1570 * scrollback buffer of this terminal.
1571 *
1572 * This is a method from the RowProvider interface. The ScrollPort uses
1573 * it to compute the size of the scrollbar.
1574 *
1575 * @return {integer} The number of rows in this terminal.
1576 */
1577hterm.Terminal.prototype.getRowCount = function() {
1578 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1579};
1580
1581/**
1582 * Create DOM nodes for new rows and append them to the end of the terminal.
1583 *
1584 * This is the only correct way to add a new DOM node for a row. Notice that
1585 * the new row is appended to the bottom of the list of rows, and does not
1586 * require renumbering (of the rowIndex property) of previous rows.
1587 *
1588 * If you think you want a new blank row somewhere in the middle of the
1589 * terminal, look into moveRows_().
1590 *
1591 * This method does not pay attention to vtScrollTop/Bottom, since you should
1592 * be using moveRows() in cases where they would matter.
1593 *
1594 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001595 *
1596 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001597 */
1598hterm.Terminal.prototype.appendRows_ = function(count) {
1599 var cursorRow = this.screen_.rowsArray.length;
1600 var offset = this.scrollbackRows_.length + cursorRow;
1601 for (var i = 0; i < count; i++) {
1602 var row = this.document_.createElement('x-row');
1603 row.appendChild(this.document_.createTextNode(''));
1604 row.rowIndex = offset + i;
1605 this.screen_.pushRow(row);
1606 }
1607
1608 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1609 if (extraRows > 0) {
1610 var ary = this.screen_.shiftRows(extraRows);
1611 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001612 if (this.scrollPort_.isScrolledEnd)
1613 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001614 }
1615
1616 if (cursorRow >= this.screen_.rowsArray.length)
1617 cursorRow = this.screen_.rowsArray.length - 1;
1618
rginda87b86462011-12-14 13:48:03 -08001619 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001620};
1621
1622/**
1623 * Relocate rows from one part of the addressable screen to another.
1624 *
1625 * This is used to recycle rows during VT scrolls (those which are driven
1626 * by VT commands, rather than by the user manipulating the scrollbar.)
1627 *
1628 * In this case, the blank lines scrolled into the scroll region are made of
1629 * the nodes we scrolled off. These have their rowIndex properties carefully
1630 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001631 *
1632 * @param {number} fromIndex The start index.
1633 * @param {number} count The number of rows to move.
1634 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001635 */
1636hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1637 var ary = this.screen_.removeRows(fromIndex, count);
1638 this.screen_.insertRows(toIndex, ary);
1639
1640 var start, end;
1641 if (fromIndex < toIndex) {
1642 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001643 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001644 } else {
1645 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001646 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001647 }
1648
1649 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001650 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001651};
1652
1653/**
1654 * Renumber the rowIndex property of the given range of rows.
1655 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001656 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001657 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001658 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001659 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001660 *
1661 * @param {number} start The start index.
1662 * @param {number} end The end index.
1663 * @param {hterm.Screen} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001664 */
Robert Ginda40932892012-12-10 17:26:40 -08001665hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1666 var screen = opt_screen || this.screen_;
1667
rginda8ba33642011-12-14 12:31:31 -08001668 var offset = this.scrollbackRows_.length;
1669 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001670 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001671 }
1672};
1673
1674/**
1675 * Print a string to the terminal.
1676 *
1677 * This respects the current insert and wraparound modes. It will add new lines
1678 * to the end of the terminal, scrolling off the top into the scrollback buffer
1679 * if necessary.
1680 *
1681 * The string is *not* parsed for escape codes. Use the interpret() method if
1682 * that's what you're after.
1683 *
1684 * @param{string} str The string to print.
1685 */
1686hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001687 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001688
Ricky Liang48f05cb2013-12-31 23:35:29 +08001689 var strWidth = lib.wc.strWidth(str);
1690
1691 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001692 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1693 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001694 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001695 }
rgindaa19afe22012-01-25 15:40:22 -08001696
Ricky Liang48f05cb2013-12-31 23:35:29 +08001697 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001698 var didOverflow = false;
1699 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001700
rgindaa9abdd82012-08-06 18:05:09 -07001701 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1702 didOverflow = true;
1703 count = this.screenSize.width - this.screen_.cursorPosition.column;
1704 }
rgindaa19afe22012-01-25 15:40:22 -08001705
rgindaa9abdd82012-08-06 18:05:09 -07001706 if (didOverflow && !this.options_.wraparound) {
1707 // If the string overflowed the line but wraparound is off, then the
1708 // last printed character should be the last of the string.
1709 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001710 substr = lib.wc.substr(str, startOffset, count - 1) +
1711 lib.wc.substr(str, strWidth - 1);
1712 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001713 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001714 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001715 }
rgindaa19afe22012-01-25 15:40:22 -08001716
Ricky Liang48f05cb2013-12-31 23:35:29 +08001717 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1718 for (var i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001719 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
1720 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001721
1722 if (this.options_.insertMode) {
1723 this.screen_.insertString(tokens[i].str);
1724 } else {
1725 this.screen_.overwriteString(tokens[i].str);
1726 }
1727 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001728 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07001729 }
1730
1731 this.screen_.maybeClipCurrentRow();
1732 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001733 }
rginda8ba33642011-12-14 12:31:31 -08001734
1735 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001736
rginda9f5222b2012-03-05 11:53:28 -08001737 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001738 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001739};
1740
1741/**
rginda87b86462011-12-14 13:48:03 -08001742 * Set the VT scroll region.
1743 *
rginda87b86462011-12-14 13:48:03 -08001744 * This also resets the cursor position to the absolute (0, 0) position, since
1745 * that's what xterm appears to do.
1746 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001747 * Setting the scroll region to the full height of the terminal will clear
1748 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1749 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1750 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1751 * continue to work as most users would expect.
1752 *
rginda87b86462011-12-14 13:48:03 -08001753 * @param {integer} scrollTop The zero-based top of the scroll region.
1754 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1755 * inclusive.
1756 */
1757hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001758 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001759 this.vtScrollTop_ = null;
1760 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001761 } else {
1762 this.vtScrollTop_ = scrollTop;
1763 this.vtScrollBottom_ = scrollBottom;
1764 }
rginda87b86462011-12-14 13:48:03 -08001765};
1766
1767/**
rginda8ba33642011-12-14 12:31:31 -08001768 * Return the top row index according to the VT.
1769 *
1770 * This will return 0 unless the terminal has been told to restrict scrolling
1771 * to some lower row. It is used for some VT cursor positioning and scrolling
1772 * commands.
1773 *
1774 * @return {integer} The topmost row in the terminal's scroll region.
1775 */
1776hterm.Terminal.prototype.getVTScrollTop = function() {
1777 if (this.vtScrollTop_ != null)
1778 return this.vtScrollTop_;
1779
1780 return 0;
rginda87b86462011-12-14 13:48:03 -08001781};
rginda8ba33642011-12-14 12:31:31 -08001782
1783/**
1784 * Return the bottom row index according to the VT.
1785 *
1786 * This will return the height of the terminal unless the it has been told to
1787 * restrict scrolling to some higher row. It is used for some VT cursor
1788 * positioning and scrolling commands.
1789 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001790 * @return {integer} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001791 */
1792hterm.Terminal.prototype.getVTScrollBottom = function() {
1793 if (this.vtScrollBottom_ != null)
1794 return this.vtScrollBottom_;
1795
rginda87b86462011-12-14 13:48:03 -08001796 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001797}
1798
1799/**
1800 * Process a '\n' character.
1801 *
1802 * If the cursor is on the final row of the terminal this will append a new
1803 * blank row to the screen and scroll the topmost row into the scrollback
1804 * buffer.
1805 *
1806 * Otherwise, this moves the cursor to column zero of the next row.
1807 */
1808hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001809 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1810 this.screen_.rowsArray.length - 1);
1811
1812 if (this.vtScrollBottom_ != null) {
1813 // A VT Scroll region is active, we never append new rows.
1814 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1815 // We're at the end of the VT Scroll Region, perform a VT scroll.
1816 this.vtScrollUp(1);
1817 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1818 } else if (cursorAtEndOfScreen) {
1819 // We're at the end of the screen, the only thing to do is put the
1820 // cursor to column 0.
1821 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1822 } else {
1823 // Anywhere else, advance the cursor row, and reset the column.
1824 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1825 }
1826 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001827 // We're at the end of the screen. Append a new row to the terminal,
1828 // shifting the top row into the scrollback.
1829 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001830 } else {
rginda87b86462011-12-14 13:48:03 -08001831 // Anywhere else in the screen just moves the cursor.
1832 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001833 }
1834};
1835
1836/**
1837 * Like newLine(), except maintain the cursor column.
1838 */
1839hterm.Terminal.prototype.lineFeed = function() {
1840 var column = this.screen_.cursorPosition.column;
1841 this.newLine();
1842 this.setCursorColumn(column);
1843};
1844
1845/**
rginda87b86462011-12-14 13:48:03 -08001846 * If autoCarriageReturn is set then newLine(), else lineFeed().
1847 */
1848hterm.Terminal.prototype.formFeed = function() {
1849 if (this.options_.autoCarriageReturn) {
1850 this.newLine();
1851 } else {
1852 this.lineFeed();
1853 }
1854};
1855
1856/**
1857 * Move the cursor up one row, possibly inserting a blank line.
1858 *
1859 * The cursor column is not changed.
1860 */
1861hterm.Terminal.prototype.reverseLineFeed = function() {
1862 var scrollTop = this.getVTScrollTop();
1863 var currentRow = this.screen_.cursorPosition.row;
1864
1865 if (currentRow == scrollTop) {
1866 this.insertLines(1);
1867 } else {
1868 this.setAbsoluteCursorRow(currentRow - 1);
1869 }
1870};
1871
1872/**
rginda8ba33642011-12-14 12:31:31 -08001873 * Replace all characters to the left of the current cursor with the space
1874 * character.
1875 *
1876 * TODO(rginda): This should probably *remove* the characters (not just replace
1877 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001878 * position.
rginda8ba33642011-12-14 12:31:31 -08001879 */
1880hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001881 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001882 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001883 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001884 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001885};
1886
1887/**
David Benjamin684a9b72012-05-01 17:19:58 -04001888 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001889 *
1890 * The cursor position is unchanged.
1891 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001892 * If the current background color is not the default background color this
1893 * will insert spaces rather than delete. This is unfortunate because the
1894 * trailing space will affect text selection, but it's difficult to come up
1895 * with a way to style empty space that wouldn't trip up the hterm.Screen
1896 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001897 *
1898 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1899 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1900 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05001901 *
1902 * @param {number} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08001903 */
1904hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001905 if (this.screen_.cursorPosition.overflow)
1906 return;
1907
Robert Ginda7fd57082012-09-25 14:41:47 -07001908 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1909 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001910
1911 if (this.screen_.textAttributes.background ===
1912 this.screen_.textAttributes.DEFAULT_COLOR) {
1913 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08001914 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07001915 this.screen_.cursorPosition.column + count) {
1916 this.screen_.deleteChars(count);
1917 this.clearCursorOverflow();
1918 return;
1919 }
1920 }
1921
rginda87b86462011-12-14 13:48:03 -08001922 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001923 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001924 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001925 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001926};
1927
1928/**
1929 * Erase the current line.
1930 *
1931 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001932 */
1933hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001934 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001935 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001936 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001937 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001938};
1939
1940/**
David Benjamina08d78f2012-05-05 00:28:49 -04001941 * Erase all characters from the start of the screen to the current cursor
1942 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001943 *
1944 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001945 */
1946hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001947 var cursor = this.saveCursor();
1948
1949 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001950
David Benjamina08d78f2012-05-05 00:28:49 -04001951 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001952 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001953 this.screen_.clearCursorRow();
1954 }
1955
rginda87b86462011-12-14 13:48:03 -08001956 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001957 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001958};
1959
1960/**
1961 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001962 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001963 *
1964 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001965 */
1966hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001967 var cursor = this.saveCursor();
1968
1969 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001970
David Benjamina08d78f2012-05-05 00:28:49 -04001971 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001972 for (var i = cursor.row + 1; i <= bottom; i++) {
1973 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001974 this.screen_.clearCursorRow();
1975 }
1976
rginda87b86462011-12-14 13:48:03 -08001977 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001978 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001979};
1980
1981/**
1982 * Fill the terminal with a given character.
1983 *
1984 * This methods does not respect the VT scroll region.
1985 *
1986 * @param {string} ch The character to use for the fill.
1987 */
1988hterm.Terminal.prototype.fill = function(ch) {
1989 var cursor = this.saveCursor();
1990
1991 this.setAbsoluteCursorPosition(0, 0);
1992 for (var row = 0; row < this.screenSize.height; row++) {
1993 for (var col = 0; col < this.screenSize.width; col++) {
1994 this.setAbsoluteCursorPosition(row, col);
1995 this.screen_.overwriteString(ch);
1996 }
1997 }
1998
1999 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002000};
2001
2002/**
rginda9ea433c2012-03-16 11:57:00 -07002003 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08002004 *
rginda9ea433c2012-03-16 11:57:00 -07002005 * This does not respect the scroll region.
2006 *
2007 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2008 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08002009 */
rginda9ea433c2012-03-16 11:57:00 -07002010hterm.Terminal.prototype.clearHome = function(opt_screen) {
2011 var screen = opt_screen || this.screen_;
2012 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002013
rginda11057d52012-04-25 12:29:56 -07002014 if (bottom == 0) {
2015 // Empty screen, nothing to do.
2016 return;
2017 }
2018
rgindae4d29232012-01-19 10:47:13 -08002019 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002020 screen.setCursorPosition(i, 0);
2021 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002022 }
2023
rginda9ea433c2012-03-16 11:57:00 -07002024 screen.setCursorPosition(0, 0);
2025};
2026
2027/**
2028 * Erase the entire display without changing the cursor position.
2029 *
2030 * The cursor position is unchanged. This does not respect the scroll
2031 * region.
2032 *
2033 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2034 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002035 */
2036hterm.Terminal.prototype.clear = function(opt_screen) {
2037 var screen = opt_screen || this.screen_;
2038 var cursor = screen.cursorPosition.clone();
2039 this.clearHome(screen);
2040 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002041};
2042
2043/**
2044 * VT command to insert lines at the current cursor row.
2045 *
2046 * This respects the current scroll region. Rows pushed off the bottom are
2047 * lost (they won't show up in the scrollback buffer).
2048 *
rginda8ba33642011-12-14 12:31:31 -08002049 * @param {integer} count The number of lines to insert.
2050 */
2051hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002052 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002053
2054 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002055 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002056
Robert Ginda579186b2012-09-26 11:40:04 -07002057 // The moveCount is the number of rows we need to relocate to make room for
2058 // the new row(s). The count is the distance to move them.
2059 var moveCount = bottom - cursorRow - count + 1;
2060 if (moveCount)
2061 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002062
Robert Ginda579186b2012-09-26 11:40:04 -07002063 for (var i = count - 1; i >= 0; i--) {
2064 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002065 this.screen_.clearCursorRow();
2066 }
rginda8ba33642011-12-14 12:31:31 -08002067};
2068
2069/**
2070 * VT command to delete lines at the current cursor row.
2071 *
2072 * New rows are added to the bottom of scroll region to take their place. New
2073 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002074 *
2075 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002076 */
2077hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002078 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002079
rginda87b86462011-12-14 13:48:03 -08002080 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002081 var bottom = this.getVTScrollBottom();
2082
rginda87b86462011-12-14 13:48:03 -08002083 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002084 count = Math.min(count, maxCount);
2085
rginda87b86462011-12-14 13:48:03 -08002086 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002087 if (count != maxCount)
2088 this.moveRows_(top, count, moveStart);
2089
2090 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002091 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002092 this.screen_.clearCursorRow();
2093 }
2094
rginda87b86462011-12-14 13:48:03 -08002095 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002096 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002097};
2098
2099/**
2100 * Inserts the given number of spaces at the current cursor position.
2101 *
rginda87b86462011-12-14 13:48:03 -08002102 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002103 *
2104 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002105 */
2106hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002107 var cursor = this.saveCursor();
2108
rgindacbbd7482012-06-13 15:06:16 -07002109 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08002110 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08002111 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002112
2113 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002114 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002115};
2116
2117/**
2118 * Forward-delete the specified number of characters starting at the cursor
2119 * position.
2120 *
2121 * @param {integer} count The number of characters to delete.
2122 */
2123hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002124 var deleted = this.screen_.deleteChars(count);
2125 if (deleted && !this.screen_.textAttributes.isDefault()) {
2126 var cursor = this.saveCursor();
2127 this.setCursorColumn(this.screenSize.width - deleted);
2128 this.screen_.insertString(lib.f.getWhitespace(deleted));
2129 this.restoreCursor(cursor);
2130 }
2131
David Benjamin54e8bf62012-06-01 22:31:40 -04002132 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002133};
2134
2135/**
2136 * Shift rows in the scroll region upwards by a given number of lines.
2137 *
2138 * New rows are inserted at the bottom of the scroll region to fill the
2139 * vacated rows. The new rows not filled out with the current text attributes.
2140 *
2141 * This function does not affect the scrollback rows at all. Rows shifted
2142 * off the top are lost.
2143 *
rginda87b86462011-12-14 13:48:03 -08002144 * The cursor position is not altered.
2145 *
rginda8ba33642011-12-14 12:31:31 -08002146 * @param {integer} count The number of rows to scroll.
2147 */
2148hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002149 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002150
rginda87b86462011-12-14 13:48:03 -08002151 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002152 this.deleteLines(count);
2153
rginda87b86462011-12-14 13:48:03 -08002154 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002155};
2156
2157/**
2158 * Shift rows below the cursor down by a given number of lines.
2159 *
2160 * This function respects the current scroll region.
2161 *
2162 * New rows are inserted at the top of the scroll region to fill the
2163 * vacated rows. The new rows not filled out with the current text attributes.
2164 *
2165 * This function does not affect the scrollback rows at all. Rows shifted
2166 * off the bottom are lost.
2167 *
2168 * @param {integer} count The number of rows to scroll.
2169 */
2170hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08002171 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002172
rginda87b86462011-12-14 13:48:03 -08002173 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08002174 this.insertLines(opt_count);
2175
rginda87b86462011-12-14 13:48:03 -08002176 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002177};
2178
rginda87b86462011-12-14 13:48:03 -08002179
rginda8ba33642011-12-14 12:31:31 -08002180/**
2181 * Set the cursor position.
2182 *
2183 * The cursor row is relative to the scroll region if the terminal has
2184 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2185 *
2186 * @param {integer} row The new zero-based cursor row.
2187 * @param {integer} row The new zero-based cursor column.
2188 */
2189hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2190 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002191 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002192 } else {
rginda87b86462011-12-14 13:48:03 -08002193 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002194 }
rginda87b86462011-12-14 13:48:03 -08002195};
rginda8ba33642011-12-14 12:31:31 -08002196
Evan Jones2600d4f2016-12-06 09:29:36 -05002197/**
2198 * Move the cursor relative to its current position.
2199 *
2200 * @param {number} row
2201 * @param {number} column
2202 */
rginda87b86462011-12-14 13:48:03 -08002203hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2204 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002205 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2206 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002207 this.screen_.setCursorPosition(row, column);
2208};
2209
Evan Jones2600d4f2016-12-06 09:29:36 -05002210/**
2211 * Move the cursor to the specified position.
2212 *
2213 * @param {number} row
2214 * @param {number} column
2215 */
rginda87b86462011-12-14 13:48:03 -08002216hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002217 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2218 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002219 this.screen_.setCursorPosition(row, column);
2220};
2221
2222/**
2223 * Set the cursor column.
2224 *
2225 * @param {integer} column The new zero-based cursor column.
2226 */
2227hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002228 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002229};
2230
2231/**
2232 * Return the cursor column.
2233 *
2234 * @return {integer} The zero-based cursor column.
2235 */
2236hterm.Terminal.prototype.getCursorColumn = function() {
2237 return this.screen_.cursorPosition.column;
2238};
2239
2240/**
2241 * Set the cursor row.
2242 *
2243 * The cursor row is relative to the scroll region if the terminal has
2244 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2245 *
2246 * @param {integer} row The new cursor row.
2247 */
rginda87b86462011-12-14 13:48:03 -08002248hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2249 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002250};
2251
2252/**
2253 * Return the cursor row.
2254 *
2255 * @return {integer} The zero-based cursor row.
2256 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002257hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002258 return this.screen_.cursorPosition.row;
2259};
2260
2261/**
2262 * Request that the ScrollPort redraw itself soon.
2263 *
2264 * The redraw will happen asynchronously, soon after the call stack winds down.
2265 * Multiple calls will be coalesced into a single redraw.
2266 */
2267hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002268 if (this.timeouts_.redraw)
2269 return;
rginda8ba33642011-12-14 12:31:31 -08002270
2271 var self = this;
rginda87b86462011-12-14 13:48:03 -08002272 this.timeouts_.redraw = setTimeout(function() {
2273 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002274 self.scrollPort_.redraw_();
2275 }, 0);
2276};
2277
2278/**
2279 * Request that the ScrollPort be scrolled to the bottom.
2280 *
2281 * The scroll will happen asynchronously, soon after the call stack winds down.
2282 * Multiple calls will be coalesced into a single scroll.
2283 *
2284 * This affects the scrollbar position of the ScrollPort, and has nothing to
2285 * do with the VT scroll commands.
2286 */
2287hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2288 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002289 return;
rginda8ba33642011-12-14 12:31:31 -08002290
2291 var self = this;
2292 this.timeouts_.scrollDown = setTimeout(function() {
2293 delete self.timeouts_.scrollDown;
2294 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2295 }, 10);
2296};
2297
2298/**
2299 * Move the cursor up a specified number of rows.
2300 *
2301 * @param {integer} count The number of rows to move the cursor.
2302 */
2303hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002304 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002305};
2306
2307/**
2308 * Move the cursor down a specified number of rows.
2309 *
2310 * @param {integer} count The number of rows to move the cursor.
2311 */
2312hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002313 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002314 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2315 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2316 this.screenSize.height - 1);
2317
rgindacbbd7482012-06-13 15:06:16 -07002318 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002319 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002320 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002321};
2322
2323/**
2324 * Move the cursor left a specified number of columns.
2325 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002326 * If reverse wraparound mode is enabled and the previous row wrapped into
2327 * the current row then we back up through the wraparound as well.
2328 *
rginda8ba33642011-12-14 12:31:31 -08002329 * @param {integer} count The number of columns to move the cursor.
2330 */
2331hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002332 count = count || 1;
2333
2334 if (count < 1)
2335 return;
2336
2337 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002338 if (this.options_.reverseWraparound) {
2339 if (this.screen_.cursorPosition.overflow) {
2340 // If this cursor is in the right margin, consume one count to get it
2341 // back to the last column. This only applies when we're in reverse
2342 // wraparound mode.
2343 count--;
2344 this.clearCursorOverflow();
2345
2346 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002347 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002348 }
2349
Robert Gindabfb32622014-07-17 13:20:27 -07002350 var newRow = this.screen_.cursorPosition.row;
2351 var newColumn = currentColumn - count;
2352 if (newColumn < 0) {
2353 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2354 if (newRow < 0) {
2355 // xterm also wraps from row 0 to the last row.
2356 newRow = this.screenSize.height + newRow % this.screenSize.height;
2357 }
2358 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2359 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002360
Robert Gindabfb32622014-07-17 13:20:27 -07002361 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2362
2363 } else {
2364 var newColumn = Math.max(currentColumn - count, 0);
2365 this.setCursorColumn(newColumn);
2366 }
rginda8ba33642011-12-14 12:31:31 -08002367};
2368
2369/**
2370 * Move the cursor right a specified number of columns.
2371 *
2372 * @param {integer} count The number of columns to move the cursor.
2373 */
2374hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002375 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002376
2377 if (count < 1)
2378 return;
2379
rgindacbbd7482012-06-13 15:06:16 -07002380 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002381 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002382 this.setCursorColumn(column);
2383};
2384
2385/**
2386 * Reverse the foreground and background colors of the terminal.
2387 *
2388 * This only affects text that was drawn with no attributes.
2389 *
2390 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2391 * been drawn with attributes that happen to coincide with the default
2392 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002393 *
2394 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002395 */
2396hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002397 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002398 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002399 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2400 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002401 } else {
rginda9f5222b2012-03-05 11:53:28 -08002402 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2403 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002404 }
2405};
2406
2407/**
rginda87b86462011-12-14 13:48:03 -08002408 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002409 *
2410 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002411 */
2412hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002413 this.cursorNode_.style.backgroundColor =
2414 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002415
2416 var self = this;
2417 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08002418 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08002419 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002420
Michael Kelly485ecd12014-06-09 11:41:56 -04002421 // bellSquelchTimeout_ affects both audio and notification bells.
2422 if (this.bellSquelchTimeout_)
2423 return;
2424
Robert Ginda92e18102013-03-14 13:56:37 -07002425 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002426 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002427 this.bellSequelchTimeout_ = setTimeout(function() {
2428 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002429 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002430 } else {
2431 delete this.bellSquelchTimeout_;
2432 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002433
2434 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002435 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002436 this.bellNotificationList_.push(n);
2437 // TODO: Should we try to raise the window here?
2438 n.onclick = function() { self.closeBellNotifications_(); };
2439 }
rginda87b86462011-12-14 13:48:03 -08002440};
2441
2442/**
rginda8ba33642011-12-14 12:31:31 -08002443 * Set the origin mode bit.
2444 *
2445 * If origin mode is on, certain VT cursor and scrolling commands measure their
2446 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2447 * to the top of the addressable screen.
2448 *
2449 * Defaults to off.
2450 *
2451 * @param {boolean} state True to set origin mode, false to unset.
2452 */
2453hterm.Terminal.prototype.setOriginMode = function(state) {
2454 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002455 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002456};
2457
2458/**
2459 * Set the insert mode bit.
2460 *
2461 * If insert mode is on, existing text beyond the cursor position will be
2462 * shifted right to make room for new text. Otherwise, new text overwrites
2463 * any existing text.
2464 *
2465 * Defaults to off.
2466 *
2467 * @param {boolean} state True to set insert mode, false to unset.
2468 */
2469hterm.Terminal.prototype.setInsertMode = function(state) {
2470 this.options_.insertMode = state;
2471};
2472
2473/**
rginda87b86462011-12-14 13:48:03 -08002474 * Set the auto carriage return bit.
2475 *
2476 * If auto carriage return is on then a formfeed character is interpreted
2477 * as a newline, otherwise it's the same as a linefeed. The difference boils
2478 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002479 *
2480 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002481 */
2482hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2483 this.options_.autoCarriageReturn = state;
2484};
2485
2486/**
rginda8ba33642011-12-14 12:31:31 -08002487 * Set the wraparound mode bit.
2488 *
2489 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2490 * to the start of the following row. Otherwise, the cursor is clamped to the
2491 * end of the screen and attempts to write past it are ignored.
2492 *
2493 * Defaults to on.
2494 *
2495 * @param {boolean} state True to set wraparound mode, false to unset.
2496 */
2497hterm.Terminal.prototype.setWraparound = function(state) {
2498 this.options_.wraparound = state;
2499};
2500
2501/**
2502 * Set the reverse-wraparound mode bit.
2503 *
2504 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2505 * to the end of the previous row. Otherwise, the cursor is clamped to column
2506 * 0.
2507 *
2508 * Defaults to off.
2509 *
2510 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2511 */
2512hterm.Terminal.prototype.setReverseWraparound = function(state) {
2513 this.options_.reverseWraparound = state;
2514};
2515
2516/**
2517 * Selects between the primary and alternate screens.
2518 *
2519 * If alternate mode is on, the alternate screen is active. Otherwise the
2520 * primary screen is active.
2521 *
2522 * Swapping screens has no effect on the scrollback buffer.
2523 *
2524 * Each screen maintains its own cursor position.
2525 *
2526 * Defaults to off.
2527 *
2528 * @param {boolean} state True to set alternate mode, false to unset.
2529 */
2530hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002531 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002532 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2533
rginda35c456b2012-02-09 17:29:05 -08002534 if (this.screen_.rowsArray.length &&
2535 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2536 // If the screen changed sizes while we were away, our rowIndexes may
2537 // be incorrect.
2538 var offset = this.scrollbackRows_.length;
2539 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002540 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002541 ary[i].rowIndex = offset + i;
2542 }
2543 }
rginda8ba33642011-12-14 12:31:31 -08002544
rginda35c456b2012-02-09 17:29:05 -08002545 this.realizeWidth_(this.screenSize.width);
2546 this.realizeHeight_(this.screenSize.height);
2547 this.scrollPort_.syncScrollHeight();
2548 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002549
rginda6d397402012-01-17 10:58:29 -08002550 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002551 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002552};
2553
2554/**
2555 * Set the cursor-blink mode bit.
2556 *
2557 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2558 * a visible cursor does not blink.
2559 *
2560 * You should make sure to turn blinking off if you're going to dispose of a
2561 * terminal, otherwise you'll leak a timeout.
2562 *
2563 * Defaults to on.
2564 *
2565 * @param {boolean} state True to set cursor-blink mode, false to unset.
2566 */
2567hterm.Terminal.prototype.setCursorBlink = function(state) {
2568 this.options_.cursorBlink = state;
2569
2570 if (!state && this.timeouts_.cursorBlink) {
2571 clearTimeout(this.timeouts_.cursorBlink);
2572 delete this.timeouts_.cursorBlink;
2573 }
2574
2575 if (this.options_.cursorVisible)
2576 this.setCursorVisible(true);
2577};
2578
2579/**
2580 * Set the cursor-visible mode bit.
2581 *
2582 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2583 *
2584 * Defaults to on.
2585 *
2586 * @param {boolean} state True to set cursor-visible mode, false to unset.
2587 */
2588hterm.Terminal.prototype.setCursorVisible = function(state) {
2589 this.options_.cursorVisible = state;
2590
2591 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002592 if (this.timeouts_.cursorBlink) {
2593 clearTimeout(this.timeouts_.cursorBlink);
2594 delete this.timeouts_.cursorBlink;
2595 }
rginda87b86462011-12-14 13:48:03 -08002596 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002597 return;
2598 }
2599
rginda87b86462011-12-14 13:48:03 -08002600 this.syncCursorPosition_();
2601
2602 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002603
2604 if (this.options_.cursorBlink) {
2605 if (this.timeouts_.cursorBlink)
2606 return;
2607
Robert Gindaea2183e2014-07-17 09:51:51 -07002608 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002609 } else {
2610 if (this.timeouts_.cursorBlink) {
2611 clearTimeout(this.timeouts_.cursorBlink);
2612 delete this.timeouts_.cursorBlink;
2613 }
2614 }
2615};
2616
2617/**
rginda87b86462011-12-14 13:48:03 -08002618 * Synchronizes the visible cursor and document selection with the current
2619 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002620 */
2621hterm.Terminal.prototype.syncCursorPosition_ = function() {
2622 var topRowIndex = this.scrollPort_.getTopRowIndex();
2623 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2624 var cursorRowIndex = this.scrollbackRows_.length +
2625 this.screen_.cursorPosition.row;
2626
2627 if (cursorRowIndex > bottomRowIndex) {
2628 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04002629 this.setCssVar('cursor-offset-row', '-1');
rginda8ba33642011-12-14 12:31:31 -08002630 return;
2631 }
2632
Robert Gindab837c052014-08-11 11:17:51 -07002633 if (this.options_.cursorVisible &&
2634 this.cursorNode_.style.display == 'none') {
2635 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2636 this.cursorNode_.style.display = '';
2637 }
2638
Mike Frysinger44c32202017-08-05 01:13:09 -04002639 // Position the cursor using CSS variable math. If we do the math in JS,
2640 // the float math will end up being more precise than the CSS which will
2641 // cause the cursor tracking to be off.
2642 this.setCssVar(
2643 'cursor-offset-row',
2644 `${cursorRowIndex - topRowIndex} + ` +
2645 `${this.scrollPort_.visibleRowTopMargin}px`);
2646 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08002647
2648 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04002649 '(' + this.screen_.cursorPosition.column +
2650 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08002651 ')');
2652
2653 // Update the caret for a11y purposes.
2654 var selection = this.document_.getSelection();
2655 if (selection && selection.isCollapsed)
2656 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002657};
2658
Robert Gindafb1be6a2013-12-11 11:56:22 -08002659/**
2660 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2661 * and character cell dimensions.
2662 */
Robert Ginda830583c2013-08-07 13:20:46 -07002663hterm.Terminal.prototype.restyleCursor_ = function() {
2664 var shape = this.cursorShape_;
2665
2666 if (this.cursorNode_.getAttribute('focus') == 'false') {
2667 // Always show a block cursor when unfocused.
2668 shape = hterm.Terminal.cursorShape.BLOCK;
2669 }
2670
2671 var style = this.cursorNode_.style;
2672
2673 switch (shape) {
2674 case hterm.Terminal.cursorShape.BEAM:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002675 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002676 style.backgroundColor = 'transparent';
2677 style.borderBottomStyle = null;
2678 style.borderLeftStyle = 'solid';
2679 break;
2680
2681 case hterm.Terminal.cursorShape.UNDERLINE:
2682 style.height = this.scrollPort_.characterSize.baseline + 'px';
2683 style.backgroundColor = 'transparent';
2684 style.borderBottomStyle = 'solid';
2685 // correct the size to put it exactly at the baseline
2686 style.borderLeftStyle = null;
2687 break;
2688
2689 default:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002690 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002691 style.backgroundColor = this.cursorColor_;
2692 style.borderBottomStyle = null;
2693 style.borderLeftStyle = null;
2694 break;
2695 }
2696};
2697
rginda8ba33642011-12-14 12:31:31 -08002698/**
2699 * Synchronizes the visible cursor with the current cursor coordinates.
2700 *
2701 * The sync will happen asynchronously, soon after the call stack winds down.
2702 * Multiple calls will be coalesced into a single sync.
2703 */
2704hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2705 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002706 return;
rginda8ba33642011-12-14 12:31:31 -08002707
2708 var self = this;
2709 this.timeouts_.syncCursor = setTimeout(function() {
2710 self.syncCursorPosition_();
2711 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002712 }, 0);
2713};
2714
rgindacc2996c2012-02-24 14:59:31 -08002715/**
rgindaf522ce02012-04-17 17:49:17 -07002716 * Show or hide the zoom warning.
2717 *
2718 * The zoom warning is a message warning the user that their browser zoom must
2719 * be set to 100% in order for hterm to function properly.
2720 *
2721 * @param {boolean} state True to show the message, false to hide it.
2722 */
2723hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2724 if (!this.zoomWarningNode_) {
2725 if (!state)
2726 return;
2727
2728 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002729 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07002730 this.zoomWarningNode_.style.cssText = (
2731 'color: black;' +
2732 'background-color: #ff2222;' +
2733 'font-size: large;' +
2734 'border-radius: 8px;' +
2735 'opacity: 0.75;' +
2736 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2737 'top: 0.5em;' +
2738 'right: 1.2em;' +
2739 'position: absolute;' +
2740 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002741 '-webkit-user-select: none;' +
2742 '-moz-text-size-adjust: none;' +
2743 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002744
2745 this.zoomWarningNode_.addEventListener('click', function(e) {
2746 this.parentNode.removeChild(this);
2747 });
rgindaf522ce02012-04-17 17:49:17 -07002748 }
2749
Robert Gindab4839c22013-02-28 16:52:10 -08002750 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2751 hterm.zoomWarningMessage,
2752 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2753
rgindaf522ce02012-04-17 17:49:17 -07002754 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2755
2756 if (state) {
2757 if (!this.zoomWarningNode_.parentNode)
2758 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2759 } else if (this.zoomWarningNode_.parentNode) {
2760 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2761 }
2762};
2763
2764/**
rgindacc2996c2012-02-24 14:59:31 -08002765 * Show the terminal overlay for a given amount of time.
2766 *
2767 * The terminal overlay appears in inverse video in a large font, centered
2768 * over the terminal. You should probably keep the overlay message brief,
2769 * since it's in a large font and you probably aren't going to check the size
2770 * of the terminal first.
2771 *
2772 * @param {string} msg The text (not HTML) message to display in the overlay.
2773 * @param {number} opt_timeout The amount of time to wait before fading out
2774 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2775 * stay up forever (or until the next overlay).
2776 */
2777hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002778 if (!this.overlayNode_) {
2779 if (!this.div_)
2780 return;
2781
2782 this.overlayNode_ = this.document_.createElement('div');
2783 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002784 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002785 'font-size: xx-large;' +
2786 'opacity: 0.75;' +
2787 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2788 'position: absolute;' +
2789 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002790 '-webkit-transition: opacity 180ms ease-in;' +
2791 '-moz-user-select: none;' +
2792 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002793
2794 this.overlayNode_.addEventListener('mousedown', function(e) {
2795 e.preventDefault();
2796 e.stopPropagation();
2797 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002798 }
2799
rginda9f5222b2012-03-05 11:53:28 -08002800 this.overlayNode_.style.color = this.prefs_.get('background-color');
2801 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2802 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2803
rgindaf0090c92012-02-10 14:58:52 -08002804 this.overlayNode_.textContent = msg;
2805 this.overlayNode_.style.opacity = '0.75';
2806
2807 if (!this.overlayNode_.parentNode)
2808 this.div_.appendChild(this.overlayNode_);
2809
Robert Ginda97769282013-02-01 15:30:30 -08002810 var divSize = hterm.getClientSize(this.div_);
2811 var overlaySize = hterm.getClientSize(this.overlayNode_);
2812
Robert Ginda8a59f762014-07-23 11:29:55 -07002813 this.overlayNode_.style.top =
2814 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08002815 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07002816 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08002817
2818 var self = this;
2819
2820 if (this.overlayTimeout_)
2821 clearTimeout(this.overlayTimeout_);
2822
rgindacc2996c2012-02-24 14:59:31 -08002823 if (opt_timeout === null)
2824 return;
2825
rgindaf0090c92012-02-10 14:58:52 -08002826 this.overlayTimeout_ = setTimeout(function() {
2827 self.overlayNode_.style.opacity = '0';
Robert Ginda70926e42013-11-25 14:56:36 -08002828 self.overlayTimeout_ = setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002829 if (self.overlayNode_.parentNode)
2830 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002831 self.overlayTimeout_ = null;
2832 self.overlayNode_.style.opacity = '0.75';
2833 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002834 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002835};
2836
rginda4bba5e12012-06-20 16:15:30 -07002837/**
2838 * Paste from the system clipboard to the terminal.
2839 */
2840hterm.Terminal.prototype.paste = function() {
Mike Frysinger4628ad22017-07-20 02:44:20 -04002841 return hterm.pasteFromClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002842};
2843
2844/**
2845 * Copy a string to the system clipboard.
2846 *
2847 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05002848 *
2849 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07002850 */
2851hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002852 if (this.prefs_.get('enable-clipboard-notice'))
2853 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2854
rgindaa09e7332012-08-17 12:49:51 -07002855 var copySource = this.document_.createElement('pre');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002856 copySource.id = 'hterm:copy-to-clipboard-source';
rginda4bba5e12012-06-20 16:15:30 -07002857 copySource.textContent = str;
2858 copySource.style.cssText = (
2859 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002860 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07002861 'position: absolute;' +
2862 'top: -99px');
2863
2864 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002865
rginda4bba5e12012-06-20 16:15:30 -07002866 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002867 var anchorNode = selection.anchorNode;
2868 var anchorOffset = selection.anchorOffset;
2869 var focusNode = selection.focusNode;
2870 var focusOffset = selection.focusOffset;
2871
rginda4bba5e12012-06-20 16:15:30 -07002872 selection.selectAllChildren(copySource);
2873
rgindaa09e7332012-08-17 12:49:51 -07002874 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002875
Rob Spies56953412014-04-28 14:09:47 -07002876 // IE doesn't support selection.extend. This means that the selection
2877 // won't return on IE.
Robert Ginda6f4f0aa2014-07-28 10:21:39 -07002878 if (selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07002879 selection.collapse(anchorNode, anchorOffset);
2880 selection.extend(focusNode, focusOffset);
2881 }
rgindafaa74742012-08-21 13:34:03 -07002882
rginda4bba5e12012-06-20 16:15:30 -07002883 copySource.parentNode.removeChild(copySource);
2884};
2885
Evan Jones2600d4f2016-12-06 09:29:36 -05002886/**
2887 * Returns the selected text, or null if no text is selected.
2888 *
2889 * @return {string|null}
2890 */
rgindaa09e7332012-08-17 12:49:51 -07002891hterm.Terminal.prototype.getSelectionText = function() {
2892 var selection = this.scrollPort_.selection;
2893 selection.sync();
2894
2895 if (selection.isCollapsed)
2896 return null;
2897
2898
2899 // Start offset measures from the beginning of the line.
2900 var startOffset = selection.startOffset;
2901 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002902
Robert Gindafdbb3f22012-09-06 20:23:06 -07002903 if (node.nodeName != 'X-ROW') {
2904 // If the selection doesn't start on an x-row node, then it must be
2905 // somewhere inside the x-row. Add any characters from previous siblings
2906 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002907
2908 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2909 // If node is the text node in a styled span, move up to the span node.
2910 node = node.parentNode;
2911 }
2912
Robert Gindafdbb3f22012-09-06 20:23:06 -07002913 while (node.previousSibling) {
2914 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002915 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002916 }
rgindaa09e7332012-08-17 12:49:51 -07002917 }
2918
2919 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002920 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
2921 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05002922 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002923
Robert Gindafdbb3f22012-09-06 20:23:06 -07002924 if (node.nodeName != 'X-ROW') {
2925 // If the selection doesn't end on an x-row node, then it must be
2926 // somewhere inside the x-row. Add any characters from following siblings
2927 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002928
2929 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2930 // If node is the text node in a styled span, move up to the span node.
2931 node = node.parentNode;
2932 }
2933
Robert Gindafdbb3f22012-09-06 20:23:06 -07002934 while (node.nextSibling) {
2935 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002936 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002937 }
rgindaa09e7332012-08-17 12:49:51 -07002938 }
2939
2940 var rv = this.getRowsText(selection.startRow.rowIndex,
2941 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002942 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002943};
2944
rginda4bba5e12012-06-20 16:15:30 -07002945/**
2946 * Copy the current selection to the system clipboard, then clear it after a
2947 * short delay.
2948 */
2949hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002950 var text = this.getSelectionText();
2951 if (text != null)
2952 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002953};
2954
rgindaf0090c92012-02-10 14:58:52 -08002955hterm.Terminal.prototype.overlaySize = function() {
2956 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2957};
2958
rginda87b86462011-12-14 13:48:03 -08002959/**
2960 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2961 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07002962 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08002963 */
2964hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002965 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002966 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2967
Robert Ginda8cb7d902013-06-20 14:37:18 -07002968 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08002969};
2970
2971/**
Mike Frysinger70b94692017-01-26 18:57:50 -10002972 * Launches url in a new tab.
2973 *
2974 * @param {string} url URL to launch in a new tab.
2975 */
2976hterm.Terminal.prototype.openUrl = function(url) {
Mike Frysingerac437a12017-07-13 02:35:59 -04002977 if (window.chrome && window.chrome.browser) {
2978 // For Chrome v2 apps, we need to use this API to properly open windows.
2979 chrome.browser.openTab({'url': url});
2980 } else {
2981 var win = window.open(url, '_blank');
2982 win.focus();
2983 }
Mike Frysinger70b94692017-01-26 18:57:50 -10002984}
2985
2986/**
2987 * Open the selected url.
2988 */
2989hterm.Terminal.prototype.openSelectedUrl_ = function() {
2990 var str = this.getSelectionText();
2991
2992 // If there is no selection, try and expand wherever they clicked.
2993 if (str == null) {
2994 this.screen_.expandSelection(this.document_.getSelection());
2995 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04002996
2997 // If clicking in empty space, return.
2998 if (str == null)
2999 return;
Mike Frysinger70b94692017-01-26 18:57:50 -10003000 }
3001
3002 // Make sure URL is valid before opening.
3003 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
3004 return;
Mike Frysinger43472622017-06-26 18:11:07 -04003005
3006 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10003007 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04003008 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
3009 // We have to whitelist a few protocols that lack authorities and thus
3010 // never use the //. Like mailto.
3011 switch (str.split(':', 1)[0]) {
3012 case 'mailto':
3013 break;
3014 default:
3015 str = 'http://' + str;
3016 break;
3017 }
3018 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003019
3020 this.openUrl(str);
3021}
3022
3023
3024/**
rgindad5613292012-06-19 15:40:37 -07003025 * Add the terminalRow and terminalColumn properties to mouse events and
3026 * then forward on to onMouse().
3027 *
3028 * The terminalRow and terminalColumn properties contain the (row, column)
3029 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003030 *
3031 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003032 */
3033hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003034 if (e.processedByTerminalHandler_) {
3035 // We register our event handlers on the document, as well as the cursor
3036 // and the scroll blocker. Mouse events that occur on the cursor or
3037 // scroll blocker will also appear on the document, but we don't want to
3038 // process them twice.
3039 //
3040 // We can't just prevent bubbling because that has other side effects, so
3041 // we decorate the event object with this property instead.
3042 return;
3043 }
3044
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003045 var reportMouseEvents = (!this.defeatMouseReports_ &&
3046 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3047
rgindafaa74742012-08-21 13:34:03 -07003048 e.processedByTerminalHandler_ = true;
3049
Robert Gindaeda48db2014-07-17 09:25:30 -07003050 // One based row/column stored on the mouse event.
3051 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
3052 this.scrollPort_.characterSize.height) + 1;
3053 e.terminalColumn = parseInt(e.clientX /
3054 this.scrollPort_.characterSize.width) + 1;
3055
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003056 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3057 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003058 return;
3059 }
3060
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003061 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003062 // If the cursor is visible and we're not sending mouse events to the
3063 // host app, then we want to hide the terminal cursor when the mouse
3064 // cursor is over top. This keeps the terminal cursor from interfering
3065 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003066 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3067 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3068 this.cursorNode_.style.display = 'none';
3069 } else if (this.cursorNode_.style.display == 'none') {
3070 this.cursorNode_.style.display = '';
3071 }
3072 }
rgindad5613292012-06-19 15:40:37 -07003073
Robert Ginda928cf632014-03-05 15:07:41 -08003074 if (e.type == 'mousedown') {
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003075 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003076 // If VT mouse reporting is disabled, or has been defeated with
3077 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003078 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003079 this.setSelectionEnabled(true);
3080 } else {
3081 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003082 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003083 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003084 this.setSelectionEnabled(false);
3085 e.preventDefault();
3086 }
3087 }
3088
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003089 if (!reportMouseEvents) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003090 if (e.type == 'dblclick' && this.copyOnSelect) {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003091 this.screen_.expandSelection(this.document_.getSelection());
Robert Ginda15ed4902016-07-12 10:43:22 -07003092 this.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07003093 }
3094
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003095 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003096 // Debounce this event with the dblclick event. If you try to doubleclick
3097 // a URL to open it, Chrome will fire click then dblclick, but we won't
3098 // have expanded the selection text at the first click event.
3099 clearTimeout(this.timeouts_.openUrl);
3100 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3101 500);
3102 return;
3103 }
3104
Mike Frysinger847577f2017-05-23 23:25:57 -04003105 if (e.type == 'mousedown') {
3106 if ((this.mouseRightClickPaste && e.button == 2 /* right button */) ||
Mike Frysinger2edd3612017-05-24 00:54:39 -04003107 e.button == this.mousePasteButton) {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003108 if (!this.paste())
3109 console.warning('Could not paste manually due to web restrictions');;
Mike Frysinger847577f2017-05-23 23:25:57 -04003110 }
3111 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003112
Mike Frysinger2edd3612017-05-24 00:54:39 -04003113 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003114 !this.document_.getSelection().isCollapsed) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003115 this.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003116 }
3117
3118 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3119 this.scrollBlockerNode_.engaged) {
3120 // Disengage the scroll-blocker after one of these events.
3121 this.scrollBlockerNode_.engaged = false;
3122 this.scrollBlockerNode_.style.top = '-99px';
3123 }
3124
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003125 // Emulate arrow key presses via scroll wheel events.
3126 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3127 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003128 if (e.type == 'wheel') {
3129 var delta = this.scrollPort_.scrollWheelDelta(e);
3130 var lines = lib.f.smartFloorDivide(
3131 Math.abs(delta), this.scrollPort_.characterSize.height);
3132
3133 var data = '\x1bO' + (delta < 0 ? 'B' : 'A');
3134 this.io.sendString(data.repeat(lines));
3135
3136 e.preventDefault();
3137 }
3138 }
Robert Ginda928cf632014-03-05 15:07:41 -08003139 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003140 if (!this.scrollBlockerNode_.engaged) {
3141 if (e.type == 'mousedown') {
3142 // Move the scroll-blocker into place if we want to keep the scrollport
3143 // from scrolling.
3144 this.scrollBlockerNode_.engaged = true;
3145 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3146 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3147 } else if (e.type == 'mousemove') {
3148 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3149 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003150 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003151 e.preventDefault();
3152 }
3153 }
Robert Ginda928cf632014-03-05 15:07:41 -08003154
3155 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003156 }
3157
Robert Ginda928cf632014-03-05 15:07:41 -08003158 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3159 // Restore this on mouseup in case it was temporarily defeated with a
3160 // alt-mousedown. Only do this when the selection is empty so that
3161 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003162 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003163 }
rgindad5613292012-06-19 15:40:37 -07003164};
3165
3166/**
3167 * Clients should override this if they care to know about mouse events.
3168 *
3169 * The event parameter will be a normal DOM mouse click event with additional
3170 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003171 *
3172 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003173 */
3174hterm.Terminal.prototype.onMouse = function(e) { };
3175
3176/**
rginda8e92a692012-05-20 19:37:20 -07003177 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003178 *
3179 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003180 */
Rob Spies06533ba2014-04-24 11:20:37 -07003181hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3182 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003183 this.restyleCursor_();
Michael Kelly485ecd12014-06-09 11:41:56 -04003184 if (focused === true)
3185 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003186};
3187
3188/**
rginda8ba33642011-12-14 12:31:31 -08003189 * React when the ScrollPort is scrolled.
3190 */
3191hterm.Terminal.prototype.onScroll_ = function() {
3192 this.scheduleSyncCursorPosition_();
3193};
3194
3195/**
rginda9846e2f2012-01-27 13:53:33 -08003196 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003197 *
3198 * @param {Event} e The DOM paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003199 */
3200hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07003201 var data = e.text.replace(/\n/mg, '\r');
Connor Hegartyf525ceb2014-09-03 13:36:03 -07003202 data = this.keyboard.encode(data);
Robert Gindaa063b202014-07-21 11:08:25 -07003203 if (this.options_.bracketedPaste)
3204 data = '\x1b[200~' + data + '\x1b[201~';
3205
3206 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003207};
3208
3209/**
rgindaa09e7332012-08-17 12:49:51 -07003210 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003211 *
3212 * @param {Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003213 */
3214hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003215 if (!this.useDefaultWindowCopy) {
3216 e.preventDefault();
3217 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3218 }
rgindaa09e7332012-08-17 12:49:51 -07003219};
3220
3221/**
rginda8ba33642011-12-14 12:31:31 -08003222 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003223 *
3224 * Note: This function should not directly contain code that alters the internal
3225 * state of the terminal. That kind of code belongs in realizeWidth or
3226 * realizeHeight, so that it can be executed synchronously in the case of a
3227 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003228 */
3229hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003230 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003231 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003232 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003233 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003234
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003235 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003236 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003237 // gets removed from the document or during the initial load, and we can't
3238 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003239 // This can also happen if called before the scrollPort calculates the
3240 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003241 return;
3242 }
3243
rgindaa8ba17d2012-08-15 14:41:10 -07003244 var isNewSize = (columnCount != this.screenSize.width ||
3245 rowCount != this.screenSize.height);
3246
3247 // We do this even if the size didn't change, just to be sure everything is
3248 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003249 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003250 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003251
3252 if (isNewSize)
3253 this.overlaySize();
3254
Robert Gindafb1be6a2013-12-11 11:56:22 -08003255 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003256 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08003257};
3258
3259/**
3260 * Service the cursor blink timeout.
3261 */
3262hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003263 if (!this.options_.cursorBlink) {
3264 delete this.timeouts_.cursorBlink;
3265 return;
3266 }
3267
Robert Ginda830583c2013-08-07 13:20:46 -07003268 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3269 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003270 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003271 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3272 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003273 } else {
rginda87b86462011-12-14 13:48:03 -08003274 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003275 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3276 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003277 }
3278};
David Reveman8f552492012-03-28 12:18:41 -04003279
3280/**
3281 * Set the scrollbar-visible mode bit.
3282 *
3283 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3284 * Otherwise it will not.
3285 *
3286 * Defaults to on.
3287 *
3288 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3289 */
3290hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3291 this.scrollPort_.setScrollbarVisible(state);
3292};
Michael Kelly485ecd12014-06-09 11:41:56 -04003293
3294/**
Rob Spies49039e52014-12-17 13:40:04 -08003295 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003296 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003297 *
3298 * Defaults to 1.
3299 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003300 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003301 */
3302hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3303 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3304};
3305
3306/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003307 * Close all web notifications created by terminal bells.
3308 */
3309hterm.Terminal.prototype.closeBellNotifications_ = function() {
3310 this.bellNotificationList_.forEach(function(n) {
3311 n.close();
3312 });
3313 this.bellNotificationList_.length = 0;
3314};