blob: c1066ae66c1ca26cb1b3ca00c5a5bb4b651e8fc6 [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',
Evan Jones5f9df812016-12-06 09:38:58 -0500149 function() { this.onTerminalReady(); }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800150};
151
152/**
Robert Ginda830583c2013-08-07 13:20:46 -0700153 * Possible cursor shapes.
154 */
155hterm.Terminal.cursorShape = {
156 BLOCK: 'BLOCK',
157 BEAM: 'BEAM',
158 UNDERLINE: 'UNDERLINE'
159};
160
161/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700162 * Clients should override this to be notified when the terminal is ready
163 * for use.
164 *
165 * The terminal initialization is asynchronous, and shouldn't be used before
166 * this method is called.
167 */
168hterm.Terminal.prototype.onTerminalReady = function() { };
169
170/**
rginda35c456b2012-02-09 17:29:05 -0800171 * Default tab with of 8 to match xterm.
172 */
173hterm.Terminal.prototype.tabWidth = 8;
174
175/**
rginda9f5222b2012-03-05 11:53:28 -0800176 * Select a preference profile.
177 *
178 * This will load the terminal preferences for the given profile name and
179 * associate subsequent preference changes with the new preference profile.
180 *
Evan Jones2600d4f2016-12-06 09:29:36 -0500181 * @param {string} profileId The name of the preference profile. Forward slash
rginda9f5222b2012-03-05 11:53:28 -0800182 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700183 * @param {function} opt_callback Optional callback to invoke when the profile
184 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800185 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700186hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
187 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800188
Robert Ginda57f03b42012-09-13 11:02:48 -0700189 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800190
Robert Ginda57f03b42012-09-13 11:02:48 -0700191 if (this.prefs_)
192 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800193
Robert Ginda57f03b42012-09-13 11:02:48 -0700194 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
195 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800196 'alt-gr-mode': function(v) {
197 if (v == null) {
198 if (navigator.language.toLowerCase() == 'en-us') {
199 v = 'none';
200 } else {
201 v = 'right-alt';
202 }
203 } else if (typeof v == 'string') {
204 v = v.toLowerCase();
205 } else {
206 v = 'none';
207 }
208
209 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v))
210 v = 'none';
211
212 terminal.keyboard.altGrMode = v;
213 },
214
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700215 'alt-backspace-is-meta-backspace': function(v) {
216 terminal.keyboard.altBackspaceIsMetaBackspace = v;
217 },
218
Robert Ginda57f03b42012-09-13 11:02:48 -0700219 'alt-is-meta': function(v) {
220 terminal.keyboard.altIsMeta = v;
221 },
222
223 'alt-sends-what': function(v) {
224 if (!/^(escape|8-bit|browser-key)$/.test(v))
225 v = 'escape';
226
227 terminal.keyboard.altSendsWhat = v;
228 },
229
230 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800231 var ary = v.match(/^lib-resource:(\S+)/);
232 if (ary) {
233 terminal.bellAudio_.setAttribute('src',
234 lib.resource.getDataUrl(ary[1]));
235 } else {
236 terminal.bellAudio_.setAttribute('src', v);
237 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700238 },
239
Michael Kelly485ecd12014-06-09 11:41:56 -0400240 'desktop-notification-bell': function(v) {
241 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700242 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400243 Notification.permission === 'granted';
244 if (!terminal.desktopNotificationBell_) {
245 // Note: We don't call Notification.requestPermission here because
246 // Chrome requires the call be the result of a user action (such as an
247 // onclick handler), and pref listeners are run asynchronously.
248 //
249 // A way of working around this would be to display a dialog in the
250 // terminal with a "click-to-request-permission" button.
251 console.warn('desktop-notification-bell is true but we do not have ' +
252 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400253 }
254 } else {
255 terminal.desktopNotificationBell_ = false;
256 }
257 },
258
Robert Ginda57f03b42012-09-13 11:02:48 -0700259 'background-color': function(v) {
260 terminal.setBackgroundColor(v);
261 },
262
263 'background-image': function(v) {
264 terminal.scrollPort_.setBackgroundImage(v);
265 },
266
267 'background-size': function(v) {
268 terminal.scrollPort_.setBackgroundSize(v);
269 },
270
271 'background-position': function(v) {
272 terminal.scrollPort_.setBackgroundPosition(v);
273 },
274
275 'backspace-sends-backspace': function(v) {
276 terminal.keyboard.backspaceSendsBackspace = v;
277 },
278
Brad Town18654b62015-03-12 00:27:45 -0700279 'character-map-overrides': function(v) {
280 if (!(v == null || v instanceof Object)) {
281 console.warn('Preference character-map-modifications is not an ' +
282 'object: ' + v);
283 return;
284 }
285
286 for (var code in v) {
287 var glmap = hterm.VT.CharacterMap.maps[code].glmap;
288 for (var received in v[code]) {
289 glmap[received] = v[code][received];
290 }
291 hterm.VT.CharacterMap.maps[code].reset(glmap);
292 }
293 },
294
Robert Ginda57f03b42012-09-13 11:02:48 -0700295 'cursor-blink': function(v) {
296 terminal.setCursorBlink(!!v);
297 },
298
Robert Gindaea2183e2014-07-17 09:51:51 -0700299 'cursor-blink-cycle': function(v) {
300 if (v instanceof Array &&
301 typeof v[0] == 'number' &&
302 typeof v[1] == 'number') {
303 terminal.cursorBlinkCycle_ = v;
304 } else if (typeof v == 'number') {
305 terminal.cursorBlinkCycle_ = [v, v];
306 } else {
307 // Fast blink indicates an error.
308 terminal.cursorBlinkCycle_ = [100, 100];
309 }
310 },
311
Robert Ginda57f03b42012-09-13 11:02:48 -0700312 'cursor-color': function(v) {
313 terminal.setCursorColor(v);
314 },
315
316 'color-palette-overrides': function(v) {
317 if (!(v == null || v instanceof Object || v instanceof Array)) {
318 console.warn('Preference color-palette-overrides is not an array or ' +
319 'object: ' + v);
320 return;
rginda9f5222b2012-03-05 11:53:28 -0800321 }
rginda9f5222b2012-03-05 11:53:28 -0800322
Robert Ginda57f03b42012-09-13 11:02:48 -0700323 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700324
Robert Ginda57f03b42012-09-13 11:02:48 -0700325 if (v) {
326 for (var key in v) {
327 var i = parseInt(key);
328 if (isNaN(i) || i < 0 || i > 255) {
329 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
330 continue;
331 }
332
333 if (v[i]) {
334 var rgb = lib.colors.normalizeCSS(v[i]);
335 if (rgb)
336 lib.colors.colorPalette[i] = rgb;
337 }
338 }
rginda30f20f62012-04-05 16:36:19 -0700339 }
rginda30f20f62012-04-05 16:36:19 -0700340
Evan Jones5f9df812016-12-06 09:38:58 -0500341 terminal.primaryScreen_.textAttributes.resetColorPalette();
Robert Ginda57f03b42012-09-13 11:02:48 -0700342 terminal.alternateScreen_.textAttributes.resetColorPalette();
343 },
rginda30f20f62012-04-05 16:36:19 -0700344
Robert Ginda57f03b42012-09-13 11:02:48 -0700345 'copy-on-select': function(v) {
346 terminal.copyOnSelect = !!v;
347 },
rginda9f5222b2012-03-05 11:53:28 -0800348
Rob Spies0bec09b2014-06-06 15:58:09 -0700349 'use-default-window-copy': function(v) {
350 terminal.useDefaultWindowCopy = !!v;
351 },
352
353 'clear-selection-after-copy': function(v) {
354 terminal.clearSelectionAfterCopy = !!v;
355 },
356
Robert Ginda7e5e9522014-03-14 12:23:58 -0700357 'ctrl-plus-minus-zero-zoom': function(v) {
358 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
359 },
360
Robert Gindafb5a3f92014-05-13 14:12:00 -0700361 'ctrl-c-copy': function(v) {
362 terminal.keyboard.ctrlCCopy = v;
363 },
364
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100365 'ctrl-v-paste': function(v) {
366 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700367 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100368 },
369
Masaya Suzuki273aa982014-05-31 07:25:55 +0900370 'east-asian-ambiguous-as-two-column': function(v) {
371 lib.wc.regardCjkAmbiguous = v;
372 },
373
Robert Ginda57f03b42012-09-13 11:02:48 -0700374 'enable-8-bit-control': function(v) {
375 terminal.vt.enable8BitControl = !!v;
376 },
rginda30f20f62012-04-05 16:36:19 -0700377
Robert Ginda57f03b42012-09-13 11:02:48 -0700378 'enable-bold': function(v) {
379 terminal.syncBoldSafeState();
380 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400381
Robert Ginda3e278d72014-03-25 13:18:51 -0700382 'enable-bold-as-bright': function(v) {
383 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
384 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
385 },
386
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
Evan Jones5f9df812016-12-06 09:38:58 -05001277 this.document_.body.oncontextmenu = function() { return false; };
rginda4bba5e12012-06-20 16:15:30 -07001278
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
rgindad5613292012-06-19 15:40:37 -07001349 this.scrollPort_.onScrollWheel = onMouse;
1350 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1351 ].forEach(function(event) {
1352 this.scrollBlockerNode_.addEventListener(event, onMouse);
1353 this.cursorNode_.addEventListener(event, onMouse);
1354 this.document_.addEventListener(event, onMouse);
1355 }.bind(this));
1356
1357 this.cursorNode_.addEventListener('mousedown', function() {
1358 setTimeout(this.focus.bind(this));
1359 }.bind(this));
1360
rginda8ba33642011-12-14 12:31:31 -08001361 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001362
rginda87b86462011-12-14 13:48:03 -08001363 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001364 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001365};
1366
rginda0918b652012-04-04 11:26:24 -07001367/**
1368 * Return the HTML document that contains the terminal DOM nodes.
Evan Jones2600d4f2016-12-06 09:29:36 -05001369 *
1370 * @return {HTMLDocument}
rginda0918b652012-04-04 11:26:24 -07001371 */
rginda87b86462011-12-14 13:48:03 -08001372hterm.Terminal.prototype.getDocument = function() {
1373 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001374};
1375
1376/**
rginda0918b652012-04-04 11:26:24 -07001377 * Focus the terminal.
1378 */
1379hterm.Terminal.prototype.focus = function() {
1380 this.scrollPort_.focus();
1381};
1382
1383/**
rginda8ba33642011-12-14 12:31:31 -08001384 * Return the HTML Element for a given row index.
1385 *
1386 * This is a method from the RowProvider interface. The ScrollPort uses
1387 * it to fetch rows on demand as they are scrolled into view.
1388 *
1389 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1390 * pairs to conserve memory.
1391 *
1392 * @param {integer} index The zero-based row index, measured relative to the
1393 * start of the scrollback buffer. On-screen rows will always have the
1394 * largest indicies.
1395 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1396 */
1397hterm.Terminal.prototype.getRowNode = function(index) {
1398 if (index < this.scrollbackRows_.length)
1399 return this.scrollbackRows_[index];
1400
1401 var screenIndex = index - this.scrollbackRows_.length;
1402 return this.screen_.rowsArray[screenIndex];
1403};
1404
1405/**
1406 * Return the text content for a given range of rows.
1407 *
1408 * This is a method from the RowProvider interface. The ScrollPort uses
1409 * it to fetch text content on demand when the user attempts to copy their
1410 * selection to the clipboard.
1411 *
1412 * @param {integer} start The zero-based row index to start from, measured
1413 * relative to the start of the scrollback buffer. On-screen rows will
1414 * always have the largest indicies.
1415 * @param {integer} end The zero-based row index to end on, measured
1416 * relative to the start of the scrollback buffer.
1417 * @return {string} A single string containing the text value of the range of
1418 * rows. Lines will be newline delimited, with no trailing newline.
1419 */
1420hterm.Terminal.prototype.getRowsText = function(start, end) {
1421 var ary = [];
1422 for (var i = start; i < end; i++) {
1423 var node = this.getRowNode(i);
1424 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001425 if (i < end - 1 && !node.getAttribute('line-overflow'))
1426 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001427 }
1428
rgindaa09e7332012-08-17 12:49:51 -07001429 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001430};
1431
1432/**
1433 * Return the text content for a given row.
1434 *
1435 * This is a method from the RowProvider interface. The ScrollPort uses
1436 * it to fetch text content on demand when the user attempts to copy their
1437 * selection to the clipboard.
1438 *
1439 * @param {integer} index The zero-based row index to return, measured
1440 * relative to the start of the scrollback buffer. On-screen rows will
1441 * always have the largest indicies.
1442 * @return {string} A string containing the text value of the selected row.
1443 */
1444hterm.Terminal.prototype.getRowText = function(index) {
1445 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001446 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001447};
1448
1449/**
1450 * Return the total number of rows in the addressable screen and in the
1451 * scrollback buffer of this terminal.
1452 *
1453 * This is a method from the RowProvider interface. The ScrollPort uses
1454 * it to compute the size of the scrollbar.
1455 *
1456 * @return {integer} The number of rows in this terminal.
1457 */
1458hterm.Terminal.prototype.getRowCount = function() {
1459 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1460};
1461
1462/**
1463 * Create DOM nodes for new rows and append them to the end of the terminal.
1464 *
1465 * This is the only correct way to add a new DOM node for a row. Notice that
1466 * the new row is appended to the bottom of the list of rows, and does not
1467 * require renumbering (of the rowIndex property) of previous rows.
1468 *
1469 * If you think you want a new blank row somewhere in the middle of the
1470 * terminal, look into moveRows_().
1471 *
1472 * This method does not pay attention to vtScrollTop/Bottom, since you should
1473 * be using moveRows() in cases where they would matter.
1474 *
1475 * The cursor will be positioned at column 0 of the first inserted line.
Evan Jones2600d4f2016-12-06 09:29:36 -05001476 *
1477 * @param {number} count The number of rows to created.
rginda8ba33642011-12-14 12:31:31 -08001478 */
1479hterm.Terminal.prototype.appendRows_ = function(count) {
1480 var cursorRow = this.screen_.rowsArray.length;
1481 var offset = this.scrollbackRows_.length + cursorRow;
1482 for (var i = 0; i < count; i++) {
1483 var row = this.document_.createElement('x-row');
1484 row.appendChild(this.document_.createTextNode(''));
1485 row.rowIndex = offset + i;
1486 this.screen_.pushRow(row);
1487 }
1488
1489 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1490 if (extraRows > 0) {
1491 var ary = this.screen_.shiftRows(extraRows);
1492 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001493 if (this.scrollPort_.isScrolledEnd)
1494 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001495 }
1496
1497 if (cursorRow >= this.screen_.rowsArray.length)
1498 cursorRow = this.screen_.rowsArray.length - 1;
1499
rginda87b86462011-12-14 13:48:03 -08001500 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001501};
1502
1503/**
1504 * Relocate rows from one part of the addressable screen to another.
1505 *
1506 * This is used to recycle rows during VT scrolls (those which are driven
1507 * by VT commands, rather than by the user manipulating the scrollbar.)
1508 *
1509 * In this case, the blank lines scrolled into the scroll region are made of
1510 * the nodes we scrolled off. These have their rowIndex properties carefully
1511 * renumbered so as not to confuse the ScrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05001512 *
1513 * @param {number} fromIndex The start index.
1514 * @param {number} count The number of rows to move.
1515 * @param {number} toIndex The destination index.
rginda8ba33642011-12-14 12:31:31 -08001516 */
1517hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1518 var ary = this.screen_.removeRows(fromIndex, count);
1519 this.screen_.insertRows(toIndex, ary);
1520
1521 var start, end;
1522 if (fromIndex < toIndex) {
1523 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001524 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001525 } else {
1526 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001527 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001528 }
1529
1530 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001531 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001532};
1533
1534/**
1535 * Renumber the rowIndex property of the given range of rows.
1536 *
1537 * The start and end indicies are relative to the screen, not the scrollback.
1538 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001539 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001540 * no need to renumber scrollback rows.
Evan Jones2600d4f2016-12-06 09:29:36 -05001541 *
1542 * @param {number} start The start index.
1543 * @param {number} end The end index.
1544 * @param {hterm.Screen} opt_screen The screen to renumber.
rginda8ba33642011-12-14 12:31:31 -08001545 */
Robert Ginda40932892012-12-10 17:26:40 -08001546hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1547 var screen = opt_screen || this.screen_;
1548
rginda8ba33642011-12-14 12:31:31 -08001549 var offset = this.scrollbackRows_.length;
1550 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001551 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001552 }
1553};
1554
1555/**
1556 * Print a string to the terminal.
1557 *
1558 * This respects the current insert and wraparound modes. It will add new lines
1559 * to the end of the terminal, scrolling off the top into the scrollback buffer
1560 * if necessary.
1561 *
1562 * The string is *not* parsed for escape codes. Use the interpret() method if
1563 * that's what you're after.
1564 *
1565 * @param{string} str The string to print.
1566 */
1567hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001568 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001569
Ricky Liang48f05cb2013-12-31 23:35:29 +08001570 var strWidth = lib.wc.strWidth(str);
1571
1572 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001573 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1574 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001575 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001576 }
rgindaa19afe22012-01-25 15:40:22 -08001577
Ricky Liang48f05cb2013-12-31 23:35:29 +08001578 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001579 var didOverflow = false;
1580 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001581
rgindaa9abdd82012-08-06 18:05:09 -07001582 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1583 didOverflow = true;
1584 count = this.screenSize.width - this.screen_.cursorPosition.column;
1585 }
rgindaa19afe22012-01-25 15:40:22 -08001586
rgindaa9abdd82012-08-06 18:05:09 -07001587 if (didOverflow && !this.options_.wraparound) {
1588 // If the string overflowed the line but wraparound is off, then the
1589 // last printed character should be the last of the string.
1590 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001591 substr = lib.wc.substr(str, startOffset, count - 1) +
1592 lib.wc.substr(str, strWidth - 1);
1593 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001594 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001595 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001596 }
rgindaa19afe22012-01-25 15:40:22 -08001597
Ricky Liang48f05cb2013-12-31 23:35:29 +08001598 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1599 for (var i = 0; i < tokens.length; i++) {
1600 if (tokens[i].wcNode)
1601 this.screen_.textAttributes.wcNode = true;
1602
1603 if (this.options_.insertMode) {
1604 this.screen_.insertString(tokens[i].str);
1605 } else {
1606 this.screen_.overwriteString(tokens[i].str);
1607 }
1608 this.screen_.textAttributes.wcNode = false;
rgindaa9abdd82012-08-06 18:05:09 -07001609 }
1610
1611 this.screen_.maybeClipCurrentRow();
1612 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001613 }
rginda8ba33642011-12-14 12:31:31 -08001614
1615 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001616
rginda9f5222b2012-03-05 11:53:28 -08001617 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001618 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001619};
1620
1621/**
rginda87b86462011-12-14 13:48:03 -08001622 * Set the VT scroll region.
1623 *
rginda87b86462011-12-14 13:48:03 -08001624 * This also resets the cursor position to the absolute (0, 0) position, since
1625 * that's what xterm appears to do.
1626 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001627 * Setting the scroll region to the full height of the terminal will clear
1628 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1629 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1630 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1631 * continue to work as most users would expect.
1632 *
rginda87b86462011-12-14 13:48:03 -08001633 * @param {integer} scrollTop The zero-based top of the scroll region.
1634 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1635 * inclusive.
1636 */
1637hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001638 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001639 this.vtScrollTop_ = null;
1640 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001641 } else {
1642 this.vtScrollTop_ = scrollTop;
1643 this.vtScrollBottom_ = scrollBottom;
1644 }
rginda87b86462011-12-14 13:48:03 -08001645};
1646
1647/**
rginda8ba33642011-12-14 12:31:31 -08001648 * Return the top row index according to the VT.
1649 *
1650 * This will return 0 unless the terminal has been told to restrict scrolling
1651 * to some lower row. It is used for some VT cursor positioning and scrolling
1652 * commands.
1653 *
1654 * @return {integer} The topmost row in the terminal's scroll region.
1655 */
1656hterm.Terminal.prototype.getVTScrollTop = function() {
1657 if (this.vtScrollTop_ != null)
1658 return this.vtScrollTop_;
1659
1660 return 0;
rginda87b86462011-12-14 13:48:03 -08001661};
rginda8ba33642011-12-14 12:31:31 -08001662
1663/**
1664 * Return the bottom row index according to the VT.
1665 *
1666 * This will return the height of the terminal unless the it has been told to
1667 * restrict scrolling to some higher row. It is used for some VT cursor
1668 * positioning and scrolling commands.
1669 *
1670 * @return {integer} The bottommost row in the terminal's scroll region.
1671 */
1672hterm.Terminal.prototype.getVTScrollBottom = function() {
1673 if (this.vtScrollBottom_ != null)
1674 return this.vtScrollBottom_;
1675
rginda87b86462011-12-14 13:48:03 -08001676 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001677}
1678
1679/**
1680 * Process a '\n' character.
1681 *
1682 * If the cursor is on the final row of the terminal this will append a new
1683 * blank row to the screen and scroll the topmost row into the scrollback
1684 * buffer.
1685 *
1686 * Otherwise, this moves the cursor to column zero of the next row.
1687 */
1688hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001689 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1690 this.screen_.rowsArray.length - 1);
1691
1692 if (this.vtScrollBottom_ != null) {
1693 // A VT Scroll region is active, we never append new rows.
1694 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1695 // We're at the end of the VT Scroll Region, perform a VT scroll.
1696 this.vtScrollUp(1);
1697 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1698 } else if (cursorAtEndOfScreen) {
1699 // We're at the end of the screen, the only thing to do is put the
1700 // cursor to column 0.
1701 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1702 } else {
1703 // Anywhere else, advance the cursor row, and reset the column.
1704 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1705 }
1706 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001707 // We're at the end of the screen. Append a new row to the terminal,
1708 // shifting the top row into the scrollback.
1709 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001710 } else {
rginda87b86462011-12-14 13:48:03 -08001711 // Anywhere else in the screen just moves the cursor.
1712 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001713 }
1714};
1715
1716/**
1717 * Like newLine(), except maintain the cursor column.
1718 */
1719hterm.Terminal.prototype.lineFeed = function() {
1720 var column = this.screen_.cursorPosition.column;
1721 this.newLine();
1722 this.setCursorColumn(column);
1723};
1724
1725/**
rginda87b86462011-12-14 13:48:03 -08001726 * If autoCarriageReturn is set then newLine(), else lineFeed().
1727 */
1728hterm.Terminal.prototype.formFeed = function() {
1729 if (this.options_.autoCarriageReturn) {
1730 this.newLine();
1731 } else {
1732 this.lineFeed();
1733 }
1734};
1735
1736/**
1737 * Move the cursor up one row, possibly inserting a blank line.
1738 *
1739 * The cursor column is not changed.
1740 */
1741hterm.Terminal.prototype.reverseLineFeed = function() {
1742 var scrollTop = this.getVTScrollTop();
1743 var currentRow = this.screen_.cursorPosition.row;
1744
1745 if (currentRow == scrollTop) {
1746 this.insertLines(1);
1747 } else {
1748 this.setAbsoluteCursorRow(currentRow - 1);
1749 }
1750};
1751
1752/**
rginda8ba33642011-12-14 12:31:31 -08001753 * Replace all characters to the left of the current cursor with the space
1754 * character.
1755 *
1756 * TODO(rginda): This should probably *remove* the characters (not just replace
1757 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001758 * position.
rginda8ba33642011-12-14 12:31:31 -08001759 */
1760hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001761 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001762 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001763 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001764 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001765};
1766
1767/**
David Benjamin684a9b72012-05-01 17:19:58 -04001768 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001769 *
1770 * The cursor position is unchanged.
1771 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001772 * If the current background color is not the default background color this
1773 * will insert spaces rather than delete. This is unfortunate because the
1774 * trailing space will affect text selection, but it's difficult to come up
1775 * with a way to style empty space that wouldn't trip up the hterm.Screen
1776 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001777 *
1778 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1779 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1780 * crbug.com/232390 for details.
Evan Jones2600d4f2016-12-06 09:29:36 -05001781 *
1782 * @param {number} opt_count The number of characters to erase.
rginda8ba33642011-12-14 12:31:31 -08001783 */
1784hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001785 if (this.screen_.cursorPosition.overflow)
1786 return;
1787
Robert Ginda7fd57082012-09-25 14:41:47 -07001788 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1789 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001790
1791 if (this.screen_.textAttributes.background ===
1792 this.screen_.textAttributes.DEFAULT_COLOR) {
1793 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08001794 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07001795 this.screen_.cursorPosition.column + count) {
1796 this.screen_.deleteChars(count);
1797 this.clearCursorOverflow();
1798 return;
1799 }
1800 }
1801
rginda87b86462011-12-14 13:48:03 -08001802 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001803 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001804 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001805 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001806};
1807
1808/**
1809 * Erase the current line.
1810 *
1811 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001812 */
1813hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001814 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001815 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001816 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001817 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001818};
1819
1820/**
David Benjamina08d78f2012-05-05 00:28:49 -04001821 * Erase all characters from the start of the screen to the current cursor
1822 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001823 *
1824 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001825 */
1826hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001827 var cursor = this.saveCursor();
1828
1829 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001830
David Benjamina08d78f2012-05-05 00:28:49 -04001831 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001832 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001833 this.screen_.clearCursorRow();
1834 }
1835
rginda87b86462011-12-14 13:48:03 -08001836 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001837 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001838};
1839
1840/**
1841 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001842 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001843 *
1844 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001845 */
1846hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001847 var cursor = this.saveCursor();
1848
1849 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001850
David Benjamina08d78f2012-05-05 00:28:49 -04001851 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001852 for (var i = cursor.row + 1; i <= bottom; i++) {
1853 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001854 this.screen_.clearCursorRow();
1855 }
1856
rginda87b86462011-12-14 13:48:03 -08001857 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001858 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001859};
1860
1861/**
1862 * Fill the terminal with a given character.
1863 *
1864 * This methods does not respect the VT scroll region.
1865 *
1866 * @param {string} ch The character to use for the fill.
1867 */
1868hterm.Terminal.prototype.fill = function(ch) {
1869 var cursor = this.saveCursor();
1870
1871 this.setAbsoluteCursorPosition(0, 0);
1872 for (var row = 0; row < this.screenSize.height; row++) {
1873 for (var col = 0; col < this.screenSize.width; col++) {
1874 this.setAbsoluteCursorPosition(row, col);
1875 this.screen_.overwriteString(ch);
1876 }
1877 }
1878
1879 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001880};
1881
1882/**
rginda9ea433c2012-03-16 11:57:00 -07001883 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001884 *
rginda9ea433c2012-03-16 11:57:00 -07001885 * This does not respect the scroll region.
1886 *
1887 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1888 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001889 */
rginda9ea433c2012-03-16 11:57:00 -07001890hterm.Terminal.prototype.clearHome = function(opt_screen) {
1891 var screen = opt_screen || this.screen_;
1892 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001893
rginda11057d52012-04-25 12:29:56 -07001894 if (bottom == 0) {
1895 // Empty screen, nothing to do.
1896 return;
1897 }
1898
rgindae4d29232012-01-19 10:47:13 -08001899 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001900 screen.setCursorPosition(i, 0);
1901 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001902 }
1903
rginda9ea433c2012-03-16 11:57:00 -07001904 screen.setCursorPosition(0, 0);
1905};
1906
1907/**
1908 * Erase the entire display without changing the cursor position.
1909 *
1910 * The cursor position is unchanged. This does not respect the scroll
1911 * region.
1912 *
1913 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1914 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001915 */
1916hterm.Terminal.prototype.clear = function(opt_screen) {
1917 var screen = opt_screen || this.screen_;
1918 var cursor = screen.cursorPosition.clone();
1919 this.clearHome(screen);
1920 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001921};
1922
1923/**
1924 * VT command to insert lines at the current cursor row.
1925 *
1926 * This respects the current scroll region. Rows pushed off the bottom are
1927 * lost (they won't show up in the scrollback buffer).
1928 *
rginda8ba33642011-12-14 12:31:31 -08001929 * @param {integer} count The number of lines to insert.
1930 */
1931hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07001932 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08001933
1934 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07001935 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08001936
Robert Ginda579186b2012-09-26 11:40:04 -07001937 // The moveCount is the number of rows we need to relocate to make room for
1938 // the new row(s). The count is the distance to move them.
1939 var moveCount = bottom - cursorRow - count + 1;
1940 if (moveCount)
1941 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08001942
Robert Ginda579186b2012-09-26 11:40:04 -07001943 for (var i = count - 1; i >= 0; i--) {
1944 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001945 this.screen_.clearCursorRow();
1946 }
rginda8ba33642011-12-14 12:31:31 -08001947};
1948
1949/**
1950 * VT command to delete lines at the current cursor row.
1951 *
1952 * New rows are added to the bottom of scroll region to take their place. New
1953 * rows are strictly there to take up space and have no content or style.
Evan Jones2600d4f2016-12-06 09:29:36 -05001954 *
1955 * @param {number} count The number of lines to delete.
rginda8ba33642011-12-14 12:31:31 -08001956 */
1957hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001958 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001959
rginda87b86462011-12-14 13:48:03 -08001960 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001961 var bottom = this.getVTScrollBottom();
1962
rginda87b86462011-12-14 13:48:03 -08001963 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001964 count = Math.min(count, maxCount);
1965
rginda87b86462011-12-14 13:48:03 -08001966 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001967 if (count != maxCount)
1968 this.moveRows_(top, count, moveStart);
1969
1970 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001971 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001972 this.screen_.clearCursorRow();
1973 }
1974
rginda87b86462011-12-14 13:48:03 -08001975 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001976 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001977};
1978
1979/**
1980 * Inserts the given number of spaces at the current cursor position.
1981 *
rginda87b86462011-12-14 13:48:03 -08001982 * The cursor position is not changed.
Evan Jones2600d4f2016-12-06 09:29:36 -05001983 *
1984 * @param {number} count The number of spaces to insert.
rginda8ba33642011-12-14 12:31:31 -08001985 */
1986hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001987 var cursor = this.saveCursor();
1988
rgindacbbd7482012-06-13 15:06:16 -07001989 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001990 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001991 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001992
1993 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001994 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001995};
1996
1997/**
1998 * Forward-delete the specified number of characters starting at the cursor
1999 * position.
2000 *
2001 * @param {integer} count The number of characters to delete.
2002 */
2003hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07002004 var deleted = this.screen_.deleteChars(count);
2005 if (deleted && !this.screen_.textAttributes.isDefault()) {
2006 var cursor = this.saveCursor();
2007 this.setCursorColumn(this.screenSize.width - deleted);
2008 this.screen_.insertString(lib.f.getWhitespace(deleted));
2009 this.restoreCursor(cursor);
2010 }
2011
David Benjamin54e8bf62012-06-01 22:31:40 -04002012 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08002013};
2014
2015/**
2016 * Shift rows in the scroll region upwards by a given number of lines.
2017 *
2018 * New rows are inserted at the bottom of the scroll region to fill the
2019 * vacated rows. The new rows not filled out with the current text attributes.
2020 *
2021 * This function does not affect the scrollback rows at all. Rows shifted
2022 * off the top are lost.
2023 *
rginda87b86462011-12-14 13:48:03 -08002024 * The cursor position is not altered.
2025 *
rginda8ba33642011-12-14 12:31:31 -08002026 * @param {integer} count The number of rows to scroll.
2027 */
2028hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08002029 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002030
rginda87b86462011-12-14 13:48:03 -08002031 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08002032 this.deleteLines(count);
2033
rginda87b86462011-12-14 13:48:03 -08002034 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002035};
2036
2037/**
2038 * Shift rows below the cursor down by a given number of lines.
2039 *
2040 * This function respects the current scroll region.
2041 *
2042 * New rows are inserted at the top of the scroll region to fill the
2043 * vacated rows. The new rows not filled out with the current text attributes.
2044 *
2045 * This function does not affect the scrollback rows at all. Rows shifted
2046 * off the bottom are lost.
2047 *
2048 * @param {integer} count The number of rows to scroll.
2049 */
2050hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08002051 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002052
rginda87b86462011-12-14 13:48:03 -08002053 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08002054 this.insertLines(opt_count);
2055
rginda87b86462011-12-14 13:48:03 -08002056 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08002057};
2058
rginda87b86462011-12-14 13:48:03 -08002059
rginda8ba33642011-12-14 12:31:31 -08002060/**
2061 * Set the cursor position.
2062 *
2063 * The cursor row is relative to the scroll region if the terminal has
2064 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2065 *
2066 * @param {integer} row The new zero-based cursor row.
2067 * @param {integer} row The new zero-based cursor column.
2068 */
2069hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2070 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002071 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002072 } else {
rginda87b86462011-12-14 13:48:03 -08002073 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002074 }
rginda87b86462011-12-14 13:48:03 -08002075};
rginda8ba33642011-12-14 12:31:31 -08002076
Evan Jones2600d4f2016-12-06 09:29:36 -05002077/**
2078 * Move the cursor relative to its current position.
2079 *
2080 * @param {number} row
2081 * @param {number} column
2082 */
rginda87b86462011-12-14 13:48:03 -08002083hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2084 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002085 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2086 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002087 this.screen_.setCursorPosition(row, column);
2088};
2089
Evan Jones2600d4f2016-12-06 09:29:36 -05002090/**
2091 * Move the cursor to the specified position.
2092 *
2093 * @param {number} row
2094 * @param {number} column
2095 */
rginda87b86462011-12-14 13:48:03 -08002096hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002097 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2098 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002099 this.screen_.setCursorPosition(row, column);
2100};
2101
2102/**
2103 * Set the cursor column.
2104 *
2105 * @param {integer} column The new zero-based cursor column.
2106 */
2107hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002108 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002109};
2110
2111/**
2112 * Return the cursor column.
2113 *
2114 * @return {integer} The zero-based cursor column.
2115 */
2116hterm.Terminal.prototype.getCursorColumn = function() {
2117 return this.screen_.cursorPosition.column;
2118};
2119
2120/**
2121 * Set the cursor row.
2122 *
2123 * The cursor row is relative to the scroll region if the terminal has
2124 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2125 *
2126 * @param {integer} row The new cursor row.
2127 */
rginda87b86462011-12-14 13:48:03 -08002128hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2129 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002130};
2131
2132/**
2133 * Return the cursor row.
2134 *
2135 * @return {integer} The zero-based cursor row.
2136 */
2137hterm.Terminal.prototype.getCursorRow = function(row) {
2138 return this.screen_.cursorPosition.row;
2139};
2140
2141/**
2142 * Request that the ScrollPort redraw itself soon.
2143 *
2144 * The redraw will happen asynchronously, soon after the call stack winds down.
2145 * Multiple calls will be coalesced into a single redraw.
2146 */
2147hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002148 if (this.timeouts_.redraw)
2149 return;
rginda8ba33642011-12-14 12:31:31 -08002150
2151 var self = this;
rginda87b86462011-12-14 13:48:03 -08002152 this.timeouts_.redraw = setTimeout(function() {
2153 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002154 self.scrollPort_.redraw_();
2155 }, 0);
2156};
2157
2158/**
2159 * Request that the ScrollPort be scrolled to the bottom.
2160 *
2161 * The scroll will happen asynchronously, soon after the call stack winds down.
2162 * Multiple calls will be coalesced into a single scroll.
2163 *
2164 * This affects the scrollbar position of the ScrollPort, and has nothing to
2165 * do with the VT scroll commands.
2166 */
2167hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2168 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002169 return;
rginda8ba33642011-12-14 12:31:31 -08002170
2171 var self = this;
2172 this.timeouts_.scrollDown = setTimeout(function() {
2173 delete self.timeouts_.scrollDown;
2174 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2175 }, 10);
2176};
2177
2178/**
2179 * Move the cursor up a specified number of rows.
2180 *
2181 * @param {integer} count The number of rows to move the cursor.
2182 */
2183hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002184 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002185};
2186
2187/**
2188 * Move the cursor down a specified number of rows.
2189 *
2190 * @param {integer} count The number of rows to move the cursor.
2191 */
2192hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002193 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002194 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2195 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2196 this.screenSize.height - 1);
2197
rgindacbbd7482012-06-13 15:06:16 -07002198 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002199 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002200 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002201};
2202
2203/**
2204 * Move the cursor left a specified number of columns.
2205 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002206 * If reverse wraparound mode is enabled and the previous row wrapped into
2207 * the current row then we back up through the wraparound as well.
2208 *
rginda8ba33642011-12-14 12:31:31 -08002209 * @param {integer} count The number of columns to move the cursor.
2210 */
2211hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002212 count = count || 1;
2213
2214 if (count < 1)
2215 return;
2216
2217 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002218 if (this.options_.reverseWraparound) {
2219 if (this.screen_.cursorPosition.overflow) {
2220 // If this cursor is in the right margin, consume one count to get it
2221 // back to the last column. This only applies when we're in reverse
2222 // wraparound mode.
2223 count--;
2224 this.clearCursorOverflow();
2225
2226 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002227 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002228 }
2229
Robert Gindabfb32622014-07-17 13:20:27 -07002230 var newRow = this.screen_.cursorPosition.row;
2231 var newColumn = currentColumn - count;
2232 if (newColumn < 0) {
2233 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2234 if (newRow < 0) {
2235 // xterm also wraps from row 0 to the last row.
2236 newRow = this.screenSize.height + newRow % this.screenSize.height;
2237 }
2238 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2239 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002240
Robert Gindabfb32622014-07-17 13:20:27 -07002241 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2242
2243 } else {
2244 var newColumn = Math.max(currentColumn - count, 0);
2245 this.setCursorColumn(newColumn);
2246 }
rginda8ba33642011-12-14 12:31:31 -08002247};
2248
2249/**
2250 * Move the cursor right a specified number of columns.
2251 *
2252 * @param {integer} count The number of columns to move the cursor.
2253 */
2254hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002255 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002256
2257 if (count < 1)
2258 return;
2259
rgindacbbd7482012-06-13 15:06:16 -07002260 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002261 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002262 this.setCursorColumn(column);
2263};
2264
2265/**
2266 * Reverse the foreground and background colors of the terminal.
2267 *
2268 * This only affects text that was drawn with no attributes.
2269 *
2270 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2271 * been drawn with attributes that happen to coincide with the default
2272 * 'no-attribute' colors. My guess is probably not.
Evan Jones2600d4f2016-12-06 09:29:36 -05002273 *
2274 * @param {boolean} state The state to set.
rginda8ba33642011-12-14 12:31:31 -08002275 */
2276hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002277 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002278 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002279 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2280 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002281 } else {
rginda9f5222b2012-03-05 11:53:28 -08002282 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2283 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002284 }
2285};
2286
2287/**
rginda87b86462011-12-14 13:48:03 -08002288 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002289 *
2290 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002291 */
2292hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002293 this.cursorNode_.style.backgroundColor =
2294 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002295
2296 var self = this;
2297 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08002298 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08002299 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002300
Michael Kelly485ecd12014-06-09 11:41:56 -04002301 // bellSquelchTimeout_ affects both audio and notification bells.
2302 if (this.bellSquelchTimeout_)
2303 return;
2304
Robert Ginda92e18102013-03-14 13:56:37 -07002305 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002306 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002307 this.bellSequelchTimeout_ = setTimeout(function() {
2308 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002309 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002310 } else {
2311 delete this.bellSquelchTimeout_;
2312 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002313
2314 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
2315 var n = new Notification(
2316 lib.f.replaceVars(hterm.desktopNotificationTitle,
Robert Ginda348dc2b2014-06-24 14:42:23 -07002317 {'title': window.document.title || 'hterm'}));
Michael Kelly485ecd12014-06-09 11:41:56 -04002318 this.bellNotificationList_.push(n);
2319 // TODO: Should we try to raise the window here?
2320 n.onclick = function() { self.closeBellNotifications_(); };
2321 }
rginda87b86462011-12-14 13:48:03 -08002322};
2323
2324/**
rginda8ba33642011-12-14 12:31:31 -08002325 * Set the origin mode bit.
2326 *
2327 * If origin mode is on, certain VT cursor and scrolling commands measure their
2328 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2329 * to the top of the addressable screen.
2330 *
2331 * Defaults to off.
2332 *
2333 * @param {boolean} state True to set origin mode, false to unset.
2334 */
2335hterm.Terminal.prototype.setOriginMode = function(state) {
2336 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002337 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002338};
2339
2340/**
2341 * Set the insert mode bit.
2342 *
2343 * If insert mode is on, existing text beyond the cursor position will be
2344 * shifted right to make room for new text. Otherwise, new text overwrites
2345 * any existing text.
2346 *
2347 * Defaults to off.
2348 *
2349 * @param {boolean} state True to set insert mode, false to unset.
2350 */
2351hterm.Terminal.prototype.setInsertMode = function(state) {
2352 this.options_.insertMode = state;
2353};
2354
2355/**
rginda87b86462011-12-14 13:48:03 -08002356 * Set the auto carriage return bit.
2357 *
2358 * If auto carriage return is on then a formfeed character is interpreted
2359 * as a newline, otherwise it's the same as a linefeed. The difference boils
2360 * down to whether or not the cursor column is reset.
Evan Jones2600d4f2016-12-06 09:29:36 -05002361 *
2362 * @param {boolean} state The state to set.
rginda87b86462011-12-14 13:48:03 -08002363 */
2364hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2365 this.options_.autoCarriageReturn = state;
2366};
2367
2368/**
rginda8ba33642011-12-14 12:31:31 -08002369 * Set the wraparound mode bit.
2370 *
2371 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2372 * to the start of the following row. Otherwise, the cursor is clamped to the
2373 * end of the screen and attempts to write past it are ignored.
2374 *
2375 * Defaults to on.
2376 *
2377 * @param {boolean} state True to set wraparound mode, false to unset.
2378 */
2379hterm.Terminal.prototype.setWraparound = function(state) {
2380 this.options_.wraparound = state;
2381};
2382
2383/**
2384 * Set the reverse-wraparound mode bit.
2385 *
2386 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2387 * to the end of the previous row. Otherwise, the cursor is clamped to column
2388 * 0.
2389 *
2390 * Defaults to off.
2391 *
2392 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2393 */
2394hterm.Terminal.prototype.setReverseWraparound = function(state) {
2395 this.options_.reverseWraparound = state;
2396};
2397
2398/**
2399 * Selects between the primary and alternate screens.
2400 *
2401 * If alternate mode is on, the alternate screen is active. Otherwise the
2402 * primary screen is active.
2403 *
2404 * Swapping screens has no effect on the scrollback buffer.
2405 *
2406 * Each screen maintains its own cursor position.
2407 *
2408 * Defaults to off.
2409 *
2410 * @param {boolean} state True to set alternate mode, false to unset.
2411 */
2412hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002413 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002414 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2415
rginda35c456b2012-02-09 17:29:05 -08002416 if (this.screen_.rowsArray.length &&
2417 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2418 // If the screen changed sizes while we were away, our rowIndexes may
2419 // be incorrect.
2420 var offset = this.scrollbackRows_.length;
2421 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002422 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002423 ary[i].rowIndex = offset + i;
2424 }
2425 }
rginda8ba33642011-12-14 12:31:31 -08002426
rginda35c456b2012-02-09 17:29:05 -08002427 this.realizeWidth_(this.screenSize.width);
2428 this.realizeHeight_(this.screenSize.height);
2429 this.scrollPort_.syncScrollHeight();
2430 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002431
rginda6d397402012-01-17 10:58:29 -08002432 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002433 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002434};
2435
2436/**
2437 * Set the cursor-blink mode bit.
2438 *
2439 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2440 * a visible cursor does not blink.
2441 *
2442 * You should make sure to turn blinking off if you're going to dispose of a
2443 * terminal, otherwise you'll leak a timeout.
2444 *
2445 * Defaults to on.
2446 *
2447 * @param {boolean} state True to set cursor-blink mode, false to unset.
2448 */
2449hterm.Terminal.prototype.setCursorBlink = function(state) {
2450 this.options_.cursorBlink = state;
2451
2452 if (!state && this.timeouts_.cursorBlink) {
2453 clearTimeout(this.timeouts_.cursorBlink);
2454 delete this.timeouts_.cursorBlink;
2455 }
2456
2457 if (this.options_.cursorVisible)
2458 this.setCursorVisible(true);
2459};
2460
2461/**
2462 * Set the cursor-visible mode bit.
2463 *
2464 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2465 *
2466 * Defaults to on.
2467 *
2468 * @param {boolean} state True to set cursor-visible mode, false to unset.
2469 */
2470hterm.Terminal.prototype.setCursorVisible = function(state) {
2471 this.options_.cursorVisible = state;
2472
2473 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002474 if (this.timeouts_.cursorBlink) {
2475 clearTimeout(this.timeouts_.cursorBlink);
2476 delete this.timeouts_.cursorBlink;
2477 }
rginda87b86462011-12-14 13:48:03 -08002478 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002479 return;
2480 }
2481
rginda87b86462011-12-14 13:48:03 -08002482 this.syncCursorPosition_();
2483
2484 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002485
2486 if (this.options_.cursorBlink) {
2487 if (this.timeouts_.cursorBlink)
2488 return;
2489
Robert Gindaea2183e2014-07-17 09:51:51 -07002490 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002491 } else {
2492 if (this.timeouts_.cursorBlink) {
2493 clearTimeout(this.timeouts_.cursorBlink);
2494 delete this.timeouts_.cursorBlink;
2495 }
2496 }
2497};
2498
2499/**
rginda87b86462011-12-14 13:48:03 -08002500 * Synchronizes the visible cursor and document selection with the current
2501 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002502 */
2503hterm.Terminal.prototype.syncCursorPosition_ = function() {
2504 var topRowIndex = this.scrollPort_.getTopRowIndex();
2505 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2506 var cursorRowIndex = this.scrollbackRows_.length +
2507 this.screen_.cursorPosition.row;
2508
2509 if (cursorRowIndex > bottomRowIndex) {
2510 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002511 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002512 return;
2513 }
2514
Robert Gindab837c052014-08-11 11:17:51 -07002515 if (this.options_.cursorVisible &&
2516 this.cursorNode_.style.display == 'none') {
2517 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2518 this.cursorNode_.style.display = '';
2519 }
2520
2521
rginda8ba33642011-12-14 12:31:31 -08002522 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002523 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2524 'px';
2525 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2526 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002527
2528 this.cursorNode_.setAttribute('title',
2529 '(' + this.screen_.cursorPosition.row +
2530 ', ' + this.screen_.cursorPosition.column +
2531 ')');
2532
2533 // Update the caret for a11y purposes.
2534 var selection = this.document_.getSelection();
2535 if (selection && selection.isCollapsed)
2536 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002537};
2538
Robert Gindafb1be6a2013-12-11 11:56:22 -08002539/**
2540 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2541 * and character cell dimensions.
2542 */
Robert Ginda830583c2013-08-07 13:20:46 -07002543hterm.Terminal.prototype.restyleCursor_ = function() {
2544 var shape = this.cursorShape_;
2545
2546 if (this.cursorNode_.getAttribute('focus') == 'false') {
2547 // Always show a block cursor when unfocused.
2548 shape = hterm.Terminal.cursorShape.BLOCK;
2549 }
2550
2551 var style = this.cursorNode_.style;
2552
Robert Gindafb1be6a2013-12-11 11:56:22 -08002553 style.width = this.scrollPort_.characterSize.width + 'px';
2554
Robert Ginda830583c2013-08-07 13:20:46 -07002555 switch (shape) {
2556 case hterm.Terminal.cursorShape.BEAM:
2557 style.height = this.scrollPort_.characterSize.height + 'px';
2558 style.backgroundColor = 'transparent';
2559 style.borderBottomStyle = null;
2560 style.borderLeftStyle = 'solid';
2561 break;
2562
2563 case hterm.Terminal.cursorShape.UNDERLINE:
2564 style.height = this.scrollPort_.characterSize.baseline + 'px';
2565 style.backgroundColor = 'transparent';
2566 style.borderBottomStyle = 'solid';
2567 // correct the size to put it exactly at the baseline
2568 style.borderLeftStyle = null;
2569 break;
2570
2571 default:
2572 style.height = this.scrollPort_.characterSize.height + 'px';
2573 style.backgroundColor = this.cursorColor_;
2574 style.borderBottomStyle = null;
2575 style.borderLeftStyle = null;
2576 break;
2577 }
2578};
2579
rginda8ba33642011-12-14 12:31:31 -08002580/**
2581 * Synchronizes the visible cursor with the current cursor coordinates.
2582 *
2583 * The sync will happen asynchronously, soon after the call stack winds down.
2584 * Multiple calls will be coalesced into a single sync.
2585 */
2586hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2587 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002588 return;
rginda8ba33642011-12-14 12:31:31 -08002589
2590 var self = this;
2591 this.timeouts_.syncCursor = setTimeout(function() {
2592 self.syncCursorPosition_();
2593 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002594 }, 0);
2595};
2596
rgindacc2996c2012-02-24 14:59:31 -08002597/**
rgindaf522ce02012-04-17 17:49:17 -07002598 * Show or hide the zoom warning.
2599 *
2600 * The zoom warning is a message warning the user that their browser zoom must
2601 * be set to 100% in order for hterm to function properly.
2602 *
2603 * @param {boolean} state True to show the message, false to hide it.
2604 */
2605hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2606 if (!this.zoomWarningNode_) {
2607 if (!state)
2608 return;
2609
2610 this.zoomWarningNode_ = this.document_.createElement('div');
2611 this.zoomWarningNode_.style.cssText = (
2612 'color: black;' +
2613 'background-color: #ff2222;' +
2614 'font-size: large;' +
2615 'border-radius: 8px;' +
2616 'opacity: 0.75;' +
2617 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2618 'top: 0.5em;' +
2619 'right: 1.2em;' +
2620 'position: absolute;' +
2621 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002622 '-webkit-user-select: none;' +
2623 '-moz-text-size-adjust: none;' +
2624 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002625
2626 this.zoomWarningNode_.addEventListener('click', function(e) {
2627 this.parentNode.removeChild(this);
2628 });
rgindaf522ce02012-04-17 17:49:17 -07002629 }
2630
Robert Gindab4839c22013-02-28 16:52:10 -08002631 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2632 hterm.zoomWarningMessage,
2633 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2634
rgindaf522ce02012-04-17 17:49:17 -07002635 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2636
2637 if (state) {
2638 if (!this.zoomWarningNode_.parentNode)
2639 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2640 } else if (this.zoomWarningNode_.parentNode) {
2641 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2642 }
2643};
2644
2645/**
rgindacc2996c2012-02-24 14:59:31 -08002646 * Show the terminal overlay for a given amount of time.
2647 *
2648 * The terminal overlay appears in inverse video in a large font, centered
2649 * over the terminal. You should probably keep the overlay message brief,
2650 * since it's in a large font and you probably aren't going to check the size
2651 * of the terminal first.
2652 *
2653 * @param {string} msg The text (not HTML) message to display in the overlay.
2654 * @param {number} opt_timeout The amount of time to wait before fading out
2655 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2656 * stay up forever (or until the next overlay).
2657 */
2658hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002659 if (!this.overlayNode_) {
2660 if (!this.div_)
2661 return;
2662
2663 this.overlayNode_ = this.document_.createElement('div');
2664 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002665 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002666 'font-size: xx-large;' +
2667 'opacity: 0.75;' +
2668 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2669 'position: absolute;' +
2670 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002671 '-webkit-transition: opacity 180ms ease-in;' +
2672 '-moz-user-select: none;' +
2673 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002674
2675 this.overlayNode_.addEventListener('mousedown', function(e) {
2676 e.preventDefault();
2677 e.stopPropagation();
2678 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002679 }
2680
rginda9f5222b2012-03-05 11:53:28 -08002681 this.overlayNode_.style.color = this.prefs_.get('background-color');
2682 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2683 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2684
rgindaf0090c92012-02-10 14:58:52 -08002685 this.overlayNode_.textContent = msg;
2686 this.overlayNode_.style.opacity = '0.75';
2687
2688 if (!this.overlayNode_.parentNode)
2689 this.div_.appendChild(this.overlayNode_);
2690
Robert Ginda97769282013-02-01 15:30:30 -08002691 var divSize = hterm.getClientSize(this.div_);
2692 var overlaySize = hterm.getClientSize(this.overlayNode_);
2693
Robert Ginda8a59f762014-07-23 11:29:55 -07002694 this.overlayNode_.style.top =
2695 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08002696 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07002697 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08002698
2699 var self = this;
2700
2701 if (this.overlayTimeout_)
2702 clearTimeout(this.overlayTimeout_);
2703
rgindacc2996c2012-02-24 14:59:31 -08002704 if (opt_timeout === null)
2705 return;
2706
rgindaf0090c92012-02-10 14:58:52 -08002707 this.overlayTimeout_ = setTimeout(function() {
2708 self.overlayNode_.style.opacity = '0';
Robert Ginda70926e42013-11-25 14:56:36 -08002709 self.overlayTimeout_ = setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002710 if (self.overlayNode_.parentNode)
2711 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002712 self.overlayTimeout_ = null;
2713 self.overlayNode_.style.opacity = '0.75';
2714 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002715 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002716};
2717
rginda4bba5e12012-06-20 16:15:30 -07002718/**
2719 * Paste from the system clipboard to the terminal.
2720 */
2721hterm.Terminal.prototype.paste = function() {
2722 hterm.pasteFromClipboard(this.document_);
2723};
2724
2725/**
2726 * Copy a string to the system clipboard.
2727 *
2728 * Note: If there is a selected range in the terminal, it'll be cleared.
Evan Jones2600d4f2016-12-06 09:29:36 -05002729 *
2730 * @param {string} str The string to copy.
rginda4bba5e12012-06-20 16:15:30 -07002731 */
2732hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002733 if (this.prefs_.get('enable-clipboard-notice'))
2734 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2735
rgindaa09e7332012-08-17 12:49:51 -07002736 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002737 copySource.textContent = str;
2738 copySource.style.cssText = (
2739 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002740 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07002741 'position: absolute;' +
2742 'top: -99px');
2743
2744 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002745
rginda4bba5e12012-06-20 16:15:30 -07002746 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002747 var anchorNode = selection.anchorNode;
2748 var anchorOffset = selection.anchorOffset;
2749 var focusNode = selection.focusNode;
2750 var focusOffset = selection.focusOffset;
2751
rginda4bba5e12012-06-20 16:15:30 -07002752 selection.selectAllChildren(copySource);
2753
rgindaa09e7332012-08-17 12:49:51 -07002754 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002755
Rob Spies56953412014-04-28 14:09:47 -07002756 // IE doesn't support selection.extend. This means that the selection
2757 // won't return on IE.
Robert Ginda6f4f0aa2014-07-28 10:21:39 -07002758 if (selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07002759 selection.collapse(anchorNode, anchorOffset);
2760 selection.extend(focusNode, focusOffset);
2761 }
rgindafaa74742012-08-21 13:34:03 -07002762
rginda4bba5e12012-06-20 16:15:30 -07002763 copySource.parentNode.removeChild(copySource);
2764};
2765
Evan Jones2600d4f2016-12-06 09:29:36 -05002766/**
2767 * Returns the selected text, or null if no text is selected.
2768 *
2769 * @return {string|null}
2770 */
rgindaa09e7332012-08-17 12:49:51 -07002771hterm.Terminal.prototype.getSelectionText = function() {
2772 var selection = this.scrollPort_.selection;
2773 selection.sync();
2774
2775 if (selection.isCollapsed)
2776 return null;
2777
2778
2779 // Start offset measures from the beginning of the line.
2780 var startOffset = selection.startOffset;
2781 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002782
Robert Gindafdbb3f22012-09-06 20:23:06 -07002783 if (node.nodeName != 'X-ROW') {
2784 // If the selection doesn't start on an x-row node, then it must be
2785 // somewhere inside the x-row. Add any characters from previous siblings
2786 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002787
2788 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2789 // If node is the text node in a styled span, move up to the span node.
2790 node = node.parentNode;
2791 }
2792
Robert Gindafdbb3f22012-09-06 20:23:06 -07002793 while (node.previousSibling) {
2794 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002795 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002796 }
rgindaa09e7332012-08-17 12:49:51 -07002797 }
2798
2799 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002800 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
2801 selection.endOffset);
Evan Jones5f9df812016-12-06 09:38:58 -05002802 node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002803
Robert Gindafdbb3f22012-09-06 20:23:06 -07002804 if (node.nodeName != 'X-ROW') {
2805 // If the selection doesn't end on an x-row node, then it must be
2806 // somewhere inside the x-row. Add any characters from following siblings
2807 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002808
2809 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2810 // If node is the text node in a styled span, move up to the span node.
2811 node = node.parentNode;
2812 }
2813
Robert Gindafdbb3f22012-09-06 20:23:06 -07002814 while (node.nextSibling) {
2815 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002816 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002817 }
rgindaa09e7332012-08-17 12:49:51 -07002818 }
2819
2820 var rv = this.getRowsText(selection.startRow.rowIndex,
2821 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002822 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002823};
2824
rginda4bba5e12012-06-20 16:15:30 -07002825/**
2826 * Copy the current selection to the system clipboard, then clear it after a
2827 * short delay.
2828 */
2829hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002830 var text = this.getSelectionText();
2831 if (text != null)
2832 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002833};
2834
rgindaf0090c92012-02-10 14:58:52 -08002835hterm.Terminal.prototype.overlaySize = function() {
2836 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2837};
2838
rginda87b86462011-12-14 13:48:03 -08002839/**
2840 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2841 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07002842 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08002843 */
2844hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002845 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002846 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2847
Robert Ginda8cb7d902013-06-20 14:37:18 -07002848 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08002849};
2850
2851/**
rgindad5613292012-06-19 15:40:37 -07002852 * Add the terminalRow and terminalColumn properties to mouse events and
2853 * then forward on to onMouse().
2854 *
2855 * The terminalRow and terminalColumn properties contain the (row, column)
2856 * coordinates for the mouse event.
Evan Jones2600d4f2016-12-06 09:29:36 -05002857 *
2858 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07002859 */
2860hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002861 if (e.processedByTerminalHandler_) {
2862 // We register our event handlers on the document, as well as the cursor
2863 // and the scroll blocker. Mouse events that occur on the cursor or
2864 // scroll blocker will also appear on the document, but we don't want to
2865 // process them twice.
2866 //
2867 // We can't just prevent bubbling because that has other side effects, so
2868 // we decorate the event object with this property instead.
2869 return;
2870 }
2871
Robert Ginda6aec7eb2015-06-16 10:31:30 -07002872 var reportMouseEvents = (!this.defeatMouseReports_ &&
2873 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
2874
rgindafaa74742012-08-21 13:34:03 -07002875 e.processedByTerminalHandler_ = true;
2876
Robert Gindaeda48db2014-07-17 09:25:30 -07002877 // One based row/column stored on the mouse event.
2878 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2879 this.scrollPort_.characterSize.height) + 1;
2880 e.terminalColumn = parseInt(e.clientX /
2881 this.scrollPort_.characterSize.width) + 1;
2882
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002883 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
2884 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07002885 return;
2886 }
2887
Robert Ginda6aec7eb2015-06-16 10:31:30 -07002888 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07002889 // If the cursor is visible and we're not sending mouse events to the
2890 // host app, then we want to hide the terminal cursor when the mouse
2891 // cursor is over top. This keeps the terminal cursor from interfering
2892 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07002893 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
2894 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
2895 this.cursorNode_.style.display = 'none';
2896 } else if (this.cursorNode_.style.display == 'none') {
2897 this.cursorNode_.style.display = '';
2898 }
2899 }
rgindad5613292012-06-19 15:40:37 -07002900
Robert Ginda928cf632014-03-05 15:07:41 -08002901 if (e.type == 'mousedown') {
Robert Ginda6aec7eb2015-06-16 10:31:30 -07002902 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08002903 // If VT mouse reporting is disabled, or has been defeated with
2904 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07002905 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08002906 this.setSelectionEnabled(true);
2907 } else {
2908 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07002909 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07002910 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08002911 this.setSelectionEnabled(false);
2912 e.preventDefault();
2913 }
2914 }
2915
Robert Ginda6aec7eb2015-06-16 10:31:30 -07002916 if (!reportMouseEvents) {
Robert Ginda15ed4902016-07-12 10:43:22 -07002917 if (e.type == 'dblclick' && this.copyOnSelect) {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002918 this.screen_.expandSelection(this.document_.getSelection());
Robert Ginda15ed4902016-07-12 10:43:22 -07002919 this.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07002920 }
2921
Robert Ginda928cf632014-03-05 15:07:41 -08002922 if (e.type == 'mousedown' && e.which == this.mousePasteButton)
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002923 this.paste();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002924
2925 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2926 !this.document_.getSelection().isCollapsed) {
Robert Ginda15ed4902016-07-12 10:43:22 -07002927 this.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002928 }
2929
2930 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
2931 this.scrollBlockerNode_.engaged) {
2932 // Disengage the scroll-blocker after one of these events.
2933 this.scrollBlockerNode_.engaged = false;
2934 this.scrollBlockerNode_.style.top = '-99px';
2935 }
2936
Robert Ginda928cf632014-03-05 15:07:41 -08002937 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002938 if (!this.scrollBlockerNode_.engaged) {
2939 if (e.type == 'mousedown') {
2940 // Move the scroll-blocker into place if we want to keep the scrollport
2941 // from scrolling.
2942 this.scrollBlockerNode_.engaged = true;
2943 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2944 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2945 } else if (e.type == 'mousemove') {
2946 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
2947 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07002948 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002949 e.preventDefault();
2950 }
2951 }
Robert Ginda928cf632014-03-05 15:07:41 -08002952
2953 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07002954 }
2955
Robert Ginda928cf632014-03-05 15:07:41 -08002956 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
2957 // Restore this on mouseup in case it was temporarily defeated with a
2958 // alt-mousedown. Only do this when the selection is empty so that
2959 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07002960 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08002961 }
rgindad5613292012-06-19 15:40:37 -07002962};
2963
2964/**
2965 * Clients should override this if they care to know about mouse events.
2966 *
2967 * The event parameter will be a normal DOM mouse click event with additional
2968 * 'terminalRow' and 'terminalColumn' properties.
Evan Jones2600d4f2016-12-06 09:29:36 -05002969 *
2970 * @param {Event} e The mouse event to handle.
rgindad5613292012-06-19 15:40:37 -07002971 */
2972hterm.Terminal.prototype.onMouse = function(e) { };
2973
2974/**
rginda8e92a692012-05-20 19:37:20 -07002975 * React when focus changes.
Evan Jones2600d4f2016-12-06 09:29:36 -05002976 *
2977 * @param {boolean} focused True if focused, false otherwise.
rginda8e92a692012-05-20 19:37:20 -07002978 */
Rob Spies06533ba2014-04-24 11:20:37 -07002979hterm.Terminal.prototype.onFocusChange_ = function(focused) {
2980 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07002981 this.restyleCursor_();
Michael Kelly485ecd12014-06-09 11:41:56 -04002982 if (focused === true)
2983 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07002984};
2985
2986/**
rginda8ba33642011-12-14 12:31:31 -08002987 * React when the ScrollPort is scrolled.
2988 */
2989hterm.Terminal.prototype.onScroll_ = function() {
2990 this.scheduleSyncCursorPosition_();
2991};
2992
2993/**
rginda9846e2f2012-01-27 13:53:33 -08002994 * React when text is pasted into the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05002995 *
2996 * @param {Event} e The DOM paste event to handle.
rginda9846e2f2012-01-27 13:53:33 -08002997 */
2998hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07002999 var data = e.text.replace(/\n/mg, '\r');
Connor Hegartyf525ceb2014-09-03 13:36:03 -07003000 data = this.keyboard.encode(data);
Robert Gindaa063b202014-07-21 11:08:25 -07003001 if (this.options_.bracketedPaste)
3002 data = '\x1b[200~' + data + '\x1b[201~';
3003
3004 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08003005};
3006
3007/**
rgindaa09e7332012-08-17 12:49:51 -07003008 * React when the user tries to copy from the scrollPort.
Evan Jones2600d4f2016-12-06 09:29:36 -05003009 *
3010 * @param {Event} e The DOM copy event.
rgindaa09e7332012-08-17 12:49:51 -07003011 */
3012hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07003013 if (!this.useDefaultWindowCopy) {
3014 e.preventDefault();
3015 setTimeout(this.copySelectionToClipboard.bind(this), 0);
3016 }
rgindaa09e7332012-08-17 12:49:51 -07003017};
3018
3019/**
rginda8ba33642011-12-14 12:31:31 -08003020 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08003021 *
3022 * Note: This function should not directly contain code that alters the internal
3023 * state of the terminal. That kind of code belongs in realizeWidth or
3024 * realizeHeight, so that it can be executed synchronously in the case of a
3025 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08003026 */
3027hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08003028 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07003029 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08003030 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07003031 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08003032
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003033 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08003034 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07003035 // gets removed from the document or during the initial load, and we can't
3036 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07003037 // This can also happen if called before the scrollPort calculates the
3038 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08003039 return;
3040 }
3041
rgindaa8ba17d2012-08-15 14:41:10 -07003042 var isNewSize = (columnCount != this.screenSize.width ||
3043 rowCount != this.screenSize.height);
3044
3045 // We do this even if the size didn't change, just to be sure everything is
3046 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04003047 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07003048 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07003049
3050 if (isNewSize)
3051 this.overlaySize();
3052
Robert Gindafb1be6a2013-12-11 11:56:22 -08003053 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07003054 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08003055};
3056
3057/**
3058 * Service the cursor blink timeout.
3059 */
3060hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07003061 if (!this.options_.cursorBlink) {
3062 delete this.timeouts_.cursorBlink;
3063 return;
3064 }
3065
Robert Ginda830583c2013-08-07 13:20:46 -07003066 if (this.cursorNode_.getAttribute('focus') == 'false' ||
3067 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08003068 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07003069 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3070 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08003071 } else {
rginda87b86462011-12-14 13:48:03 -08003072 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07003073 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
3074 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08003075 }
3076};
David Reveman8f552492012-03-28 12:18:41 -04003077
3078/**
3079 * Set the scrollbar-visible mode bit.
3080 *
3081 * If scrollbar-visible is on, the vertical scrollbar will be visible.
3082 * Otherwise it will not.
3083 *
3084 * Defaults to on.
3085 *
3086 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
3087 */
3088hterm.Terminal.prototype.setScrollbarVisible = function(state) {
3089 this.scrollPort_.setScrollbarVisible(state);
3090};
Michael Kelly485ecd12014-06-09 11:41:56 -04003091
3092/**
Rob Spies49039e52014-12-17 13:40:04 -08003093 * Set the scroll wheel move multiplier. This will affect how fast the page
3094 * scrolls on mousewheel events.
3095 *
3096 * Defaults to 1.
3097 *
Evan Jones2600d4f2016-12-06 09:29:36 -05003098 * @param {number} multiplier The multiplier to set.
Rob Spies49039e52014-12-17 13:40:04 -08003099 */
3100hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3101 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3102};
3103
3104/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003105 * Close all web notifications created by terminal bells.
3106 */
3107hterm.Terminal.prototype.closeBellNotifications_ = function() {
3108 this.bellNotificationList_.forEach(function(n) {
3109 n.close();
3110 });
3111 this.bellNotificationList_.length = 0;
3112};