blob: 601101ad0f6bdf002c64ba8c567665a67b9476d7 [file] [log] [blame]
rginda87b86462011-12-14 13:48:03 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rginda8ba33642011-12-14 12:31:31 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rgindacbbd7482012-06-13 15:06:16 -07005'use strict';
6
Masaya Suzuki273aa982014-05-31 07:25:55 +09007lib.rtdep('lib.colors', 'lib.PreferenceManager', 'lib.resource', 'lib.wc',
Rob Spiesf4e90e82015-01-28 12:10:13 -08008 'lib.f', 'hterm.Keyboard', 'hterm.Options', 'hterm.PreferenceManager',
Ricky Liang48f05cb2013-12-31 23:35:29 +08009 'hterm.Screen', 'hterm.ScrollPort', 'hterm.Size',
10 'hterm.TextAttributes', 'hterm.VT');
rgindacbbd7482012-06-13 15:06:16 -070011
rginda8ba33642011-12-14 12:31:31 -080012/**
13 * Constructor for the Terminal class.
14 *
15 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
16 * classes to provide the complete terminal functionality.
17 *
18 * There are a number of lower-level Terminal methods that can be called
19 * directly to manipulate the cursor, text, scroll region, and other terminal
20 * attributes. However, the primary method is interpret(), which parses VT
21 * escape sequences and invokes the appropriate Terminal methods.
22 *
23 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
24 *
25 * TODO(rginda): Eventually we're going to need to support characters which are
26 * displayed twice as wide as standard latin characters. This is to support
27 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080028 *
Robert Ginda57f03b42012-09-13 11:02:48 -070029 * @param {string} opt_profileId Optional preference profile name. If not
rginda9f5222b2012-03-05 11:53:28 -080030 * provided, defaults to 'default'.
rginda8ba33642011-12-14 12:31:31 -080031 */
Robert Ginda57f03b42012-09-13 11:02:48 -070032hterm.Terminal = function(opt_profileId) {
33 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080034
rginda8ba33642011-12-14 12:31:31 -080035 // Two screen instances.
36 this.primaryScreen_ = new hterm.Screen();
37 this.alternateScreen_ = new hterm.Screen();
38
39 // The "current" screen.
40 this.screen_ = this.primaryScreen_;
41
rginda8ba33642011-12-14 12:31:31 -080042 // The local notion of the screen size. ScreenBuffers also have a size which
43 // indicates their present size. During size changes, the two may disagree.
44 // Also, the inactive screen's size is not altered until it is made the active
45 // screen.
46 this.screenSize = new hterm.Size(0, 0);
47
rginda8ba33642011-12-14 12:31:31 -080048 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080049 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080050 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
51 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080052 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070053 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080054
rginda87b86462011-12-14 13:48:03 -080055 // The div that contains this terminal.
56 this.div_ = null;
57
rgindac9bc5502012-01-18 11:48:44 -080058 // The document that contains the scrollPort. Defaulted to the global
59 // document here so that the terminal is functional even if it hasn't been
60 // inserted into a document yet, but re-set in decorate().
61 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080062
rginda8ba33642011-12-14 12:31:31 -080063 // The rows that have scrolled off screen and are no longer addressable.
64 this.scrollbackRows_ = [];
65
rgindac9bc5502012-01-18 11:48:44 -080066 // Saved tab stops.
67 this.tabStops_ = [];
68
David Benjamin66e954d2012-05-05 21:08:12 -040069 // Keep track of whether default tab stops have been erased; after a TBC
70 // clears all tab stops, defaults aren't restored on resize until a reset.
71 this.defaultTabStops = true;
72
rginda8ba33642011-12-14 12:31:31 -080073 // The VT's notion of the top and bottom rows. Used during some VT
74 // cursor positioning and scrolling commands.
75 this.vtScrollTop_ = null;
76 this.vtScrollBottom_ = null;
77
78 // The DIV element for the visible cursor.
79 this.cursorNode_ = null;
80
Robert Ginda830583c2013-08-07 13:20:46 -070081 // The current cursor shape of the terminal.
82 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
83
84 // The current color of the cursor.
85 this.cursorColor_ = null;
86
Robert Gindaea2183e2014-07-17 09:51:51 -070087 // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded.
88 this.cursorBlinkCycle_ = [100, 100];
89
90 // Pre-bound onCursorBlink_ handler, so we don't have to do this for each
91 // cursor on/off servicing.
92 this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
93
rginda9f5222b2012-03-05 11:53:28 -080094 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070095 // each output and keystroke. They are initialized by the preference manager.
Robert Ginda8cb7d902013-06-20 14:37:18 -070096 this.backgroundColor_ = null;
97 this.foregroundColor_ = null;
Robert Ginda57f03b42012-09-13 11:02:48 -070098 this.scrollOnOutput_ = null;
99 this.scrollOnKeystroke_ = null;
rginda9f5222b2012-03-05 11:53:28 -0800100
Robert Ginda6aec7eb2015-06-16 10:31:30 -0700101 // True if we should override mouse event reporting to allow local selection.
102 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -0800103
rgindaf0090c92012-02-10 14:58:52 -0800104 // Terminal bell sound.
105 this.bellAudio_ = this.document_.createElement('audio');
rgindaf0090c92012-02-10 14:58:52 -0800106 this.bellAudio_.setAttribute('preload', 'auto');
107
Michael Kelly485ecd12014-06-09 11:41:56 -0400108 // All terminal bell notifications that have been generated (not necessarily
109 // shown).
110 this.bellNotificationList_ = [];
111
112 // Whether we have permission to display notifications.
113 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400114
rginda6d397402012-01-17 10:58:29 -0800115 // Cursor position and attributes saved with DECSC.
116 this.savedOptions_ = {};
117
rginda8ba33642011-12-14 12:31:31 -0800118 // The current mode bits for the terminal.
119 this.options_ = new hterm.Options();
120
121 // Timeouts we might need to clear.
122 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800123
124 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800125 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800126
rgindafeaf3142012-01-31 15:14:20 -0800127 // The keyboard hander.
128 this.keyboard = new hterm.Keyboard(this);
129
rginda87b86462011-12-14 13:48:03 -0800130 // General IO interface that can be given to third parties without exposing
131 // the entire terminal object.
132 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800133
rgindad5613292012-06-19 15:40:37 -0700134 // True if mouse-click-drag should scroll the terminal.
135 this.enableMouseDragScroll = true;
136
Robert Ginda57f03b42012-09-13 11:02:48 -0700137 this.copyOnSelect = null;
rginda4bba5e12012-06-20 16:15:30 -0700138 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700139
Rob Spies0bec09b2014-06-06 15:58:09 -0700140 // Whether to use the default window copy behaviour.
141 this.useDefaultWindowCopy = false;
142
143 this.clearSelectionAfterCopy = true;
144
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400145 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800146 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700147
148 this.setProfile(opt_profileId || 'default',
149 function() { this.onTerminalReady() }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800150};
151
152/**
Robert Ginda830583c2013-08-07 13:20:46 -0700153 * Possible cursor shapes.
154 */
155hterm.Terminal.cursorShape = {
156 BLOCK: 'BLOCK',
157 BEAM: 'BEAM',
158 UNDERLINE: 'UNDERLINE'
159};
160
161/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700162 * Clients should override this to be notified when the terminal is ready
163 * for use.
164 *
165 * The terminal initialization is asynchronous, and shouldn't be used before
166 * this method is called.
167 */
168hterm.Terminal.prototype.onTerminalReady = function() { };
169
170/**
rginda35c456b2012-02-09 17:29:05 -0800171 * Default tab with of 8 to match xterm.
172 */
173hterm.Terminal.prototype.tabWidth = 8;
174
175/**
rginda9f5222b2012-03-05 11:53:28 -0800176 * Select a preference profile.
177 *
178 * This will load the terminal preferences for the given profile name and
179 * associate subsequent preference changes with the new preference profile.
180 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500181 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800182 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700183 * @param {function} opt_callback Optional callback to invoke when the profile
184 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800185 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700186hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
187 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800188
Robert Ginda57f03b42012-09-13 11:02:48 -0700189 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800190
Robert Ginda57f03b42012-09-13 11:02:48 -0700191 if (this.prefs_)
192 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800193
Robert Ginda57f03b42012-09-13 11:02:48 -0700194 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
195 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800196 'alt-gr-mode': function(v) {
197 if (v == null) {
198 if (navigator.language.toLowerCase() == 'en-us') {
199 v = 'none';
200 } else {
201 v = 'right-alt';
202 }
203 } else if (typeof v == 'string') {
204 v = v.toLowerCase();
205 } else {
206 v = 'none';
207 }
208
209 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v))
210 v = 'none';
211
212 terminal.keyboard.altGrMode = v;
213 },
214
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700215 'alt-backspace-is-meta-backspace': function(v) {
216 terminal.keyboard.altBackspaceIsMetaBackspace = v;
217 },
218
Robert Ginda57f03b42012-09-13 11:02:48 -0700219 'alt-is-meta': function(v) {
220 terminal.keyboard.altIsMeta = v;
221 },
222
223 'alt-sends-what': function(v) {
224 if (!/^(escape|8-bit|browser-key)$/.test(v))
225 v = 'escape';
226
227 terminal.keyboard.altSendsWhat = v;
228 },
229
230 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800231 var ary = v.match(/^lib-resource:(\S+)/);
232 if (ary) {
233 terminal.bellAudio_.setAttribute('src',
234 lib.resource.getDataUrl(ary[1]));
235 } else {
236 terminal.bellAudio_.setAttribute('src', v);
237 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700238 },
239
Michael Kelly485ecd12014-06-09 11:41:56 -0400240 'desktop-notification-bell': function(v) {
241 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700242 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400243 Notification.permission === 'granted';
244 if (!terminal.desktopNotificationBell_) {
245 // Note: We don't call Notification.requestPermission here because
246 // Chrome requires the call be the result of a user action (such as an
247 // onclick handler), and pref listeners are run asynchronously.
248 //
249 // A way of working around this would be to display a dialog in the
250 // terminal with a "click-to-request-permission" button.
251 console.warn('desktop-notification-bell is true but we do not have ' +
252 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400253 }
254 } else {
255 terminal.desktopNotificationBell_ = false;
256 }
257 },
258
Robert Ginda57f03b42012-09-13 11:02:48 -0700259 'background-color': function(v) {
260 terminal.setBackgroundColor(v);
261 },
262
263 'background-image': function(v) {
264 terminal.scrollPort_.setBackgroundImage(v);
265 },
266
267 'background-size': function(v) {
268 terminal.scrollPort_.setBackgroundSize(v);
269 },
270
271 'background-position': function(v) {
272 terminal.scrollPort_.setBackgroundPosition(v);
273 },
274
275 'backspace-sends-backspace': function(v) {
276 terminal.keyboard.backspaceSendsBackspace = v;
277 },
278
Brad Town18654b62015-03-12 00:27:45 -0700279 'character-map-overrides': function(v) {
280 if (!(v == null || v instanceof Object)) {
281 console.warn('Preference character-map-modifications is not an ' +
282 'object: ' + v);
283 return;
284 }
285
286 for (var code in v) {
287 var glmap = hterm.VT.CharacterMap.maps[code].glmap;
288 for (var received in v[code]) {
289 glmap[received] = v[code][received];
290 }
291 hterm.VT.CharacterMap.maps[code].reset(glmap);
292 }
293 },
294
Robert Ginda57f03b42012-09-13 11:02:48 -0700295 'cursor-blink': function(v) {
296 terminal.setCursorBlink(!!v);
297 },
298
Robert Gindaea2183e2014-07-17 09:51:51 -0700299 'cursor-blink-cycle': function(v) {
300 if (v instanceof Array &&
301 typeof v[0] == 'number' &&
302 typeof v[1] == 'number') {
303 terminal.cursorBlinkCycle_ = v;
304 } else if (typeof v == 'number') {
305 terminal.cursorBlinkCycle_ = [v, v];
306 } else {
307 // Fast blink indicates an error.
308 terminal.cursorBlinkCycle_ = [100, 100];
309 }
310 },
311
Robert Ginda57f03b42012-09-13 11:02:48 -0700312 'cursor-color': function(v) {
313 terminal.setCursorColor(v);
314 },
315
316 'color-palette-overrides': function(v) {
317 if (!(v == null || v instanceof Object || v instanceof Array)) {
318 console.warn('Preference color-palette-overrides is not an array or ' +
319 'object: ' + v);
320 return;
rginda9f5222b2012-03-05 11:53:28 -0800321 }
rginda9f5222b2012-03-05 11:53:28 -0800322
Robert Ginda57f03b42012-09-13 11:02:48 -0700323 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700324
Robert Ginda57f03b42012-09-13 11:02:48 -0700325 if (v) {
326 for (var key in v) {
327 var i = parseInt(key);
328 if (isNaN(i) || i < 0 || i > 255) {
329 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
330 continue;
331 }
332
333 if (v[i]) {
334 var rgb = lib.colors.normalizeCSS(v[i]);
335 if (rgb)
336 lib.colors.colorPalette[i] = rgb;
337 }
338 }
rginda30f20f62012-04-05 16:36:19 -0700339 }
rginda30f20f62012-04-05 16:36:19 -0700340
Robert Ginda57f03b42012-09-13 11:02:48 -0700341 terminal.primaryScreen_.textAttributes.resetColorPalette()
342 terminal.alternateScreen_.textAttributes.resetColorPalette();
343 },
rginda30f20f62012-04-05 16:36:19 -0700344
Robert Ginda57f03b42012-09-13 11:02:48 -0700345 'copy-on-select': function(v) {
346 terminal.copyOnSelect = !!v;
347 },
rginda9f5222b2012-03-05 11:53:28 -0800348
Rob Spies0bec09b2014-06-06 15:58:09 -0700349 'use-default-window-copy': function(v) {
350 terminal.useDefaultWindowCopy = !!v;
351 },
352
353 'clear-selection-after-copy': function(v) {
354 terminal.clearSelectionAfterCopy = !!v;
355 },
356
Robert Ginda7e5e9522014-03-14 12:23:58 -0700357 'ctrl-plus-minus-zero-zoom': function(v) {
358 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
359 },
360
Robert Gindafb5a3f92014-05-13 14:12:00 -0700361 'ctrl-c-copy': function(v) {
362 terminal.keyboard.ctrlCCopy = v;
363 },
364
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100365 'ctrl-v-paste': function(v) {
366 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700367 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100368 },
369
Masaya Suzuki273aa982014-05-31 07:25:55 +0900370 'east-asian-ambiguous-as-two-column': function(v) {
371 lib.wc.regardCjkAmbiguous = v;
372 },
373
Robert Ginda57f03b42012-09-13 11:02:48 -0700374 'enable-8-bit-control': function(v) {
375 terminal.vt.enable8BitControl = !!v;
376 },
rginda30f20f62012-04-05 16:36:19 -0700377
Robert Ginda57f03b42012-09-13 11:02:48 -0700378 'enable-bold': function(v) {
379 terminal.syncBoldSafeState();
380 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400381
Robert Ginda3e278d72014-03-25 13:18:51 -0700382 'enable-bold-as-bright': function(v) {
383 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
384 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
385 },
386
Robert Ginda57f03b42012-09-13 11:02:48 -0700387 'enable-clipboard-write': function(v) {
388 terminal.vt.enableClipboardWrite = !!v;
389 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400390
Robert Ginda3755e752013-05-31 13:34:09 -0700391 'enable-dec12': function(v) {
392 terminal.vt.enableDec12 = !!v;
393 },
394
Robert Ginda57f03b42012-09-13 11:02:48 -0700395 'font-family': function(v) {
396 terminal.syncFontFamily();
397 },
rginda30f20f62012-04-05 16:36:19 -0700398
Robert Ginda57f03b42012-09-13 11:02:48 -0700399 'font-size': function(v) {
400 terminal.setFontSize(v);
401 },
rginda9875d902012-08-20 16:21:57 -0700402
Robert Ginda57f03b42012-09-13 11:02:48 -0700403 'font-smoothing': function(v) {
404 terminal.syncFontFamily();
405 },
rgindade84e382012-04-20 15:39:31 -0700406
Robert Ginda57f03b42012-09-13 11:02:48 -0700407 'foreground-color': function(v) {
408 terminal.setForegroundColor(v);
409 },
rginda30f20f62012-04-05 16:36:19 -0700410
Robert Ginda57f03b42012-09-13 11:02:48 -0700411 'home-keys-scroll': function(v) {
412 terminal.keyboard.homeKeysScroll = v;
413 },
rginda4bba5e12012-06-20 16:15:30 -0700414
Robert Gindaa8165692015-06-15 14:46:31 -0700415 'keybindings': function(v) {
416 terminal.keyboard.bindings.clear();
417
418 if (!v)
419 return;
420
421 if (!(v instanceof Object)) {
422 console.error('Error in keybindings preference: Expected object');
423 return;
424 }
425
426 try {
427 terminal.keyboard.bindings.addBindings(v);
428 } catch (ex) {
429 console.error('Error in keybindings preference: ' + ex);
430 }
431 },
432
Robert Ginda57f03b42012-09-13 11:02:48 -0700433 'max-string-sequence': function(v) {
434 terminal.vt.maxStringSequence = v;
435 },
rginda11057d52012-04-25 12:29:56 -0700436
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700437 'media-keys-are-fkeys': function(v) {
438 terminal.keyboard.mediaKeysAreFKeys = v;
439 },
440
Robert Ginda57f03b42012-09-13 11:02:48 -0700441 'meta-sends-escape': function(v) {
442 terminal.keyboard.metaSendsEscape = v;
443 },
rginda30f20f62012-04-05 16:36:19 -0700444
Robert Ginda57f03b42012-09-13 11:02:48 -0700445 'mouse-paste-button': function(v) {
446 terminal.syncMousePasteButton();
447 },
rgindaa8ba17d2012-08-15 14:41:10 -0700448
Robert Gindae76aa9f2014-03-14 12:29:12 -0700449 'page-keys-scroll': function(v) {
450 terminal.keyboard.pageKeysScroll = v;
451 },
452
Robert Ginda40932892012-12-10 17:26:40 -0800453 'pass-alt-number': function(v) {
454 if (v == null) {
455 var osx = window.navigator.userAgent.match(/Mac OS X/);
456
457 // Let Alt-1..9 pass to the browser (to control tab switching) on
458 // non-OS X systems, or if hterm is not opened in an app window.
459 v = (!osx && hterm.windowType != 'popup');
460 }
461
462 terminal.passAltNumber = v;
463 },
464
465 'pass-ctrl-number': function(v) {
466 if (v == null) {
467 var osx = window.navigator.userAgent.match(/Mac OS X/);
468
469 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
470 // non-OS X systems, or if hterm is not opened in an app window.
471 v = (!osx && hterm.windowType != 'popup');
472 }
473
474 terminal.passCtrlNumber = v;
475 },
476
477 'pass-meta-number': function(v) {
478 if (v == null) {
479 var osx = window.navigator.userAgent.match(/Mac OS X/);
480
481 // Let Meta-1..9 pass to the browser (to control tab switching) on
482 // OS X systems, or if hterm is not opened in an app window.
483 v = (osx && hterm.windowType != 'popup');
484 }
485
486 terminal.passMetaNumber = v;
487 },
488
Marius Schilder77857b32014-05-14 16:21:26 -0700489 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700490 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700491 },
492
Robert Ginda8cb7d902013-06-20 14:37:18 -0700493 'receive-encoding': function(v) {
494 if (!(/^(utf-8|raw)$/).test(v)) {
495 console.warn('Invalid value for "receive-encoding": ' + v);
496 v = 'utf-8';
497 }
498
499 terminal.vt.characterEncoding = v;
500 },
501
Robert Ginda57f03b42012-09-13 11:02:48 -0700502 'scroll-on-keystroke': function(v) {
503 terminal.scrollOnKeystroke_ = v;
504 },
rginda9f5222b2012-03-05 11:53:28 -0800505
Robert Ginda57f03b42012-09-13 11:02:48 -0700506 'scroll-on-output': function(v) {
507 terminal.scrollOnOutput_ = v;
508 },
rginda30f20f62012-04-05 16:36:19 -0700509
Robert Ginda57f03b42012-09-13 11:02:48 -0700510 'scrollbar-visible': function(v) {
511 terminal.setScrollbarVisible(v);
512 },
rginda9f5222b2012-03-05 11:53:28 -0800513
Rob Spies49039e52014-12-17 13:40:04 -0800514 'scroll-wheel-move-multiplier': function(v) {
515 terminal.setScrollWheelMoveMultipler(v);
516 },
517
Robert Ginda8cb7d902013-06-20 14:37:18 -0700518 'send-encoding': function(v) {
519 if (!(/^(utf-8|raw)$/).test(v)) {
520 console.warn('Invalid value for "send-encoding": ' + v);
521 v = 'utf-8';
522 }
523
524 terminal.keyboard.characterEncoding = v;
525 },
526
Robert Ginda57f03b42012-09-13 11:02:48 -0700527 'shift-insert-paste': function(v) {
528 terminal.keyboard.shiftInsertPaste = v;
529 },
rginda9f5222b2012-03-05 11:53:28 -0800530
Robert Gindae76aa9f2014-03-14 12:29:12 -0700531 'user-css': function(v) {
532 terminal.scrollPort_.setUserCss(v);
Robert Ginda57f03b42012-09-13 11:02:48 -0700533 }
534 });
rginda30f20f62012-04-05 16:36:19 -0700535
Robert Ginda57f03b42012-09-13 11:02:48 -0700536 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800537 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700538
539 if (opt_callback)
540 opt_callback();
541 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800542};
543
Rob Spies56953412014-04-28 14:09:47 -0700544
545/**
546 * Returns the preferences manager used for configuring this terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500547 *
548 * @return {hterm.PreferenceManager}
Rob Spies56953412014-04-28 14:09:47 -0700549 */
550hterm.Terminal.prototype.getPrefs = function() {
551 return this.prefs_;
552};
553
Robert Gindaa063b202014-07-21 11:08:25 -0700554/**
555 * Enable or disable bracketed paste mode.
Evan Jones2600d4f2016-12-06 09:29:36 -0500556 *
557 * @param {boolean} state The value to set.
Robert Gindaa063b202014-07-21 11:08:25 -0700558 */
559hterm.Terminal.prototype.setBracketedPaste = function(state) {
560 this.options_.bracketedPaste = state;
561};
Rob Spies56953412014-04-28 14:09:47 -0700562
rginda8e92a692012-05-20 19:37:20 -0700563/**
564 * Set the color for the cursor.
565 *
566 * If you want this setting to persist, set it through prefs_, rather than
567 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500568 *
569 * @param {string} color The color to set.
rginda8e92a692012-05-20 19:37:20 -0700570 */
571hterm.Terminal.prototype.setCursorColor = function(color) {
Robert Ginda830583c2013-08-07 13:20:46 -0700572 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700573 this.cursorNode_.style.backgroundColor = color;
574 this.cursorNode_.style.borderColor = color;
575};
576
577/**
578 * Return the current cursor color as a string.
Evan Jones2600d4f2016-12-06 09:29:36 -0500579 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700580 */
581hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700582 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700583};
584
585/**
rgindad5613292012-06-19 15:40:37 -0700586 * Enable or disable mouse based text selection in the terminal.
Evan Jones2600d4f2016-12-06 09:29:36 -0500587 *
588 * @param {boolean} state The value to set.
rgindad5613292012-06-19 15:40:37 -0700589 */
590hterm.Terminal.prototype.setSelectionEnabled = function(state) {
591 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700592};
593
594/**
rginda8e92a692012-05-20 19:37:20 -0700595 * Set the background color.
596 *
597 * If you want this setting to persist, set it through prefs_, rather than
598 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500599 *
600 * @param {string} color The color to set.
rginda8e92a692012-05-20 19:37:20 -0700601 */
602hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700603 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700604 this.primaryScreen_.textAttributes.setDefaults(
605 this.foregroundColor_, this.backgroundColor_);
606 this.alternateScreen_.textAttributes.setDefaults(
607 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700608 this.scrollPort_.setBackgroundColor(color);
609};
610
rginda9f5222b2012-03-05 11:53:28 -0800611/**
612 * Return the current terminal background color.
613 *
614 * Intended for use by other classes, so we don't have to expose the entire
615 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500616 *
617 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800618 */
619hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700620 return this.backgroundColor_;
621};
622
623/**
624 * Set the foreground color.
625 *
626 * If you want this setting to persist, set it through prefs_, rather than
627 * with this method.
Evan Jones2600d4f2016-12-06 09:29:36 -0500628 *
629 * @param {string} color The color to set.
rginda8e92a692012-05-20 19:37:20 -0700630 */
631hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700632 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700633 this.primaryScreen_.textAttributes.setDefaults(
634 this.foregroundColor_, this.backgroundColor_);
635 this.alternateScreen_.textAttributes.setDefaults(
636 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700637 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800638};
639
640/**
641 * Return the current terminal foreground color.
642 *
643 * Intended for use by other classes, so we don't have to expose the entire
644 * prefs_ object.
Evan Jones2600d4f2016-12-06 09:29:36 -0500645 *
646 * @return {string}
rginda9f5222b2012-03-05 11:53:28 -0800647 */
648hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700649 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800650};
651
652/**
rginda87b86462011-12-14 13:48:03 -0800653 * Create a new instance of a terminal command and run it with a given
654 * argument string.
655 *
656 * @param {function} commandClass The constructor for a terminal command.
657 * @param {string} argString The argument string to pass to the command.
658 */
659hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700660 var environment = this.prefs_.get('environment');
661 if (typeof environment != 'object' || environment == null)
662 environment = {};
663
rginda87b86462011-12-14 13:48:03 -0800664 var self = this;
665 this.command = new commandClass(
666 { argString: argString || '',
667 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700668 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800669 onExit: function(code) {
670 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800671 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700672 if (self.prefs_.get('close-on-exit'))
673 window.close();
rginda87b86462011-12-14 13:48:03 -0800674 }
675 });
676
rgindafeaf3142012-01-31 15:14:20 -0800677 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800678 this.command.run();
679};
680
681/**
rgindafeaf3142012-01-31 15:14:20 -0800682 * Returns true if the current screen is the primary screen, false otherwise.
Evan Jones2600d4f2016-12-06 09:29:36 -0500683 *
684 * @return {boolean}
rgindafeaf3142012-01-31 15:14:20 -0800685 */
686hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700687 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800688};
689
690/**
691 * Install the keyboard handler for this terminal.
692 *
693 * This will prevent the browser from seeing any keystrokes sent to the
694 * terminal.
695 */
696hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700697 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
rgindafeaf3142012-01-31 15:14:20 -0800698}
699
700/**
701 * Uninstall the keyboard handler for this terminal.
702 */
703hterm.Terminal.prototype.uninstallKeyboard = function() {
704 this.keyboard.installKeyboard(null);
705}
706
707/**
rginda35c456b2012-02-09 17:29:05 -0800708 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800709 *
710 * Call setFontSize(0) to reset to the default font size.
711 *
712 * This function does not modify the font-size preference.
713 *
714 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800715 */
716hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800717 if (px === 0)
718 px = this.prefs_.get('font-size');
719
rginda35c456b2012-02-09 17:29:05 -0800720 this.scrollPort_.setFontSize(px);
Ricky Liang48f05cb2013-12-31 23:35:29 +0800721 if (this.wcCssRule_) {
722 this.wcCssRule_.style.width = this.scrollPort_.characterSize.width * 2 +
723 'px';
724 }
rginda35c456b2012-02-09 17:29:05 -0800725};
726
727/**
728 * Get the current font size.
Evan Jones2600d4f2016-12-06 09:29:36 -0500729 *
730 * @return {number}
rginda35c456b2012-02-09 17:29:05 -0800731 */
732hterm.Terminal.prototype.getFontSize = function() {
733 return this.scrollPort_.getFontSize();
734};
735
736/**
rginda8e92a692012-05-20 19:37:20 -0700737 * Get the current font family.
Evan Jones2600d4f2016-12-06 09:29:36 -0500738 *
739 * @return {string}
rginda8e92a692012-05-20 19:37:20 -0700740 */
741hterm.Terminal.prototype.getFontFamily = function() {
742 return this.scrollPort_.getFontFamily();
743};
744
745/**
rginda35c456b2012-02-09 17:29:05 -0800746 * Set the CSS "font-family" for this terminal.
747 */
rginda9f5222b2012-03-05 11:53:28 -0800748hterm.Terminal.prototype.syncFontFamily = function() {
749 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
750 this.prefs_.get('font-smoothing'));
751 this.syncBoldSafeState();
752};
753
rginda4bba5e12012-06-20 16:15:30 -0700754/**
755 * Set this.mousePasteButton based on the mouse-paste-button pref,
756 * autodetecting if necessary.
757 */
758hterm.Terminal.prototype.syncMousePasteButton = function() {
759 var button = this.prefs_.get('mouse-paste-button');
760 if (typeof button == 'number') {
761 this.mousePasteButton = button;
762 return;
763 }
764
765 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
766 if (!ary || ary[2] == 'CrOS') {
767 this.mousePasteButton = 2;
768 } else {
769 this.mousePasteButton = 3;
770 }
771};
772
773/**
774 * Enable or disable bold based on the enable-bold pref, autodetecting if
775 * necessary.
776 */
rginda9f5222b2012-03-05 11:53:28 -0800777hterm.Terminal.prototype.syncBoldSafeState = function() {
778 var enableBold = this.prefs_.get('enable-bold');
779 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700780 this.primaryScreen_.textAttributes.enableBold = enableBold;
781 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800782 return;
783 }
784
rgindaf7521392012-02-28 17:20:34 -0800785 var normalSize = this.scrollPort_.measureCharacterSize();
786 var boldSize = this.scrollPort_.measureCharacterSize('bold');
787
788 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800789 if (!isBoldSafe) {
790 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700791 'from normal. Font family is: ' +
792 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800793 }
rginda9f5222b2012-03-05 11:53:28 -0800794
Robert Gindaed016262012-10-26 16:27:09 -0700795 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
796 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800797};
798
799/**
rginda87b86462011-12-14 13:48:03 -0800800 * Return a copy of the current cursor position.
801 *
802 * @return {hterm.RowCol} The RowCol object representing the current position.
803 */
804hterm.Terminal.prototype.saveCursor = function() {
805 return this.screen_.cursorPosition.clone();
806};
807
Evan Jones2600d4f2016-12-06 09:29:36 -0500808/**
809 * Return the current text attributes.
810 *
811 * @return {string}
812 */
rgindaa19afe22012-01-25 15:40:22 -0800813hterm.Terminal.prototype.getTextAttributes = function() {
814 return this.screen_.textAttributes;
815};
816
Evan Jones2600d4f2016-12-06 09:29:36 -0500817/**
818 * Set the text attributes.
819 *
820 * @param {string} textAttributes The attributes to set.
821 */
rginda1a09aa02012-06-18 21:11:25 -0700822hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
823 this.screen_.textAttributes = textAttributes;
824};
825
rginda87b86462011-12-14 13:48:03 -0800826/**
rgindaf522ce02012-04-17 17:49:17 -0700827 * Return the current browser zoom factor applied to the terminal.
828 *
829 * @return {number} The current browser zoom factor.
830 */
831hterm.Terminal.prototype.getZoomFactor = function() {
832 return this.scrollPort_.characterSize.zoomFactor;
833};
834
835/**
rginda9846e2f2012-01-27 13:53:33 -0800836 * Change the title of this terminal's window.
Evan Jones2600d4f2016-12-06 09:29:36 -0500837 *
838 * @param {string} title The title to set.
rginda9846e2f2012-01-27 13:53:33 -0800839 */
840hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800841 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800842};
843
844/**
rginda87b86462011-12-14 13:48:03 -0800845 * Restore a previously saved cursor position.
846 *
847 * @param {hterm.RowCol} cursor The position to restore.
848 */
849hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700850 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
851 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800852 this.screen_.setCursorPosition(row, column);
853 if (cursor.column > column ||
854 cursor.column == column && cursor.overflow) {
855 this.screen_.cursorPosition.overflow = true;
856 }
rginda87b86462011-12-14 13:48:03 -0800857};
858
859/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400860 * Clear the cursor's overflow flag.
861 */
862hterm.Terminal.prototype.clearCursorOverflow = function() {
863 this.screen_.cursorPosition.overflow = false;
864};
865
866/**
Robert Ginda830583c2013-08-07 13:20:46 -0700867 * Sets the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -0500868 *
869 * @param {string} shape The shape to set.
Robert Ginda830583c2013-08-07 13:20:46 -0700870 */
871hterm.Terminal.prototype.setCursorShape = function(shape) {
872 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -0800873 this.restyleCursor_();
Robert Ginda830583c2013-08-07 13:20:46 -0700874}
875
876/**
877 * Get the cursor shape
Evan Jones2600d4f2016-12-06 09:29:36 -0500878 *
879 * @return {string}
Robert Ginda830583c2013-08-07 13:20:46 -0700880 */
881hterm.Terminal.prototype.getCursorShape = function() {
882 return this.cursorShape_;
883}
884
885/**
rginda87b86462011-12-14 13:48:03 -0800886 * Set the width of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -0500887 *
888 * @param {number} columnCount
rginda87b86462011-12-14 13:48:03 -0800889 */
890hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800891 if (columnCount == null) {
892 this.div_.style.width = '100%';
893 return;
894 }
895
Robert Ginda26806d12014-07-24 13:44:07 -0700896 this.div_.style.width = Math.ceil(
897 this.scrollPort_.characterSize.width *
898 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400899 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800900 this.scheduleSyncCursorPosition_();
901};
rginda87b86462011-12-14 13:48:03 -0800902
rgindac9bc5502012-01-18 11:48:44 -0800903/**
rginda35c456b2012-02-09 17:29:05 -0800904 * Set the height of the terminal, resizing the UI to match.
Evan Jones2600d4f2016-12-06 09:29:36 -0500905 *
906 * @param {number} rowCount The height in rows.
rginda35c456b2012-02-09 17:29:05 -0800907 */
908hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800909 if (rowCount == null) {
910 this.div_.style.height = '100%';
911 return;
912 }
913
rginda35c456b2012-02-09 17:29:05 -0800914 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700915 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800916 this.realizeSize_(this.screenSize.width, rowCount);
917 this.scheduleSyncCursorPosition_();
918};
919
920/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400921 * Deal with terminal size changes.
922 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500923 * @param {number} columnCount The number of columns.
924 * @param {number} rowCount The number of rows.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400925 */
926hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
927 if (columnCount != this.screenSize.width)
928 this.realizeWidth_(columnCount);
929
930 if (rowCount != this.screenSize.height)
931 this.realizeHeight_(rowCount);
932
933 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -0700934 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400935};
936
937/**
rgindac9bc5502012-01-18 11:48:44 -0800938 * Deal with terminal width changes.
939 *
940 * This function does what needs to be done when the terminal width changes
941 * out from under us. It happens here rather than in onResize_() because this
942 * code may need to run synchronously to handle programmatic changes of
943 * terminal width.
944 *
945 * Relying on the browser to send us an async resize event means we may not be
946 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -0500947 *
948 * @param {number} columnCount The number of columns.
rgindac9bc5502012-01-18 11:48:44 -0800949 */
950hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700951 if (columnCount <= 0)
952 throw new Error('Attempt to realize bad width: ' + columnCount);
953
rgindac9bc5502012-01-18 11:48:44 -0800954 var deltaColumns = columnCount - this.screen_.getWidth();
955
rginda87b86462011-12-14 13:48:03 -0800956 this.screenSize.width = columnCount;
957 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800958
959 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400960 if (this.defaultTabStops)
961 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800962 } else {
963 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400964 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800965 break;
966
967 this.tabStops_.pop();
968 }
969 }
970
971 this.screen_.setColumnCount(this.screenSize.width);
972};
973
974/**
975 * Deal with terminal height changes.
976 *
977 * This function does what needs to be done when the terminal height changes
978 * out from under us. It happens here rather than in onResize_() because this
979 * code may need to run synchronously to handle programmatic changes of
980 * terminal height.
981 *
982 * Relying on the browser to send us an async resize event means we may not be
983 * in the correct state yet when the next escape sequence hits.
Evan Jones2600d4f2016-12-06 09:29:36 -0500984 *
985 * @param {number} rowCount The number of rows.
rgindac9bc5502012-01-18 11:48:44 -0800986 */
987hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700988 if (rowCount <= 0)
989 throw new Error('Attempt to realize bad height: ' + rowCount);
990
rgindac9bc5502012-01-18 11:48:44 -0800991 var deltaRows = rowCount - this.screen_.getHeight();
992
993 this.screenSize.height = rowCount;
994
995 var cursor = this.saveCursor();
996
997 if (deltaRows < 0) {
998 // Screen got smaller.
999 deltaRows *= -1;
1000 while (deltaRows) {
1001 var lastRow = this.getRowCount() - 1;
1002 if (lastRow - this.scrollbackRows_.length == cursor.row)
1003 break;
1004
1005 if (this.getRowText(lastRow))
1006 break;
1007
1008 this.screen_.popRow();
1009 deltaRows--;
1010 }
1011
1012 var ary = this.screen_.shiftRows(deltaRows);
1013 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
1014
1015 // We just removed rows from the top of the screen, we need to update
1016 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -08001017 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -08001018 } else if (deltaRows > 0) {
1019 // Screen got larger.
1020
1021 if (deltaRows <= this.scrollbackRows_.length) {
1022 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
1023 var rows = this.scrollbackRows_.splice(
1024 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
1025 this.screen_.unshiftRows(rows);
1026 deltaRows -= scrollbackCount;
1027 cursor.row += scrollbackCount;
1028 }
1029
1030 if (deltaRows)
1031 this.appendRows_(deltaRows);
1032 }
1033
rginda35c456b2012-02-09 17:29:05 -08001034 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -08001035 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -08001036};
1037
1038/**
1039 * Scroll the terminal to the top of the scrollback buffer.
1040 */
1041hterm.Terminal.prototype.scrollHome = function() {
1042 this.scrollPort_.scrollRowToTop(0);
1043};
1044
1045/**
1046 * Scroll the terminal to the end.
1047 */
1048hterm.Terminal.prototype.scrollEnd = function() {
1049 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1050};
1051
1052/**
1053 * Scroll the terminal one page up (minus one line) relative to the current
1054 * position.
1055 */
1056hterm.Terminal.prototype.scrollPageUp = function() {
1057 var i = this.scrollPort_.getTopRowIndex();
1058 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
1059};
1060
1061/**
1062 * Scroll the terminal one page down (minus one line) relative to the current
1063 * position.
1064 */
1065hterm.Terminal.prototype.scrollPageDown = function() {
1066 var i = this.scrollPort_.getTopRowIndex();
1067 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -08001068};
1069
rgindac9bc5502012-01-18 11:48:44 -08001070/**
Robert Ginda40932892012-12-10 17:26:40 -08001071 * Clear primary screen, secondary screen, and the scrollback buffer.
1072 */
1073hterm.Terminal.prototype.wipeContents = function() {
1074 this.scrollbackRows_.length = 0;
1075 this.scrollPort_.resetCache();
1076
1077 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
1078 var bottom = screen.getHeight();
1079 if (bottom > 0) {
1080 this.renumberRows_(0, bottom);
1081 this.clearHome(screen);
1082 }
1083 }.bind(this));
1084
1085 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001086 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001087};
1088
1089/**
rgindac9bc5502012-01-18 11:48:44 -08001090 * Full terminal reset.
1091 */
rginda87b86462011-12-14 13:48:03 -08001092hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -08001093 this.clearAllTabStops();
1094 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001095
1096 this.clearHome(this.primaryScreen_);
1097 this.primaryScreen_.textAttributes.reset();
1098
1099 this.clearHome(this.alternateScreen_);
1100 this.alternateScreen_.textAttributes.reset();
1101
rgindab8bc8932012-04-27 12:45:03 -07001102 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1103
Robert Ginda92e18102013-03-14 13:56:37 -07001104 this.vt.reset();
1105
rgindac9bc5502012-01-18 11:48:44 -08001106 this.softReset();
rginda87b86462011-12-14 13:48:03 -08001107};
1108
rgindac9bc5502012-01-18 11:48:44 -08001109/**
1110 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001111 *
1112 * Perform a soft reset to the default values listed in
1113 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001114 */
rginda0f5c0292012-01-13 11:00:13 -08001115hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -07001116 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001117 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001118
Brad Townb62dfdc2015-03-16 19:07:15 -07001119 // We show the cursor on soft reset but do not alter the blink state.
1120 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1121
rgindab8bc8932012-04-27 12:45:03 -07001122 // Xterm also resets the color palette on soft reset, even though it doesn't
1123 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -07001124 this.primaryScreen_.textAttributes.resetColorPalette();
1125 this.alternateScreen_.textAttributes.resetColorPalette();
1126
rgindab8bc8932012-04-27 12:45:03 -07001127 // The xterm man page explicitly says this will happen on soft reset.
1128 this.setVTScrollRegion(null, null);
1129
1130 // Xterm also shows the cursor on soft reset, but does not alter the blink
1131 // state.
rgindaa19afe22012-01-25 15:40:22 -08001132 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001133};
1134
rgindac9bc5502012-01-18 11:48:44 -08001135/**
1136 * Move the cursor forward to the next tab stop, or to the last column
1137 * if no more tab stops are set.
1138 */
1139hterm.Terminal.prototype.forwardTabStop = function() {
1140 var column = this.screen_.cursorPosition.column;
1141
1142 for (var i = 0; i < this.tabStops_.length; i++) {
1143 if (this.tabStops_[i] > column) {
1144 this.setCursorColumn(this.tabStops_[i]);
1145 return;
1146 }
1147 }
1148
David Benjamin66e954d2012-05-05 21:08:12 -04001149 // xterm does not clear the overflow flag on HT or CHT.
1150 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001151 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001152 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001153};
1154
rgindac9bc5502012-01-18 11:48:44 -08001155/**
1156 * Move the cursor backward to the previous tab stop, or to the first column
1157 * if no previous tab stops are set.
1158 */
1159hterm.Terminal.prototype.backwardTabStop = function() {
1160 var column = this.screen_.cursorPosition.column;
1161
1162 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1163 if (this.tabStops_[i] < column) {
1164 this.setCursorColumn(this.tabStops_[i]);
1165 return;
1166 }
1167 }
1168
1169 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001170};
1171
rgindac9bc5502012-01-18 11:48:44 -08001172/**
1173 * Set a tab stop at the given column.
1174 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001175 * @param {integer} column Zero based column.
rgindac9bc5502012-01-18 11:48:44 -08001176 */
1177hterm.Terminal.prototype.setTabStop = function(column) {
1178 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1179 if (this.tabStops_[i] == column)
1180 return;
1181
1182 if (this.tabStops_[i] < column) {
1183 this.tabStops_.splice(i + 1, 0, column);
1184 return;
1185 }
1186 }
1187
1188 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001189};
1190
rgindac9bc5502012-01-18 11:48:44 -08001191/**
1192 * Clear the tab stop at the current cursor position.
1193 *
1194 * No effect if there is no tab stop at the current cursor position.
1195 */
1196hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1197 var column = this.screen_.cursorPosition.column;
1198
1199 var i = this.tabStops_.indexOf(column);
1200 if (i == -1)
1201 return;
1202
1203 this.tabStops_.splice(i, 1);
1204};
1205
1206/**
1207 * Clear all tab stops.
1208 */
1209hterm.Terminal.prototype.clearAllTabStops = function() {
1210 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001211 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001212};
1213
1214/**
1215 * Set up the default tab stops, starting from a given column.
1216 *
1217 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001218 * from the specified column, or 0 if no column is provided. It also flags
1219 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001220 *
1221 * This does not clear the existing tab stops first, use clearAllTabStops
1222 * for that.
1223 *
Evan Jones2600d4f2016-12-06 09:29:36 -05001224 * @param {integer} opt_start Optional starting zero based starting column, useful
rgindac9bc5502012-01-18 11:48:44 -08001225 * for filling out missing tab stops when the terminal is resized.
1226 */
1227hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1228 var start = opt_start || 0;
1229 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001230 // Round start up to a default tab stop.
1231 start = start - 1 - ((start - 1) % w) + w;
1232 for (var i = start; i < this.screenSize.width; i += w) {
1233 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001234 }
David Benjamin66e954d2012-05-05 21:08:12 -04001235
1236 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001237};
1238
rginda6d397402012-01-17 10:58:29 -08001239/**
rginda8ba33642011-12-14 12:31:31 -08001240 * Interpret a sequence of characters.
1241 *
1242 * Incomplete escape sequences are buffered until the next call.
1243 *
1244 * @param {string} str Sequence of characters to interpret or pass through.
1245 */
1246hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001247 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001248 this.scheduleSyncCursorPosition_();
1249};
1250
1251/**
1252 * Take over the given DIV for use as the terminal display.
1253 *
1254 * @param {HTMLDivElement} div The div to use as the terminal display.
1255 */
1256hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001257 this.div_ = div;
1258
rginda8ba33642011-12-14 12:31:31 -08001259 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001260 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001261 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1262 this.scrollPort_.setBackgroundPosition(
1263 this.prefs_.get('background-position'));
Robert Gindae76aa9f2014-03-14 12:29:12 -07001264 this.scrollPort_.setUserCss(this.prefs_.get('user-css'));
rginda30f20f62012-04-05 16:36:19 -07001265
rginda0918b652012-04-04 11:26:24 -07001266 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001267
rginda9f5222b2012-03-05 11:53:28 -08001268 this.setFontSize(this.prefs_.get('font-size'));
1269 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001270
David Reveman8f552492012-03-28 12:18:41 -04001271 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001272 this.setScrollWheelMoveMultipler(
1273 this.prefs_.get('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001274
rginda8ba33642011-12-14 12:31:31 -08001275 this.document_ = this.scrollPort_.getDocument();
1276
rginda4bba5e12012-06-20 16:15:30 -07001277 this.document_.body.oncontextmenu = function() { return false };
1278
1279 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001280 var screenNode = this.scrollPort_.getScreenNode();
1281 screenNode.addEventListener('mousedown', onMouse);
1282 screenNode.addEventListener('mouseup', onMouse);
1283 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001284 this.scrollPort_.onScrollWheel = onMouse;
1285
Toni Barzic0bfa8922013-11-22 11:18:35 -08001286 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001287 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001288 // Listen for mousedown events on the screenNode as in FF the focus
1289 // events don't bubble.
1290 screenNode.addEventListener('mousedown', function() {
1291 setTimeout(this.onFocusChange_.bind(this, true));
1292 }.bind(this));
1293
Toni Barzic0bfa8922013-11-22 11:18:35 -08001294 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001295 'blur', this.onFocusChange_.bind(this, false));
1296
1297 var style = this.document_.createElement('style');
1298 style.textContent =
1299 ('.cursor-node[focus="false"] {' +
1300 ' box-sizing: border-box;' +
1301 ' background-color: transparent !important;' +
1302 ' border-width: 2px;' +
1303 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001304 '}' +
1305 '.wc-node {' +
1306 ' display: inline-block;' +
1307 ' text-align: center;' +
1308 ' width: ' + this.scrollPort_.characterSize.width * 2 + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001309 '}');
1310 this.document_.head.appendChild(style);
1311
Ricky Liang48f05cb2013-12-31 23:35:29 +08001312 var styleSheets = this.document_.styleSheets;
1313 var cssRules = styleSheets[styleSheets.length - 1].cssRules;
1314 this.wcCssRule_ = cssRules[cssRules.length - 1];
1315
rginda8ba33642011-12-14 12:31:31 -08001316 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001317 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001318 this.cursorNode_.style.cssText =
1319 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001320 'top: -99px;' +
1321 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001322 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1323 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
Rob Spies06533ba2014-04-24 11:20:37 -07001324 '-webkit-transition: opacity, background-color 100ms linear;' +
1325 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001326
rginda8e92a692012-05-20 19:37:20 -07001327 this.setCursorColor(this.prefs_.get('cursor-color'));
Robert Gindafb1be6a2013-12-11 11:56:22 -08001328 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1329 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001330
rginda8ba33642011-12-14 12:31:31 -08001331 this.document_.body.appendChild(this.cursorNode_);
1332
rgindad5613292012-06-19 15:40:37 -07001333 // When 'enableMouseDragScroll' is off we reposition this element directly
1334 // under the mouse cursor after a click. This makes Chrome associate
1335 // subsequent mousemove events with the scroll-blocker. Since the
1336 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1337 // events do not cause the scrollport to scroll.
1338 //
1339 // It's a hack, but it's the cleanest way I could find.
1340 this.scrollBlockerNode_ = this.document_.createElement('div');
1341 this.scrollBlockerNode_.style.cssText =
1342 ('position: absolute;' +
1343 'top: -99px;' +
1344 'display: block;' +
1345 'width: 10px;' +
1346 'height: 10px;');
1347 this.document_.body.appendChild(this.scrollBlockerNode_);
1348
1349 var onMouse = this.onMouse_.bind(this);
1350 this.scrollPort_.onScrollWheel = onMouse;
1351 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1352 ].forEach(function(event) {
1353 this.scrollBlockerNode_.addEventListener(event, onMouse);
1354 this.cursorNode_.addEventListener(event, onMouse);
1355 this.document_.addEventListener(event, onMouse);
1356 }.bind(this));
1357
1358 this.cursorNode_.addEventListener('mousedown', function() {
1359 setTimeout(this.focus.bind(this));
1360 }.bind(this));
1361
rginda8ba33642011-12-14 12:31:31 -08001362 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001363
rginda87b86462011-12-14 13:48:03 -08001364 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001365 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001366};
1367
rginda0918b652012-04-04 11:26:24 -07001368/**
1369 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001370 *
1371 * @return {HTMLDocument}
rginda0918b652012-04-04 11:26:24 -07001372 */
rginda87b86462011-12-14 13:48:03 -08001373hterm.Terminal.prototype.getDocument = function() {
1374 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001375};
1376
1377/**
rginda0918b652012-04-04 11:26:24 -07001378 * Focus the terminal.
1379 */
1380hterm.Terminal.prototype.focus = function() {
1381 this.scrollPort_.focus();
1382};
1383
1384/**
rginda8ba33642011-12-14 12:31:31 -08001385 * Return the HTML Element for a given row index.
1386 *
1387 * This is a method from the RowProvider interface. The ScrollPort uses
1388 * it to fetch rows on demand as they are scrolled into view.
1389 *
1390 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1391 * pairs to conserve memory.
1392 *
1393 * @param {integer} index The zero-based row index, measured relative to the
1394 * start of the scrollback buffer. On-screen rows will always have the
1395 * largest indicies.
1396 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1397 */
1398hterm.Terminal.prototype.getRowNode = function(index) {
1399 if (index < this.scrollbackRows_.length)
1400 return this.scrollbackRows_[index];
1401
1402 var screenIndex = index - this.scrollbackRows_.length;
1403 return this.screen_.rowsArray[screenIndex];
1404};
1405
1406/**
1407 * Return the text content for a given range of rows.
1408 *
1409 * This is a method from the RowProvider interface. The ScrollPort uses
1410 * it to fetch text content on demand when the user attempts to copy their
1411 * selection to the clipboard.
1412 *
1413 * @param {integer} start The zero-based row index to start from, measured
1414 * relative to the start of the scrollback buffer. On-screen rows will
1415 * always have the largest indicies.
1416 * @param {integer} end The zero-based row index to end on, measured
1417 * relative to the start of the scrollback buffer.
1418 * @return {string} A single string containing the text value of the range of
1419 * rows. Lines will be newline delimited, with no trailing newline.
1420 */
1421hterm.Terminal.prototype.getRowsText = function(start, end) {
1422 var ary = [];
1423 for (var i = start; i < end; i++) {
1424 var node = this.getRowNode(i);
1425 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001426 if (i < end - 1 && !node.getAttribute('line-overflow'))
1427 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001428 }
1429
rgindaa09e7332012-08-17 12:49:51 -07001430 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001431};
1432
1433/**
1434 * Return the text content for a given row.
1435 *
1436 * This is a method from the RowProvider interface. The ScrollPort uses
1437 * it to fetch text content on demand when the user attempts to copy their
1438 * selection to the clipboard.
1439 *
1440 * @param {integer} index The zero-based row index to return, measured
1441 * relative to the start of the scrollback buffer. On-screen rows will
1442 * always have the largest indicies.
1443 * @return {string} A string containing the text value of the selected row.
1444 */
1445hterm.Terminal.prototype.getRowText = function(index) {
1446 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001447 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001448};
1449
1450/**
1451 * Return the total number of rows in the addressable screen and in the
1452 * scrollback buffer of this terminal.
1453 *
1454 * This is a method from the RowProvider interface. The ScrollPort uses
1455 * it to compute the size of the scrollbar.
1456 *
1457 * @return {integer} The number of rows in this terminal.
1458 */
1459hterm.Terminal.prototype.getRowCount = function() {
1460 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1461};
1462
1463/**
1464 * Create DOM nodes for new rows and append them to the end of the terminal.
1465 *
1466 * This is the only correct way to add a new DOM node for a row. Notice that
1467 * the new row is appended to the bottom of the list of rows, and does not
1468 * require renumbering (of the rowIndex property) of previous rows.
1469 *
1470 * If you think you want a new blank row somewhere in the middle of the
1471 * terminal, look into moveRows_().
1472 *
1473 * This method does not pay attention to vtScrollTop/Bottom, since you should
1474 * be using moveRows() in cases where they would matter.
1475 *
1476 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001477 *
1478 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001479 */
1480hterm.Terminal.prototype.appendRows_ = function(count) {
1481 var cursorRow = this.screen_.rowsArray.length;
1482 var offset = this.scrollbackRows_.length + cursorRow;
1483 for (var i = 0; i < count; i++) {
1484 var row = this.document_.createElement('x-row');
1485 row.appendChild(this.document_.createTextNode(''));
1486 row.rowIndex = offset + i;
1487 this.screen_.pushRow(row);
1488 }
1489
1490 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1491 if (extraRows > 0) {
1492 var ary = this.screen_.shiftRows(extraRows);
1493 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001494 if (this.scrollPort_.isScrolledEnd)
1495 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001496 }
1497
1498 if (cursorRow >= this.screen_.rowsArray.length)
1499 cursorRow = this.screen_.rowsArray.length - 1;
1500
rginda87b86462011-12-14 13:48:03 -08001501 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001502};
1503
1504/**
1505 * Relocate rows from one part of the addressable screen to another.
1506 *
1507 * This is used to recycle rows during VT scrolls (those which are driven
1508 * by VT commands, rather than by the user manipulating the scrollbar.)
1509 *
1510 * In this case, the blank lines scrolled into the scroll region are made of
1511 * the nodes we scrolled off. These have their rowIndex properties carefully
1512 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001513 *
1514 * @param {number} fromIndex The start index.
1515 * @param {number} count The number of rows to move.
1516 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001517 */
1518hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1519 var ary = this.screen_.removeRows(fromIndex, count);
1520 this.screen_.insertRows(toIndex, ary);
1521
1522 var start, end;
1523 if (fromIndex < toIndex) {
1524 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001525 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001526 } else {
1527 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001528 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001529 }
1530
1531 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001532 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001533};
1534
1535/**
1536 * Renumber the rowIndex property of the given range of rows.
1537 *
1538 * The start and end indicies are relative to the screen, not the scrollback.
1539 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001540 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001541 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001542 *
1543 * @param {number} start The start index.
1544 * @param {number} end The end index.
1545 * @param {hterm.Screen} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001546 */
Robert Ginda40932892012-12-10 17:26:40 -08001547hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1548 var screen = opt_screen || this.screen_;
1549
rginda8ba33642011-12-14 12:31:31 -08001550 var offset = this.scrollbackRows_.length;
1551 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001552 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001553 }
1554};
1555
1556/**
1557 * Print a string to the terminal.
1558 *
1559 * This respects the current insert and wraparound modes. It will add new lines
1560 * to the end of the terminal, scrolling off the top into the scrollback buffer
1561 * if necessary.
1562 *
1563 * The string is *not* parsed for escape codes. Use the interpret() method if
1564 * that's what you're after.
1565 *
1566 * @param{string} str The string to print.
1567 */
1568hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001569 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001570
Ricky Liang48f05cb2013-12-31 23:35:29 +08001571 var strWidth = lib.wc.strWidth(str);
1572
1573 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001574 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1575 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001576 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001577 }
rgindaa19afe22012-01-25 15:40:22 -08001578
Ricky Liang48f05cb2013-12-31 23:35:29 +08001579 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001580 var didOverflow = false;
1581 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001582
rgindaa9abdd82012-08-06 18:05:09 -07001583 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1584 didOverflow = true;
1585 count = this.screenSize.width - this.screen_.cursorPosition.column;
1586 }
rgindaa19afe22012-01-25 15:40:22 -08001587
rgindaa9abdd82012-08-06 18:05:09 -07001588 if (didOverflow && !this.options_.wraparound) {
1589 // If the string overflowed the line but wraparound is off, then the
1590 // last printed character should be the last of the string.
1591 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001592 substr = lib.wc.substr(str, startOffset, count - 1) +
1593 lib.wc.substr(str, strWidth - 1);
1594 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001595 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001596 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001597 }
rgindaa19afe22012-01-25 15:40:22 -08001598
Ricky Liang48f05cb2013-12-31 23:35:29 +08001599 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1600 for (var i = 0; i < tokens.length; i++) {
1601 if (tokens[i].wcNode)
1602 this.screen_.textAttributes.wcNode = true;
1603
1604 if (this.options_.insertMode) {
1605 this.screen_.insertString(tokens[i].str);
1606 } else {
1607 this.screen_.overwriteString(tokens[i].str);
1608 }
1609 this.screen_.textAttributes.wcNode = false;
rgindaa9abdd82012-08-06 18:05:09 -07001610 }
1611
1612 this.screen_.maybeClipCurrentRow();
1613 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001614 }
rginda8ba33642011-12-14 12:31:31 -08001615
1616 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001617
rginda9f5222b2012-03-05 11:53:28 -08001618 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001619 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001620};
1621
1622/**
rginda87b86462011-12-14 13:48:03 -08001623 * Set the VT scroll region.
1624 *
rginda87b86462011-12-14 13:48:03 -08001625 * This also resets the cursor position to the absolute (0, 0) position, since
1626 * that's what xterm appears to do.
1627 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001628 * Setting the scroll region to the full height of the terminal will clear
1629 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1630 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1631 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1632 * continue to work as most users would expect.
1633 *
rginda87b86462011-12-14 13:48:03 -08001634 * @param {integer} scrollTop The zero-based top of the scroll region.
1635 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1636 * inclusive.
1637 */
1638hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001639 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001640 this.vtScrollTop_ = null;
1641 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001642 } else {
1643 this.vtScrollTop_ = scrollTop;
1644 this.vtScrollBottom_ = scrollBottom;
1645 }
rginda87b86462011-12-14 13:48:03 -08001646};
1647
1648/**
rginda8ba33642011-12-14 12:31:31 -08001649 * Return the top row index according to the VT.
1650 *
1651 * This will return 0 unless the terminal has been told to restrict scrolling
1652 * to some lower row. It is used for some VT cursor positioning and scrolling
1653 * commands.
1654 *
1655 * @return {integer} The topmost row in the terminal's scroll region.
1656 */
1657hterm.Terminal.prototype.getVTScrollTop = function() {
1658 if (this.vtScrollTop_ != null)
1659 return this.vtScrollTop_;
1660
1661 return 0;
rginda87b86462011-12-14 13:48:03 -08001662};
rginda8ba33642011-12-14 12:31:31 -08001663
1664/**
1665 * Return the bottom row index according to the VT.
1666 *
1667 * This will return the height of the terminal unless the it has been told to
1668 * restrict scrolling to some higher row. It is used for some VT cursor
1669 * positioning and scrolling commands.
1670 *
1671 * @return {integer} The bottommost row in the terminal's scroll region.
1672 */
1673hterm.Terminal.prototype.getVTScrollBottom = function() {
1674 if (this.vtScrollBottom_ != null)
1675 return this.vtScrollBottom_;
1676
rginda87b86462011-12-14 13:48:03 -08001677 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001678}
1679
1680/**
1681 * Process a '\n' character.
1682 *
1683 * If the cursor is on the final row of the terminal this will append a new
1684 * blank row to the screen and scroll the topmost row into the scrollback
1685 * buffer.
1686 *
1687 * Otherwise, this moves the cursor to column zero of the next row.
1688 */
1689hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001690 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1691 this.screen_.rowsArray.length - 1);
1692
1693 if (this.vtScrollBottom_ != null) {
1694 // A VT Scroll region is active, we never append new rows.
1695 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1696 // We're at the end of the VT Scroll Region, perform a VT scroll.
1697 this.vtScrollUp(1);
1698 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1699 } else if (cursorAtEndOfScreen) {
1700 // We're at the end of the screen, the only thing to do is put the
1701 // cursor to column 0.
1702 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1703 } else {
1704 // Anywhere else, advance the cursor row, and reset the column.
1705 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1706 }
1707 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001708 // We're at the end of the screen. Append a new row to the terminal,
1709 // shifting the top row into the scrollback.
1710 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001711 } else {
rginda87b86462011-12-14 13:48:03 -08001712 // Anywhere else in the screen just moves the cursor.
1713 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001714 }
1715};
1716
1717/**
1718 * Like newLine(), except maintain the cursor column.
1719 */
1720hterm.Terminal.prototype.lineFeed = function() {
1721 var column = this.screen_.cursorPosition.column;
1722 this.newLine();
1723 this.setCursorColumn(column);
1724};
1725
1726/**
rginda87b86462011-12-14 13:48:03 -08001727 * If autoCarriageReturn is set then newLine(), else lineFeed().
1728 */
1729hterm.Terminal.prototype.formFeed = function() {
1730 if (this.options_.autoCarriageReturn) {
1731 this.newLine();
1732 } else {
1733 this.lineFeed();
1734 }
1735};
1736
1737/**
1738 * Move the cursor up one row, possibly inserting a blank line.
1739 *
1740 * The cursor column is not changed.
1741 */
1742hterm.Terminal.prototype.reverseLineFeed = function() {
1743 var scrollTop = this.getVTScrollTop();
1744 var currentRow = this.screen_.cursorPosition.row;
1745
1746 if (currentRow == scrollTop) {
1747 this.insertLines(1);
1748 } else {
1749 this.setAbsoluteCursorRow(currentRow - 1);
1750 }
1751};
1752
1753/**
rginda8ba33642011-12-14 12:31:31 -08001754 * Replace all characters to the left of the current cursor with the space
1755 * character.
1756 *
1757 * TODO(rginda): This should probably *remove* the characters (not just replace
1758 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001759 * position.
rginda8ba33642011-12-14 12:31:31 -08001760 */
1761hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001762 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001763 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001764 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001765 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001766};
1767
1768/**
David Benjamin684a9b72012-05-01 17:19:58 -04001769 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001770 *
1771 * The cursor position is unchanged.
1772 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001773 * If the current background color is not the default background color this
1774 * will insert spaces rather than delete. This is unfortunate because the
1775 * trailing space will affect text selection, but it's difficult to come up
1776 * with a way to style empty space that wouldn't trip up the hterm.Screen
1777 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001778 *
1779 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1780 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1781 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05001782 *
1783 * @param {number} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08001784 */
1785hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001786 if (this.screen_.cursorPosition.overflow)
1787 return;
1788
Robert Ginda7fd57082012-09-25 14:41:47 -07001789 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1790 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001791
1792 if (this.screen_.textAttributes.background ===
1793 this.screen_.textAttributes.DEFAULT_COLOR) {
1794 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08001795 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07001796 this.screen_.cursorPosition.column + count) {
1797 this.screen_.deleteChars(count);
1798 this.clearCursorOverflow();
1799 return;
1800 }
1801 }
1802
rginda87b86462011-12-14 13:48:03 -08001803 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001804 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001805 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001806 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001807};
1808
1809/**
1810 * Erase the current line.
1811 *
1812 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001813 */
1814hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001815 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001816 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001817 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001818 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001819};
1820
1821/**
David Benjamina08d78f2012-05-05 00:28:49 -04001822 * Erase all characters from the start of the screen to the current cursor
1823 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001824 *
1825 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001826 */
1827hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001828 var cursor = this.saveCursor();
1829
1830 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001831
David Benjamina08d78f2012-05-05 00:28:49 -04001832 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001833 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001834 this.screen_.clearCursorRow();
1835 }
1836
rginda87b86462011-12-14 13:48:03 -08001837 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001838 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001839};
1840
1841/**
1842 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001843 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001844 *
1845 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001846 */
1847hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001848 var cursor = this.saveCursor();
1849
1850 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001851
David Benjamina08d78f2012-05-05 00:28:49 -04001852 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001853 for (var i = cursor.row + 1; i <= bottom; i++) {
1854 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001855 this.screen_.clearCursorRow();
1856 }
1857
rginda87b86462011-12-14 13:48:03 -08001858 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001859 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001860};
1861
1862/**
1863 * Fill the terminal with a given character.
1864 *
1865 * This methods does not respect the VT scroll region.
1866 *
1867 * @param {string} ch The character to use for the fill.
1868 */
1869hterm.Terminal.prototype.fill = function(ch) {
1870 var cursor = this.saveCursor();
1871
1872 this.setAbsoluteCursorPosition(0, 0);
1873 for (var row = 0; row < this.screenSize.height; row++) {
1874 for (var col = 0; col < this.screenSize.width; col++) {
1875 this.setAbsoluteCursorPosition(row, col);
1876 this.screen_.overwriteString(ch);
1877 }
1878 }
1879
1880 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001881};
1882
1883/**
rginda9ea433c2012-03-16 11:57:00 -07001884 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001885 *
rginda9ea433c2012-03-16 11:57:00 -07001886 * This does not respect the scroll region.
1887 *
1888 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1889 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001890 */
rginda9ea433c2012-03-16 11:57:00 -07001891hterm.Terminal.prototype.clearHome = function(opt_screen) {
1892 var screen = opt_screen || this.screen_;
1893 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001894
rginda11057d52012-04-25 12:29:56 -07001895 if (bottom == 0) {
1896 // Empty screen, nothing to do.
1897 return;
1898 }
1899
rgindae4d29232012-01-19 10:47:13 -08001900 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001901 screen.setCursorPosition(i, 0);
1902 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001903 }
1904
rginda9ea433c2012-03-16 11:57:00 -07001905 screen.setCursorPosition(0, 0);
1906};
1907
1908/**
1909 * Erase the entire display without changing the cursor position.
1910 *
1911 * The cursor position is unchanged. This does not respect the scroll
1912 * region.
1913 *
1914 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1915 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001916 */
1917hterm.Terminal.prototype.clear = function(opt_screen) {
1918 var screen = opt_screen || this.screen_;
1919 var cursor = screen.cursorPosition.clone();
1920 this.clearHome(screen);
1921 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001922};
1923
1924/**
1925 * VT command to insert lines at the current cursor row.
1926 *
1927 * This respects the current scroll region. Rows pushed off the bottom are
1928 * lost (they won't show up in the scrollback buffer).
1929 *
rginda8ba33642011-12-14 12:31:31 -08001930 * @param {integer} count The number of lines to insert.
1931 */
1932hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07001933 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08001934
1935 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07001936 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08001937
Robert Ginda579186b2012-09-26 11:40:04 -07001938 // The moveCount is the number of rows we need to relocate to make room for
1939 // the new row(s). The count is the distance to move them.
1940 var moveCount = bottom - cursorRow - count + 1;
1941 if (moveCount)
1942 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08001943
Robert Ginda579186b2012-09-26 11:40:04 -07001944 for (var i = count - 1; i >= 0; i--) {
1945 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001946 this.screen_.clearCursorRow();
1947 }
rginda8ba33642011-12-14 12:31:31 -08001948};
1949
1950/**
1951 * VT command to delete lines at the current cursor row.
1952 *
1953 * New rows are added to the bottom of scroll region to take their place. New
1954 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05001955 *
1956 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08001957 */
1958hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001959 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001960
rginda87b86462011-12-14 13:48:03 -08001961 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001962 var bottom = this.getVTScrollBottom();
1963
rginda87b86462011-12-14 13:48:03 -08001964 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001965 count = Math.min(count, maxCount);
1966
rginda87b86462011-12-14 13:48:03 -08001967 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001968 if (count != maxCount)
1969 this.moveRows_(top, count, moveStart);
1970
1971 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001972 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001973 this.screen_.clearCursorRow();
1974 }
1975
rginda87b86462011-12-14 13:48:03 -08001976 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001977 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001978};
1979
1980/**
1981 * Inserts the given number of spaces at the current cursor position.
1982 *
rginda87b86462011-12-14 13:48:03 -08001983 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05001984 *
1985 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08001986 */
1987hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001988 var cursor = this.saveCursor();
1989
rgindacbbd7482012-06-13 15:06:16 -07001990 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001991 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001992 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001993
1994 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001995 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001996};
1997
1998/**
1999 * Forward-delete the specified number of characters starting at the cursor
2000 * position.
2001 *
2002 * @param {integer} count The number of characters to delete.
2003 */
2004hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002005 var deleted = this.screen_.deleteChars(count);
2006 if (deleted && !this.screen_.textAttributes.isDefault()) {
2007 var cursor = this.saveCursor();
2008 this.setCursorColumn(this.screenSize.width - deleted);
2009 this.screen_.insertString(lib.f.getWhitespace(deleted));
2010 this.restoreCursor(cursor);
2011 }
2012
David Benjamin54e8bf62012-06-01 22:31:40 -04002013 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002014};
2015
2016/**
2017 * Shift rows in the scroll region upwards by a given number of lines.
2018 *
2019 * New rows are inserted at the bottom of the scroll region to fill the
2020 * vacated rows. The new rows not filled out with the current text attributes.
2021 *
2022 * This function does not affect the scrollback rows at all. Rows shifted
2023 * off the top are lost.
2024 *
rginda87b86462011-12-14 13:48:03 -08002025 * The cursor position is not altered.
2026 *
rginda8ba33642011-12-14 12:31:31 -08002027 * @param {integer} count The number of rows to scroll.
2028 */
2029hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002030 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002031
rginda87b86462011-12-14 13:48:03 -08002032 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002033 this.deleteLines(count);
2034
rginda87b86462011-12-14 13:48:03 -08002035 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002036};
2037
2038/**
2039 * Shift rows below the cursor down by a given number of lines.
2040 *
2041 * This function respects the current scroll region.
2042 *
2043 * New rows are inserted at the top of the scroll region to fill the
2044 * vacated rows. The new rows not filled out with the current text attributes.
2045 *
2046 * This function does not affect the scrollback rows at all. Rows shifted
2047 * off the bottom are lost.
2048 *
2049 * @param {integer} count The number of rows to scroll.
2050 */
2051hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08002052 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002053
rginda87b86462011-12-14 13:48:03 -08002054 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08002055 this.insertLines(opt_count);
2056
rginda87b86462011-12-14 13:48:03 -08002057 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002058};
2059
rginda87b86462011-12-14 13:48:03 -08002060
rginda8ba33642011-12-14 12:31:31 -08002061/**
2062 * Set the cursor position.
2063 *
2064 * The cursor row is relative to the scroll region if the terminal has
2065 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2066 *
2067 * @param {integer} row The new zero-based cursor row.
2068 * @param {integer} row The new zero-based cursor column.
2069 */
2070hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2071 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002072 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002073 } else {
rginda87b86462011-12-14 13:48:03 -08002074 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002075 }
rginda87b86462011-12-14 13:48:03 -08002076};
rginda8ba33642011-12-14 12:31:31 -08002077
Evan Jones2600d4f2016-12-06 09:29:36 -05002078/**
2079 * Move the cursor relative to its current position.
2080 *
2081 * @param {number} row
2082 * @param {number} column
2083 */
rginda87b86462011-12-14 13:48:03 -08002084hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2085 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002086 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2087 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002088 this.screen_.setCursorPosition(row, column);
2089};
2090
Evan Jones2600d4f2016-12-06 09:29:36 -05002091/**
2092 * Move the cursor to the specified position.
2093 *
2094 * @param {number} row
2095 * @param {number} column
2096 */
rginda87b86462011-12-14 13:48:03 -08002097hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002098 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2099 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002100 this.screen_.setCursorPosition(row, column);
2101};
2102
2103/**
2104 * Set the cursor column.
2105 *
2106 * @param {integer} column The new zero-based cursor column.
2107 */
2108hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002109 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002110};
2111
2112/**
2113 * Return the cursor column.
2114 *
2115 * @return {integer} The zero-based cursor column.
2116 */
2117hterm.Terminal.prototype.getCursorColumn = function() {
2118 return this.screen_.cursorPosition.column;
2119};
2120
2121/**
2122 * Set the cursor row.
2123 *
2124 * The cursor row is relative to the scroll region if the terminal has
2125 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2126 *
2127 * @param {integer} row The new cursor row.
2128 */
rginda87b86462011-12-14 13:48:03 -08002129hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2130 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002131};
2132
2133/**
2134 * Return the cursor row.
2135 *
2136 * @return {integer} The zero-based cursor row.
2137 */
2138hterm.Terminal.prototype.getCursorRow = function(row) {
2139 return this.screen_.cursorPosition.row;
2140};
2141
2142/**
2143 * Request that the ScrollPort redraw itself soon.
2144 *
2145 * The redraw will happen asynchronously, soon after the call stack winds down.
2146 * Multiple calls will be coalesced into a single redraw.
2147 */
2148hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002149 if (this.timeouts_.redraw)
2150 return;
rginda8ba33642011-12-14 12:31:31 -08002151
2152 var self = this;
rginda87b86462011-12-14 13:48:03 -08002153 this.timeouts_.redraw = setTimeout(function() {
2154 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002155 self.scrollPort_.redraw_();
2156 }, 0);
2157};
2158
2159/**
2160 * Request that the ScrollPort be scrolled to the bottom.
2161 *
2162 * The scroll will happen asynchronously, soon after the call stack winds down.
2163 * Multiple calls will be coalesced into a single scroll.
2164 *
2165 * This affects the scrollbar position of the ScrollPort, and has nothing to
2166 * do with the VT scroll commands.
2167 */
2168hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2169 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002170 return;
rginda8ba33642011-12-14 12:31:31 -08002171
2172 var self = this;
2173 this.timeouts_.scrollDown = setTimeout(function() {
2174 delete self.timeouts_.scrollDown;
2175 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2176 }, 10);
2177};
2178
2179/**
2180 * Move the cursor up a specified number of rows.
2181 *
2182 * @param {integer} count The number of rows to move the cursor.
2183 */
2184hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002185 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002186};
2187
2188/**
2189 * Move the cursor down a specified number of rows.
2190 *
2191 * @param {integer} count The number of rows to move the cursor.
2192 */
2193hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002194 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002195 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2196 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2197 this.screenSize.height - 1);
2198
rgindacbbd7482012-06-13 15:06:16 -07002199 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002200 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002201 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002202};
2203
2204/**
2205 * Move the cursor left a specified number of columns.
2206 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002207 * If reverse wraparound mode is enabled and the previous row wrapped into
2208 * the current row then we back up through the wraparound as well.
2209 *
rginda8ba33642011-12-14 12:31:31 -08002210 * @param {integer} count The number of columns to move the cursor.
2211 */
2212hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002213 count = count || 1;
2214
2215 if (count < 1)
2216 return;
2217
2218 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002219 if (this.options_.reverseWraparound) {
2220 if (this.screen_.cursorPosition.overflow) {
2221 // If this cursor is in the right margin, consume one count to get it
2222 // back to the last column. This only applies when we're in reverse
2223 // wraparound mode.
2224 count--;
2225 this.clearCursorOverflow();
2226
2227 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002228 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002229 }
2230
Robert Gindabfb32622014-07-17 13:20:27 -07002231 var newRow = this.screen_.cursorPosition.row;
2232 var newColumn = currentColumn - count;
2233 if (newColumn < 0) {
2234 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2235 if (newRow < 0) {
2236 // xterm also wraps from row 0 to the last row.
2237 newRow = this.screenSize.height + newRow % this.screenSize.height;
2238 }
2239 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2240 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002241
Robert Gindabfb32622014-07-17 13:20:27 -07002242 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2243
2244 } else {
2245 var newColumn = Math.max(currentColumn - count, 0);
2246 this.setCursorColumn(newColumn);
2247 }
rginda8ba33642011-12-14 12:31:31 -08002248};
2249
2250/**
2251 * Move the cursor right a specified number of columns.
2252 *
2253 * @param {integer} count The number of columns to move the cursor.
2254 */
2255hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002256 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002257
2258 if (count < 1)
2259 return;
2260
rgindacbbd7482012-06-13 15:06:16 -07002261 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002262 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002263 this.setCursorColumn(column);
2264};
2265
2266/**
2267 * Reverse the foreground and background colors of the terminal.
2268 *
2269 * This only affects text that was drawn with no attributes.
2270 *
2271 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2272 * been drawn with attributes that happen to coincide with the default
2273 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002274 *
2275 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002276 */
2277hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002278 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002279 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002280 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2281 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002282 } else {
rginda9f5222b2012-03-05 11:53:28 -08002283 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2284 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002285 }
2286};
2287
2288/**
rginda87b86462011-12-14 13:48:03 -08002289 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002290 *
2291 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002292 */
2293hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002294 this.cursorNode_.style.backgroundColor =
2295 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002296
2297 var self = this;
2298 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08002299 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08002300 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002301
Michael Kelly485ecd12014-06-09 11:41:56 -04002302 // bellSquelchTimeout_ affects both audio and notification bells.
2303 if (this.bellSquelchTimeout_)
2304 return;
2305
Robert Ginda92e18102013-03-14 13:56:37 -07002306 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002307 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002308 this.bellSequelchTimeout_ = setTimeout(function() {
2309 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002310 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002311 } else {
2312 delete this.bellSquelchTimeout_;
2313 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002314
2315 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
2316 var n = new Notification(
2317 lib.f.replaceVars(hterm.desktopNotificationTitle,
Robert Ginda348dc2b2014-06-24 14:42:23 -07002318 {'title': window.document.title || 'hterm'}));
Michael Kelly485ecd12014-06-09 11:41:56 -04002319 this.bellNotificationList_.push(n);
2320 // TODO: Should we try to raise the window here?
2321 n.onclick = function() { self.closeBellNotifications_(); };
2322 }
rginda87b86462011-12-14 13:48:03 -08002323};
2324
2325/**
rginda8ba33642011-12-14 12:31:31 -08002326 * Set the origin mode bit.
2327 *
2328 * If origin mode is on, certain VT cursor and scrolling commands measure their
2329 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2330 * to the top of the addressable screen.
2331 *
2332 * Defaults to off.
2333 *
2334 * @param {boolean} state True to set origin mode, false to unset.
2335 */
2336hterm.Terminal.prototype.setOriginMode = function(state) {
2337 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002338 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002339};
2340
2341/**
2342 * Set the insert mode bit.
2343 *
2344 * If insert mode is on, existing text beyond the cursor position will be
2345 * shifted right to make room for new text. Otherwise, new text overwrites
2346 * any existing text.
2347 *
2348 * Defaults to off.
2349 *
2350 * @param {boolean} state True to set insert mode, false to unset.
2351 */
2352hterm.Terminal.prototype.setInsertMode = function(state) {
2353 this.options_.insertMode = state;
2354};
2355
2356/**
rginda87b86462011-12-14 13:48:03 -08002357 * Set the auto carriage return bit.
2358 *
2359 * If auto carriage return is on then a formfeed character is interpreted
2360 * as a newline, otherwise it's the same as a linefeed. The difference boils
2361 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002362 *
2363 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002364 */
2365hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2366 this.options_.autoCarriageReturn = state;
2367};
2368
2369/**
rginda8ba33642011-12-14 12:31:31 -08002370 * Set the wraparound mode bit.
2371 *
2372 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2373 * to the start of the following row. Otherwise, the cursor is clamped to the
2374 * end of the screen and attempts to write past it are ignored.
2375 *
2376 * Defaults to on.
2377 *
2378 * @param {boolean} state True to set wraparound mode, false to unset.
2379 */
2380hterm.Terminal.prototype.setWraparound = function(state) {
2381 this.options_.wraparound = state;
2382};
2383
2384/**
2385 * Set the reverse-wraparound mode bit.
2386 *
2387 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2388 * to the end of the previous row. Otherwise, the cursor is clamped to column
2389 * 0.
2390 *
2391 * Defaults to off.
2392 *
2393 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2394 */
2395hterm.Terminal.prototype.setReverseWraparound = function(state) {
2396 this.options_.reverseWraparound = state;
2397};
2398
2399/**
2400 * Selects between the primary and alternate screens.
2401 *
2402 * If alternate mode is on, the alternate screen is active. Otherwise the
2403 * primary screen is active.
2404 *
2405 * Swapping screens has no effect on the scrollback buffer.
2406 *
2407 * Each screen maintains its own cursor position.
2408 *
2409 * Defaults to off.
2410 *
2411 * @param {boolean} state True to set alternate mode, false to unset.
2412 */
2413hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002414 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002415 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2416
rginda35c456b2012-02-09 17:29:05 -08002417 if (this.screen_.rowsArray.length &&
2418 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2419 // If the screen changed sizes while we were away, our rowIndexes may
2420 // be incorrect.
2421 var offset = this.scrollbackRows_.length;
2422 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002423 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002424 ary[i].rowIndex = offset + i;
2425 }
2426 }
rginda8ba33642011-12-14 12:31:31 -08002427
rginda35c456b2012-02-09 17:29:05 -08002428 this.realizeWidth_(this.screenSize.width);
2429 this.realizeHeight_(this.screenSize.height);
2430 this.scrollPort_.syncScrollHeight();
2431 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002432
rginda6d397402012-01-17 10:58:29 -08002433 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002434 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002435};
2436
2437/**
2438 * Set the cursor-blink mode bit.
2439 *
2440 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2441 * a visible cursor does not blink.
2442 *
2443 * You should make sure to turn blinking off if you're going to dispose of a
2444 * terminal, otherwise you'll leak a timeout.
2445 *
2446 * Defaults to on.
2447 *
2448 * @param {boolean} state True to set cursor-blink mode, false to unset.
2449 */
2450hterm.Terminal.prototype.setCursorBlink = function(state) {
2451 this.options_.cursorBlink = state;
2452
2453 if (!state && this.timeouts_.cursorBlink) {
2454 clearTimeout(this.timeouts_.cursorBlink);
2455 delete this.timeouts_.cursorBlink;
2456 }
2457
2458 if (this.options_.cursorVisible)
2459 this.setCursorVisible(true);
2460};
2461
2462/**
2463 * Set the cursor-visible mode bit.
2464 *
2465 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2466 *
2467 * Defaults to on.
2468 *
2469 * @param {boolean} state True to set cursor-visible mode, false to unset.
2470 */
2471hterm.Terminal.prototype.setCursorVisible = function(state) {
2472 this.options_.cursorVisible = state;
2473
2474 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002475 if (this.timeouts_.cursorBlink) {
2476 clearTimeout(this.timeouts_.cursorBlink);
2477 delete this.timeouts_.cursorBlink;
2478 }
rginda87b86462011-12-14 13:48:03 -08002479 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002480 return;
2481 }
2482
rginda87b86462011-12-14 13:48:03 -08002483 this.syncCursorPosition_();
2484
2485 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002486
2487 if (this.options_.cursorBlink) {
2488 if (this.timeouts_.cursorBlink)
2489 return;
2490
Robert Gindaea2183e2014-07-17 09:51:51 -07002491 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002492 } else {
2493 if (this.timeouts_.cursorBlink) {
2494 clearTimeout(this.timeouts_.cursorBlink);
2495 delete this.timeouts_.cursorBlink;
2496 }
2497 }
2498};
2499
2500/**
rginda87b86462011-12-14 13:48:03 -08002501 * Synchronizes the visible cursor and document selection with the current
2502 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002503 */
2504hterm.Terminal.prototype.syncCursorPosition_ = function() {
2505 var topRowIndex = this.scrollPort_.getTopRowIndex();
2506 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2507 var cursorRowIndex = this.scrollbackRows_.length +
2508 this.screen_.cursorPosition.row;
2509
2510 if (cursorRowIndex > bottomRowIndex) {
2511 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002512 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002513 return;
2514 }
2515
Robert Gindab837c052014-08-11 11:17:51 -07002516 if (this.options_.cursorVisible &&
2517 this.cursorNode_.style.display == 'none') {
2518 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2519 this.cursorNode_.style.display = '';
2520 }
2521
2522
rginda8ba33642011-12-14 12:31:31 -08002523 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002524 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2525 'px';
2526 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2527 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002528
2529 this.cursorNode_.setAttribute('title',
2530 '(' + this.screen_.cursorPosition.row +
2531 ', ' + this.screen_.cursorPosition.column +
2532 ')');
2533
2534 // Update the caret for a11y purposes.
2535 var selection = this.document_.getSelection();
2536 if (selection && selection.isCollapsed)
2537 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002538};
2539
Robert Gindafb1be6a2013-12-11 11:56:22 -08002540/**
2541 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2542 * and character cell dimensions.
2543 */
Robert Ginda830583c2013-08-07 13:20:46 -07002544hterm.Terminal.prototype.restyleCursor_ = function() {
2545 var shape = this.cursorShape_;
2546
2547 if (this.cursorNode_.getAttribute('focus') == 'false') {
2548 // Always show a block cursor when unfocused.
2549 shape = hterm.Terminal.cursorShape.BLOCK;
2550 }
2551
2552 var style = this.cursorNode_.style;
2553
Robert Gindafb1be6a2013-12-11 11:56:22 -08002554 style.width = this.scrollPort_.characterSize.width + 'px';
2555
Robert Ginda830583c2013-08-07 13:20:46 -07002556 switch (shape) {
2557 case hterm.Terminal.cursorShape.BEAM:
2558 style.height = this.scrollPort_.characterSize.height + 'px';
2559 style.backgroundColor = 'transparent';
2560 style.borderBottomStyle = null;
2561 style.borderLeftStyle = 'solid';
2562 break;
2563
2564 case hterm.Terminal.cursorShape.UNDERLINE:
2565 style.height = this.scrollPort_.characterSize.baseline + 'px';
2566 style.backgroundColor = 'transparent';
2567 style.borderBottomStyle = 'solid';
2568 // correct the size to put it exactly at the baseline
2569 style.borderLeftStyle = null;
2570 break;
2571
2572 default:
2573 style.height = this.scrollPort_.characterSize.height + 'px';
2574 style.backgroundColor = this.cursorColor_;
2575 style.borderBottomStyle = null;
2576 style.borderLeftStyle = null;
2577 break;
2578 }
2579};
2580
rginda8ba33642011-12-14 12:31:31 -08002581/**
2582 * Synchronizes the visible cursor with the current cursor coordinates.
2583 *
2584 * The sync will happen asynchronously, soon after the call stack winds down.
2585 * Multiple calls will be coalesced into a single sync.
2586 */
2587hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2588 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002589 return;
rginda8ba33642011-12-14 12:31:31 -08002590
2591 var self = this;
2592 this.timeouts_.syncCursor = setTimeout(function() {
2593 self.syncCursorPosition_();
2594 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002595 }, 0);
2596};
2597
rgindacc2996c2012-02-24 14:59:31 -08002598/**
rgindaf522ce02012-04-17 17:49:17 -07002599 * Show or hide the zoom warning.
2600 *
2601 * The zoom warning is a message warning the user that their browser zoom must
2602 * be set to 100% in order for hterm to function properly.
2603 *
2604 * @param {boolean} state True to show the message, false to hide it.
2605 */
2606hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2607 if (!this.zoomWarningNode_) {
2608 if (!state)
2609 return;
2610
2611 this.zoomWarningNode_ = this.document_.createElement('div');
2612 this.zoomWarningNode_.style.cssText = (
2613 'color: black;' +
2614 'background-color: #ff2222;' +
2615 'font-size: large;' +
2616 'border-radius: 8px;' +
2617 'opacity: 0.75;' +
2618 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2619 'top: 0.5em;' +
2620 'right: 1.2em;' +
2621 'position: absolute;' +
2622 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002623 '-webkit-user-select: none;' +
2624 '-moz-text-size-adjust: none;' +
2625 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002626
2627 this.zoomWarningNode_.addEventListener('click', function(e) {
2628 this.parentNode.removeChild(this);
2629 });
rgindaf522ce02012-04-17 17:49:17 -07002630 }
2631
Robert Gindab4839c22013-02-28 16:52:10 -08002632 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2633 hterm.zoomWarningMessage,
2634 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2635
rgindaf522ce02012-04-17 17:49:17 -07002636 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2637
2638 if (state) {
2639 if (!this.zoomWarningNode_.parentNode)
2640 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2641 } else if (this.zoomWarningNode_.parentNode) {
2642 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2643 }
2644};
2645
2646/**
rgindacc2996c2012-02-24 14:59:31 -08002647 * Show the terminal overlay for a given amount of time.
2648 *
2649 * The terminal overlay appears in inverse video in a large font, centered
2650 * over the terminal. You should probably keep the overlay message brief,
2651 * since it's in a large font and you probably aren't going to check the size
2652 * of the terminal first.
2653 *
2654 * @param {string} msg The text (not HTML) message to display in the overlay.
2655 * @param {number} opt_timeout The amount of time to wait before fading out
2656 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2657 * stay up forever (or until the next overlay).
2658 */
2659hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002660 if (!this.overlayNode_) {
2661 if (!this.div_)
2662 return;
2663
2664 this.overlayNode_ = this.document_.createElement('div');
2665 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002666 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002667 'font-size: xx-large;' +
2668 'opacity: 0.75;' +
2669 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2670 'position: absolute;' +
2671 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002672 '-webkit-transition: opacity 180ms ease-in;' +
2673 '-moz-user-select: none;' +
2674 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002675
2676 this.overlayNode_.addEventListener('mousedown', function(e) {
2677 e.preventDefault();
2678 e.stopPropagation();
2679 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002680 }
2681
rginda9f5222b2012-03-05 11:53:28 -08002682 this.overlayNode_.style.color = this.prefs_.get('background-color');
2683 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2684 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2685
rgindaf0090c92012-02-10 14:58:52 -08002686 this.overlayNode_.textContent = msg;
2687 this.overlayNode_.style.opacity = '0.75';
2688
2689 if (!this.overlayNode_.parentNode)
2690 this.div_.appendChild(this.overlayNode_);
2691
Robert Ginda97769282013-02-01 15:30:30 -08002692 var divSize = hterm.getClientSize(this.div_);
2693 var overlaySize = hterm.getClientSize(this.overlayNode_);
2694
Robert Ginda8a59f762014-07-23 11:29:55 -07002695 this.overlayNode_.style.top =
2696 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08002697 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07002698 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08002699
2700 var self = this;
2701
2702 if (this.overlayTimeout_)
2703 clearTimeout(this.overlayTimeout_);
2704
rgindacc2996c2012-02-24 14:59:31 -08002705 if (opt_timeout === null)
2706 return;
2707
rgindaf0090c92012-02-10 14:58:52 -08002708 this.overlayTimeout_ = setTimeout(function() {
2709 self.overlayNode_.style.opacity = '0';
Robert Ginda70926e42013-11-25 14:56:36 -08002710 self.overlayTimeout_ = setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002711 if (self.overlayNode_.parentNode)
2712 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002713 self.overlayTimeout_ = null;
2714 self.overlayNode_.style.opacity = '0.75';
2715 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002716 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002717};
2718
rginda4bba5e12012-06-20 16:15:30 -07002719/**
2720 * Paste from the system clipboard to the terminal.
2721 */
2722hterm.Terminal.prototype.paste = function() {
2723 hterm.pasteFromClipboard(this.document_);
2724};
2725
2726/**
2727 * Copy a string to the system clipboard.
2728 *
2729 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05002730 *
2731 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07002732 */
2733hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002734 if (this.prefs_.get('enable-clipboard-notice'))
2735 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2736
rgindaa09e7332012-08-17 12:49:51 -07002737 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002738 copySource.textContent = str;
2739 copySource.style.cssText = (
2740 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002741 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07002742 'position: absolute;' +
2743 'top: -99px');
2744
2745 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002746
rginda4bba5e12012-06-20 16:15:30 -07002747 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002748 var anchorNode = selection.anchorNode;
2749 var anchorOffset = selection.anchorOffset;
2750 var focusNode = selection.focusNode;
2751 var focusOffset = selection.focusOffset;
2752
rginda4bba5e12012-06-20 16:15:30 -07002753 selection.selectAllChildren(copySource);
2754
rgindaa09e7332012-08-17 12:49:51 -07002755 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002756
Rob Spies56953412014-04-28 14:09:47 -07002757 // IE doesn't support selection.extend. This means that the selection
2758 // won't return on IE.
Robert Ginda6f4f0aa2014-07-28 10:21:39 -07002759 if (selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07002760 selection.collapse(anchorNode, anchorOffset);
2761 selection.extend(focusNode, focusOffset);
2762 }
rgindafaa74742012-08-21 13:34:03 -07002763
rginda4bba5e12012-06-20 16:15:30 -07002764 copySource.parentNode.removeChild(copySource);
2765};
2766
Evan Jones2600d4f2016-12-06 09:29:36 -05002767/**
2768 * Returns the selected text, or null if no text is selected.
2769 *
2770 * @return {string|null}
2771 */
rgindaa09e7332012-08-17 12:49:51 -07002772hterm.Terminal.prototype.getSelectionText = function() {
2773 var selection = this.scrollPort_.selection;
2774 selection.sync();
2775
2776 if (selection.isCollapsed)
2777 return null;
2778
2779
2780 // Start offset measures from the beginning of the line.
2781 var startOffset = selection.startOffset;
2782 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002783
Robert Gindafdbb3f22012-09-06 20:23:06 -07002784 if (node.nodeName != 'X-ROW') {
2785 // If the selection doesn't start on an x-row node, then it must be
2786 // somewhere inside the x-row. Add any characters from previous siblings
2787 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002788
2789 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2790 // If node is the text node in a styled span, move up to the span node.
2791 node = node.parentNode;
2792 }
2793
Robert Gindafdbb3f22012-09-06 20:23:06 -07002794 while (node.previousSibling) {
2795 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002796 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002797 }
rgindaa09e7332012-08-17 12:49:51 -07002798 }
2799
2800 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002801 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
2802 selection.endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002803 var node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002804
Robert Gindafdbb3f22012-09-06 20:23:06 -07002805 if (node.nodeName != 'X-ROW') {
2806 // If the selection doesn't end on an x-row node, then it must be
2807 // somewhere inside the x-row. Add any characters from following siblings
2808 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002809
2810 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2811 // If node is the text node in a styled span, move up to the span node.
2812 node = node.parentNode;
2813 }
2814
Robert Gindafdbb3f22012-09-06 20:23:06 -07002815 while (node.nextSibling) {
2816 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002817 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002818 }
rgindaa09e7332012-08-17 12:49:51 -07002819 }
2820
2821 var rv = this.getRowsText(selection.startRow.rowIndex,
2822 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002823 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002824};
2825
rginda4bba5e12012-06-20 16:15:30 -07002826/**
2827 * Copy the current selection to the system clipboard, then clear it after a
2828 * short delay.
2829 */
2830hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002831 var text = this.getSelectionText();
2832 if (text != null)
2833 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002834};
2835
rgindaf0090c92012-02-10 14:58:52 -08002836hterm.Terminal.prototype.overlaySize = function() {
2837 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2838};
2839
rginda87b86462011-12-14 13:48:03 -08002840/**
2841 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2842 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07002843 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08002844 */
2845hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002846 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002847 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2848
Robert Ginda8cb7d902013-06-20 14:37:18 -07002849 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08002850};
2851
2852/**
rgindad5613292012-06-19 15:40:37 -07002853 * Add the terminalRow and terminalColumn properties to mouse events and
2854 * then forward on to onMouse().
2855 *
2856 * The terminalRow and terminalColumn properties contain the (row, column)
2857 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05002858 *
2859 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07002860 */
2861hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002862 if (e.processedByTerminalHandler_) {
2863 // We register our event handlers on the document, as well as the cursor
2864 // and the scroll blocker. Mouse events that occur on the cursor or
2865 // scroll blocker will also appear on the document, but we don't want to
2866 // process them twice.
2867 //
2868 // We can't just prevent bubbling because that has other side effects, so
2869 // we decorate the event object with this property instead.
2870 return;
2871 }
2872
Robert Ginda6aec7eb2015-06-16 10:31:30 -07002873 var reportMouseEvents = (!this.defeatMouseReports_ &&
2874 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
2875
rgindafaa74742012-08-21 13:34:03 -07002876 e.processedByTerminalHandler_ = true;
2877
Robert Gindaeda48db2014-07-17 09:25:30 -07002878 // One based row/column stored on the mouse event.
2879 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2880 this.scrollPort_.characterSize.height) + 1;
2881 e.terminalColumn = parseInt(e.clientX /
2882 this.scrollPort_.characterSize.width) + 1;
2883
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002884 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
2885 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07002886 return;
2887 }
2888
Robert Ginda6aec7eb2015-06-16 10:31:30 -07002889 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07002890 // If the cursor is visible and we're not sending mouse events to the
2891 // host app, then we want to hide the terminal cursor when the mouse
2892 // cursor is over top. This keeps the terminal cursor from interfering
2893 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07002894 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
2895 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
2896 this.cursorNode_.style.display = 'none';
2897 } else if (this.cursorNode_.style.display == 'none') {
2898 this.cursorNode_.style.display = '';
2899 }
2900 }
rgindad5613292012-06-19 15:40:37 -07002901
Robert Ginda928cf632014-03-05 15:07:41 -08002902 if (e.type == 'mousedown') {
Robert Ginda6aec7eb2015-06-16 10:31:30 -07002903 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08002904 // If VT mouse reporting is disabled, or has been defeated with
2905 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07002906 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08002907 this.setSelectionEnabled(true);
2908 } else {
2909 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07002910 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07002911 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08002912 this.setSelectionEnabled(false);
2913 e.preventDefault();
2914 }
2915 }
2916
Robert Ginda6aec7eb2015-06-16 10:31:30 -07002917 if (!reportMouseEvents) {
Robert Ginda15ed4902016-07-12 10:43:22 -07002918 if (e.type == 'dblclick' && this.copyOnSelect) {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002919 this.screen_.expandSelection(this.document_.getSelection());
Robert Ginda15ed4902016-07-12 10:43:22 -07002920 this.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07002921 }
2922
Robert Ginda928cf632014-03-05 15:07:41 -08002923 if (e.type == 'mousedown' && e.which == this.mousePasteButton)
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002924 this.paste();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002925
2926 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2927 !this.document_.getSelection().isCollapsed) {
Robert Ginda15ed4902016-07-12 10:43:22 -07002928 this.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002929 }
2930
2931 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
2932 this.scrollBlockerNode_.engaged) {
2933 // Disengage the scroll-blocker after one of these events.
2934 this.scrollBlockerNode_.engaged = false;
2935 this.scrollBlockerNode_.style.top = '-99px';
2936 }
2937
Robert Ginda928cf632014-03-05 15:07:41 -08002938 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002939 if (!this.scrollBlockerNode_.engaged) {
2940 if (e.type == 'mousedown') {
2941 // Move the scroll-blocker into place if we want to keep the scrollport
2942 // from scrolling.
2943 this.scrollBlockerNode_.engaged = true;
2944 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2945 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2946 } else if (e.type == 'mousemove') {
2947 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
2948 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07002949 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002950 e.preventDefault();
2951 }
2952 }
Robert Ginda928cf632014-03-05 15:07:41 -08002953
2954 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07002955 }
2956
Robert Ginda928cf632014-03-05 15:07:41 -08002957 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
2958 // Restore this on mouseup in case it was temporarily defeated with a
2959 // alt-mousedown. Only do this when the selection is empty so that
2960 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07002961 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08002962 }
rgindad5613292012-06-19 15:40:37 -07002963};
2964
2965/**
2966 * Clients should override this if they care to know about mouse events.
2967 *
2968 * The event parameter will be a normal DOM mouse click event with additional
2969 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05002970 *
2971 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07002972 */
2973hterm.Terminal.prototype.onMouse = function(e) { };
2974
2975/**
rginda8e92a692012-05-20 19:37:20 -07002976 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05002977 *
2978 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07002979 */
Rob Spies06533ba2014-04-24 11:20:37 -07002980hterm.Terminal.prototype.onFocusChange_ = function(focused) {
2981 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07002982 this.restyleCursor_();
Michael Kelly485ecd12014-06-09 11:41:56 -04002983 if (focused === true)
2984 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07002985};
2986
2987/**
rginda8ba33642011-12-14 12:31:31 -08002988 * React when the ScrollPort is scrolled.
2989 */
2990hterm.Terminal.prototype.onScroll_ = function() {
2991 this.scheduleSyncCursorPosition_();
2992};
2993
2994/**
rginda9846e2f2012-01-27 13:53:33 -08002995 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05002996 *
2997 * @param {Event} e The DOM paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08002998 */
2999hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07003000 var data = e.text.replace(/\n/mg, '\r');
Connor Hegartyf525ceb2014-09-03 13:36:03 -07003001 data = this.keyboard.encode(data);
Robert Gindaa063b202014-07-21 11:08:25 -07003002 if (this.options_.bracketedPaste)
3003 data = '\x1b[200~' + data + '\x1b[201~';
3004
3005 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003006};
3007
3008/**
rgindaa09e7332012-08-17 12:49:51 -07003009 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003010 *
3011 * @param {Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003012 */
3013hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003014 if (!this.useDefaultWindowCopy) {
3015 e.preventDefault();
3016 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3017 }
rgindaa09e7332012-08-17 12:49:51 -07003018};
3019
3020/**
rginda8ba33642011-12-14 12:31:31 -08003021 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003022 *
3023 * Note: This function should not directly contain code that alters the internal
3024 * state of the terminal. That kind of code belongs in realizeWidth or
3025 * realizeHeight, so that it can be executed synchronously in the case of a
3026 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003027 */
3028hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003029 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003030 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003031 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003032 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003033
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003034 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003035 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003036 // gets removed from the document or during the initial load, and we can't
3037 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003038 // This can also happen if called before the scrollPort calculates the
3039 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003040 return;
3041 }
3042
rgindaa8ba17d2012-08-15 14:41:10 -07003043 var isNewSize = (columnCount != this.screenSize.width ||
3044 rowCount != this.screenSize.height);
3045
3046 // We do this even if the size didn't change, just to be sure everything is
3047 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003048 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003049 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003050
3051 if (isNewSize)
3052 this.overlaySize();
3053
Robert Gindafb1be6a2013-12-11 11:56:22 -08003054 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003055 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08003056};
3057
3058/**
3059 * Service the cursor blink timeout.
3060 */
3061hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003062 if (!this.options_.cursorBlink) {
3063 delete this.timeouts_.cursorBlink;
3064 return;
3065 }
3066
Robert Ginda830583c2013-08-07 13:20:46 -07003067 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3068 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003069 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003070 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3071 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003072 } else {
rginda87b86462011-12-14 13:48:03 -08003073 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003074 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3075 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003076 }
3077};
David Reveman8f552492012-03-28 12:18:41 -04003078
3079/**
3080 * Set the scrollbar-visible mode bit.
3081 *
3082 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3083 * Otherwise it will not.
3084 *
3085 * Defaults to on.
3086 *
3087 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3088 */
3089hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3090 this.scrollPort_.setScrollbarVisible(state);
3091};
Michael Kelly485ecd12014-06-09 11:41:56 -04003092
3093/**
Rob Spies49039e52014-12-17 13:40:04 -08003094 * Set the scroll wheel move multiplier. This will affect how fast the page
3095 * scrolls on mousewheel events.
3096 *
3097 * Defaults to 1.
3098 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003099 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003100 */
3101hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3102 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3103};
3104
3105/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003106 * Close all web notifications created by terminal bells.
3107 */
3108hterm.Terminal.prototype.closeBellNotifications_ = function() {
3109 this.bellNotificationList_.forEach(function(n) {
3110 n.close();
3111 });
3112 this.bellNotificationList_.length = 0;
3113};