blob: 5e930851c315ffc140186f305c2191fe2a1ea914 [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) {
Mike Frysingera1371e12017-08-17 01:37:17 -0400542 terminal.vt.setEncoding(v);
Mike Frysingera7768922017-07-28 15:00:12 -0400543 },
544
Robert Gindae76aa9f2014-03-14 12:29:12 -0700545 'user-css': function(v) {
Mike Frysinger08bad432017-04-24 00:50:54 -0400546 terminal.scrollPort_.setUserCssUrl(v);
547 },
548
549 'user-css-text': function(v) {
550 terminal.scrollPort_.setUserCssText(v);
551 },
Mike Frysinger664e9992017-05-19 01:24:24 -0400552
553 'word-break-match-left': function(v) {
554 terminal.primaryScreen_.wordBreakMatchLeft = v;
555 terminal.alternateScreen_.wordBreakMatchLeft = v;
556 },
557
558 'word-break-match-right': function(v) {
559 terminal.primaryScreen_.wordBreakMatchRight = v;
560 terminal.alternateScreen_.wordBreakMatchRight = v;
561 },
562
563 'word-break-match-middle': function(v) {
564 terminal.primaryScreen_.wordBreakMatchMiddle = v;
565 terminal.alternateScreen_.wordBreakMatchMiddle = v;
566 },
Robert Ginda57f03b42012-09-13 11:02:48 -0700567 });
rginda30f20f62012-04-05 16:36:19 -0700568
Robert Ginda57f03b42012-09-13 11:02:48 -0700569 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800570 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700571
572 if (opt_callback)
573 opt_callback();
574 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800575};
576
Rob Spies56953412014-04-28 14:09:47 -0700577
578/**
579 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500580 *
581 * @return {hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700582 */
583hterm.Terminal.prototype.getPrefs = function() {
584 return this.prefs_;
585};
586
Robert Gindaa063b202014-07-21 11:08:25 -0700587/**
588 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500589 *
590 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700591 */
592hterm.Terminal.prototype.setBracketedPaste = function(state) {
593 this.options_.bracketedPaste = state;
594};
Rob Spies56953412014-04-28 14:09:47 -0700595
rginda8e92a692012-05-20 19:37:20 -0700596/**
597 * Set the color for the cursor.
598 *
599 * If you want this setting to persist, set it through prefs_, rather than
600 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500601 *
602 * @param {string} color The color to set.
rginda8e92a692012-05-20 19:37:20 -0700603 */
604hterm.Terminal.prototype.setCursorColor = function(color) {
Robert Ginda830583c2013-08-07 13:20:46 -0700605 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700606 this.cursorNode_.style.backgroundColor = color;
607 this.cursorNode_.style.borderColor = color;
608};
609
610/**
611 * Return the current cursor color as a string.
Evan Jones2600d4f2016-12-06 09:29:36 -0500612 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700613 */
614hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700615 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700616};
617
618/**
rgindad5613292012-06-19 15:40:37 -0700619 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500620 *
621 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700622 */
623hterm.Terminal.prototype.setSelectionEnabled = function(state) {
624 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700625};
626
627/**
rginda8e92a692012-05-20 19:37:20 -0700628 * Set the background color.
629 *
630 * If you want this setting to persist, set it through prefs_, rather than
631 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500632 *
633 * @param {string} color The color to set.
rginda8e92a692012-05-20 19:37:20 -0700634 */
635hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700636 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700637 this.primaryScreen_.textAttributes.setDefaults(
638 this.foregroundColor_, this.backgroundColor_);
639 this.alternateScreen_.textAttributes.setDefaults(
640 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700641 this.scrollPort_.setBackgroundColor(color);
642};
643
rginda9f5222b2012-03-05 11:53:28 -0800644/**
645 * Return the current terminal background color.
646 *
647 * Intended for use by other classes, so we don't have to expose the entire
648 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500649 *
650 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800651 */
652hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700653 return this.backgroundColor_;
654};
655
656/**
657 * Set the foreground color.
658 *
659 * If you want this setting to persist, set it through prefs_, rather than
660 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500661 *
662 * @param {string} color The color to set.
rginda8e92a692012-05-20 19:37:20 -0700663 */
664hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700665 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700666 this.primaryScreen_.textAttributes.setDefaults(
667 this.foregroundColor_, this.backgroundColor_);
668 this.alternateScreen_.textAttributes.setDefaults(
669 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700670 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800671};
672
673/**
674 * Return the current terminal foreground color.
675 *
676 * Intended for use by other classes, so we don't have to expose the entire
677 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500678 *
679 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800680 */
681hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700682 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800683};
684
685/**
rginda87b86462011-12-14 13:48:03 -0800686 * Create a new instance of a terminal command and run it with a given
687 * argument string.
688 *
689 * @param {function} commandClass The constructor for a terminal command.
690 * @param {string} argString The argument string to pass to the command.
691 */
692hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700693 var environment = this.prefs_.get('environment');
694 if (typeof environment != 'object' || environment == null)
695 environment = {};
696
rginda87b86462011-12-14 13:48:03 -0800697 var self = this;
698 this.command = new commandClass(
699 { argString: argString || '',
700 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700701 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800702 onExit: function(code) {
703 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800704 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700705 if (self.prefs_.get('close-on-exit'))
706 window.close();
rginda87b86462011-12-14 13:48:03 -0800707 }
708 });
709
rgindafeaf3142012-01-31 15:14:20 -0800710 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800711 this.command.run();
712};
713
714/**
rgindafeaf3142012-01-31 15:14:20 -0800715 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500716 *
717 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800718 */
719hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700720 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800721};
722
723/**
724 * Install the keyboard handler for this terminal.
725 *
726 * This will prevent the browser from seeing any keystrokes sent to the
727 * terminal.
728 */
729hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700730 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
rgindafeaf3142012-01-31 15:14:20 -0800731}
732
733/**
734 * Uninstall the keyboard handler for this terminal.
735 */
736hterm.Terminal.prototype.uninstallKeyboard = function() {
737 this.keyboard.installKeyboard(null);
738}
739
740/**
Mike Frysingercce97c42017-08-05 01:11:22 -0400741 * Set a CSS variable.
742 *
743 * Normally this is used to set variables in the hterm namespace.
744 *
745 * @param {string} name The variable to set.
746 * @param {string} value The value to assign to the variable.
747 * @param {string?} opt_prefix The variable namespace/prefix to use.
748 */
749hterm.Terminal.prototype.setCssVar = function(name, value,
750 opt_prefix='--hterm-') {
751 this.document_.documentElement.style.setProperty(
752 `${opt_prefix}${name}`, value);
753};
754
755/**
rginda35c456b2012-02-09 17:29:05 -0800756 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800757 *
758 * Call setFontSize(0) to reset to the default font size.
759 *
760 * This function does not modify the font-size preference.
761 *
762 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800763 */
764hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800765 if (px === 0)
766 px = this.prefs_.get('font-size');
767
rginda35c456b2012-02-09 17:29:05 -0800768 this.scrollPort_.setFontSize(px);
Mike Frysingercce97c42017-08-05 01:11:22 -0400769 this.setCssVar('charsize-width', this.scrollPort_.characterSize.width + 'px');
770 this.setCssVar('charsize-height',
771 this.scrollPort_.characterSize.height + 'px');
rginda35c456b2012-02-09 17:29:05 -0800772};
773
774/**
775 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500776 *
777 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800778 */
779hterm.Terminal.prototype.getFontSize = function() {
780 return this.scrollPort_.getFontSize();
781};
782
783/**
rginda8e92a692012-05-20 19:37:20 -0700784 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500785 *
786 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700787 */
788hterm.Terminal.prototype.getFontFamily = function() {
789 return this.scrollPort_.getFontFamily();
790};
791
792/**
rginda35c456b2012-02-09 17:29:05 -0800793 * Set the CSS "font-family" for this terminal.
794 */
rginda9f5222b2012-03-05 11:53:28 -0800795hterm.Terminal.prototype.syncFontFamily = function() {
796 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
797 this.prefs_.get('font-smoothing'));
798 this.syncBoldSafeState();
799};
800
rginda4bba5e12012-06-20 16:15:30 -0700801/**
802 * Set this.mousePasteButton based on the mouse-paste-button pref,
803 * autodetecting if necessary.
804 */
805hterm.Terminal.prototype.syncMousePasteButton = function() {
806 var button = this.prefs_.get('mouse-paste-button');
807 if (typeof button == 'number') {
808 this.mousePasteButton = button;
809 return;
810 }
811
812 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
Mike Frysinger98dd15b2017-05-18 22:52:23 -0400813 if (!ary || ary[1] == 'CrOS') {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400814 this.mousePasteButton = 1; // Middle mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700815 } else {
Mike Frysinger2edd3612017-05-24 00:54:39 -0400816 this.mousePasteButton = 2; // Right mouse button.
rginda4bba5e12012-06-20 16:15:30 -0700817 }
818};
819
820/**
821 * Enable or disable bold based on the enable-bold pref, autodetecting if
822 * necessary.
823 */
rginda9f5222b2012-03-05 11:53:28 -0800824hterm.Terminal.prototype.syncBoldSafeState = function() {
825 var enableBold = this.prefs_.get('enable-bold');
826 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700827 this.primaryScreen_.textAttributes.enableBold = enableBold;
828 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800829 return;
830 }
831
rgindaf7521392012-02-28 17:20:34 -0800832 var normalSize = this.scrollPort_.measureCharacterSize();
833 var boldSize = this.scrollPort_.measureCharacterSize('bold');
834
835 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800836 if (!isBoldSafe) {
837 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700838 'from normal. Font family is: ' +
839 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800840 }
rginda9f5222b2012-03-05 11:53:28 -0800841
Robert Gindaed016262012-10-26 16:27:09 -0700842 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
843 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800844};
845
846/**
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400847 * Enable or disable blink based on the enable-blink pref.
848 */
849hterm.Terminal.prototype.syncBlinkState = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400850 this.setCssVar('node-duration',
851 this.prefs_.get('enable-blink') ? '0.7s' : '0');
Mike Frysinger93b75ba2017-04-05 19:43:18 -0400852};
853
854/**
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400855 * Set the mouse cursor style based on the current terminal mode.
856 */
857hterm.Terminal.prototype.syncMouseStyle = function() {
Mike Frysingercce97c42017-08-05 01:11:22 -0400858 this.setCssVar('mouse-cursor-style',
859 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED ?
860 'var(--hterm-mouse-cursor-text)' :
861 'var(--hterm-mouse-cursor-pointer)');
Mike Frysinger6ab275c2017-05-28 12:48:44 -0400862};
863
864/**
rginda87b86462011-12-14 13:48:03 -0800865 * Return a copy of the current cursor position.
866 *
867 * @return {hterm.RowCol} The RowCol object representing the current position.
868 */
869hterm.Terminal.prototype.saveCursor = function() {
870 return this.screen_.cursorPosition.clone();
871};
872
Evan Jones2600d4f2016-12-06 09:29:36 -0500873/**
874 * Return the current text attributes.
875 *
876 * @return {string}
877 */
rgindaa19afe22012-01-25 15:40:22 -0800878hterm.Terminal.prototype.getTextAttributes = function() {
879 return this.screen_.textAttributes;
880};
881
Evan Jones2600d4f2016-12-06 09:29:36 -0500882/**
883 * Set the text attributes.
884 *
885 * @param {string} textAttributes The attributes to set.
886 */
rginda1a09aa02012-06-18 21:11:25 -0700887hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
888 this.screen_.textAttributes = textAttributes;
889};
890
rginda87b86462011-12-14 13:48:03 -0800891/**
rgindaf522ce02012-04-17 17:49:17 -0700892 * Return the current browser zoom factor applied to the terminal.
893 *
894 * @return {number} The current browser zoom factor.
895 */
896hterm.Terminal.prototype.getZoomFactor = function() {
897 return this.scrollPort_.characterSize.zoomFactor;
898};
899
900/**
rginda9846e2f2012-01-27 13:53:33 -0800901 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500902 *
903 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800904 */
905hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800906 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800907};
908
909/**
rginda87b86462011-12-14 13:48:03 -0800910 * Restore a previously saved cursor position.
911 *
912 * @param {hterm.RowCol} cursor The position to restore.
913 */
914hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700915 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
916 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800917 this.screen_.setCursorPosition(row, column);
918 if (cursor.column > column ||
919 cursor.column == column && cursor.overflow) {
920 this.screen_.cursorPosition.overflow = true;
921 }
rginda87b86462011-12-14 13:48:03 -0800922};
923
924/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400925 * Clear the cursor's overflow flag.
926 */
927hterm.Terminal.prototype.clearCursorOverflow = function() {
928 this.screen_.cursorPosition.overflow = false;
929};
930
931/**
Robert Ginda830583c2013-08-07 13:20:46 -0700932 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -0500933 *
934 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -0700935 */
936hterm.Terminal.prototype.setCursorShape = function(shape) {
937 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -0800938 this.restyleCursor_();
Robert Ginda830583c2013-08-07 13:20:46 -0700939}
940
941/**
942 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -0500943 *
944 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -0700945 */
946hterm.Terminal.prototype.getCursorShape = function() {
947 return this.cursorShape_;
948}
949
950/**
rginda87b86462011-12-14 13:48:03 -0800951 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -0500952 *
953 * @param {number} columnCount
rginda87b86462011-12-14 13:48:03 -0800954 */
955hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800956 if (columnCount == null) {
957 this.div_.style.width = '100%';
958 return;
959 }
960
Robert Ginda26806d12014-07-24 13:44:07 -0700961 this.div_.style.width = Math.ceil(
962 this.scrollPort_.characterSize.width *
963 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400964 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800965 this.scheduleSyncCursorPosition_();
966};
rginda87b86462011-12-14 13:48:03 -0800967
rgindac9bc5502012-01-18 11:48:44 -0800968/**
rginda35c456b2012-02-09 17:29:05 -0800969 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -0500970 *
971 * @param {number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -0800972 */
973hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800974 if (rowCount == null) {
975 this.div_.style.height = '100%';
976 return;
977 }
978
rginda35c456b2012-02-09 17:29:05 -0800979 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700980 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800981 this.realizeSize_(this.screenSize.width, rowCount);
982 this.scheduleSyncCursorPosition_();
983};
984
985/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400986 * Deal with terminal size changes.
987 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500988 * @param {number} columnCount The number of columns.
989 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400990 */
991hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
992 if (columnCount != this.screenSize.width)
993 this.realizeWidth_(columnCount);
994
995 if (rowCount != this.screenSize.height)
996 this.realizeHeight_(rowCount);
997
998 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -0700999 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04001000};
1001
1002/**
rgindac9bc5502012-01-18 11:48:44 -08001003 * Deal with terminal width changes.
1004 *
1005 * This function does what needs to be done when the terminal width changes
1006 * out from under us. It happens here rather than in onResize_() because this
1007 * code may need to run synchronously to handle programmatic changes of
1008 * terminal width.
1009 *
1010 * Relying on the browser to send us an async resize event means we may not be
1011 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001012 *
1013 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -08001014 */
1015hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001016 if (columnCount <= 0)
1017 throw new Error('Attempt to realize bad width: ' + columnCount);
1018
rgindac9bc5502012-01-18 11:48:44 -08001019 var deltaColumns = columnCount - this.screen_.getWidth();
1020
rginda87b86462011-12-14 13:48:03 -08001021 this.screenSize.width = columnCount;
1022 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -08001023
1024 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -04001025 if (this.defaultTabStops)
1026 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -08001027 } else {
1028 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -04001029 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -08001030 break;
1031
1032 this.tabStops_.pop();
1033 }
1034 }
1035
1036 this.screen_.setColumnCount(this.screenSize.width);
1037};
1038
1039/**
1040 * Deal with terminal height changes.
1041 *
1042 * This function does what needs to be done when the terminal height changes
1043 * out from under us. It happens here rather than in onResize_() because this
1044 * code may need to run synchronously to handle programmatic changes of
1045 * terminal height.
1046 *
1047 * Relying on the browser to send us an async resize event means we may not be
1048 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -05001049 *
1050 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -08001051 */
1052hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -07001053 if (rowCount <= 0)
1054 throw new Error('Attempt to realize bad height: ' + rowCount);
1055
rgindac9bc5502012-01-18 11:48:44 -08001056 var deltaRows = rowCount - this.screen_.getHeight();
1057
1058 this.screenSize.height = rowCount;
1059
1060 var cursor = this.saveCursor();
1061
1062 if (deltaRows < 0) {
1063 // Screen got smaller.
1064 deltaRows *= -1;
1065 while (deltaRows) {
1066 var lastRow = this.getRowCount() - 1;
1067 if (lastRow - this.scrollbackRows_.length == cursor.row)
1068 break;
1069
1070 if (this.getRowText(lastRow))
1071 break;
1072
1073 this.screen_.popRow();
1074 deltaRows--;
1075 }
1076
1077 var ary = this.screen_.shiftRows(deltaRows);
1078 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1079
1080 // We just removed rows from the top of the screen, we need to update
1081 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001082 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001083 } else if (deltaRows > 0) {
1084 // Screen got larger.
1085
1086 if (deltaRows <= this.scrollbackRows_.length) {
1087 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1088 var rows = this.scrollbackRows_.splice(
1089 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1090 this.screen_.unshiftRows(rows);
1091 deltaRows -= scrollbackCount;
1092 cursor.row += scrollbackCount;
1093 }
1094
1095 if (deltaRows)
1096 this.appendRows_(deltaRows);
1097 }
1098
rginda35c456b2012-02-09 17:29:05 -08001099 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001100 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001101};
1102
1103/**
1104 * Scroll the terminal to the top of the scrollback buffer.
1105 */
1106hterm.Terminal.prototype.scrollHome = function() {
1107 this.scrollPort_.scrollRowToTop(0);
1108};
1109
1110/**
1111 * Scroll the terminal to the end.
1112 */
1113hterm.Terminal.prototype.scrollEnd = function() {
1114 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1115};
1116
1117/**
1118 * Scroll the terminal one page up (minus one line) relative to the current
1119 * position.
1120 */
1121hterm.Terminal.prototype.scrollPageUp = function() {
1122 var i = this.scrollPort_.getTopRowIndex();
1123 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
1124};
1125
1126/**
1127 * Scroll the terminal one page down (minus one line) relative to the current
1128 * position.
1129 */
1130hterm.Terminal.prototype.scrollPageDown = function() {
1131 var i = this.scrollPort_.getTopRowIndex();
1132 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -08001133};
1134
rgindac9bc5502012-01-18 11:48:44 -08001135/**
Mike Frysingercd56a632017-05-10 14:45:28 -04001136 * Scroll the terminal one line up relative to the current position.
1137 */
1138hterm.Terminal.prototype.scrollLineUp = function() {
1139 var i = this.scrollPort_.getTopRowIndex();
1140 this.scrollPort_.scrollRowToTop(i - 1);
1141};
1142
1143/**
1144 * Scroll the terminal one line down relative to the current position.
1145 */
1146hterm.Terminal.prototype.scrollLineDown = function() {
1147 var i = this.scrollPort_.getTopRowIndex();
1148 this.scrollPort_.scrollRowToTop(i + 1);
1149};
1150
1151/**
Robert Ginda40932892012-12-10 17:26:40 -08001152 * Clear primary screen, secondary screen, and the scrollback buffer.
1153 */
1154hterm.Terminal.prototype.wipeContents = function() {
1155 this.scrollbackRows_.length = 0;
1156 this.scrollPort_.resetCache();
1157
1158 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
1159 var bottom = screen.getHeight();
1160 if (bottom > 0) {
1161 this.renumberRows_(0, bottom);
1162 this.clearHome(screen);
1163 }
1164 }.bind(this));
1165
1166 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001167 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001168};
1169
1170/**
rgindac9bc5502012-01-18 11:48:44 -08001171 * Full terminal reset.
1172 */
rginda87b86462011-12-14 13:48:03 -08001173hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -08001174 this.clearAllTabStops();
1175 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001176
1177 this.clearHome(this.primaryScreen_);
1178 this.primaryScreen_.textAttributes.reset();
1179
1180 this.clearHome(this.alternateScreen_);
1181 this.alternateScreen_.textAttributes.reset();
1182
rgindab8bc8932012-04-27 12:45:03 -07001183 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1184
Robert Ginda92e18102013-03-14 13:56:37 -07001185 this.vt.reset();
1186
rgindac9bc5502012-01-18 11:48:44 -08001187 this.softReset();
rginda87b86462011-12-14 13:48:03 -08001188};
1189
rgindac9bc5502012-01-18 11:48:44 -08001190/**
1191 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001192 *
1193 * Perform a soft reset to the default values listed in
1194 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001195 */
rginda0f5c0292012-01-13 11:00:13 -08001196hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -07001197 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001198 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001199
Brad Townb62dfdc2015-03-16 19:07:15 -07001200 // We show the cursor on soft reset but do not alter the blink state.
1201 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1202
rgindab8bc8932012-04-27 12:45:03 -07001203 // Xterm also resets the color palette on soft reset, even though it doesn't
1204 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -07001205 this.primaryScreen_.textAttributes.resetColorPalette();
1206 this.alternateScreen_.textAttributes.resetColorPalette();
1207
rgindab8bc8932012-04-27 12:45:03 -07001208 // The xterm man page explicitly says this will happen on soft reset.
1209 this.setVTScrollRegion(null, null);
1210
1211 // Xterm also shows the cursor on soft reset, but does not alter the blink
1212 // state.
rgindaa19afe22012-01-25 15:40:22 -08001213 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001214};
1215
rgindac9bc5502012-01-18 11:48:44 -08001216/**
1217 * Move the cursor forward to the next tab stop, or to the last column
1218 * if no more tab stops are set.
1219 */
1220hterm.Terminal.prototype.forwardTabStop = function() {
1221 var column = this.screen_.cursorPosition.column;
1222
1223 for (var i = 0; i < this.tabStops_.length; i++) {
1224 if (this.tabStops_[i] > column) {
1225 this.setCursorColumn(this.tabStops_[i]);
1226 return;
1227 }
1228 }
1229
David Benjamin66e954d2012-05-05 21:08:12 -04001230 // xterm does not clear the overflow flag on HT or CHT.
1231 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001232 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001233 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001234};
1235
rgindac9bc5502012-01-18 11:48:44 -08001236/**
1237 * Move the cursor backward to the previous tab stop, or to the first column
1238 * if no previous tab stops are set.
1239 */
1240hterm.Terminal.prototype.backwardTabStop = function() {
1241 var column = this.screen_.cursorPosition.column;
1242
1243 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1244 if (this.tabStops_[i] < column) {
1245 this.setCursorColumn(this.tabStops_[i]);
1246 return;
1247 }
1248 }
1249
1250 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001251};
1252
rgindac9bc5502012-01-18 11:48:44 -08001253/**
1254 * Set a tab stop at the given column.
1255 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001256 * @param {integer} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001257 */
1258hterm.Terminal.prototype.setTabStop = function(column) {
1259 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1260 if (this.tabStops_[i] == column)
1261 return;
1262
1263 if (this.tabStops_[i] < column) {
1264 this.tabStops_.splice(i + 1, 0, column);
1265 return;
1266 }
1267 }
1268
1269 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001270};
1271
rgindac9bc5502012-01-18 11:48:44 -08001272/**
1273 * Clear the tab stop at the current cursor position.
1274 *
1275 * No effect if there is no tab stop at the current cursor position.
1276 */
1277hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1278 var column = this.screen_.cursorPosition.column;
1279
1280 var i = this.tabStops_.indexOf(column);
1281 if (i == -1)
1282 return;
1283
1284 this.tabStops_.splice(i, 1);
1285};
1286
1287/**
1288 * Clear all tab stops.
1289 */
1290hterm.Terminal.prototype.clearAllTabStops = function() {
1291 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001292 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001293};
1294
1295/**
1296 * Set up the default tab stops, starting from a given column.
1297 *
1298 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001299 * from the specified column, or 0 if no column is provided. It also flags
1300 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001301 *
1302 * This does not clear the existing tab stops first, use clearAllTabStops
1303 * for that.
1304 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001305 * @param {integer} opt_start Optional starting zero based starting column, useful
rgindac9bc5502012-01-18 11:48:44 -08001306 * for filling out missing tab stops when the terminal is resized.
1307 */
1308hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1309 var start = opt_start || 0;
1310 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001311 // Round start up to a default tab stop.
1312 start = start - 1 - ((start - 1) % w) + w;
1313 for (var i = start; i < this.screenSize.width; i += w) {
1314 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001315 }
David Benjamin66e954d2012-05-05 21:08:12 -04001316
1317 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001318};
1319
rginda6d397402012-01-17 10:58:29 -08001320/**
rginda8ba33642011-12-14 12:31:31 -08001321 * Interpret a sequence of characters.
1322 *
1323 * Incomplete escape sequences are buffered until the next call.
1324 *
1325 * @param {string} str Sequence of characters to interpret or pass through.
1326 */
1327hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001328 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001329 this.scheduleSyncCursorPosition_();
1330};
1331
1332/**
1333 * Take over the given DIV for use as the terminal display.
1334 *
1335 * @param {HTMLDivElement} div The div to use as the terminal display.
1336 */
1337hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001338 this.div_ = div;
1339
rginda8ba33642011-12-14 12:31:31 -08001340 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001341 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001342 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1343 this.scrollPort_.setBackgroundPosition(
1344 this.prefs_.get('background-position'));
Mike Frysinger08bad432017-04-24 00:50:54 -04001345 this.scrollPort_.setUserCssUrl(this.prefs_.get('user-css'));
1346 this.scrollPort_.setUserCssText(this.prefs_.get('user-css-text'));
rginda30f20f62012-04-05 16:36:19 -07001347
rginda0918b652012-04-04 11:26:24 -07001348 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001349
rginda9f5222b2012-03-05 11:53:28 -08001350 this.setFontSize(this.prefs_.get('font-size'));
1351 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001352
David Reveman8f552492012-03-28 12:18:41 -04001353 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001354 this.setScrollWheelMoveMultipler(
1355 this.prefs_.get('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001356
rginda8ba33642011-12-14 12:31:31 -08001357 this.document_ = this.scrollPort_.getDocument();
1358
Evan Jones5f9df812016-12-06 09:38:58 -05001359 this.document_.body.oncontextmenu = function() { return false; };
rginda4bba5e12012-06-20 16:15:30 -07001360
1361 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001362 var screenNode = this.scrollPort_.getScreenNode();
1363 screenNode.addEventListener('mousedown', onMouse);
1364 screenNode.addEventListener('mouseup', onMouse);
1365 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001366 this.scrollPort_.onScrollWheel = onMouse;
1367
Toni Barzic0bfa8922013-11-22 11:18:35 -08001368 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001369 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001370 // Listen for mousedown events on the screenNode as in FF the focus
1371 // events don't bubble.
1372 screenNode.addEventListener('mousedown', function() {
1373 setTimeout(this.onFocusChange_.bind(this, true));
1374 }.bind(this));
1375
Toni Barzic0bfa8922013-11-22 11:18:35 -08001376 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001377 'blur', this.onFocusChange_.bind(this, false));
1378
1379 var style = this.document_.createElement('style');
1380 style.textContent =
1381 ('.cursor-node[focus="false"] {' +
1382 ' box-sizing: border-box;' +
1383 ' background-color: transparent !important;' +
1384 ' border-width: 2px;' +
1385 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001386 '}' +
1387 '.wc-node {' +
1388 ' display: inline-block;' +
1389 ' text-align: center;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001390 ' width: calc(var(--hterm-charsize-width) * 2);' +
Mike Frysinger4036f6e2017-05-31 14:02:55 -04001391 ' line-height: var(--hterm-charsize-height);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001392 '}' +
1393 ':root {' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001394 ' --hterm-charsize-width: ' + this.scrollPort_.characterSize.width + 'px;' +
1395 ' --hterm-charsize-height: ' + this.scrollPort_.characterSize.height + 'px;' +
Mike Frysingera27c0502017-08-23 22:37:10 -04001396 // Default position hides the cursor for when the window is initializing.
1397 ' --hterm-cursor-offset-col: -1;' +
1398 ' --hterm-cursor-offset-row: -1;' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001399 ' --hterm-blink-node-duration: 0.7s;' +
Mike Frysinger6ab275c2017-05-28 12:48:44 -04001400 ' --hterm-mouse-cursor-text: text;' +
1401 ' --hterm-mouse-cursor-pointer: default;' +
1402 ' --hterm-mouse-cursor-style: var(--hterm-mouse-cursor-text);' +
Mike Frysinger93b75ba2017-04-05 19:43:18 -04001403 '}' +
1404 '@keyframes blink {' +
1405 ' from { opacity: 1.0; }' +
1406 ' to { opacity: 0.0; }' +
1407 '}' +
1408 '.blink-node {' +
1409 ' animation-name: blink;' +
1410 ' animation-duration: var(--hterm-blink-node-duration);' +
1411 ' animation-iteration-count: infinite;' +
1412 ' animation-timing-function: ease-in-out;' +
1413 ' animation-direction: alternate;' +
rginda8e92a692012-05-20 19:37:20 -07001414 '}');
1415 this.document_.head.appendChild(style);
1416
rginda8ba33642011-12-14 12:31:31 -08001417 this.cursorNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001418 this.cursorNode_.id = 'hterm:terminal-cursor';
rginda8e92a692012-05-20 19:37:20 -07001419 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001420 this.cursorNode_.style.cssText =
1421 ('position: absolute;' +
Mike Frysinger44c32202017-08-05 01:13:09 -04001422 'left: calc(var(--hterm-charsize-width) * var(--hterm-cursor-offset-col));' +
1423 'top: calc(var(--hterm-charsize-height) * var(--hterm-cursor-offset-row));' +
rginda87b86462011-12-14 13:48:03 -08001424 'display: block;' +
Mike Frysinger66beb0b2017-05-30 19:44:51 -04001425 'width: var(--hterm-charsize-width);' +
1426 'height: var(--hterm-charsize-height);' +
Rob Spies06533ba2014-04-24 11:20:37 -07001427 '-webkit-transition: opacity, background-color 100ms linear;' +
1428 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001429
rginda8e92a692012-05-20 19:37:20 -07001430 this.setCursorColor(this.prefs_.get('cursor-color'));
Robert Gindafb1be6a2013-12-11 11:56:22 -08001431 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1432 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001433
rginda8ba33642011-12-14 12:31:31 -08001434 this.document_.body.appendChild(this.cursorNode_);
1435
rgindad5613292012-06-19 15:40:37 -07001436 // When 'enableMouseDragScroll' is off we reposition this element directly
1437 // under the mouse cursor after a click. This makes Chrome associate
1438 // subsequent mousemove events with the scroll-blocker. Since the
1439 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1440 // events do not cause the scrollport to scroll.
1441 //
1442 // It's a hack, but it's the cleanest way I could find.
1443 this.scrollBlockerNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04001444 this.scrollBlockerNode_.id = 'hterm:mouse-drag-scroll-blocker';
rgindad5613292012-06-19 15:40:37 -07001445 this.scrollBlockerNode_.style.cssText =
1446 ('position: absolute;' +
1447 'top: -99px;' +
1448 'display: block;' +
1449 'width: 10px;' +
1450 'height: 10px;');
1451 this.document_.body.appendChild(this.scrollBlockerNode_);
1452
rgindad5613292012-06-19 15:40:37 -07001453 this.scrollPort_.onScrollWheel = onMouse;
1454 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1455 ].forEach(function(event) {
1456 this.scrollBlockerNode_.addEventListener(event, onMouse);
1457 this.cursorNode_.addEventListener(event, onMouse);
1458 this.document_.addEventListener(event, onMouse);
1459 }.bind(this));
1460
1461 this.cursorNode_.addEventListener('mousedown', function() {
1462 setTimeout(this.focus.bind(this));
1463 }.bind(this));
1464
rginda8ba33642011-12-14 12:31:31 -08001465 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001466
rginda87b86462011-12-14 13:48:03 -08001467 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001468 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001469};
1470
rginda0918b652012-04-04 11:26:24 -07001471/**
1472 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001473 *
1474 * @return {HTMLDocument}
rginda0918b652012-04-04 11:26:24 -07001475 */
rginda87b86462011-12-14 13:48:03 -08001476hterm.Terminal.prototype.getDocument = function() {
1477 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001478};
1479
1480/**
rginda0918b652012-04-04 11:26:24 -07001481 * Focus the terminal.
1482 */
1483hterm.Terminal.prototype.focus = function() {
1484 this.scrollPort_.focus();
1485};
1486
1487/**
rginda8ba33642011-12-14 12:31:31 -08001488 * Return the HTML Element for a given row index.
1489 *
1490 * This is a method from the RowProvider interface. The ScrollPort uses
1491 * it to fetch rows on demand as they are scrolled into view.
1492 *
1493 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1494 * pairs to conserve memory.
1495 *
1496 * @param {integer} index The zero-based row index, measured relative to the
1497 * start of the scrollback buffer. On-screen rows will always have the
Zhu Qunying30d40712017-03-14 16:27:00 -07001498 * largest indices.
rginda8ba33642011-12-14 12:31:31 -08001499 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1500 */
1501hterm.Terminal.prototype.getRowNode = function(index) {
1502 if (index < this.scrollbackRows_.length)
1503 return this.scrollbackRows_[index];
1504
1505 var screenIndex = index - this.scrollbackRows_.length;
1506 return this.screen_.rowsArray[screenIndex];
1507};
1508
1509/**
1510 * Return the text content for a given range of rows.
1511 *
1512 * This is a method from the RowProvider interface. The ScrollPort uses
1513 * it to fetch text content on demand when the user attempts to copy their
1514 * selection to the clipboard.
1515 *
1516 * @param {integer} start The zero-based row index to start from, measured
1517 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001518 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001519 * @param {integer} end The zero-based row index to end on, measured
1520 * relative to the start of the scrollback buffer.
1521 * @return {string} A single string containing the text value of the range of
1522 * rows. Lines will be newline delimited, with no trailing newline.
1523 */
1524hterm.Terminal.prototype.getRowsText = function(start, end) {
1525 var ary = [];
1526 for (var i = start; i < end; i++) {
1527 var node = this.getRowNode(i);
1528 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001529 if (i < end - 1 && !node.getAttribute('line-overflow'))
1530 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001531 }
1532
rgindaa09e7332012-08-17 12:49:51 -07001533 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001534};
1535
1536/**
1537 * Return the text content for a given row.
1538 *
1539 * This is a method from the RowProvider interface. The ScrollPort uses
1540 * it to fetch text content on demand when the user attempts to copy their
1541 * selection to the clipboard.
1542 *
1543 * @param {integer} index The zero-based row index to return, measured
1544 * relative to the start of the scrollback buffer. On-screen rows will
Zhu Qunying30d40712017-03-14 16:27:00 -07001545 * always have the largest indices.
rginda8ba33642011-12-14 12:31:31 -08001546 * @return {string} A string containing the text value of the selected row.
1547 */
1548hterm.Terminal.prototype.getRowText = function(index) {
1549 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001550 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001551};
1552
1553/**
1554 * Return the total number of rows in the addressable screen and in the
1555 * scrollback buffer of this terminal.
1556 *
1557 * This is a method from the RowProvider interface. The ScrollPort uses
1558 * it to compute the size of the scrollbar.
1559 *
1560 * @return {integer} The number of rows in this terminal.
1561 */
1562hterm.Terminal.prototype.getRowCount = function() {
1563 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1564};
1565
1566/**
1567 * Create DOM nodes for new rows and append them to the end of the terminal.
1568 *
1569 * This is the only correct way to add a new DOM node for a row. Notice that
1570 * the new row is appended to the bottom of the list of rows, and does not
1571 * require renumbering (of the rowIndex property) of previous rows.
1572 *
1573 * If you think you want a new blank row somewhere in the middle of the
1574 * terminal, look into moveRows_().
1575 *
1576 * This method does not pay attention to vtScrollTop/Bottom, since you should
1577 * be using moveRows() in cases where they would matter.
1578 *
1579 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001580 *
1581 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001582 */
1583hterm.Terminal.prototype.appendRows_ = function(count) {
1584 var cursorRow = this.screen_.rowsArray.length;
1585 var offset = this.scrollbackRows_.length + cursorRow;
1586 for (var i = 0; i < count; i++) {
1587 var row = this.document_.createElement('x-row');
1588 row.appendChild(this.document_.createTextNode(''));
1589 row.rowIndex = offset + i;
1590 this.screen_.pushRow(row);
1591 }
1592
1593 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1594 if (extraRows > 0) {
1595 var ary = this.screen_.shiftRows(extraRows);
1596 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001597 if (this.scrollPort_.isScrolledEnd)
1598 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001599 }
1600
1601 if (cursorRow >= this.screen_.rowsArray.length)
1602 cursorRow = this.screen_.rowsArray.length - 1;
1603
rginda87b86462011-12-14 13:48:03 -08001604 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001605};
1606
1607/**
1608 * Relocate rows from one part of the addressable screen to another.
1609 *
1610 * This is used to recycle rows during VT scrolls (those which are driven
1611 * by VT commands, rather than by the user manipulating the scrollbar.)
1612 *
1613 * In this case, the blank lines scrolled into the scroll region are made of
1614 * the nodes we scrolled off. These have their rowIndex properties carefully
1615 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001616 *
1617 * @param {number} fromIndex The start index.
1618 * @param {number} count The number of rows to move.
1619 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001620 */
1621hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1622 var ary = this.screen_.removeRows(fromIndex, count);
1623 this.screen_.insertRows(toIndex, ary);
1624
1625 var start, end;
1626 if (fromIndex < toIndex) {
1627 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001628 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001629 } else {
1630 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001631 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001632 }
1633
1634 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001635 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001636};
1637
1638/**
1639 * Renumber the rowIndex property of the given range of rows.
1640 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001641 * The start and end indices are relative to the screen, not the scrollback.
rginda8ba33642011-12-14 12:31:31 -08001642 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001643 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001644 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001645 *
1646 * @param {number} start The start index.
1647 * @param {number} end The end index.
1648 * @param {hterm.Screen} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001649 */
Robert Ginda40932892012-12-10 17:26:40 -08001650hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1651 var screen = opt_screen || this.screen_;
1652
rginda8ba33642011-12-14 12:31:31 -08001653 var offset = this.scrollbackRows_.length;
1654 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001655 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001656 }
1657};
1658
1659/**
1660 * Print a string to the terminal.
1661 *
1662 * This respects the current insert and wraparound modes. It will add new lines
1663 * to the end of the terminal, scrolling off the top into the scrollback buffer
1664 * if necessary.
1665 *
1666 * The string is *not* parsed for escape codes. Use the interpret() method if
1667 * that's what you're after.
1668 *
1669 * @param{string} str The string to print.
1670 */
1671hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001672 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001673
Ricky Liang48f05cb2013-12-31 23:35:29 +08001674 var strWidth = lib.wc.strWidth(str);
Mike Frysinger67fc8ef2017-08-21 16:03:16 -04001675 // Fun edge case: If the string only contains zero width codepoints (like
1676 // combining characters), we make sure to iterate at least once below.
1677 if (strWidth == 0 && str)
1678 strWidth = 1;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001679
1680 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001681 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1682 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001683 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001684 }
rgindaa19afe22012-01-25 15:40:22 -08001685
Ricky Liang48f05cb2013-12-31 23:35:29 +08001686 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001687 var didOverflow = false;
1688 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001689
rgindaa9abdd82012-08-06 18:05:09 -07001690 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1691 didOverflow = true;
1692 count = this.screenSize.width - this.screen_.cursorPosition.column;
1693 }
rgindaa19afe22012-01-25 15:40:22 -08001694
rgindaa9abdd82012-08-06 18:05:09 -07001695 if (didOverflow && !this.options_.wraparound) {
1696 // If the string overflowed the line but wraparound is off, then the
1697 // last printed character should be the last of the string.
1698 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001699 substr = lib.wc.substr(str, startOffset, count - 1) +
1700 lib.wc.substr(str, strWidth - 1);
1701 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001702 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001703 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001704 }
rgindaa19afe22012-01-25 15:40:22 -08001705
Ricky Liang48f05cb2013-12-31 23:35:29 +08001706 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1707 for (var i = 0; i < tokens.length; i++) {
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001708 this.screen_.textAttributes.wcNode = tokens[i].wcNode;
1709 this.screen_.textAttributes.asciiNode = tokens[i].asciiNode;
Ricky Liang48f05cb2013-12-31 23:35:29 +08001710
1711 if (this.options_.insertMode) {
1712 this.screen_.insertString(tokens[i].str);
1713 } else {
1714 this.screen_.overwriteString(tokens[i].str);
1715 }
1716 this.screen_.textAttributes.wcNode = false;
Mike Frysinger1e98c0f2017-08-15 01:21:31 -04001717 this.screen_.textAttributes.asciiNode = true;
rgindaa9abdd82012-08-06 18:05:09 -07001718 }
1719
1720 this.screen_.maybeClipCurrentRow();
1721 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001722 }
rginda8ba33642011-12-14 12:31:31 -08001723
1724 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001725
rginda9f5222b2012-03-05 11:53:28 -08001726 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001727 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001728};
1729
1730/**
rginda87b86462011-12-14 13:48:03 -08001731 * Set the VT scroll region.
1732 *
rginda87b86462011-12-14 13:48:03 -08001733 * This also resets the cursor position to the absolute (0, 0) position, since
1734 * that's what xterm appears to do.
1735 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001736 * Setting the scroll region to the full height of the terminal will clear
1737 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1738 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1739 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1740 * continue to work as most users would expect.
1741 *
rginda87b86462011-12-14 13:48:03 -08001742 * @param {integer} scrollTop The zero-based top of the scroll region.
1743 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1744 * inclusive.
1745 */
1746hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001747 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001748 this.vtScrollTop_ = null;
1749 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001750 } else {
1751 this.vtScrollTop_ = scrollTop;
1752 this.vtScrollBottom_ = scrollBottom;
1753 }
rginda87b86462011-12-14 13:48:03 -08001754};
1755
1756/**
rginda8ba33642011-12-14 12:31:31 -08001757 * Return the top row index according to the VT.
1758 *
1759 * This will return 0 unless the terminal has been told to restrict scrolling
1760 * to some lower row. It is used for some VT cursor positioning and scrolling
1761 * commands.
1762 *
1763 * @return {integer} The topmost row in the terminal's scroll region.
1764 */
1765hterm.Terminal.prototype.getVTScrollTop = function() {
1766 if (this.vtScrollTop_ != null)
1767 return this.vtScrollTop_;
1768
1769 return 0;
rginda87b86462011-12-14 13:48:03 -08001770};
rginda8ba33642011-12-14 12:31:31 -08001771
1772/**
1773 * Return the bottom row index according to the VT.
1774 *
1775 * This will return the height of the terminal unless the it has been told to
1776 * restrict scrolling to some higher row. It is used for some VT cursor
1777 * positioning and scrolling commands.
1778 *
Zhu Qunying30d40712017-03-14 16:27:00 -07001779 * @return {integer} The bottom most row in the terminal's scroll region.
rginda8ba33642011-12-14 12:31:31 -08001780 */
1781hterm.Terminal.prototype.getVTScrollBottom = function() {
1782 if (this.vtScrollBottom_ != null)
1783 return this.vtScrollBottom_;
1784
rginda87b86462011-12-14 13:48:03 -08001785 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001786}
1787
1788/**
1789 * Process a '\n' character.
1790 *
1791 * If the cursor is on the final row of the terminal this will append a new
1792 * blank row to the screen and scroll the topmost row into the scrollback
1793 * buffer.
1794 *
1795 * Otherwise, this moves the cursor to column zero of the next row.
1796 */
1797hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001798 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1799 this.screen_.rowsArray.length - 1);
1800
1801 if (this.vtScrollBottom_ != null) {
1802 // A VT Scroll region is active, we never append new rows.
1803 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1804 // We're at the end of the VT Scroll Region, perform a VT scroll.
1805 this.vtScrollUp(1);
1806 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1807 } else if (cursorAtEndOfScreen) {
1808 // We're at the end of the screen, the only thing to do is put the
1809 // cursor to column 0.
1810 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1811 } else {
1812 // Anywhere else, advance the cursor row, and reset the column.
1813 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1814 }
1815 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001816 // We're at the end of the screen. Append a new row to the terminal,
1817 // shifting the top row into the scrollback.
1818 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001819 } else {
rginda87b86462011-12-14 13:48:03 -08001820 // Anywhere else in the screen just moves the cursor.
1821 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001822 }
1823};
1824
1825/**
1826 * Like newLine(), except maintain the cursor column.
1827 */
1828hterm.Terminal.prototype.lineFeed = function() {
1829 var column = this.screen_.cursorPosition.column;
1830 this.newLine();
1831 this.setCursorColumn(column);
1832};
1833
1834/**
rginda87b86462011-12-14 13:48:03 -08001835 * If autoCarriageReturn is set then newLine(), else lineFeed().
1836 */
1837hterm.Terminal.prototype.formFeed = function() {
1838 if (this.options_.autoCarriageReturn) {
1839 this.newLine();
1840 } else {
1841 this.lineFeed();
1842 }
1843};
1844
1845/**
1846 * Move the cursor up one row, possibly inserting a blank line.
1847 *
1848 * The cursor column is not changed.
1849 */
1850hterm.Terminal.prototype.reverseLineFeed = function() {
1851 var scrollTop = this.getVTScrollTop();
1852 var currentRow = this.screen_.cursorPosition.row;
1853
1854 if (currentRow == scrollTop) {
1855 this.insertLines(1);
1856 } else {
1857 this.setAbsoluteCursorRow(currentRow - 1);
1858 }
1859};
1860
1861/**
rginda8ba33642011-12-14 12:31:31 -08001862 * Replace all characters to the left of the current cursor with the space
1863 * character.
1864 *
1865 * TODO(rginda): This should probably *remove* the characters (not just replace
1866 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001867 * position.
rginda8ba33642011-12-14 12:31:31 -08001868 */
1869hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001870 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001871 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001872 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001873 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001874};
1875
1876/**
David Benjamin684a9b72012-05-01 17:19:58 -04001877 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001878 *
1879 * The cursor position is unchanged.
1880 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001881 * If the current background color is not the default background color this
1882 * will insert spaces rather than delete. This is unfortunate because the
1883 * trailing space will affect text selection, but it's difficult to come up
1884 * with a way to style empty space that wouldn't trip up the hterm.Screen
1885 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001886 *
1887 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1888 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1889 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05001890 *
1891 * @param {number} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08001892 */
1893hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001894 if (this.screen_.cursorPosition.overflow)
1895 return;
1896
Robert Ginda7fd57082012-09-25 14:41:47 -07001897 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1898 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001899
1900 if (this.screen_.textAttributes.background ===
1901 this.screen_.textAttributes.DEFAULT_COLOR) {
1902 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08001903 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07001904 this.screen_.cursorPosition.column + count) {
1905 this.screen_.deleteChars(count);
1906 this.clearCursorOverflow();
1907 return;
1908 }
1909 }
1910
rginda87b86462011-12-14 13:48:03 -08001911 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001912 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001913 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001914 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001915};
1916
1917/**
1918 * Erase the current line.
1919 *
1920 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001921 */
1922hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001923 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001924 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001925 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001926 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001927};
1928
1929/**
David Benjamina08d78f2012-05-05 00:28:49 -04001930 * Erase all characters from the start of the screen to the current cursor
1931 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001932 *
1933 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001934 */
1935hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001936 var cursor = this.saveCursor();
1937
1938 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001939
David Benjamina08d78f2012-05-05 00:28:49 -04001940 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001941 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001942 this.screen_.clearCursorRow();
1943 }
1944
rginda87b86462011-12-14 13:48:03 -08001945 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001946 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001947};
1948
1949/**
1950 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001951 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001952 *
1953 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001954 */
1955hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001956 var cursor = this.saveCursor();
1957
1958 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001959
David Benjamina08d78f2012-05-05 00:28:49 -04001960 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001961 for (var i = cursor.row + 1; i <= bottom; i++) {
1962 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001963 this.screen_.clearCursorRow();
1964 }
1965
rginda87b86462011-12-14 13:48:03 -08001966 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001967 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001968};
1969
1970/**
1971 * Fill the terminal with a given character.
1972 *
1973 * This methods does not respect the VT scroll region.
1974 *
1975 * @param {string} ch The character to use for the fill.
1976 */
1977hterm.Terminal.prototype.fill = function(ch) {
1978 var cursor = this.saveCursor();
1979
1980 this.setAbsoluteCursorPosition(0, 0);
1981 for (var row = 0; row < this.screenSize.height; row++) {
1982 for (var col = 0; col < this.screenSize.width; col++) {
1983 this.setAbsoluteCursorPosition(row, col);
1984 this.screen_.overwriteString(ch);
1985 }
1986 }
1987
1988 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001989};
1990
1991/**
rginda9ea433c2012-03-16 11:57:00 -07001992 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001993 *
rginda9ea433c2012-03-16 11:57:00 -07001994 * This does not respect the scroll region.
1995 *
1996 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1997 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001998 */
rginda9ea433c2012-03-16 11:57:00 -07001999hterm.Terminal.prototype.clearHome = function(opt_screen) {
2000 var screen = opt_screen || this.screen_;
2001 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08002002
rginda11057d52012-04-25 12:29:56 -07002003 if (bottom == 0) {
2004 // Empty screen, nothing to do.
2005 return;
2006 }
2007
rgindae4d29232012-01-19 10:47:13 -08002008 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07002009 screen.setCursorPosition(i, 0);
2010 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08002011 }
2012
rginda9ea433c2012-03-16 11:57:00 -07002013 screen.setCursorPosition(0, 0);
2014};
2015
2016/**
2017 * Erase the entire display without changing the cursor position.
2018 *
2019 * The cursor position is unchanged. This does not respect the scroll
2020 * region.
2021 *
2022 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
2023 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07002024 */
2025hterm.Terminal.prototype.clear = function(opt_screen) {
2026 var screen = opt_screen || this.screen_;
2027 var cursor = screen.cursorPosition.clone();
2028 this.clearHome(screen);
2029 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08002030};
2031
2032/**
2033 * VT command to insert lines at the current cursor row.
2034 *
2035 * This respects the current scroll region. Rows pushed off the bottom are
2036 * lost (they won't show up in the scrollback buffer).
2037 *
rginda8ba33642011-12-14 12:31:31 -08002038 * @param {integer} count The number of lines to insert.
2039 */
2040hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07002041 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08002042
2043 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07002044 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08002045
Robert Ginda579186b2012-09-26 11:40:04 -07002046 // The moveCount is the number of rows we need to relocate to make room for
2047 // the new row(s). The count is the distance to move them.
2048 var moveCount = bottom - cursorRow - count + 1;
2049 if (moveCount)
2050 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08002051
Robert Ginda579186b2012-09-26 11:40:04 -07002052 for (var i = count - 1; i >= 0; i--) {
2053 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002054 this.screen_.clearCursorRow();
2055 }
rginda8ba33642011-12-14 12:31:31 -08002056};
2057
2058/**
2059 * VT command to delete lines at the current cursor row.
2060 *
2061 * New rows are added to the bottom of scroll region to take their place. New
2062 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05002063 *
2064 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08002065 */
2066hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08002067 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002068
rginda87b86462011-12-14 13:48:03 -08002069 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08002070 var bottom = this.getVTScrollBottom();
2071
rginda87b86462011-12-14 13:48:03 -08002072 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08002073 count = Math.min(count, maxCount);
2074
rginda87b86462011-12-14 13:48:03 -08002075 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08002076 if (count != maxCount)
2077 this.moveRows_(top, count, moveStart);
2078
2079 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08002080 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08002081 this.screen_.clearCursorRow();
2082 }
2083
rginda87b86462011-12-14 13:48:03 -08002084 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002085 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002086};
2087
2088/**
2089 * Inserts the given number of spaces at the current cursor position.
2090 *
rginda87b86462011-12-14 13:48:03 -08002091 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05002092 *
2093 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08002094 */
2095hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08002096 var cursor = this.saveCursor();
2097
rgindacbbd7482012-06-13 15:06:16 -07002098 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08002099 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08002100 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08002101
2102 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04002103 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002104};
2105
2106/**
2107 * Forward-delete the specified number of characters starting at the cursor
2108 * position.
2109 *
2110 * @param {integer} count The number of characters to delete.
2111 */
2112hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002113 var deleted = this.screen_.deleteChars(count);
2114 if (deleted && !this.screen_.textAttributes.isDefault()) {
2115 var cursor = this.saveCursor();
2116 this.setCursorColumn(this.screenSize.width - deleted);
2117 this.screen_.insertString(lib.f.getWhitespace(deleted));
2118 this.restoreCursor(cursor);
2119 }
2120
David Benjamin54e8bf62012-06-01 22:31:40 -04002121 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002122};
2123
2124/**
2125 * Shift rows in the scroll region upwards by a given number of lines.
2126 *
2127 * New rows are inserted at the bottom of the scroll region to fill the
2128 * vacated rows. The new rows not filled out with the current text attributes.
2129 *
2130 * This function does not affect the scrollback rows at all. Rows shifted
2131 * off the top are lost.
2132 *
rginda87b86462011-12-14 13:48:03 -08002133 * The cursor position is not altered.
2134 *
rginda8ba33642011-12-14 12:31:31 -08002135 * @param {integer} count The number of rows to scroll.
2136 */
2137hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002138 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002139
rginda87b86462011-12-14 13:48:03 -08002140 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002141 this.deleteLines(count);
2142
rginda87b86462011-12-14 13:48:03 -08002143 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002144};
2145
2146/**
2147 * Shift rows below the cursor down by a given number of lines.
2148 *
2149 * This function respects the current scroll region.
2150 *
2151 * New rows are inserted at the top of the scroll region to fill the
2152 * vacated rows. The new rows not filled out with the current text attributes.
2153 *
2154 * This function does not affect the scrollback rows at all. Rows shifted
2155 * off the bottom are lost.
2156 *
2157 * @param {integer} count The number of rows to scroll.
2158 */
2159hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08002160 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002161
rginda87b86462011-12-14 13:48:03 -08002162 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08002163 this.insertLines(opt_count);
2164
rginda87b86462011-12-14 13:48:03 -08002165 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002166};
2167
rginda87b86462011-12-14 13:48:03 -08002168
rginda8ba33642011-12-14 12:31:31 -08002169/**
2170 * Set the cursor position.
2171 *
2172 * The cursor row is relative to the scroll region if the terminal has
2173 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2174 *
2175 * @param {integer} row The new zero-based cursor row.
2176 * @param {integer} row The new zero-based cursor column.
2177 */
2178hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2179 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002180 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002181 } else {
rginda87b86462011-12-14 13:48:03 -08002182 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002183 }
rginda87b86462011-12-14 13:48:03 -08002184};
rginda8ba33642011-12-14 12:31:31 -08002185
Evan Jones2600d4f2016-12-06 09:29:36 -05002186/**
2187 * Move the cursor relative to its current position.
2188 *
2189 * @param {number} row
2190 * @param {number} column
2191 */
rginda87b86462011-12-14 13:48:03 -08002192hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2193 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002194 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2195 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002196 this.screen_.setCursorPosition(row, column);
2197};
2198
Evan Jones2600d4f2016-12-06 09:29:36 -05002199/**
2200 * Move the cursor to the specified position.
2201 *
2202 * @param {number} row
2203 * @param {number} column
2204 */
rginda87b86462011-12-14 13:48:03 -08002205hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002206 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2207 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002208 this.screen_.setCursorPosition(row, column);
2209};
2210
2211/**
2212 * Set the cursor column.
2213 *
2214 * @param {integer} column The new zero-based cursor column.
2215 */
2216hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002217 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002218};
2219
2220/**
2221 * Return the cursor column.
2222 *
2223 * @return {integer} The zero-based cursor column.
2224 */
2225hterm.Terminal.prototype.getCursorColumn = function() {
2226 return this.screen_.cursorPosition.column;
2227};
2228
2229/**
2230 * Set the cursor row.
2231 *
2232 * The cursor row is relative to the scroll region if the terminal has
2233 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2234 *
2235 * @param {integer} row The new cursor row.
2236 */
rginda87b86462011-12-14 13:48:03 -08002237hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2238 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002239};
2240
2241/**
2242 * Return the cursor row.
2243 *
2244 * @return {integer} The zero-based cursor row.
2245 */
Mike Frysingercf3c7622017-04-21 11:37:33 -04002246hterm.Terminal.prototype.getCursorRow = function() {
rginda8ba33642011-12-14 12:31:31 -08002247 return this.screen_.cursorPosition.row;
2248};
2249
2250/**
2251 * Request that the ScrollPort redraw itself soon.
2252 *
2253 * The redraw will happen asynchronously, soon after the call stack winds down.
2254 * Multiple calls will be coalesced into a single redraw.
2255 */
2256hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002257 if (this.timeouts_.redraw)
2258 return;
rginda8ba33642011-12-14 12:31:31 -08002259
2260 var self = this;
rginda87b86462011-12-14 13:48:03 -08002261 this.timeouts_.redraw = setTimeout(function() {
2262 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002263 self.scrollPort_.redraw_();
2264 }, 0);
2265};
2266
2267/**
2268 * Request that the ScrollPort be scrolled to the bottom.
2269 *
2270 * The scroll will happen asynchronously, soon after the call stack winds down.
2271 * Multiple calls will be coalesced into a single scroll.
2272 *
2273 * This affects the scrollbar position of the ScrollPort, and has nothing to
2274 * do with the VT scroll commands.
2275 */
2276hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2277 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002278 return;
rginda8ba33642011-12-14 12:31:31 -08002279
2280 var self = this;
2281 this.timeouts_.scrollDown = setTimeout(function() {
2282 delete self.timeouts_.scrollDown;
2283 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2284 }, 10);
2285};
2286
2287/**
2288 * Move the cursor up a specified number of rows.
2289 *
2290 * @param {integer} count The number of rows to move the cursor.
2291 */
2292hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002293 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002294};
2295
2296/**
2297 * Move the cursor down a specified number of rows.
2298 *
2299 * @param {integer} count The number of rows to move the cursor.
2300 */
2301hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002302 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002303 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2304 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2305 this.screenSize.height - 1);
2306
rgindacbbd7482012-06-13 15:06:16 -07002307 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002308 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002309 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002310};
2311
2312/**
2313 * Move the cursor left a specified number of columns.
2314 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002315 * If reverse wraparound mode is enabled and the previous row wrapped into
2316 * the current row then we back up through the wraparound as well.
2317 *
rginda8ba33642011-12-14 12:31:31 -08002318 * @param {integer} count The number of columns to move the cursor.
2319 */
2320hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002321 count = count || 1;
2322
2323 if (count < 1)
2324 return;
2325
2326 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002327 if (this.options_.reverseWraparound) {
2328 if (this.screen_.cursorPosition.overflow) {
2329 // If this cursor is in the right margin, consume one count to get it
2330 // back to the last column. This only applies when we're in reverse
2331 // wraparound mode.
2332 count--;
2333 this.clearCursorOverflow();
2334
2335 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002336 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002337 }
2338
Robert Gindabfb32622014-07-17 13:20:27 -07002339 var newRow = this.screen_.cursorPosition.row;
2340 var newColumn = currentColumn - count;
2341 if (newColumn < 0) {
2342 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2343 if (newRow < 0) {
2344 // xterm also wraps from row 0 to the last row.
2345 newRow = this.screenSize.height + newRow % this.screenSize.height;
2346 }
2347 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2348 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002349
Robert Gindabfb32622014-07-17 13:20:27 -07002350 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2351
2352 } else {
2353 var newColumn = Math.max(currentColumn - count, 0);
2354 this.setCursorColumn(newColumn);
2355 }
rginda8ba33642011-12-14 12:31:31 -08002356};
2357
2358/**
2359 * Move the cursor right a specified number of columns.
2360 *
2361 * @param {integer} count The number of columns to move the cursor.
2362 */
2363hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002364 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002365
2366 if (count < 1)
2367 return;
2368
rgindacbbd7482012-06-13 15:06:16 -07002369 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002370 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002371 this.setCursorColumn(column);
2372};
2373
2374/**
2375 * Reverse the foreground and background colors of the terminal.
2376 *
2377 * This only affects text that was drawn with no attributes.
2378 *
2379 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2380 * been drawn with attributes that happen to coincide with the default
2381 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002382 *
2383 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002384 */
2385hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002386 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002387 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002388 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2389 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002390 } else {
rginda9f5222b2012-03-05 11:53:28 -08002391 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2392 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002393 }
2394};
2395
2396/**
rginda87b86462011-12-14 13:48:03 -08002397 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002398 *
2399 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002400 */
2401hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002402 this.cursorNode_.style.backgroundColor =
2403 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002404
2405 var self = this;
2406 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08002407 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08002408 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002409
Michael Kelly485ecd12014-06-09 11:41:56 -04002410 // bellSquelchTimeout_ affects both audio and notification bells.
2411 if (this.bellSquelchTimeout_)
2412 return;
2413
Robert Ginda92e18102013-03-14 13:56:37 -07002414 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002415 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002416 this.bellSequelchTimeout_ = setTimeout(function() {
2417 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002418 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002419 } else {
2420 delete this.bellSquelchTimeout_;
2421 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002422
2423 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
Mike Frysingera5fb83c2017-06-22 14:48:35 -07002424 var n = hterm.notify();
Michael Kelly485ecd12014-06-09 11:41:56 -04002425 this.bellNotificationList_.push(n);
2426 // TODO: Should we try to raise the window here?
2427 n.onclick = function() { self.closeBellNotifications_(); };
2428 }
rginda87b86462011-12-14 13:48:03 -08002429};
2430
2431/**
rginda8ba33642011-12-14 12:31:31 -08002432 * Set the origin mode bit.
2433 *
2434 * If origin mode is on, certain VT cursor and scrolling commands measure their
2435 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2436 * to the top of the addressable screen.
2437 *
2438 * Defaults to off.
2439 *
2440 * @param {boolean} state True to set origin mode, false to unset.
2441 */
2442hterm.Terminal.prototype.setOriginMode = function(state) {
2443 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002444 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002445};
2446
2447/**
2448 * Set the insert mode bit.
2449 *
2450 * If insert mode is on, existing text beyond the cursor position will be
2451 * shifted right to make room for new text. Otherwise, new text overwrites
2452 * any existing text.
2453 *
2454 * Defaults to off.
2455 *
2456 * @param {boolean} state True to set insert mode, false to unset.
2457 */
2458hterm.Terminal.prototype.setInsertMode = function(state) {
2459 this.options_.insertMode = state;
2460};
2461
2462/**
rginda87b86462011-12-14 13:48:03 -08002463 * Set the auto carriage return bit.
2464 *
2465 * If auto carriage return is on then a formfeed character is interpreted
2466 * as a newline, otherwise it's the same as a linefeed. The difference boils
2467 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002468 *
2469 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002470 */
2471hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2472 this.options_.autoCarriageReturn = state;
2473};
2474
2475/**
rginda8ba33642011-12-14 12:31:31 -08002476 * Set the wraparound mode bit.
2477 *
2478 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2479 * to the start of the following row. Otherwise, the cursor is clamped to the
2480 * end of the screen and attempts to write past it are ignored.
2481 *
2482 * Defaults to on.
2483 *
2484 * @param {boolean} state True to set wraparound mode, false to unset.
2485 */
2486hterm.Terminal.prototype.setWraparound = function(state) {
2487 this.options_.wraparound = state;
2488};
2489
2490/**
2491 * Set the reverse-wraparound mode bit.
2492 *
2493 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2494 * to the end of the previous row. Otherwise, the cursor is clamped to column
2495 * 0.
2496 *
2497 * Defaults to off.
2498 *
2499 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2500 */
2501hterm.Terminal.prototype.setReverseWraparound = function(state) {
2502 this.options_.reverseWraparound = state;
2503};
2504
2505/**
2506 * Selects between the primary and alternate screens.
2507 *
2508 * If alternate mode is on, the alternate screen is active. Otherwise the
2509 * primary screen is active.
2510 *
2511 * Swapping screens has no effect on the scrollback buffer.
2512 *
2513 * Each screen maintains its own cursor position.
2514 *
2515 * Defaults to off.
2516 *
2517 * @param {boolean} state True to set alternate mode, false to unset.
2518 */
2519hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002520 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002521 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2522
rginda35c456b2012-02-09 17:29:05 -08002523 if (this.screen_.rowsArray.length &&
2524 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2525 // If the screen changed sizes while we were away, our rowIndexes may
2526 // be incorrect.
2527 var offset = this.scrollbackRows_.length;
2528 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002529 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002530 ary[i].rowIndex = offset + i;
2531 }
2532 }
rginda8ba33642011-12-14 12:31:31 -08002533
rginda35c456b2012-02-09 17:29:05 -08002534 this.realizeWidth_(this.screenSize.width);
2535 this.realizeHeight_(this.screenSize.height);
2536 this.scrollPort_.syncScrollHeight();
2537 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002538
rginda6d397402012-01-17 10:58:29 -08002539 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002540 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002541};
2542
2543/**
2544 * Set the cursor-blink mode bit.
2545 *
2546 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2547 * a visible cursor does not blink.
2548 *
2549 * You should make sure to turn blinking off if you're going to dispose of a
2550 * terminal, otherwise you'll leak a timeout.
2551 *
2552 * Defaults to on.
2553 *
2554 * @param {boolean} state True to set cursor-blink mode, false to unset.
2555 */
2556hterm.Terminal.prototype.setCursorBlink = function(state) {
2557 this.options_.cursorBlink = state;
2558
2559 if (!state && this.timeouts_.cursorBlink) {
2560 clearTimeout(this.timeouts_.cursorBlink);
2561 delete this.timeouts_.cursorBlink;
2562 }
2563
2564 if (this.options_.cursorVisible)
2565 this.setCursorVisible(true);
2566};
2567
2568/**
2569 * Set the cursor-visible mode bit.
2570 *
2571 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2572 *
2573 * Defaults to on.
2574 *
2575 * @param {boolean} state True to set cursor-visible mode, false to unset.
2576 */
2577hterm.Terminal.prototype.setCursorVisible = function(state) {
2578 this.options_.cursorVisible = state;
2579
2580 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002581 if (this.timeouts_.cursorBlink) {
2582 clearTimeout(this.timeouts_.cursorBlink);
2583 delete this.timeouts_.cursorBlink;
2584 }
rginda87b86462011-12-14 13:48:03 -08002585 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002586 return;
2587 }
2588
rginda87b86462011-12-14 13:48:03 -08002589 this.syncCursorPosition_();
2590
2591 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002592
2593 if (this.options_.cursorBlink) {
2594 if (this.timeouts_.cursorBlink)
2595 return;
2596
Robert Gindaea2183e2014-07-17 09:51:51 -07002597 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002598 } else {
2599 if (this.timeouts_.cursorBlink) {
2600 clearTimeout(this.timeouts_.cursorBlink);
2601 delete this.timeouts_.cursorBlink;
2602 }
2603 }
2604};
2605
2606/**
rginda87b86462011-12-14 13:48:03 -08002607 * Synchronizes the visible cursor and document selection with the current
2608 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002609 */
2610hterm.Terminal.prototype.syncCursorPosition_ = function() {
2611 var topRowIndex = this.scrollPort_.getTopRowIndex();
2612 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2613 var cursorRowIndex = this.scrollbackRows_.length +
2614 this.screen_.cursorPosition.row;
2615
2616 if (cursorRowIndex > bottomRowIndex) {
2617 // Cursor is scrolled off screen, move it outside of the visible area.
Mike Frysinger44c32202017-08-05 01:13:09 -04002618 this.setCssVar('cursor-offset-row', '-1');
rginda8ba33642011-12-14 12:31:31 -08002619 return;
2620 }
2621
Robert Gindab837c052014-08-11 11:17:51 -07002622 if (this.options_.cursorVisible &&
2623 this.cursorNode_.style.display == 'none') {
2624 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2625 this.cursorNode_.style.display = '';
2626 }
2627
Mike Frysinger44c32202017-08-05 01:13:09 -04002628 // Position the cursor using CSS variable math. If we do the math in JS,
2629 // the float math will end up being more precise than the CSS which will
2630 // cause the cursor tracking to be off.
2631 this.setCssVar(
2632 'cursor-offset-row',
2633 `${cursorRowIndex - topRowIndex} + ` +
2634 `${this.scrollPort_.visibleRowTopMargin}px`);
2635 this.setCssVar('cursor-offset-col', this.screen_.cursorPosition.column);
rginda87b86462011-12-14 13:48:03 -08002636
2637 this.cursorNode_.setAttribute('title',
Mike Frysinger44c32202017-08-05 01:13:09 -04002638 '(' + this.screen_.cursorPosition.column +
2639 ', ' + this.screen_.cursorPosition.row +
rginda87b86462011-12-14 13:48:03 -08002640 ')');
2641
2642 // Update the caret for a11y purposes.
2643 var selection = this.document_.getSelection();
2644 if (selection && selection.isCollapsed)
2645 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002646};
2647
Robert Gindafb1be6a2013-12-11 11:56:22 -08002648/**
2649 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2650 * and character cell dimensions.
2651 */
Robert Ginda830583c2013-08-07 13:20:46 -07002652hterm.Terminal.prototype.restyleCursor_ = function() {
2653 var shape = this.cursorShape_;
2654
2655 if (this.cursorNode_.getAttribute('focus') == 'false') {
2656 // Always show a block cursor when unfocused.
2657 shape = hterm.Terminal.cursorShape.BLOCK;
2658 }
2659
2660 var style = this.cursorNode_.style;
2661
2662 switch (shape) {
2663 case hterm.Terminal.cursorShape.BEAM:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002664 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002665 style.backgroundColor = 'transparent';
2666 style.borderBottomStyle = null;
2667 style.borderLeftStyle = 'solid';
2668 break;
2669
2670 case hterm.Terminal.cursorShape.UNDERLINE:
2671 style.height = this.scrollPort_.characterSize.baseline + 'px';
2672 style.backgroundColor = 'transparent';
2673 style.borderBottomStyle = 'solid';
2674 // correct the size to put it exactly at the baseline
2675 style.borderLeftStyle = null;
2676 break;
2677
2678 default:
Mike Frysinger66beb0b2017-05-30 19:44:51 -04002679 style.height = 'var(--hterm-charsize-height)';
Robert Ginda830583c2013-08-07 13:20:46 -07002680 style.backgroundColor = this.cursorColor_;
2681 style.borderBottomStyle = null;
2682 style.borderLeftStyle = null;
2683 break;
2684 }
2685};
2686
rginda8ba33642011-12-14 12:31:31 -08002687/**
2688 * Synchronizes the visible cursor with the current cursor coordinates.
2689 *
2690 * The sync will happen asynchronously, soon after the call stack winds down.
2691 * Multiple calls will be coalesced into a single sync.
2692 */
2693hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2694 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002695 return;
rginda8ba33642011-12-14 12:31:31 -08002696
2697 var self = this;
2698 this.timeouts_.syncCursor = setTimeout(function() {
2699 self.syncCursorPosition_();
2700 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002701 }, 0);
2702};
2703
rgindacc2996c2012-02-24 14:59:31 -08002704/**
rgindaf522ce02012-04-17 17:49:17 -07002705 * Show or hide the zoom warning.
2706 *
2707 * The zoom warning is a message warning the user that their browser zoom must
2708 * be set to 100% in order for hterm to function properly.
2709 *
2710 * @param {boolean} state True to show the message, false to hide it.
2711 */
2712hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2713 if (!this.zoomWarningNode_) {
2714 if (!state)
2715 return;
2716
2717 this.zoomWarningNode_ = this.document_.createElement('div');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002718 this.zoomWarningNode_.id = 'hterm:zoom-warning';
rgindaf522ce02012-04-17 17:49:17 -07002719 this.zoomWarningNode_.style.cssText = (
2720 'color: black;' +
2721 'background-color: #ff2222;' +
2722 'font-size: large;' +
2723 'border-radius: 8px;' +
2724 'opacity: 0.75;' +
2725 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2726 'top: 0.5em;' +
2727 'right: 1.2em;' +
2728 'position: absolute;' +
2729 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002730 '-webkit-user-select: none;' +
2731 '-moz-text-size-adjust: none;' +
2732 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002733
2734 this.zoomWarningNode_.addEventListener('click', function(e) {
2735 this.parentNode.removeChild(this);
2736 });
rgindaf522ce02012-04-17 17:49:17 -07002737 }
2738
Robert Gindab4839c22013-02-28 16:52:10 -08002739 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2740 hterm.zoomWarningMessage,
2741 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2742
rgindaf522ce02012-04-17 17:49:17 -07002743 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2744
2745 if (state) {
2746 if (!this.zoomWarningNode_.parentNode)
2747 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2748 } else if (this.zoomWarningNode_.parentNode) {
2749 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2750 }
2751};
2752
2753/**
rgindacc2996c2012-02-24 14:59:31 -08002754 * Show the terminal overlay for a given amount of time.
2755 *
2756 * The terminal overlay appears in inverse video in a large font, centered
2757 * over the terminal. You should probably keep the overlay message brief,
2758 * since it's in a large font and you probably aren't going to check the size
2759 * of the terminal first.
2760 *
2761 * @param {string} msg The text (not HTML) message to display in the overlay.
2762 * @param {number} opt_timeout The amount of time to wait before fading out
2763 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2764 * stay up forever (or until the next overlay).
2765 */
2766hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002767 if (!this.overlayNode_) {
2768 if (!this.div_)
2769 return;
2770
2771 this.overlayNode_ = this.document_.createElement('div');
2772 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002773 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002774 'font-size: xx-large;' +
2775 'opacity: 0.75;' +
2776 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2777 'position: absolute;' +
2778 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002779 '-webkit-transition: opacity 180ms ease-in;' +
2780 '-moz-user-select: none;' +
2781 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002782
2783 this.overlayNode_.addEventListener('mousedown', function(e) {
2784 e.preventDefault();
2785 e.stopPropagation();
2786 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002787 }
2788
rginda9f5222b2012-03-05 11:53:28 -08002789 this.overlayNode_.style.color = this.prefs_.get('background-color');
2790 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2791 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2792
rgindaf0090c92012-02-10 14:58:52 -08002793 this.overlayNode_.textContent = msg;
2794 this.overlayNode_.style.opacity = '0.75';
2795
2796 if (!this.overlayNode_.parentNode)
2797 this.div_.appendChild(this.overlayNode_);
2798
Robert Ginda97769282013-02-01 15:30:30 -08002799 var divSize = hterm.getClientSize(this.div_);
2800 var overlaySize = hterm.getClientSize(this.overlayNode_);
2801
Robert Ginda8a59f762014-07-23 11:29:55 -07002802 this.overlayNode_.style.top =
2803 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08002804 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07002805 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08002806
2807 var self = this;
2808
2809 if (this.overlayTimeout_)
2810 clearTimeout(this.overlayTimeout_);
2811
rgindacc2996c2012-02-24 14:59:31 -08002812 if (opt_timeout === null)
2813 return;
2814
rgindaf0090c92012-02-10 14:58:52 -08002815 this.overlayTimeout_ = setTimeout(function() {
2816 self.overlayNode_.style.opacity = '0';
Robert Ginda70926e42013-11-25 14:56:36 -08002817 self.overlayTimeout_ = setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002818 if (self.overlayNode_.parentNode)
2819 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002820 self.overlayTimeout_ = null;
2821 self.overlayNode_.style.opacity = '0.75';
2822 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002823 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002824};
2825
rginda4bba5e12012-06-20 16:15:30 -07002826/**
2827 * Paste from the system clipboard to the terminal.
2828 */
2829hterm.Terminal.prototype.paste = function() {
Mike Frysinger4628ad22017-07-20 02:44:20 -04002830 return hterm.pasteFromClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002831};
2832
2833/**
2834 * Copy a string to the system clipboard.
2835 *
2836 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05002837 *
2838 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07002839 */
2840hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002841 if (this.prefs_.get('enable-clipboard-notice'))
2842 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2843
rgindaa09e7332012-08-17 12:49:51 -07002844 var copySource = this.document_.createElement('pre');
Mike Frysingerd826f1a2017-07-06 16:20:06 -04002845 copySource.id = 'hterm:copy-to-clipboard-source';
rginda4bba5e12012-06-20 16:15:30 -07002846 copySource.textContent = str;
2847 copySource.style.cssText = (
2848 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002849 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07002850 'position: absolute;' +
2851 'top: -99px');
2852
2853 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002854
rginda4bba5e12012-06-20 16:15:30 -07002855 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002856 var anchorNode = selection.anchorNode;
2857 var anchorOffset = selection.anchorOffset;
2858 var focusNode = selection.focusNode;
2859 var focusOffset = selection.focusOffset;
2860
rginda4bba5e12012-06-20 16:15:30 -07002861 selection.selectAllChildren(copySource);
2862
rgindaa09e7332012-08-17 12:49:51 -07002863 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002864
Rob Spies56953412014-04-28 14:09:47 -07002865 // IE doesn't support selection.extend. This means that the selection
2866 // won't return on IE.
Robert Ginda6f4f0aa2014-07-28 10:21:39 -07002867 if (selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07002868 selection.collapse(anchorNode, anchorOffset);
2869 selection.extend(focusNode, focusOffset);
2870 }
rgindafaa74742012-08-21 13:34:03 -07002871
rginda4bba5e12012-06-20 16:15:30 -07002872 copySource.parentNode.removeChild(copySource);
2873};
2874
Evan Jones2600d4f2016-12-06 09:29:36 -05002875/**
2876 * Returns the selected text, or null if no text is selected.
2877 *
2878 * @return {string|null}
2879 */
rgindaa09e7332012-08-17 12:49:51 -07002880hterm.Terminal.prototype.getSelectionText = function() {
2881 var selection = this.scrollPort_.selection;
2882 selection.sync();
2883
2884 if (selection.isCollapsed)
2885 return null;
2886
2887
2888 // Start offset measures from the beginning of the line.
2889 var startOffset = selection.startOffset;
2890 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002891
Robert Gindafdbb3f22012-09-06 20:23:06 -07002892 if (node.nodeName != 'X-ROW') {
2893 // If the selection doesn't start on an x-row node, then it must be
2894 // somewhere inside the x-row. Add any characters from previous siblings
2895 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002896
2897 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2898 // If node is the text node in a styled span, move up to the span node.
2899 node = node.parentNode;
2900 }
2901
Robert Gindafdbb3f22012-09-06 20:23:06 -07002902 while (node.previousSibling) {
2903 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002904 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002905 }
rgindaa09e7332012-08-17 12:49:51 -07002906 }
2907
2908 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002909 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
2910 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05002911 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002912
Robert Gindafdbb3f22012-09-06 20:23:06 -07002913 if (node.nodeName != 'X-ROW') {
2914 // If the selection doesn't end on an x-row node, then it must be
2915 // somewhere inside the x-row. Add any characters from following siblings
2916 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002917
2918 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2919 // If node is the text node in a styled span, move up to the span node.
2920 node = node.parentNode;
2921 }
2922
Robert Gindafdbb3f22012-09-06 20:23:06 -07002923 while (node.nextSibling) {
2924 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002925 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002926 }
rgindaa09e7332012-08-17 12:49:51 -07002927 }
2928
2929 var rv = this.getRowsText(selection.startRow.rowIndex,
2930 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002931 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002932};
2933
rginda4bba5e12012-06-20 16:15:30 -07002934/**
2935 * Copy the current selection to the system clipboard, then clear it after a
2936 * short delay.
2937 */
2938hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002939 var text = this.getSelectionText();
2940 if (text != null)
2941 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002942};
2943
rgindaf0090c92012-02-10 14:58:52 -08002944hterm.Terminal.prototype.overlaySize = function() {
2945 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2946};
2947
rginda87b86462011-12-14 13:48:03 -08002948/**
2949 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2950 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07002951 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08002952 */
2953hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002954 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002955 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2956
Robert Ginda8cb7d902013-06-20 14:37:18 -07002957 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08002958};
2959
2960/**
Mike Frysinger70b94692017-01-26 18:57:50 -10002961 * Launches url in a new tab.
2962 *
2963 * @param {string} url URL to launch in a new tab.
2964 */
2965hterm.Terminal.prototype.openUrl = function(url) {
Mike Frysingerac437a12017-07-13 02:35:59 -04002966 if (window.chrome && window.chrome.browser) {
2967 // For Chrome v2 apps, we need to use this API to properly open windows.
2968 chrome.browser.openTab({'url': url});
2969 } else {
2970 var win = window.open(url, '_blank');
2971 win.focus();
2972 }
Mike Frysinger70b94692017-01-26 18:57:50 -10002973}
2974
2975/**
2976 * Open the selected url.
2977 */
2978hterm.Terminal.prototype.openSelectedUrl_ = function() {
2979 var str = this.getSelectionText();
2980
2981 // If there is no selection, try and expand wherever they clicked.
2982 if (str == null) {
2983 this.screen_.expandSelection(this.document_.getSelection());
2984 str = this.getSelectionText();
Mike Frysinger498192d2017-06-26 18:23:31 -04002985
2986 // If clicking in empty space, return.
2987 if (str == null)
2988 return;
Mike Frysinger70b94692017-01-26 18:57:50 -10002989 }
2990
2991 // Make sure URL is valid before opening.
2992 if (str.length > 2048 || str.search(/[\s\[\](){}<>"'\\^`]/) >= 0)
2993 return;
Mike Frysinger43472622017-06-26 18:11:07 -04002994
2995 // If the URI isn't anchored, it'll open relative to the extension.
Mike Frysinger70b94692017-01-26 18:57:50 -10002996 // We have no way of knowing the correct schema, so assume http.
Mike Frysinger43472622017-06-26 18:11:07 -04002997 if (str.search('^[a-zA-Z][a-zA-Z0-9+.-]*://') < 0) {
2998 // We have to whitelist a few protocols that lack authorities and thus
2999 // never use the //. Like mailto.
3000 switch (str.split(':', 1)[0]) {
3001 case 'mailto':
3002 break;
3003 default:
3004 str = 'http://' + str;
3005 break;
3006 }
3007 }
Mike Frysinger70b94692017-01-26 18:57:50 -10003008
3009 this.openUrl(str);
3010}
3011
3012
3013/**
rgindad5613292012-06-19 15:40:37 -07003014 * Add the terminalRow and terminalColumn properties to mouse events and
3015 * then forward on to onMouse().
3016 *
3017 * The terminalRow and terminalColumn properties contain the (row, column)
3018 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05003019 *
3020 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003021 */
3022hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07003023 if (e.processedByTerminalHandler_) {
3024 // We register our event handlers on the document, as well as the cursor
3025 // and the scroll blocker. Mouse events that occur on the cursor or
3026 // scroll blocker will also appear on the document, but we don't want to
3027 // process them twice.
3028 //
3029 // We can't just prevent bubbling because that has other side effects, so
3030 // we decorate the event object with this property instead.
3031 return;
3032 }
3033
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003034 var reportMouseEvents = (!this.defeatMouseReports_ &&
3035 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
3036
rgindafaa74742012-08-21 13:34:03 -07003037 e.processedByTerminalHandler_ = true;
3038
Robert Gindaeda48db2014-07-17 09:25:30 -07003039 // One based row/column stored on the mouse event.
3040 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
3041 this.scrollPort_.characterSize.height) + 1;
3042 e.terminalColumn = parseInt(e.clientX /
3043 this.scrollPort_.characterSize.width) + 1;
3044
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003045 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
3046 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07003047 return;
3048 }
3049
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003050 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07003051 // If the cursor is visible and we're not sending mouse events to the
3052 // host app, then we want to hide the terminal cursor when the mouse
3053 // cursor is over top. This keeps the terminal cursor from interfering
3054 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07003055 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
3056 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
3057 this.cursorNode_.style.display = 'none';
3058 } else if (this.cursorNode_.style.display == 'none') {
3059 this.cursorNode_.style.display = '';
3060 }
3061 }
rgindad5613292012-06-19 15:40:37 -07003062
Robert Ginda928cf632014-03-05 15:07:41 -08003063 if (e.type == 'mousedown') {
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003064 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08003065 // If VT mouse reporting is disabled, or has been defeated with
3066 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003067 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08003068 this.setSelectionEnabled(true);
3069 } else {
3070 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003071 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07003072 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08003073 this.setSelectionEnabled(false);
3074 e.preventDefault();
3075 }
3076 }
3077
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003078 if (!reportMouseEvents) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003079 if (e.type == 'dblclick' && this.copyOnSelect) {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003080 this.screen_.expandSelection(this.document_.getSelection());
Robert Ginda15ed4902016-07-12 10:43:22 -07003081 this.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07003082 }
3083
Mihir Nimbalkar467fd8b2017-07-12 12:14:16 -07003084 if (e.type == 'click' && !e.shiftKey && (e.ctrlKey || e.metaKey)) {
Mike Frysinger70b94692017-01-26 18:57:50 -10003085 // Debounce this event with the dblclick event. If you try to doubleclick
3086 // a URL to open it, Chrome will fire click then dblclick, but we won't
3087 // have expanded the selection text at the first click event.
3088 clearTimeout(this.timeouts_.openUrl);
3089 this.timeouts_.openUrl = setTimeout(this.openSelectedUrl_.bind(this),
3090 500);
3091 return;
3092 }
3093
Mike Frysinger847577f2017-05-23 23:25:57 -04003094 if (e.type == 'mousedown') {
3095 if ((this.mouseRightClickPaste && e.button == 2 /* right button */) ||
Mike Frysinger2edd3612017-05-24 00:54:39 -04003096 e.button == this.mousePasteButton) {
Mike Frysinger4628ad22017-07-20 02:44:20 -04003097 if (!this.paste())
3098 console.warning('Could not paste manually due to web restrictions');;
Mike Frysinger847577f2017-05-23 23:25:57 -04003099 }
3100 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003101
Mike Frysinger2edd3612017-05-24 00:54:39 -04003102 if (e.type == 'mouseup' && e.button == 0 && this.copyOnSelect &&
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003103 !this.document_.getSelection().isCollapsed) {
Robert Ginda15ed4902016-07-12 10:43:22 -07003104 this.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003105 }
3106
3107 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
3108 this.scrollBlockerNode_.engaged) {
3109 // Disengage the scroll-blocker after one of these events.
3110 this.scrollBlockerNode_.engaged = false;
3111 this.scrollBlockerNode_.style.top = '-99px';
3112 }
3113
Mike Frysinger3c9fa072017-07-13 10:21:13 -04003114 // Emulate arrow key presses via scroll wheel events.
3115 if (this.scrollWheelArrowKeys_ && !e.shiftKey &&
3116 this.keyboard.applicationCursor && !this.isPrimaryScreen()) {
Mike Frysingerc3030a82017-05-29 14:16:11 -04003117 if (e.type == 'wheel') {
3118 var delta = this.scrollPort_.scrollWheelDelta(e);
3119 var lines = lib.f.smartFloorDivide(
3120 Math.abs(delta), this.scrollPort_.characterSize.height);
3121
3122 var data = '\x1bO' + (delta < 0 ? 'B' : 'A');
3123 this.io.sendString(data.repeat(lines));
3124
3125 e.preventDefault();
3126 }
3127 }
Robert Ginda928cf632014-03-05 15:07:41 -08003128 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003129 if (!this.scrollBlockerNode_.engaged) {
3130 if (e.type == 'mousedown') {
3131 // Move the scroll-blocker into place if we want to keep the scrollport
3132 // from scrolling.
3133 this.scrollBlockerNode_.engaged = true;
3134 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
3135 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
3136 } else if (e.type == 'mousemove') {
3137 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
3138 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07003139 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08003140 e.preventDefault();
3141 }
3142 }
Robert Ginda928cf632014-03-05 15:07:41 -08003143
3144 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07003145 }
3146
Robert Ginda928cf632014-03-05 15:07:41 -08003147 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
3148 // Restore this on mouseup in case it was temporarily defeated with a
3149 // alt-mousedown. Only do this when the selection is empty so that
3150 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07003151 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08003152 }
rgindad5613292012-06-19 15:40:37 -07003153};
3154
3155/**
3156 * Clients should override this if they care to know about mouse events.
3157 *
3158 * The event parameter will be a normal DOM mouse click event with additional
3159 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05003160 *
3161 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07003162 */
3163hterm.Terminal.prototype.onMouse = function(e) { };
3164
3165/**
rginda8e92a692012-05-20 19:37:20 -07003166 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05003167 *
3168 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07003169 */
Rob Spies06533ba2014-04-24 11:20:37 -07003170hterm.Terminal.prototype.onFocusChange_ = function(focused) {
3171 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07003172 this.restyleCursor_();
Michael Kelly485ecd12014-06-09 11:41:56 -04003173 if (focused === true)
3174 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07003175};
3176
3177/**
rginda8ba33642011-12-14 12:31:31 -08003178 * React when the ScrollPort is scrolled.
3179 */
3180hterm.Terminal.prototype.onScroll_ = function() {
3181 this.scheduleSyncCursorPosition_();
3182};
3183
3184/**
rginda9846e2f2012-01-27 13:53:33 -08003185 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003186 *
3187 * @param {Event} e The DOM paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08003188 */
3189hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07003190 var data = e.text.replace(/\n/mg, '\r');
Connor Hegartyf525ceb2014-09-03 13:36:03 -07003191 data = this.keyboard.encode(data);
Robert Gindaa063b202014-07-21 11:08:25 -07003192 if (this.options_.bracketedPaste)
3193 data = '\x1b[200~' + data + '\x1b[201~';
3194
3195 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003196};
3197
3198/**
rgindaa09e7332012-08-17 12:49:51 -07003199 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003200 *
3201 * @param {Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003202 */
3203hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003204 if (!this.useDefaultWindowCopy) {
3205 e.preventDefault();
3206 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3207 }
rgindaa09e7332012-08-17 12:49:51 -07003208};
3209
3210/**
rginda8ba33642011-12-14 12:31:31 -08003211 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003212 *
3213 * Note: This function should not directly contain code that alters the internal
3214 * state of the terminal. That kind of code belongs in realizeWidth or
3215 * realizeHeight, so that it can be executed synchronously in the case of a
3216 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003217 */
3218hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003219 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003220 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003221 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003222 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003223
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003224 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003225 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003226 // gets removed from the document or during the initial load, and we can't
3227 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003228 // This can also happen if called before the scrollPort calculates the
3229 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003230 return;
3231 }
3232
rgindaa8ba17d2012-08-15 14:41:10 -07003233 var isNewSize = (columnCount != this.screenSize.width ||
3234 rowCount != this.screenSize.height);
3235
3236 // We do this even if the size didn't change, just to be sure everything is
3237 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003238 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003239 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003240
3241 if (isNewSize)
3242 this.overlaySize();
3243
Robert Gindafb1be6a2013-12-11 11:56:22 -08003244 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003245 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08003246};
3247
3248/**
3249 * Service the cursor blink timeout.
3250 */
3251hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003252 if (!this.options_.cursorBlink) {
3253 delete this.timeouts_.cursorBlink;
3254 return;
3255 }
3256
Robert Ginda830583c2013-08-07 13:20:46 -07003257 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3258 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003259 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003260 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3261 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003262 } else {
rginda87b86462011-12-14 13:48:03 -08003263 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003264 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3265 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003266 }
3267};
David Reveman8f552492012-03-28 12:18:41 -04003268
3269/**
3270 * Set the scrollbar-visible mode bit.
3271 *
3272 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3273 * Otherwise it will not.
3274 *
3275 * Defaults to on.
3276 *
3277 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3278 */
3279hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3280 this.scrollPort_.setScrollbarVisible(state);
3281};
Michael Kelly485ecd12014-06-09 11:41:56 -04003282
3283/**
Rob Spies49039e52014-12-17 13:40:04 -08003284 * Set the scroll wheel move multiplier. This will affect how fast the page
Mike Frysinger9975de62017-04-28 00:01:14 -04003285 * scrolls on wheel events.
Rob Spies49039e52014-12-17 13:40:04 -08003286 *
3287 * Defaults to 1.
3288 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003289 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003290 */
3291hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3292 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3293};
3294
3295/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003296 * Close all web notifications created by terminal bells.
3297 */
3298hterm.Terminal.prototype.closeBellNotifications_ = function() {
3299 this.bellNotificationList_.forEach(function(n) {
3300 n.close();
3301 });
3302 this.bellNotificationList_.length = 0;
3303};