blob: b4f42fa3f471101cc4cb52f31b15e1866564e3c6 [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 Ginda928cf632014-03-05 15:07:41 -0800101 // True if we should send mouse events to the vt, false if we want them
102 // to manage the local text selection.
103 this.reportMouseEvents_ = false;
104
rgindaf0090c92012-02-10 14:58:52 -0800105 // Terminal bell sound.
106 this.bellAudio_ = this.document_.createElement('audio');
rgindaf0090c92012-02-10 14:58:52 -0800107 this.bellAudio_.setAttribute('preload', 'auto');
108
Michael Kelly485ecd12014-06-09 11:41:56 -0400109 // All terminal bell notifications that have been generated (not necessarily
110 // shown).
111 this.bellNotificationList_ = [];
112
113 // Whether we have permission to display notifications.
114 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400115
rginda6d397402012-01-17 10:58:29 -0800116 // Cursor position and attributes saved with DECSC.
117 this.savedOptions_ = {};
118
rginda8ba33642011-12-14 12:31:31 -0800119 // The current mode bits for the terminal.
120 this.options_ = new hterm.Options();
121
122 // Timeouts we might need to clear.
123 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800124
125 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800126 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800127
rgindafeaf3142012-01-31 15:14:20 -0800128 // The keyboard hander.
129 this.keyboard = new hterm.Keyboard(this);
130
rginda87b86462011-12-14 13:48:03 -0800131 // General IO interface that can be given to third parties without exposing
132 // the entire terminal object.
133 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800134
rgindad5613292012-06-19 15:40:37 -0700135 // True if mouse-click-drag should scroll the terminal.
136 this.enableMouseDragScroll = true;
137
Robert Ginda57f03b42012-09-13 11:02:48 -0700138 this.copyOnSelect = null;
rginda4bba5e12012-06-20 16:15:30 -0700139 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700140
Rob Spies0bec09b2014-06-06 15:58:09 -0700141 // Whether to use the default window copy behaviour.
142 this.useDefaultWindowCopy = false;
143
144 this.clearSelectionAfterCopy = true;
145
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400146 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800147 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700148
149 this.setProfile(opt_profileId || 'default',
150 function() { this.onTerminalReady() }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800151};
152
153/**
Robert Ginda830583c2013-08-07 13:20:46 -0700154 * Possible cursor shapes.
155 */
156hterm.Terminal.cursorShape = {
157 BLOCK: 'BLOCK',
158 BEAM: 'BEAM',
159 UNDERLINE: 'UNDERLINE'
160};
161
162/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700163 * Clients should override this to be notified when the terminal is ready
164 * for use.
165 *
166 * The terminal initialization is asynchronous, and shouldn't be used before
167 * this method is called.
168 */
169hterm.Terminal.prototype.onTerminalReady = function() { };
170
171/**
rginda35c456b2012-02-09 17:29:05 -0800172 * Default tab with of 8 to match xterm.
173 */
174hterm.Terminal.prototype.tabWidth = 8;
175
176/**
rginda9f5222b2012-03-05 11:53:28 -0800177 * Select a preference profile.
178 *
179 * This will load the terminal preferences for the given profile name and
180 * associate subsequent preference changes with the new preference profile.
181 *
182 * @param {string} newName The name of the preference profile. Forward slash
183 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700184 * @param {function} opt_callback Optional callback to invoke when the profile
185 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800186 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700187hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
188 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800189
Robert Ginda57f03b42012-09-13 11:02:48 -0700190 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800191
Robert Ginda57f03b42012-09-13 11:02:48 -0700192 if (this.prefs_)
193 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800194
Robert Ginda57f03b42012-09-13 11:02:48 -0700195 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
196 this.prefs_.addObservers(null, {
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700197 'alt-backspace-is-meta-backspace': function(v) {
198 terminal.keyboard.altBackspaceIsMetaBackspace = v;
199 },
200
Robert Ginda57f03b42012-09-13 11:02:48 -0700201 'alt-is-meta': function(v) {
202 terminal.keyboard.altIsMeta = v;
203 },
204
205 'alt-sends-what': function(v) {
206 if (!/^(escape|8-bit|browser-key)$/.test(v))
207 v = 'escape';
208
209 terminal.keyboard.altSendsWhat = v;
210 },
211
212 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800213 var ary = v.match(/^lib-resource:(\S+)/);
214 if (ary) {
215 terminal.bellAudio_.setAttribute('src',
216 lib.resource.getDataUrl(ary[1]));
217 } else {
218 terminal.bellAudio_.setAttribute('src', v);
219 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700220 },
221
Michael Kelly485ecd12014-06-09 11:41:56 -0400222 'desktop-notification-bell': function(v) {
223 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700224 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400225 Notification.permission === 'granted';
226 if (!terminal.desktopNotificationBell_) {
227 // Note: We don't call Notification.requestPermission here because
228 // Chrome requires the call be the result of a user action (such as an
229 // onclick handler), and pref listeners are run asynchronously.
230 //
231 // A way of working around this would be to display a dialog in the
232 // terminal with a "click-to-request-permission" button.
233 console.warn('desktop-notification-bell is true but we do not have ' +
234 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400235 }
236 } else {
237 terminal.desktopNotificationBell_ = false;
238 }
239 },
240
Robert Ginda57f03b42012-09-13 11:02:48 -0700241 'background-color': function(v) {
242 terminal.setBackgroundColor(v);
243 },
244
245 'background-image': function(v) {
246 terminal.scrollPort_.setBackgroundImage(v);
247 },
248
249 'background-size': function(v) {
250 terminal.scrollPort_.setBackgroundSize(v);
251 },
252
253 'background-position': function(v) {
254 terminal.scrollPort_.setBackgroundPosition(v);
255 },
256
257 'backspace-sends-backspace': function(v) {
258 terminal.keyboard.backspaceSendsBackspace = v;
259 },
260
261 'cursor-blink': function(v) {
262 terminal.setCursorBlink(!!v);
263 },
264
Robert Gindaea2183e2014-07-17 09:51:51 -0700265 'cursor-blink-cycle': function(v) {
266 if (v instanceof Array &&
267 typeof v[0] == 'number' &&
268 typeof v[1] == 'number') {
269 terminal.cursorBlinkCycle_ = v;
270 } else if (typeof v == 'number') {
271 terminal.cursorBlinkCycle_ = [v, v];
272 } else {
273 // Fast blink indicates an error.
274 terminal.cursorBlinkCycle_ = [100, 100];
275 }
276 },
277
Robert Ginda57f03b42012-09-13 11:02:48 -0700278 'cursor-color': function(v) {
279 terminal.setCursorColor(v);
280 },
281
282 'color-palette-overrides': function(v) {
283 if (!(v == null || v instanceof Object || v instanceof Array)) {
284 console.warn('Preference color-palette-overrides is not an array or ' +
285 'object: ' + v);
286 return;
rginda9f5222b2012-03-05 11:53:28 -0800287 }
rginda9f5222b2012-03-05 11:53:28 -0800288
Robert Ginda57f03b42012-09-13 11:02:48 -0700289 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700290
Robert Ginda57f03b42012-09-13 11:02:48 -0700291 if (v) {
292 for (var key in v) {
293 var i = parseInt(key);
294 if (isNaN(i) || i < 0 || i > 255) {
295 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
296 continue;
297 }
298
299 if (v[i]) {
300 var rgb = lib.colors.normalizeCSS(v[i]);
301 if (rgb)
302 lib.colors.colorPalette[i] = rgb;
303 }
304 }
rginda30f20f62012-04-05 16:36:19 -0700305 }
rginda30f20f62012-04-05 16:36:19 -0700306
Robert Ginda57f03b42012-09-13 11:02:48 -0700307 terminal.primaryScreen_.textAttributes.resetColorPalette()
308 terminal.alternateScreen_.textAttributes.resetColorPalette();
309 },
rginda30f20f62012-04-05 16:36:19 -0700310
Robert Ginda57f03b42012-09-13 11:02:48 -0700311 'copy-on-select': function(v) {
312 terminal.copyOnSelect = !!v;
313 },
rginda9f5222b2012-03-05 11:53:28 -0800314
Rob Spies0bec09b2014-06-06 15:58:09 -0700315 'use-default-window-copy': function(v) {
316 terminal.useDefaultWindowCopy = !!v;
317 },
318
319 'clear-selection-after-copy': function(v) {
320 terminal.clearSelectionAfterCopy = !!v;
321 },
322
Robert Ginda7e5e9522014-03-14 12:23:58 -0700323 'ctrl-plus-minus-zero-zoom': function(v) {
324 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
325 },
326
Robert Gindafb5a3f92014-05-13 14:12:00 -0700327 'ctrl-c-copy': function(v) {
328 terminal.keyboard.ctrlCCopy = v;
329 },
330
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100331 'ctrl-v-paste': function(v) {
332 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700333 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100334 },
335
Masaya Suzuki273aa982014-05-31 07:25:55 +0900336 'east-asian-ambiguous-as-two-column': function(v) {
337 lib.wc.regardCjkAmbiguous = v;
338 },
339
Robert Ginda57f03b42012-09-13 11:02:48 -0700340 'enable-8-bit-control': function(v) {
341 terminal.vt.enable8BitControl = !!v;
342 },
rginda30f20f62012-04-05 16:36:19 -0700343
Robert Ginda57f03b42012-09-13 11:02:48 -0700344 'enable-bold': function(v) {
345 terminal.syncBoldSafeState();
346 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400347
Robert Ginda3e278d72014-03-25 13:18:51 -0700348 'enable-bold-as-bright': function(v) {
349 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
350 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
351 },
352
Robert Ginda57f03b42012-09-13 11:02:48 -0700353 'enable-clipboard-write': function(v) {
354 terminal.vt.enableClipboardWrite = !!v;
355 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400356
Robert Ginda3755e752013-05-31 13:34:09 -0700357 'enable-dec12': function(v) {
358 terminal.vt.enableDec12 = !!v;
359 },
360
Robert Ginda57f03b42012-09-13 11:02:48 -0700361 'font-family': function(v) {
362 terminal.syncFontFamily();
363 },
rginda30f20f62012-04-05 16:36:19 -0700364
Robert Ginda57f03b42012-09-13 11:02:48 -0700365 'font-size': function(v) {
366 terminal.setFontSize(v);
367 },
rginda9875d902012-08-20 16:21:57 -0700368
Robert Ginda57f03b42012-09-13 11:02:48 -0700369 'font-smoothing': function(v) {
370 terminal.syncFontFamily();
371 },
rgindade84e382012-04-20 15:39:31 -0700372
Robert Ginda57f03b42012-09-13 11:02:48 -0700373 'foreground-color': function(v) {
374 terminal.setForegroundColor(v);
375 },
rginda30f20f62012-04-05 16:36:19 -0700376
Robert Ginda57f03b42012-09-13 11:02:48 -0700377 'home-keys-scroll': function(v) {
378 terminal.keyboard.homeKeysScroll = v;
379 },
rginda4bba5e12012-06-20 16:15:30 -0700380
Robert Ginda57f03b42012-09-13 11:02:48 -0700381 'max-string-sequence': function(v) {
382 terminal.vt.maxStringSequence = v;
383 },
rginda11057d52012-04-25 12:29:56 -0700384
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700385 'media-keys-are-fkeys': function(v) {
386 terminal.keyboard.mediaKeysAreFKeys = v;
387 },
388
Robert Ginda57f03b42012-09-13 11:02:48 -0700389 'meta-sends-escape': function(v) {
390 terminal.keyboard.metaSendsEscape = v;
391 },
rginda30f20f62012-04-05 16:36:19 -0700392
Robert Ginda57f03b42012-09-13 11:02:48 -0700393 'mouse-paste-button': function(v) {
394 terminal.syncMousePasteButton();
395 },
rgindaa8ba17d2012-08-15 14:41:10 -0700396
Robert Gindae76aa9f2014-03-14 12:29:12 -0700397 'page-keys-scroll': function(v) {
398 terminal.keyboard.pageKeysScroll = v;
399 },
400
Robert Ginda40932892012-12-10 17:26:40 -0800401 'pass-alt-number': function(v) {
402 if (v == null) {
403 var osx = window.navigator.userAgent.match(/Mac OS X/);
404
405 // Let Alt-1..9 pass to the browser (to control tab switching) on
406 // non-OS X systems, or if hterm is not opened in an app window.
407 v = (!osx && hterm.windowType != 'popup');
408 }
409
410 terminal.passAltNumber = v;
411 },
412
413 'pass-ctrl-number': function(v) {
414 if (v == null) {
415 var osx = window.navigator.userAgent.match(/Mac OS X/);
416
417 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
418 // non-OS X systems, or if hterm is not opened in an app window.
419 v = (!osx && hterm.windowType != 'popup');
420 }
421
422 terminal.passCtrlNumber = v;
423 },
424
425 'pass-meta-number': function(v) {
426 if (v == null) {
427 var osx = window.navigator.userAgent.match(/Mac OS X/);
428
429 // Let Meta-1..9 pass to the browser (to control tab switching) on
430 // OS X systems, or if hterm is not opened in an app window.
431 v = (osx && hterm.windowType != 'popup');
432 }
433
434 terminal.passMetaNumber = v;
435 },
436
Marius Schilder77857b32014-05-14 16:21:26 -0700437 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700438 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700439 },
440
Robert Ginda8cb7d902013-06-20 14:37:18 -0700441 'receive-encoding': function(v) {
442 if (!(/^(utf-8|raw)$/).test(v)) {
443 console.warn('Invalid value for "receive-encoding": ' + v);
444 v = 'utf-8';
445 }
446
447 terminal.vt.characterEncoding = v;
448 },
449
Robert Ginda57f03b42012-09-13 11:02:48 -0700450 'scroll-on-keystroke': function(v) {
451 terminal.scrollOnKeystroke_ = v;
452 },
rginda9f5222b2012-03-05 11:53:28 -0800453
Robert Ginda57f03b42012-09-13 11:02:48 -0700454 'scroll-on-output': function(v) {
455 terminal.scrollOnOutput_ = v;
456 },
rginda30f20f62012-04-05 16:36:19 -0700457
Robert Ginda57f03b42012-09-13 11:02:48 -0700458 'scrollbar-visible': function(v) {
459 terminal.setScrollbarVisible(v);
460 },
rginda9f5222b2012-03-05 11:53:28 -0800461
Rob Spies49039e52014-12-17 13:40:04 -0800462 'scroll-wheel-move-multiplier': function(v) {
463 terminal.setScrollWheelMoveMultipler(v);
464 },
465
Robert Ginda8cb7d902013-06-20 14:37:18 -0700466 'send-encoding': function(v) {
467 if (!(/^(utf-8|raw)$/).test(v)) {
468 console.warn('Invalid value for "send-encoding": ' + v);
469 v = 'utf-8';
470 }
471
472 terminal.keyboard.characterEncoding = v;
473 },
474
Robert Ginda57f03b42012-09-13 11:02:48 -0700475 'shift-insert-paste': function(v) {
476 terminal.keyboard.shiftInsertPaste = v;
477 },
rginda9f5222b2012-03-05 11:53:28 -0800478
Robert Gindae76aa9f2014-03-14 12:29:12 -0700479 'user-css': function(v) {
480 terminal.scrollPort_.setUserCss(v);
Robert Ginda57f03b42012-09-13 11:02:48 -0700481 }
482 });
rginda30f20f62012-04-05 16:36:19 -0700483
Robert Ginda57f03b42012-09-13 11:02:48 -0700484 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800485 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700486
487 if (opt_callback)
488 opt_callback();
489 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800490};
491
Rob Spies56953412014-04-28 14:09:47 -0700492
493/**
494 * Returns the preferences manager used for configuring this terminal.
495 */
496hterm.Terminal.prototype.getPrefs = function() {
497 return this.prefs_;
498};
499
Robert Gindaa063b202014-07-21 11:08:25 -0700500/**
501 * Enable or disable bracketed paste mode.
502 */
503hterm.Terminal.prototype.setBracketedPaste = function(state) {
504 this.options_.bracketedPaste = state;
505};
Rob Spies56953412014-04-28 14:09:47 -0700506
rginda8e92a692012-05-20 19:37:20 -0700507/**
508 * Set the color for the cursor.
509 *
510 * If you want this setting to persist, set it through prefs_, rather than
511 * with this method.
512 */
513hterm.Terminal.prototype.setCursorColor = function(color) {
Robert Ginda830583c2013-08-07 13:20:46 -0700514 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700515 this.cursorNode_.style.backgroundColor = color;
516 this.cursorNode_.style.borderColor = color;
517};
518
519/**
520 * Return the current cursor color as a string.
521 */
522hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700523 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700524};
525
526/**
rgindad5613292012-06-19 15:40:37 -0700527 * Enable or disable mouse based text selection in the terminal.
528 */
529hterm.Terminal.prototype.setSelectionEnabled = function(state) {
530 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700531};
532
533/**
rginda8e92a692012-05-20 19:37:20 -0700534 * Set the background color.
535 *
536 * If you want this setting to persist, set it through prefs_, rather than
537 * with this method.
538 */
539hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700540 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700541 this.primaryScreen_.textAttributes.setDefaults(
542 this.foregroundColor_, this.backgroundColor_);
543 this.alternateScreen_.textAttributes.setDefaults(
544 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700545 this.scrollPort_.setBackgroundColor(color);
546};
547
rginda9f5222b2012-03-05 11:53:28 -0800548/**
549 * Return the current terminal background color.
550 *
551 * Intended for use by other classes, so we don't have to expose the entire
552 * prefs_ object.
553 */
554hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700555 return this.backgroundColor_;
556};
557
558/**
559 * Set the foreground color.
560 *
561 * If you want this setting to persist, set it through prefs_, rather than
562 * with this method.
563 */
564hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700565 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700566 this.primaryScreen_.textAttributes.setDefaults(
567 this.foregroundColor_, this.backgroundColor_);
568 this.alternateScreen_.textAttributes.setDefaults(
569 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700570 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800571};
572
573/**
574 * Return the current terminal foreground color.
575 *
576 * Intended for use by other classes, so we don't have to expose the entire
577 * prefs_ object.
578 */
579hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700580 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800581};
582
583/**
rginda87b86462011-12-14 13:48:03 -0800584 * Create a new instance of a terminal command and run it with a given
585 * argument string.
586 *
587 * @param {function} commandClass The constructor for a terminal command.
588 * @param {string} argString The argument string to pass to the command.
589 */
590hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700591 var environment = this.prefs_.get('environment');
592 if (typeof environment != 'object' || environment == null)
593 environment = {};
594
rginda87b86462011-12-14 13:48:03 -0800595 var self = this;
596 this.command = new commandClass(
597 { argString: argString || '',
598 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700599 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800600 onExit: function(code) {
601 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800602 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700603 if (self.prefs_.get('close-on-exit'))
604 window.close();
rginda87b86462011-12-14 13:48:03 -0800605 }
606 });
607
rgindafeaf3142012-01-31 15:14:20 -0800608 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800609 this.command.run();
610};
611
612/**
rgindafeaf3142012-01-31 15:14:20 -0800613 * Returns true if the current screen is the primary screen, false otherwise.
614 */
615hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700616 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800617};
618
619/**
620 * Install the keyboard handler for this terminal.
621 *
622 * This will prevent the browser from seeing any keystrokes sent to the
623 * terminal.
624 */
625hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700626 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
rgindafeaf3142012-01-31 15:14:20 -0800627}
628
629/**
630 * Uninstall the keyboard handler for this terminal.
631 */
632hterm.Terminal.prototype.uninstallKeyboard = function() {
633 this.keyboard.installKeyboard(null);
634}
635
636/**
rginda35c456b2012-02-09 17:29:05 -0800637 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800638 *
639 * Call setFontSize(0) to reset to the default font size.
640 *
641 * This function does not modify the font-size preference.
642 *
643 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800644 */
645hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800646 if (px === 0)
647 px = this.prefs_.get('font-size');
648
rginda35c456b2012-02-09 17:29:05 -0800649 this.scrollPort_.setFontSize(px);
Ricky Liang48f05cb2013-12-31 23:35:29 +0800650 if (this.wcCssRule_) {
651 this.wcCssRule_.style.width = this.scrollPort_.characterSize.width * 2 +
652 'px';
653 }
rginda35c456b2012-02-09 17:29:05 -0800654};
655
656/**
657 * Get the current font size.
658 */
659hterm.Terminal.prototype.getFontSize = function() {
660 return this.scrollPort_.getFontSize();
661};
662
663/**
rginda8e92a692012-05-20 19:37:20 -0700664 * Get the current font family.
665 */
666hterm.Terminal.prototype.getFontFamily = function() {
667 return this.scrollPort_.getFontFamily();
668};
669
670/**
rginda35c456b2012-02-09 17:29:05 -0800671 * Set the CSS "font-family" for this terminal.
672 */
rginda9f5222b2012-03-05 11:53:28 -0800673hterm.Terminal.prototype.syncFontFamily = function() {
674 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
675 this.prefs_.get('font-smoothing'));
676 this.syncBoldSafeState();
677};
678
rginda4bba5e12012-06-20 16:15:30 -0700679/**
680 * Set this.mousePasteButton based on the mouse-paste-button pref,
681 * autodetecting if necessary.
682 */
683hterm.Terminal.prototype.syncMousePasteButton = function() {
684 var button = this.prefs_.get('mouse-paste-button');
685 if (typeof button == 'number') {
686 this.mousePasteButton = button;
687 return;
688 }
689
690 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
691 if (!ary || ary[2] == 'CrOS') {
692 this.mousePasteButton = 2;
693 } else {
694 this.mousePasteButton = 3;
695 }
696};
697
698/**
699 * Enable or disable bold based on the enable-bold pref, autodetecting if
700 * necessary.
701 */
rginda9f5222b2012-03-05 11:53:28 -0800702hterm.Terminal.prototype.syncBoldSafeState = function() {
703 var enableBold = this.prefs_.get('enable-bold');
704 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700705 this.primaryScreen_.textAttributes.enableBold = enableBold;
706 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800707 return;
708 }
709
rgindaf7521392012-02-28 17:20:34 -0800710 var normalSize = this.scrollPort_.measureCharacterSize();
711 var boldSize = this.scrollPort_.measureCharacterSize('bold');
712
713 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800714 if (!isBoldSafe) {
715 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700716 'from normal. Font family is: ' +
717 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800718 }
rginda9f5222b2012-03-05 11:53:28 -0800719
Robert Gindaed016262012-10-26 16:27:09 -0700720 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
721 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800722};
723
724/**
rginda87b86462011-12-14 13:48:03 -0800725 * Return a copy of the current cursor position.
726 *
727 * @return {hterm.RowCol} The RowCol object representing the current position.
728 */
729hterm.Terminal.prototype.saveCursor = function() {
730 return this.screen_.cursorPosition.clone();
731};
732
rgindaa19afe22012-01-25 15:40:22 -0800733hterm.Terminal.prototype.getTextAttributes = function() {
734 return this.screen_.textAttributes;
735};
736
rginda1a09aa02012-06-18 21:11:25 -0700737hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
738 this.screen_.textAttributes = textAttributes;
739};
740
rginda87b86462011-12-14 13:48:03 -0800741/**
rgindaf522ce02012-04-17 17:49:17 -0700742 * Return the current browser zoom factor applied to the terminal.
743 *
744 * @return {number} The current browser zoom factor.
745 */
746hterm.Terminal.prototype.getZoomFactor = function() {
747 return this.scrollPort_.characterSize.zoomFactor;
748};
749
750/**
rginda9846e2f2012-01-27 13:53:33 -0800751 * Change the title of this terminal's window.
752 */
753hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800754 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800755};
756
757/**
rginda87b86462011-12-14 13:48:03 -0800758 * Restore a previously saved cursor position.
759 *
760 * @param {hterm.RowCol} cursor The position to restore.
761 */
762hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700763 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
764 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800765 this.screen_.setCursorPosition(row, column);
766 if (cursor.column > column ||
767 cursor.column == column && cursor.overflow) {
768 this.screen_.cursorPosition.overflow = true;
769 }
rginda87b86462011-12-14 13:48:03 -0800770};
771
772/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400773 * Clear the cursor's overflow flag.
774 */
775hterm.Terminal.prototype.clearCursorOverflow = function() {
776 this.screen_.cursorPosition.overflow = false;
777};
778
779/**
Robert Ginda830583c2013-08-07 13:20:46 -0700780 * Sets the cursor shape
781 */
782hterm.Terminal.prototype.setCursorShape = function(shape) {
783 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -0800784 this.restyleCursor_();
Robert Ginda830583c2013-08-07 13:20:46 -0700785}
786
787/**
788 * Get the cursor shape
789 */
790hterm.Terminal.prototype.getCursorShape = function() {
791 return this.cursorShape_;
792}
793
794/**
rginda87b86462011-12-14 13:48:03 -0800795 * Set the width of the terminal, resizing the UI to match.
796 */
797hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800798 if (columnCount == null) {
799 this.div_.style.width = '100%';
800 return;
801 }
802
Robert Ginda26806d12014-07-24 13:44:07 -0700803 this.div_.style.width = Math.ceil(
804 this.scrollPort_.characterSize.width *
805 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400806 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800807 this.scheduleSyncCursorPosition_();
808};
rginda87b86462011-12-14 13:48:03 -0800809
rgindac9bc5502012-01-18 11:48:44 -0800810/**
rginda35c456b2012-02-09 17:29:05 -0800811 * Set the height of the terminal, resizing the UI to match.
812 */
813hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800814 if (rowCount == null) {
815 this.div_.style.height = '100%';
816 return;
817 }
818
rginda35c456b2012-02-09 17:29:05 -0800819 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700820 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800821 this.realizeSize_(this.screenSize.width, rowCount);
822 this.scheduleSyncCursorPosition_();
823};
824
825/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400826 * Deal with terminal size changes.
827 *
828 */
829hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
830 if (columnCount != this.screenSize.width)
831 this.realizeWidth_(columnCount);
832
833 if (rowCount != this.screenSize.height)
834 this.realizeHeight_(rowCount);
835
836 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -0700837 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400838};
839
840/**
rgindac9bc5502012-01-18 11:48:44 -0800841 * Deal with terminal width changes.
842 *
843 * This function does what needs to be done when the terminal width changes
844 * out from under us. It happens here rather than in onResize_() because this
845 * code may need to run synchronously to handle programmatic changes of
846 * terminal width.
847 *
848 * Relying on the browser to send us an async resize event means we may not be
849 * in the correct state yet when the next escape sequence hits.
850 */
851hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700852 if (columnCount <= 0)
853 throw new Error('Attempt to realize bad width: ' + columnCount);
854
rgindac9bc5502012-01-18 11:48:44 -0800855 var deltaColumns = columnCount - this.screen_.getWidth();
856
rginda87b86462011-12-14 13:48:03 -0800857 this.screenSize.width = columnCount;
858 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800859
860 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400861 if (this.defaultTabStops)
862 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800863 } else {
864 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400865 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800866 break;
867
868 this.tabStops_.pop();
869 }
870 }
871
872 this.screen_.setColumnCount(this.screenSize.width);
873};
874
875/**
876 * Deal with terminal height changes.
877 *
878 * This function does what needs to be done when the terminal height changes
879 * out from under us. It happens here rather than in onResize_() because this
880 * code may need to run synchronously to handle programmatic changes of
881 * terminal height.
882 *
883 * Relying on the browser to send us an async resize event means we may not be
884 * in the correct state yet when the next escape sequence hits.
885 */
886hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700887 if (rowCount <= 0)
888 throw new Error('Attempt to realize bad height: ' + rowCount);
889
rgindac9bc5502012-01-18 11:48:44 -0800890 var deltaRows = rowCount - this.screen_.getHeight();
891
892 this.screenSize.height = rowCount;
893
894 var cursor = this.saveCursor();
895
896 if (deltaRows < 0) {
897 // Screen got smaller.
898 deltaRows *= -1;
899 while (deltaRows) {
900 var lastRow = this.getRowCount() - 1;
901 if (lastRow - this.scrollbackRows_.length == cursor.row)
902 break;
903
904 if (this.getRowText(lastRow))
905 break;
906
907 this.screen_.popRow();
908 deltaRows--;
909 }
910
911 var ary = this.screen_.shiftRows(deltaRows);
912 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
913
914 // We just removed rows from the top of the screen, we need to update
915 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800916 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800917 } else if (deltaRows > 0) {
918 // Screen got larger.
919
920 if (deltaRows <= this.scrollbackRows_.length) {
921 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
922 var rows = this.scrollbackRows_.splice(
923 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
924 this.screen_.unshiftRows(rows);
925 deltaRows -= scrollbackCount;
926 cursor.row += scrollbackCount;
927 }
928
929 if (deltaRows)
930 this.appendRows_(deltaRows);
931 }
932
rginda35c456b2012-02-09 17:29:05 -0800933 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800934 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800935};
936
937/**
938 * Scroll the terminal to the top of the scrollback buffer.
939 */
940hterm.Terminal.prototype.scrollHome = function() {
941 this.scrollPort_.scrollRowToTop(0);
942};
943
944/**
945 * Scroll the terminal to the end.
946 */
947hterm.Terminal.prototype.scrollEnd = function() {
948 this.scrollPort_.scrollRowToBottom(this.getRowCount());
949};
950
951/**
952 * Scroll the terminal one page up (minus one line) relative to the current
953 * position.
954 */
955hterm.Terminal.prototype.scrollPageUp = function() {
956 var i = this.scrollPort_.getTopRowIndex();
957 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
958};
959
960/**
961 * Scroll the terminal one page down (minus one line) relative to the current
962 * position.
963 */
964hterm.Terminal.prototype.scrollPageDown = function() {
965 var i = this.scrollPort_.getTopRowIndex();
966 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800967};
968
rgindac9bc5502012-01-18 11:48:44 -0800969/**
Robert Ginda40932892012-12-10 17:26:40 -0800970 * Clear primary screen, secondary screen, and the scrollback buffer.
971 */
972hterm.Terminal.prototype.wipeContents = function() {
973 this.scrollbackRows_.length = 0;
974 this.scrollPort_.resetCache();
975
976 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
977 var bottom = screen.getHeight();
978 if (bottom > 0) {
979 this.renumberRows_(0, bottom);
980 this.clearHome(screen);
981 }
982 }.bind(this));
983
984 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -0700985 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -0800986};
987
988/**
rgindac9bc5502012-01-18 11:48:44 -0800989 * Full terminal reset.
990 */
rginda87b86462011-12-14 13:48:03 -0800991hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800992 this.clearAllTabStops();
993 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -0700994
995 this.clearHome(this.primaryScreen_);
996 this.primaryScreen_.textAttributes.reset();
997
998 this.clearHome(this.alternateScreen_);
999 this.alternateScreen_.textAttributes.reset();
1000
rgindab8bc8932012-04-27 12:45:03 -07001001 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1002
Robert Ginda92e18102013-03-14 13:56:37 -07001003 this.vt.reset();
1004
rgindac9bc5502012-01-18 11:48:44 -08001005 this.softReset();
rginda87b86462011-12-14 13:48:03 -08001006};
1007
rgindac9bc5502012-01-18 11:48:44 -08001008/**
1009 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001010 *
1011 * Perform a soft reset to the default values listed in
1012 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001013 */
rginda0f5c0292012-01-13 11:00:13 -08001014hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -07001015 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001016 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001017
rgindab8bc8932012-04-27 12:45:03 -07001018 // Xterm also resets the color palette on soft reset, even though it doesn't
1019 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -07001020 this.primaryScreen_.textAttributes.resetColorPalette();
1021 this.alternateScreen_.textAttributes.resetColorPalette();
1022
rgindab8bc8932012-04-27 12:45:03 -07001023 // The xterm man page explicitly says this will happen on soft reset.
1024 this.setVTScrollRegion(null, null);
1025
1026 // Xterm also shows the cursor on soft reset, but does not alter the blink
1027 // state.
rgindaa19afe22012-01-25 15:40:22 -08001028 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001029};
1030
rgindac9bc5502012-01-18 11:48:44 -08001031/**
1032 * Move the cursor forward to the next tab stop, or to the last column
1033 * if no more tab stops are set.
1034 */
1035hterm.Terminal.prototype.forwardTabStop = function() {
1036 var column = this.screen_.cursorPosition.column;
1037
1038 for (var i = 0; i < this.tabStops_.length; i++) {
1039 if (this.tabStops_[i] > column) {
1040 this.setCursorColumn(this.tabStops_[i]);
1041 return;
1042 }
1043 }
1044
David Benjamin66e954d2012-05-05 21:08:12 -04001045 // xterm does not clear the overflow flag on HT or CHT.
1046 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001047 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001048 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001049};
1050
rgindac9bc5502012-01-18 11:48:44 -08001051/**
1052 * Move the cursor backward to the previous tab stop, or to the first column
1053 * if no previous tab stops are set.
1054 */
1055hterm.Terminal.prototype.backwardTabStop = function() {
1056 var column = this.screen_.cursorPosition.column;
1057
1058 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1059 if (this.tabStops_[i] < column) {
1060 this.setCursorColumn(this.tabStops_[i]);
1061 return;
1062 }
1063 }
1064
1065 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001066};
1067
rgindac9bc5502012-01-18 11:48:44 -08001068/**
1069 * Set a tab stop at the given column.
1070 *
1071 * @param {int} column Zero based column.
1072 */
1073hterm.Terminal.prototype.setTabStop = function(column) {
1074 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1075 if (this.tabStops_[i] == column)
1076 return;
1077
1078 if (this.tabStops_[i] < column) {
1079 this.tabStops_.splice(i + 1, 0, column);
1080 return;
1081 }
1082 }
1083
1084 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001085};
1086
rgindac9bc5502012-01-18 11:48:44 -08001087/**
1088 * Clear the tab stop at the current cursor position.
1089 *
1090 * No effect if there is no tab stop at the current cursor position.
1091 */
1092hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1093 var column = this.screen_.cursorPosition.column;
1094
1095 var i = this.tabStops_.indexOf(column);
1096 if (i == -1)
1097 return;
1098
1099 this.tabStops_.splice(i, 1);
1100};
1101
1102/**
1103 * Clear all tab stops.
1104 */
1105hterm.Terminal.prototype.clearAllTabStops = function() {
1106 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001107 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001108};
1109
1110/**
1111 * Set up the default tab stops, starting from a given column.
1112 *
1113 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001114 * from the specified column, or 0 if no column is provided. It also flags
1115 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001116 *
1117 * This does not clear the existing tab stops first, use clearAllTabStops
1118 * for that.
1119 *
1120 * @param {int} opt_start Optional starting zero based starting column, useful
1121 * for filling out missing tab stops when the terminal is resized.
1122 */
1123hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1124 var start = opt_start || 0;
1125 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001126 // Round start up to a default tab stop.
1127 start = start - 1 - ((start - 1) % w) + w;
1128 for (var i = start; i < this.screenSize.width; i += w) {
1129 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001130 }
David Benjamin66e954d2012-05-05 21:08:12 -04001131
1132 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001133};
1134
rginda6d397402012-01-17 10:58:29 -08001135/**
rginda8ba33642011-12-14 12:31:31 -08001136 * Interpret a sequence of characters.
1137 *
1138 * Incomplete escape sequences are buffered until the next call.
1139 *
1140 * @param {string} str Sequence of characters to interpret or pass through.
1141 */
1142hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001143 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001144 this.scheduleSyncCursorPosition_();
1145};
1146
1147/**
1148 * Take over the given DIV for use as the terminal display.
1149 *
1150 * @param {HTMLDivElement} div The div to use as the terminal display.
1151 */
1152hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001153 this.div_ = div;
1154
rginda8ba33642011-12-14 12:31:31 -08001155 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001156 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001157 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1158 this.scrollPort_.setBackgroundPosition(
1159 this.prefs_.get('background-position'));
Robert Gindae76aa9f2014-03-14 12:29:12 -07001160 this.scrollPort_.setUserCss(this.prefs_.get('user-css'));
rginda30f20f62012-04-05 16:36:19 -07001161
rginda0918b652012-04-04 11:26:24 -07001162 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001163
rginda9f5222b2012-03-05 11:53:28 -08001164 this.setFontSize(this.prefs_.get('font-size'));
1165 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001166
David Reveman8f552492012-03-28 12:18:41 -04001167 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001168 this.setScrollWheelMoveMultipler(
1169 this.prefs_.get('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001170
rginda8ba33642011-12-14 12:31:31 -08001171 this.document_ = this.scrollPort_.getDocument();
1172
rginda4bba5e12012-06-20 16:15:30 -07001173 this.document_.body.oncontextmenu = function() { return false };
1174
1175 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001176 var screenNode = this.scrollPort_.getScreenNode();
1177 screenNode.addEventListener('mousedown', onMouse);
1178 screenNode.addEventListener('mouseup', onMouse);
1179 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001180 this.scrollPort_.onScrollWheel = onMouse;
1181
Toni Barzic0bfa8922013-11-22 11:18:35 -08001182 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001183 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001184 // Listen for mousedown events on the screenNode as in FF the focus
1185 // events don't bubble.
1186 screenNode.addEventListener('mousedown', function() {
1187 setTimeout(this.onFocusChange_.bind(this, true));
1188 }.bind(this));
1189
Toni Barzic0bfa8922013-11-22 11:18:35 -08001190 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001191 'blur', this.onFocusChange_.bind(this, false));
1192
1193 var style = this.document_.createElement('style');
1194 style.textContent =
1195 ('.cursor-node[focus="false"] {' +
1196 ' box-sizing: border-box;' +
1197 ' background-color: transparent !important;' +
1198 ' border-width: 2px;' +
1199 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001200 '}' +
1201 '.wc-node {' +
1202 ' display: inline-block;' +
1203 ' text-align: center;' +
1204 ' width: ' + this.scrollPort_.characterSize.width * 2 + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001205 '}');
1206 this.document_.head.appendChild(style);
1207
Ricky Liang48f05cb2013-12-31 23:35:29 +08001208 var styleSheets = this.document_.styleSheets;
1209 var cssRules = styleSheets[styleSheets.length - 1].cssRules;
1210 this.wcCssRule_ = cssRules[cssRules.length - 1];
1211
rginda8ba33642011-12-14 12:31:31 -08001212 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001213 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001214 this.cursorNode_.style.cssText =
1215 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001216 'top: -99px;' +
1217 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001218 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1219 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
Rob Spies06533ba2014-04-24 11:20:37 -07001220 '-webkit-transition: opacity, background-color 100ms linear;' +
1221 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001222
rginda8e92a692012-05-20 19:37:20 -07001223 this.setCursorColor(this.prefs_.get('cursor-color'));
Robert Gindafb1be6a2013-12-11 11:56:22 -08001224 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1225 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001226
rginda8ba33642011-12-14 12:31:31 -08001227 this.document_.body.appendChild(this.cursorNode_);
1228
rgindad5613292012-06-19 15:40:37 -07001229 // When 'enableMouseDragScroll' is off we reposition this element directly
1230 // under the mouse cursor after a click. This makes Chrome associate
1231 // subsequent mousemove events with the scroll-blocker. Since the
1232 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1233 // events do not cause the scrollport to scroll.
1234 //
1235 // It's a hack, but it's the cleanest way I could find.
1236 this.scrollBlockerNode_ = this.document_.createElement('div');
1237 this.scrollBlockerNode_.style.cssText =
1238 ('position: absolute;' +
1239 'top: -99px;' +
1240 'display: block;' +
1241 'width: 10px;' +
1242 'height: 10px;');
1243 this.document_.body.appendChild(this.scrollBlockerNode_);
1244
1245 var onMouse = this.onMouse_.bind(this);
1246 this.scrollPort_.onScrollWheel = onMouse;
1247 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1248 ].forEach(function(event) {
1249 this.scrollBlockerNode_.addEventListener(event, onMouse);
1250 this.cursorNode_.addEventListener(event, onMouse);
1251 this.document_.addEventListener(event, onMouse);
1252 }.bind(this));
1253
1254 this.cursorNode_.addEventListener('mousedown', function() {
1255 setTimeout(this.focus.bind(this));
1256 }.bind(this));
1257
rginda8ba33642011-12-14 12:31:31 -08001258 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001259
rginda87b86462011-12-14 13:48:03 -08001260 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001261 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001262};
1263
rginda0918b652012-04-04 11:26:24 -07001264/**
1265 * Return the HTML document that contains the terminal DOM nodes.
1266 */
rginda87b86462011-12-14 13:48:03 -08001267hterm.Terminal.prototype.getDocument = function() {
1268 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001269};
1270
1271/**
rginda0918b652012-04-04 11:26:24 -07001272 * Focus the terminal.
1273 */
1274hterm.Terminal.prototype.focus = function() {
1275 this.scrollPort_.focus();
1276};
1277
1278/**
rginda8ba33642011-12-14 12:31:31 -08001279 * Return the HTML Element for a given row index.
1280 *
1281 * This is a method from the RowProvider interface. The ScrollPort uses
1282 * it to fetch rows on demand as they are scrolled into view.
1283 *
1284 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1285 * pairs to conserve memory.
1286 *
1287 * @param {integer} index The zero-based row index, measured relative to the
1288 * start of the scrollback buffer. On-screen rows will always have the
1289 * largest indicies.
1290 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1291 */
1292hterm.Terminal.prototype.getRowNode = function(index) {
1293 if (index < this.scrollbackRows_.length)
1294 return this.scrollbackRows_[index];
1295
1296 var screenIndex = index - this.scrollbackRows_.length;
1297 return this.screen_.rowsArray[screenIndex];
1298};
1299
1300/**
1301 * Return the text content for a given range of rows.
1302 *
1303 * This is a method from the RowProvider interface. The ScrollPort uses
1304 * it to fetch text content on demand when the user attempts to copy their
1305 * selection to the clipboard.
1306 *
1307 * @param {integer} start The zero-based row index to start from, measured
1308 * relative to the start of the scrollback buffer. On-screen rows will
1309 * always have the largest indicies.
1310 * @param {integer} end The zero-based row index to end on, measured
1311 * relative to the start of the scrollback buffer.
1312 * @return {string} A single string containing the text value of the range of
1313 * rows. Lines will be newline delimited, with no trailing newline.
1314 */
1315hterm.Terminal.prototype.getRowsText = function(start, end) {
1316 var ary = [];
1317 for (var i = start; i < end; i++) {
1318 var node = this.getRowNode(i);
1319 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001320 if (i < end - 1 && !node.getAttribute('line-overflow'))
1321 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001322 }
1323
rgindaa09e7332012-08-17 12:49:51 -07001324 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001325};
1326
1327/**
1328 * Return the text content for a given row.
1329 *
1330 * This is a method from the RowProvider interface. The ScrollPort uses
1331 * it to fetch text content on demand when the user attempts to copy their
1332 * selection to the clipboard.
1333 *
1334 * @param {integer} index The zero-based row index to return, measured
1335 * relative to the start of the scrollback buffer. On-screen rows will
1336 * always have the largest indicies.
1337 * @return {string} A string containing the text value of the selected row.
1338 */
1339hterm.Terminal.prototype.getRowText = function(index) {
1340 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001341 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001342};
1343
1344/**
1345 * Return the total number of rows in the addressable screen and in the
1346 * scrollback buffer of this terminal.
1347 *
1348 * This is a method from the RowProvider interface. The ScrollPort uses
1349 * it to compute the size of the scrollbar.
1350 *
1351 * @return {integer} The number of rows in this terminal.
1352 */
1353hterm.Terminal.prototype.getRowCount = function() {
1354 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1355};
1356
1357/**
1358 * Create DOM nodes for new rows and append them to the end of the terminal.
1359 *
1360 * This is the only correct way to add a new DOM node for a row. Notice that
1361 * the new row is appended to the bottom of the list of rows, and does not
1362 * require renumbering (of the rowIndex property) of previous rows.
1363 *
1364 * If you think you want a new blank row somewhere in the middle of the
1365 * terminal, look into moveRows_().
1366 *
1367 * This method does not pay attention to vtScrollTop/Bottom, since you should
1368 * be using moveRows() in cases where they would matter.
1369 *
1370 * The cursor will be positioned at column 0 of the first inserted line.
1371 */
1372hterm.Terminal.prototype.appendRows_ = function(count) {
1373 var cursorRow = this.screen_.rowsArray.length;
1374 var offset = this.scrollbackRows_.length + cursorRow;
1375 for (var i = 0; i < count; i++) {
1376 var row = this.document_.createElement('x-row');
1377 row.appendChild(this.document_.createTextNode(''));
1378 row.rowIndex = offset + i;
1379 this.screen_.pushRow(row);
1380 }
1381
1382 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1383 if (extraRows > 0) {
1384 var ary = this.screen_.shiftRows(extraRows);
1385 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001386 if (this.scrollPort_.isScrolledEnd)
1387 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001388 }
1389
1390 if (cursorRow >= this.screen_.rowsArray.length)
1391 cursorRow = this.screen_.rowsArray.length - 1;
1392
rginda87b86462011-12-14 13:48:03 -08001393 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001394};
1395
1396/**
1397 * Relocate rows from one part of the addressable screen to another.
1398 *
1399 * This is used to recycle rows during VT scrolls (those which are driven
1400 * by VT commands, rather than by the user manipulating the scrollbar.)
1401 *
1402 * In this case, the blank lines scrolled into the scroll region are made of
1403 * the nodes we scrolled off. These have their rowIndex properties carefully
1404 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001405 */
1406hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1407 var ary = this.screen_.removeRows(fromIndex, count);
1408 this.screen_.insertRows(toIndex, ary);
1409
1410 var start, end;
1411 if (fromIndex < toIndex) {
1412 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001413 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001414 } else {
1415 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001416 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001417 }
1418
1419 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001420 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001421};
1422
1423/**
1424 * Renumber the rowIndex property of the given range of rows.
1425 *
1426 * The start and end indicies are relative to the screen, not the scrollback.
1427 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001428 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001429 * no need to renumber scrollback rows.
1430 */
Robert Ginda40932892012-12-10 17:26:40 -08001431hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1432 var screen = opt_screen || this.screen_;
1433
rginda8ba33642011-12-14 12:31:31 -08001434 var offset = this.scrollbackRows_.length;
1435 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001436 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001437 }
1438};
1439
1440/**
1441 * Print a string to the terminal.
1442 *
1443 * This respects the current insert and wraparound modes. It will add new lines
1444 * to the end of the terminal, scrolling off the top into the scrollback buffer
1445 * if necessary.
1446 *
1447 * The string is *not* parsed for escape codes. Use the interpret() method if
1448 * that's what you're after.
1449 *
1450 * @param{string} str The string to print.
1451 */
1452hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001453 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001454
Ricky Liang48f05cb2013-12-31 23:35:29 +08001455 var strWidth = lib.wc.strWidth(str);
1456
1457 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001458 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1459 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001460 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001461 }
rgindaa19afe22012-01-25 15:40:22 -08001462
Ricky Liang48f05cb2013-12-31 23:35:29 +08001463 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001464 var didOverflow = false;
1465 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001466
rgindaa9abdd82012-08-06 18:05:09 -07001467 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1468 didOverflow = true;
1469 count = this.screenSize.width - this.screen_.cursorPosition.column;
1470 }
rgindaa19afe22012-01-25 15:40:22 -08001471
rgindaa9abdd82012-08-06 18:05:09 -07001472 if (didOverflow && !this.options_.wraparound) {
1473 // If the string overflowed the line but wraparound is off, then the
1474 // last printed character should be the last of the string.
1475 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001476 substr = lib.wc.substr(str, startOffset, count - 1) +
1477 lib.wc.substr(str, strWidth - 1);
1478 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001479 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001480 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001481 }
rgindaa19afe22012-01-25 15:40:22 -08001482
Ricky Liang48f05cb2013-12-31 23:35:29 +08001483 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1484 for (var i = 0; i < tokens.length; i++) {
1485 if (tokens[i].wcNode)
1486 this.screen_.textAttributes.wcNode = true;
1487
1488 if (this.options_.insertMode) {
1489 this.screen_.insertString(tokens[i].str);
1490 } else {
1491 this.screen_.overwriteString(tokens[i].str);
1492 }
1493 this.screen_.textAttributes.wcNode = false;
rgindaa9abdd82012-08-06 18:05:09 -07001494 }
1495
1496 this.screen_.maybeClipCurrentRow();
1497 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001498 }
rginda8ba33642011-12-14 12:31:31 -08001499
1500 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001501
rginda9f5222b2012-03-05 11:53:28 -08001502 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001503 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001504};
1505
1506/**
rginda87b86462011-12-14 13:48:03 -08001507 * Set the VT scroll region.
1508 *
rginda87b86462011-12-14 13:48:03 -08001509 * This also resets the cursor position to the absolute (0, 0) position, since
1510 * that's what xterm appears to do.
1511 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001512 * Setting the scroll region to the full height of the terminal will clear
1513 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1514 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1515 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1516 * continue to work as most users would expect.
1517 *
rginda87b86462011-12-14 13:48:03 -08001518 * @param {integer} scrollTop The zero-based top of the scroll region.
1519 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1520 * inclusive.
1521 */
1522hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001523 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001524 this.vtScrollTop_ = null;
1525 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001526 } else {
1527 this.vtScrollTop_ = scrollTop;
1528 this.vtScrollBottom_ = scrollBottom;
1529 }
rginda87b86462011-12-14 13:48:03 -08001530};
1531
1532/**
rginda8ba33642011-12-14 12:31:31 -08001533 * Return the top row index according to the VT.
1534 *
1535 * This will return 0 unless the terminal has been told to restrict scrolling
1536 * to some lower row. It is used for some VT cursor positioning and scrolling
1537 * commands.
1538 *
1539 * @return {integer} The topmost row in the terminal's scroll region.
1540 */
1541hterm.Terminal.prototype.getVTScrollTop = function() {
1542 if (this.vtScrollTop_ != null)
1543 return this.vtScrollTop_;
1544
1545 return 0;
rginda87b86462011-12-14 13:48:03 -08001546};
rginda8ba33642011-12-14 12:31:31 -08001547
1548/**
1549 * Return the bottom row index according to the VT.
1550 *
1551 * This will return the height of the terminal unless the it has been told to
1552 * restrict scrolling to some higher row. It is used for some VT cursor
1553 * positioning and scrolling commands.
1554 *
1555 * @return {integer} The bottommost row in the terminal's scroll region.
1556 */
1557hterm.Terminal.prototype.getVTScrollBottom = function() {
1558 if (this.vtScrollBottom_ != null)
1559 return this.vtScrollBottom_;
1560
rginda87b86462011-12-14 13:48:03 -08001561 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001562}
1563
1564/**
1565 * Process a '\n' character.
1566 *
1567 * If the cursor is on the final row of the terminal this will append a new
1568 * blank row to the screen and scroll the topmost row into the scrollback
1569 * buffer.
1570 *
1571 * Otherwise, this moves the cursor to column zero of the next row.
1572 */
1573hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001574 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1575 this.screen_.rowsArray.length - 1);
1576
1577 if (this.vtScrollBottom_ != null) {
1578 // A VT Scroll region is active, we never append new rows.
1579 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1580 // We're at the end of the VT Scroll Region, perform a VT scroll.
1581 this.vtScrollUp(1);
1582 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1583 } else if (cursorAtEndOfScreen) {
1584 // We're at the end of the screen, the only thing to do is put the
1585 // cursor to column 0.
1586 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1587 } else {
1588 // Anywhere else, advance the cursor row, and reset the column.
1589 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1590 }
1591 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001592 // We're at the end of the screen. Append a new row to the terminal,
1593 // shifting the top row into the scrollback.
1594 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001595 } else {
rginda87b86462011-12-14 13:48:03 -08001596 // Anywhere else in the screen just moves the cursor.
1597 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001598 }
1599};
1600
1601/**
1602 * Like newLine(), except maintain the cursor column.
1603 */
1604hterm.Terminal.prototype.lineFeed = function() {
1605 var column = this.screen_.cursorPosition.column;
1606 this.newLine();
1607 this.setCursorColumn(column);
1608};
1609
1610/**
rginda87b86462011-12-14 13:48:03 -08001611 * If autoCarriageReturn is set then newLine(), else lineFeed().
1612 */
1613hterm.Terminal.prototype.formFeed = function() {
1614 if (this.options_.autoCarriageReturn) {
1615 this.newLine();
1616 } else {
1617 this.lineFeed();
1618 }
1619};
1620
1621/**
1622 * Move the cursor up one row, possibly inserting a blank line.
1623 *
1624 * The cursor column is not changed.
1625 */
1626hterm.Terminal.prototype.reverseLineFeed = function() {
1627 var scrollTop = this.getVTScrollTop();
1628 var currentRow = this.screen_.cursorPosition.row;
1629
1630 if (currentRow == scrollTop) {
1631 this.insertLines(1);
1632 } else {
1633 this.setAbsoluteCursorRow(currentRow - 1);
1634 }
1635};
1636
1637/**
rginda8ba33642011-12-14 12:31:31 -08001638 * Replace all characters to the left of the current cursor with the space
1639 * character.
1640 *
1641 * TODO(rginda): This should probably *remove* the characters (not just replace
1642 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001643 * position.
rginda8ba33642011-12-14 12:31:31 -08001644 */
1645hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001646 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001647 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001648 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001649 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001650};
1651
1652/**
David Benjamin684a9b72012-05-01 17:19:58 -04001653 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001654 *
1655 * The cursor position is unchanged.
1656 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001657 * If the current background color is not the default background color this
1658 * will insert spaces rather than delete. This is unfortunate because the
1659 * trailing space will affect text selection, but it's difficult to come up
1660 * with a way to style empty space that wouldn't trip up the hterm.Screen
1661 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001662 *
1663 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1664 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1665 * crbug.com/232390 for details.
rginda8ba33642011-12-14 12:31:31 -08001666 */
1667hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001668 if (this.screen_.cursorPosition.overflow)
1669 return;
1670
Robert Ginda7fd57082012-09-25 14:41:47 -07001671 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1672 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001673
1674 if (this.screen_.textAttributes.background ===
1675 this.screen_.textAttributes.DEFAULT_COLOR) {
1676 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08001677 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07001678 this.screen_.cursorPosition.column + count) {
1679 this.screen_.deleteChars(count);
1680 this.clearCursorOverflow();
1681 return;
1682 }
1683 }
1684
rginda87b86462011-12-14 13:48:03 -08001685 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001686 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001687 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001688 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001689};
1690
1691/**
1692 * Erase the current line.
1693 *
1694 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001695 */
1696hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001697 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001698 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001699 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001700 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001701};
1702
1703/**
David Benjamina08d78f2012-05-05 00:28:49 -04001704 * Erase all characters from the start of the screen to the current cursor
1705 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001706 *
1707 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001708 */
1709hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001710 var cursor = this.saveCursor();
1711
1712 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001713
David Benjamina08d78f2012-05-05 00:28:49 -04001714 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001715 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001716 this.screen_.clearCursorRow();
1717 }
1718
rginda87b86462011-12-14 13:48:03 -08001719 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001720 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001721};
1722
1723/**
1724 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001725 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001726 *
1727 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001728 */
1729hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001730 var cursor = this.saveCursor();
1731
1732 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001733
David Benjamina08d78f2012-05-05 00:28:49 -04001734 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001735 for (var i = cursor.row + 1; i <= bottom; i++) {
1736 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001737 this.screen_.clearCursorRow();
1738 }
1739
rginda87b86462011-12-14 13:48:03 -08001740 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001741 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001742};
1743
1744/**
1745 * Fill the terminal with a given character.
1746 *
1747 * This methods does not respect the VT scroll region.
1748 *
1749 * @param {string} ch The character to use for the fill.
1750 */
1751hterm.Terminal.prototype.fill = function(ch) {
1752 var cursor = this.saveCursor();
1753
1754 this.setAbsoluteCursorPosition(0, 0);
1755 for (var row = 0; row < this.screenSize.height; row++) {
1756 for (var col = 0; col < this.screenSize.width; col++) {
1757 this.setAbsoluteCursorPosition(row, col);
1758 this.screen_.overwriteString(ch);
1759 }
1760 }
1761
1762 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001763};
1764
1765/**
rginda9ea433c2012-03-16 11:57:00 -07001766 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001767 *
rginda9ea433c2012-03-16 11:57:00 -07001768 * This does not respect the scroll region.
1769 *
1770 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1771 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001772 */
rginda9ea433c2012-03-16 11:57:00 -07001773hterm.Terminal.prototype.clearHome = function(opt_screen) {
1774 var screen = opt_screen || this.screen_;
1775 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001776
rginda11057d52012-04-25 12:29:56 -07001777 if (bottom == 0) {
1778 // Empty screen, nothing to do.
1779 return;
1780 }
1781
rgindae4d29232012-01-19 10:47:13 -08001782 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001783 screen.setCursorPosition(i, 0);
1784 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001785 }
1786
rginda9ea433c2012-03-16 11:57:00 -07001787 screen.setCursorPosition(0, 0);
1788};
1789
1790/**
1791 * Erase the entire display without changing the cursor position.
1792 *
1793 * The cursor position is unchanged. This does not respect the scroll
1794 * region.
1795 *
1796 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1797 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001798 */
1799hterm.Terminal.prototype.clear = function(opt_screen) {
1800 var screen = opt_screen || this.screen_;
1801 var cursor = screen.cursorPosition.clone();
1802 this.clearHome(screen);
1803 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001804};
1805
1806/**
1807 * VT command to insert lines at the current cursor row.
1808 *
1809 * This respects the current scroll region. Rows pushed off the bottom are
1810 * lost (they won't show up in the scrollback buffer).
1811 *
rginda8ba33642011-12-14 12:31:31 -08001812 * @param {integer} count The number of lines to insert.
1813 */
1814hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07001815 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08001816
1817 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07001818 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08001819
Robert Ginda579186b2012-09-26 11:40:04 -07001820 // The moveCount is the number of rows we need to relocate to make room for
1821 // the new row(s). The count is the distance to move them.
1822 var moveCount = bottom - cursorRow - count + 1;
1823 if (moveCount)
1824 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08001825
Robert Ginda579186b2012-09-26 11:40:04 -07001826 for (var i = count - 1; i >= 0; i--) {
1827 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001828 this.screen_.clearCursorRow();
1829 }
rginda8ba33642011-12-14 12:31:31 -08001830};
1831
1832/**
1833 * VT command to delete lines at the current cursor row.
1834 *
1835 * New rows are added to the bottom of scroll region to take their place. New
1836 * rows are strictly there to take up space and have no content or style.
1837 */
1838hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001839 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001840
rginda87b86462011-12-14 13:48:03 -08001841 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001842 var bottom = this.getVTScrollBottom();
1843
rginda87b86462011-12-14 13:48:03 -08001844 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001845 count = Math.min(count, maxCount);
1846
rginda87b86462011-12-14 13:48:03 -08001847 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001848 if (count != maxCount)
1849 this.moveRows_(top, count, moveStart);
1850
1851 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001852 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001853 this.screen_.clearCursorRow();
1854 }
1855
rginda87b86462011-12-14 13:48:03 -08001856 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001857 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001858};
1859
1860/**
1861 * Inserts the given number of spaces at the current cursor position.
1862 *
rginda87b86462011-12-14 13:48:03 -08001863 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001864 */
1865hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001866 var cursor = this.saveCursor();
1867
rgindacbbd7482012-06-13 15:06:16 -07001868 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001869 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001870 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001871
1872 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001873 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001874};
1875
1876/**
1877 * Forward-delete the specified number of characters starting at the cursor
1878 * position.
1879 *
1880 * @param {integer} count The number of characters to delete.
1881 */
1882hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001883 var deleted = this.screen_.deleteChars(count);
1884 if (deleted && !this.screen_.textAttributes.isDefault()) {
1885 var cursor = this.saveCursor();
1886 this.setCursorColumn(this.screenSize.width - deleted);
1887 this.screen_.insertString(lib.f.getWhitespace(deleted));
1888 this.restoreCursor(cursor);
1889 }
1890
David Benjamin54e8bf62012-06-01 22:31:40 -04001891 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001892};
1893
1894/**
1895 * Shift rows in the scroll region upwards by a given number of lines.
1896 *
1897 * New rows are inserted at the bottom of the scroll region to fill the
1898 * vacated rows. The new rows not filled out with the current text attributes.
1899 *
1900 * This function does not affect the scrollback rows at all. Rows shifted
1901 * off the top are lost.
1902 *
rginda87b86462011-12-14 13:48:03 -08001903 * The cursor position is not altered.
1904 *
rginda8ba33642011-12-14 12:31:31 -08001905 * @param {integer} count The number of rows to scroll.
1906 */
1907hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001908 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001909
rginda87b86462011-12-14 13:48:03 -08001910 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001911 this.deleteLines(count);
1912
rginda87b86462011-12-14 13:48:03 -08001913 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001914};
1915
1916/**
1917 * Shift rows below the cursor down by a given number of lines.
1918 *
1919 * This function respects the current scroll region.
1920 *
1921 * New rows are inserted at the top of the scroll region to fill the
1922 * vacated rows. The new rows not filled out with the current text attributes.
1923 *
1924 * This function does not affect the scrollback rows at all. Rows shifted
1925 * off the bottom are lost.
1926 *
1927 * @param {integer} count The number of rows to scroll.
1928 */
1929hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001930 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001931
rginda87b86462011-12-14 13:48:03 -08001932 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001933 this.insertLines(opt_count);
1934
rginda87b86462011-12-14 13:48:03 -08001935 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001936};
1937
rginda87b86462011-12-14 13:48:03 -08001938
rginda8ba33642011-12-14 12:31:31 -08001939/**
1940 * Set the cursor position.
1941 *
1942 * The cursor row is relative to the scroll region if the terminal has
1943 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1944 *
1945 * @param {integer} row The new zero-based cursor row.
1946 * @param {integer} row The new zero-based cursor column.
1947 */
1948hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1949 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001950 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001951 } else {
rginda87b86462011-12-14 13:48:03 -08001952 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001953 }
rginda87b86462011-12-14 13:48:03 -08001954};
rginda8ba33642011-12-14 12:31:31 -08001955
rginda87b86462011-12-14 13:48:03 -08001956hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1957 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07001958 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
1959 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001960 this.screen_.setCursorPosition(row, column);
1961};
1962
1963hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07001964 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
1965 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001966 this.screen_.setCursorPosition(row, column);
1967};
1968
1969/**
1970 * Set the cursor column.
1971 *
1972 * @param {integer} column The new zero-based cursor column.
1973 */
1974hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001975 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001976};
1977
1978/**
1979 * Return the cursor column.
1980 *
1981 * @return {integer} The zero-based cursor column.
1982 */
1983hterm.Terminal.prototype.getCursorColumn = function() {
1984 return this.screen_.cursorPosition.column;
1985};
1986
1987/**
1988 * Set the cursor row.
1989 *
1990 * The cursor row is relative to the scroll region if the terminal has
1991 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1992 *
1993 * @param {integer} row The new cursor row.
1994 */
rginda87b86462011-12-14 13:48:03 -08001995hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1996 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001997};
1998
1999/**
2000 * Return the cursor row.
2001 *
2002 * @return {integer} The zero-based cursor row.
2003 */
2004hterm.Terminal.prototype.getCursorRow = function(row) {
2005 return this.screen_.cursorPosition.row;
2006};
2007
2008/**
2009 * Request that the ScrollPort redraw itself soon.
2010 *
2011 * The redraw will happen asynchronously, soon after the call stack winds down.
2012 * Multiple calls will be coalesced into a single redraw.
2013 */
2014hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002015 if (this.timeouts_.redraw)
2016 return;
rginda8ba33642011-12-14 12:31:31 -08002017
2018 var self = this;
rginda87b86462011-12-14 13:48:03 -08002019 this.timeouts_.redraw = setTimeout(function() {
2020 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002021 self.scrollPort_.redraw_();
2022 }, 0);
2023};
2024
2025/**
2026 * Request that the ScrollPort be scrolled to the bottom.
2027 *
2028 * The scroll will happen asynchronously, soon after the call stack winds down.
2029 * Multiple calls will be coalesced into a single scroll.
2030 *
2031 * This affects the scrollbar position of the ScrollPort, and has nothing to
2032 * do with the VT scroll commands.
2033 */
2034hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2035 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002036 return;
rginda8ba33642011-12-14 12:31:31 -08002037
2038 var self = this;
2039 this.timeouts_.scrollDown = setTimeout(function() {
2040 delete self.timeouts_.scrollDown;
2041 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2042 }, 10);
2043};
2044
2045/**
2046 * Move the cursor up a specified number of rows.
2047 *
2048 * @param {integer} count The number of rows to move the cursor.
2049 */
2050hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002051 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002052};
2053
2054/**
2055 * Move the cursor down a specified number of rows.
2056 *
2057 * @param {integer} count The number of rows to move the cursor.
2058 */
2059hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002060 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002061 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2062 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2063 this.screenSize.height - 1);
2064
rgindacbbd7482012-06-13 15:06:16 -07002065 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002066 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002067 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002068};
2069
2070/**
2071 * Move the cursor left a specified number of columns.
2072 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002073 * If reverse wraparound mode is enabled and the previous row wrapped into
2074 * the current row then we back up through the wraparound as well.
2075 *
rginda8ba33642011-12-14 12:31:31 -08002076 * @param {integer} count The number of columns to move the cursor.
2077 */
2078hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002079 count = count || 1;
2080
2081 if (count < 1)
2082 return;
2083
2084 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002085 if (this.options_.reverseWraparound) {
2086 if (this.screen_.cursorPosition.overflow) {
2087 // If this cursor is in the right margin, consume one count to get it
2088 // back to the last column. This only applies when we're in reverse
2089 // wraparound mode.
2090 count--;
2091 this.clearCursorOverflow();
2092
2093 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002094 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002095 }
2096
Robert Gindabfb32622014-07-17 13:20:27 -07002097 var newRow = this.screen_.cursorPosition.row;
2098 var newColumn = currentColumn - count;
2099 if (newColumn < 0) {
2100 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2101 if (newRow < 0) {
2102 // xterm also wraps from row 0 to the last row.
2103 newRow = this.screenSize.height + newRow % this.screenSize.height;
2104 }
2105 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2106 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002107
Robert Gindabfb32622014-07-17 13:20:27 -07002108 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2109
2110 } else {
2111 var newColumn = Math.max(currentColumn - count, 0);
2112 this.setCursorColumn(newColumn);
2113 }
rginda8ba33642011-12-14 12:31:31 -08002114};
2115
2116/**
2117 * Move the cursor right a specified number of columns.
2118 *
2119 * @param {integer} count The number of columns to move the cursor.
2120 */
2121hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002122 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002123
2124 if (count < 1)
2125 return;
2126
rgindacbbd7482012-06-13 15:06:16 -07002127 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002128 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002129 this.setCursorColumn(column);
2130};
2131
2132/**
2133 * Reverse the foreground and background colors of the terminal.
2134 *
2135 * This only affects text that was drawn with no attributes.
2136 *
2137 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2138 * been drawn with attributes that happen to coincide with the default
2139 * 'no-attribute' colors. My guess is probably not.
2140 */
2141hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002142 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002143 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002144 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2145 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002146 } else {
rginda9f5222b2012-03-05 11:53:28 -08002147 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2148 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002149 }
2150};
2151
2152/**
rginda87b86462011-12-14 13:48:03 -08002153 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002154 *
2155 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002156 */
2157hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002158 this.cursorNode_.style.backgroundColor =
2159 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002160
2161 var self = this;
2162 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08002163 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08002164 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002165
Michael Kelly485ecd12014-06-09 11:41:56 -04002166 // bellSquelchTimeout_ affects both audio and notification bells.
2167 if (this.bellSquelchTimeout_)
2168 return;
2169
Robert Ginda92e18102013-03-14 13:56:37 -07002170 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002171 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002172 this.bellSequelchTimeout_ = setTimeout(function() {
2173 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002174 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002175 } else {
2176 delete this.bellSquelchTimeout_;
2177 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002178
2179 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
2180 var n = new Notification(
2181 lib.f.replaceVars(hterm.desktopNotificationTitle,
Robert Ginda348dc2b2014-06-24 14:42:23 -07002182 {'title': window.document.title || 'hterm'}));
Michael Kelly485ecd12014-06-09 11:41:56 -04002183 this.bellNotificationList_.push(n);
2184 // TODO: Should we try to raise the window here?
2185 n.onclick = function() { self.closeBellNotifications_(); };
2186 }
rginda87b86462011-12-14 13:48:03 -08002187};
2188
2189/**
rginda8ba33642011-12-14 12:31:31 -08002190 * Set the origin mode bit.
2191 *
2192 * If origin mode is on, certain VT cursor and scrolling commands measure their
2193 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2194 * to the top of the addressable screen.
2195 *
2196 * Defaults to off.
2197 *
2198 * @param {boolean} state True to set origin mode, false to unset.
2199 */
2200hterm.Terminal.prototype.setOriginMode = function(state) {
2201 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002202 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002203};
2204
2205/**
2206 * Set the insert mode bit.
2207 *
2208 * If insert mode is on, existing text beyond the cursor position will be
2209 * shifted right to make room for new text. Otherwise, new text overwrites
2210 * any existing text.
2211 *
2212 * Defaults to off.
2213 *
2214 * @param {boolean} state True to set insert mode, false to unset.
2215 */
2216hterm.Terminal.prototype.setInsertMode = function(state) {
2217 this.options_.insertMode = state;
2218};
2219
2220/**
rginda87b86462011-12-14 13:48:03 -08002221 * Set the auto carriage return bit.
2222 *
2223 * If auto carriage return is on then a formfeed character is interpreted
2224 * as a newline, otherwise it's the same as a linefeed. The difference boils
2225 * down to whether or not the cursor column is reset.
2226 */
2227hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2228 this.options_.autoCarriageReturn = state;
2229};
2230
2231/**
rginda8ba33642011-12-14 12:31:31 -08002232 * Set the wraparound mode bit.
2233 *
2234 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2235 * to the start of the following row. Otherwise, the cursor is clamped to the
2236 * end of the screen and attempts to write past it are ignored.
2237 *
2238 * Defaults to on.
2239 *
2240 * @param {boolean} state True to set wraparound mode, false to unset.
2241 */
2242hterm.Terminal.prototype.setWraparound = function(state) {
2243 this.options_.wraparound = state;
2244};
2245
2246/**
2247 * Set the reverse-wraparound mode bit.
2248 *
2249 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2250 * to the end of the previous row. Otherwise, the cursor is clamped to column
2251 * 0.
2252 *
2253 * Defaults to off.
2254 *
2255 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2256 */
2257hterm.Terminal.prototype.setReverseWraparound = function(state) {
2258 this.options_.reverseWraparound = state;
2259};
2260
2261/**
2262 * Selects between the primary and alternate screens.
2263 *
2264 * If alternate mode is on, the alternate screen is active. Otherwise the
2265 * primary screen is active.
2266 *
2267 * Swapping screens has no effect on the scrollback buffer.
2268 *
2269 * Each screen maintains its own cursor position.
2270 *
2271 * Defaults to off.
2272 *
2273 * @param {boolean} state True to set alternate mode, false to unset.
2274 */
2275hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002276 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002277 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2278
rginda35c456b2012-02-09 17:29:05 -08002279 if (this.screen_.rowsArray.length &&
2280 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2281 // If the screen changed sizes while we were away, our rowIndexes may
2282 // be incorrect.
2283 var offset = this.scrollbackRows_.length;
2284 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002285 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002286 ary[i].rowIndex = offset + i;
2287 }
2288 }
rginda8ba33642011-12-14 12:31:31 -08002289
rginda35c456b2012-02-09 17:29:05 -08002290 this.realizeWidth_(this.screenSize.width);
2291 this.realizeHeight_(this.screenSize.height);
2292 this.scrollPort_.syncScrollHeight();
2293 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002294
rginda6d397402012-01-17 10:58:29 -08002295 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002296 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002297};
2298
2299/**
2300 * Set the cursor-blink mode bit.
2301 *
2302 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2303 * a visible cursor does not blink.
2304 *
2305 * You should make sure to turn blinking off if you're going to dispose of a
2306 * terminal, otherwise you'll leak a timeout.
2307 *
2308 * Defaults to on.
2309 *
2310 * @param {boolean} state True to set cursor-blink mode, false to unset.
2311 */
2312hterm.Terminal.prototype.setCursorBlink = function(state) {
2313 this.options_.cursorBlink = state;
2314
2315 if (!state && this.timeouts_.cursorBlink) {
2316 clearTimeout(this.timeouts_.cursorBlink);
2317 delete this.timeouts_.cursorBlink;
2318 }
2319
2320 if (this.options_.cursorVisible)
2321 this.setCursorVisible(true);
2322};
2323
2324/**
2325 * Set the cursor-visible mode bit.
2326 *
2327 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2328 *
2329 * Defaults to on.
2330 *
2331 * @param {boolean} state True to set cursor-visible mode, false to unset.
2332 */
2333hterm.Terminal.prototype.setCursorVisible = function(state) {
2334 this.options_.cursorVisible = state;
2335
2336 if (!state) {
rginda87b86462011-12-14 13:48:03 -08002337 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002338 return;
2339 }
2340
rginda87b86462011-12-14 13:48:03 -08002341 this.syncCursorPosition_();
2342
2343 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002344
2345 if (this.options_.cursorBlink) {
2346 if (this.timeouts_.cursorBlink)
2347 return;
2348
Robert Gindaea2183e2014-07-17 09:51:51 -07002349 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002350 } else {
2351 if (this.timeouts_.cursorBlink) {
2352 clearTimeout(this.timeouts_.cursorBlink);
2353 delete this.timeouts_.cursorBlink;
2354 }
2355 }
2356};
2357
2358/**
rginda87b86462011-12-14 13:48:03 -08002359 * Synchronizes the visible cursor and document selection with the current
2360 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002361 */
2362hterm.Terminal.prototype.syncCursorPosition_ = function() {
2363 var topRowIndex = this.scrollPort_.getTopRowIndex();
2364 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2365 var cursorRowIndex = this.scrollbackRows_.length +
2366 this.screen_.cursorPosition.row;
2367
2368 if (cursorRowIndex > bottomRowIndex) {
2369 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002370 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002371 return;
2372 }
2373
Robert Gindab837c052014-08-11 11:17:51 -07002374 if (this.options_.cursorVisible &&
2375 this.cursorNode_.style.display == 'none') {
2376 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2377 this.cursorNode_.style.display = '';
2378 }
2379
2380
rginda8ba33642011-12-14 12:31:31 -08002381 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002382 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2383 'px';
2384 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2385 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002386
2387 this.cursorNode_.setAttribute('title',
2388 '(' + this.screen_.cursorPosition.row +
2389 ', ' + this.screen_.cursorPosition.column +
2390 ')');
2391
2392 // Update the caret for a11y purposes.
2393 var selection = this.document_.getSelection();
2394 if (selection && selection.isCollapsed)
2395 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002396};
2397
Robert Gindafb1be6a2013-12-11 11:56:22 -08002398/**
2399 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2400 * and character cell dimensions.
2401 */
Robert Ginda830583c2013-08-07 13:20:46 -07002402hterm.Terminal.prototype.restyleCursor_ = function() {
2403 var shape = this.cursorShape_;
2404
2405 if (this.cursorNode_.getAttribute('focus') == 'false') {
2406 // Always show a block cursor when unfocused.
2407 shape = hterm.Terminal.cursorShape.BLOCK;
2408 }
2409
2410 var style = this.cursorNode_.style;
2411
Robert Gindafb1be6a2013-12-11 11:56:22 -08002412 style.width = this.scrollPort_.characterSize.width + 'px';
2413
Robert Ginda830583c2013-08-07 13:20:46 -07002414 switch (shape) {
2415 case hterm.Terminal.cursorShape.BEAM:
2416 style.height = this.scrollPort_.characterSize.height + 'px';
2417 style.backgroundColor = 'transparent';
2418 style.borderBottomStyle = null;
2419 style.borderLeftStyle = 'solid';
2420 break;
2421
2422 case hterm.Terminal.cursorShape.UNDERLINE:
2423 style.height = this.scrollPort_.characterSize.baseline + 'px';
2424 style.backgroundColor = 'transparent';
2425 style.borderBottomStyle = 'solid';
2426 // correct the size to put it exactly at the baseline
2427 style.borderLeftStyle = null;
2428 break;
2429
2430 default:
2431 style.height = this.scrollPort_.characterSize.height + 'px';
2432 style.backgroundColor = this.cursorColor_;
2433 style.borderBottomStyle = null;
2434 style.borderLeftStyle = null;
2435 break;
2436 }
2437};
2438
rginda8ba33642011-12-14 12:31:31 -08002439/**
2440 * Synchronizes the visible cursor with the current cursor coordinates.
2441 *
2442 * The sync will happen asynchronously, soon after the call stack winds down.
2443 * Multiple calls will be coalesced into a single sync.
2444 */
2445hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2446 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002447 return;
rginda8ba33642011-12-14 12:31:31 -08002448
2449 var self = this;
2450 this.timeouts_.syncCursor = setTimeout(function() {
2451 self.syncCursorPosition_();
2452 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002453 }, 0);
2454};
2455
rgindacc2996c2012-02-24 14:59:31 -08002456/**
rgindaf522ce02012-04-17 17:49:17 -07002457 * Show or hide the zoom warning.
2458 *
2459 * The zoom warning is a message warning the user that their browser zoom must
2460 * be set to 100% in order for hterm to function properly.
2461 *
2462 * @param {boolean} state True to show the message, false to hide it.
2463 */
2464hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2465 if (!this.zoomWarningNode_) {
2466 if (!state)
2467 return;
2468
2469 this.zoomWarningNode_ = this.document_.createElement('div');
2470 this.zoomWarningNode_.style.cssText = (
2471 'color: black;' +
2472 'background-color: #ff2222;' +
2473 'font-size: large;' +
2474 'border-radius: 8px;' +
2475 'opacity: 0.75;' +
2476 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2477 'top: 0.5em;' +
2478 'right: 1.2em;' +
2479 'position: absolute;' +
2480 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002481 '-webkit-user-select: none;' +
2482 '-moz-text-size-adjust: none;' +
2483 '-moz-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002484 }
2485
Robert Gindab4839c22013-02-28 16:52:10 -08002486 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2487 hterm.zoomWarningMessage,
2488 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2489
rgindaf522ce02012-04-17 17:49:17 -07002490 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2491
2492 if (state) {
2493 if (!this.zoomWarningNode_.parentNode)
2494 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2495 } else if (this.zoomWarningNode_.parentNode) {
2496 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2497 }
2498};
2499
2500/**
rgindacc2996c2012-02-24 14:59:31 -08002501 * Show the terminal overlay for a given amount of time.
2502 *
2503 * The terminal overlay appears in inverse video in a large font, centered
2504 * over the terminal. You should probably keep the overlay message brief,
2505 * since it's in a large font and you probably aren't going to check the size
2506 * of the terminal first.
2507 *
2508 * @param {string} msg The text (not HTML) message to display in the overlay.
2509 * @param {number} opt_timeout The amount of time to wait before fading out
2510 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2511 * stay up forever (or until the next overlay).
2512 */
2513hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002514 if (!this.overlayNode_) {
2515 if (!this.div_)
2516 return;
2517
2518 this.overlayNode_ = this.document_.createElement('div');
2519 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002520 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002521 'font-size: xx-large;' +
2522 'opacity: 0.75;' +
2523 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2524 'position: absolute;' +
2525 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002526 '-webkit-transition: opacity 180ms ease-in;' +
2527 '-moz-user-select: none;' +
2528 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002529
2530 this.overlayNode_.addEventListener('mousedown', function(e) {
2531 e.preventDefault();
2532 e.stopPropagation();
2533 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002534 }
2535
rginda9f5222b2012-03-05 11:53:28 -08002536 this.overlayNode_.style.color = this.prefs_.get('background-color');
2537 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2538 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2539
rgindaf0090c92012-02-10 14:58:52 -08002540 this.overlayNode_.textContent = msg;
2541 this.overlayNode_.style.opacity = '0.75';
2542
2543 if (!this.overlayNode_.parentNode)
2544 this.div_.appendChild(this.overlayNode_);
2545
Robert Ginda97769282013-02-01 15:30:30 -08002546 var divSize = hterm.getClientSize(this.div_);
2547 var overlaySize = hterm.getClientSize(this.overlayNode_);
2548
Robert Ginda8a59f762014-07-23 11:29:55 -07002549 this.overlayNode_.style.top =
2550 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08002551 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07002552 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08002553
2554 var self = this;
2555
2556 if (this.overlayTimeout_)
2557 clearTimeout(this.overlayTimeout_);
2558
rgindacc2996c2012-02-24 14:59:31 -08002559 if (opt_timeout === null)
2560 return;
2561
rgindaf0090c92012-02-10 14:58:52 -08002562 this.overlayTimeout_ = setTimeout(function() {
2563 self.overlayNode_.style.opacity = '0';
Robert Ginda70926e42013-11-25 14:56:36 -08002564 self.overlayTimeout_ = setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002565 if (self.overlayNode_.parentNode)
2566 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002567 self.overlayTimeout_ = null;
2568 self.overlayNode_.style.opacity = '0.75';
2569 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002570 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002571};
2572
rginda4bba5e12012-06-20 16:15:30 -07002573/**
2574 * Paste from the system clipboard to the terminal.
2575 */
2576hterm.Terminal.prototype.paste = function() {
2577 hterm.pasteFromClipboard(this.document_);
2578};
2579
2580/**
2581 * Copy a string to the system clipboard.
2582 *
2583 * Note: If there is a selected range in the terminal, it'll be cleared.
2584 */
2585hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002586 if (this.prefs_.get('enable-clipboard-notice'))
2587 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2588
rgindaa09e7332012-08-17 12:49:51 -07002589 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002590 copySource.textContent = str;
2591 copySource.style.cssText = (
2592 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002593 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07002594 'position: absolute;' +
2595 'top: -99px');
2596
2597 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002598
rginda4bba5e12012-06-20 16:15:30 -07002599 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002600 var anchorNode = selection.anchorNode;
2601 var anchorOffset = selection.anchorOffset;
2602 var focusNode = selection.focusNode;
2603 var focusOffset = selection.focusOffset;
2604
rginda4bba5e12012-06-20 16:15:30 -07002605 selection.selectAllChildren(copySource);
2606
rgindaa09e7332012-08-17 12:49:51 -07002607 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002608
Rob Spies56953412014-04-28 14:09:47 -07002609 // IE doesn't support selection.extend. This means that the selection
2610 // won't return on IE.
Robert Ginda6f4f0aa2014-07-28 10:21:39 -07002611 if (selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07002612 selection.collapse(anchorNode, anchorOffset);
2613 selection.extend(focusNode, focusOffset);
2614 }
rgindafaa74742012-08-21 13:34:03 -07002615
rginda4bba5e12012-06-20 16:15:30 -07002616 copySource.parentNode.removeChild(copySource);
2617};
2618
rgindaa09e7332012-08-17 12:49:51 -07002619hterm.Terminal.prototype.getSelectionText = function() {
2620 var selection = this.scrollPort_.selection;
2621 selection.sync();
2622
2623 if (selection.isCollapsed)
2624 return null;
2625
2626
2627 // Start offset measures from the beginning of the line.
2628 var startOffset = selection.startOffset;
2629 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002630
Robert Gindafdbb3f22012-09-06 20:23:06 -07002631 if (node.nodeName != 'X-ROW') {
2632 // If the selection doesn't start on an x-row node, then it must be
2633 // somewhere inside the x-row. Add any characters from previous siblings
2634 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002635
2636 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2637 // If node is the text node in a styled span, move up to the span node.
2638 node = node.parentNode;
2639 }
2640
Robert Gindafdbb3f22012-09-06 20:23:06 -07002641 while (node.previousSibling) {
2642 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002643 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002644 }
rgindaa09e7332012-08-17 12:49:51 -07002645 }
2646
2647 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002648 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
2649 selection.endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002650 var node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002651
Robert Gindafdbb3f22012-09-06 20:23:06 -07002652 if (node.nodeName != 'X-ROW') {
2653 // If the selection doesn't end on an x-row node, then it must be
2654 // somewhere inside the x-row. Add any characters from following siblings
2655 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002656
2657 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2658 // If node is the text node in a styled span, move up to the span node.
2659 node = node.parentNode;
2660 }
2661
Robert Gindafdbb3f22012-09-06 20:23:06 -07002662 while (node.nextSibling) {
2663 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002664 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002665 }
rgindaa09e7332012-08-17 12:49:51 -07002666 }
2667
2668 var rv = this.getRowsText(selection.startRow.rowIndex,
2669 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002670 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002671};
2672
rginda4bba5e12012-06-20 16:15:30 -07002673/**
2674 * Copy the current selection to the system clipboard, then clear it after a
2675 * short delay.
2676 */
2677hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002678 var text = this.getSelectionText();
2679 if (text != null)
2680 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002681};
2682
rgindaf0090c92012-02-10 14:58:52 -08002683hterm.Terminal.prototype.overlaySize = function() {
2684 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2685};
2686
rginda87b86462011-12-14 13:48:03 -08002687/**
2688 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2689 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07002690 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08002691 */
2692hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002693 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002694 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2695
Robert Ginda8cb7d902013-06-20 14:37:18 -07002696 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08002697};
2698
2699/**
rgindad5613292012-06-19 15:40:37 -07002700 * Add the terminalRow and terminalColumn properties to mouse events and
2701 * then forward on to onMouse().
2702 *
2703 * The terminalRow and terminalColumn properties contain the (row, column)
2704 * coordinates for the mouse event.
2705 */
2706hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002707 if (e.processedByTerminalHandler_) {
2708 // We register our event handlers on the document, as well as the cursor
2709 // and the scroll blocker. Mouse events that occur on the cursor or
2710 // scroll blocker will also appear on the document, but we don't want to
2711 // process them twice.
2712 //
2713 // We can't just prevent bubbling because that has other side effects, so
2714 // we decorate the event object with this property instead.
2715 return;
2716 }
2717
2718 e.processedByTerminalHandler_ = true;
2719
Robert Gindaeda48db2014-07-17 09:25:30 -07002720 // One based row/column stored on the mouse event.
2721 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2722 this.scrollPort_.characterSize.height) + 1;
2723 e.terminalColumn = parseInt(e.clientX /
2724 this.scrollPort_.characterSize.width) + 1;
2725
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002726 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
2727 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07002728 return;
2729 }
2730
Robert Gindab837c052014-08-11 11:17:51 -07002731 if (this.options_.cursorVisible &&
2732 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED) {
2733 // If the cursor is visible and we're not sending mouse events to the
2734 // host app, then we want to hide the terminal cursor when the mouse
2735 // cursor is over top. This keeps the terminal cursor from interfering
2736 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07002737 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
2738 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
2739 this.cursorNode_.style.display = 'none';
2740 } else if (this.cursorNode_.style.display == 'none') {
2741 this.cursorNode_.style.display = '';
2742 }
2743 }
rgindad5613292012-06-19 15:40:37 -07002744
Robert Ginda928cf632014-03-05 15:07:41 -08002745 if (e.type == 'mousedown') {
2746 if (e.altKey || this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED) {
2747 // If VT mouse reporting is disabled, or has been defeated with
2748 // alt-mousedown, then the mouse will act on the local selection.
2749 this.reportMouseEvents_ = false;
2750 this.setSelectionEnabled(true);
2751 } else {
2752 // Otherwise we defer ownership of the mouse to the VT.
2753 this.reportMouseEvents_ = true;
Robert Ginda3ae37822014-05-15 13:05:35 -07002754 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08002755 this.setSelectionEnabled(false);
2756 e.preventDefault();
2757 }
2758 }
2759
2760 if (!this.reportMouseEvents_) {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002761 if (e.type == 'dblclick') {
2762 this.screen_.expandSelection(this.document_.getSelection());
2763 hterm.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07002764 }
2765
Robert Ginda928cf632014-03-05 15:07:41 -08002766 if (e.type == 'mousedown' && e.which == this.mousePasteButton)
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002767 this.paste();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002768
2769 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2770 !this.document_.getSelection().isCollapsed) {
2771 hterm.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002772 }
2773
2774 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
2775 this.scrollBlockerNode_.engaged) {
2776 // Disengage the scroll-blocker after one of these events.
2777 this.scrollBlockerNode_.engaged = false;
2778 this.scrollBlockerNode_.style.top = '-99px';
2779 }
2780
Robert Ginda928cf632014-03-05 15:07:41 -08002781 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002782 if (!this.scrollBlockerNode_.engaged) {
2783 if (e.type == 'mousedown') {
2784 // Move the scroll-blocker into place if we want to keep the scrollport
2785 // from scrolling.
2786 this.scrollBlockerNode_.engaged = true;
2787 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2788 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2789 } else if (e.type == 'mousemove') {
2790 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
2791 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07002792 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002793 e.preventDefault();
2794 }
2795 }
Robert Ginda928cf632014-03-05 15:07:41 -08002796
2797 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07002798 }
2799
Robert Ginda928cf632014-03-05 15:07:41 -08002800 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
2801 // Restore this on mouseup in case it was temporarily defeated with a
2802 // alt-mousedown. Only do this when the selection is empty so that
2803 // we don't immediately kill the users selection.
2804 this.reportMouseEvents_ = (this.vt.mouseReport !=
2805 this.vt.MOUSE_REPORT_DISABLED);
2806 }
rgindad5613292012-06-19 15:40:37 -07002807};
2808
2809/**
2810 * Clients should override this if they care to know about mouse events.
2811 *
2812 * The event parameter will be a normal DOM mouse click event with additional
2813 * 'terminalRow' and 'terminalColumn' properties.
2814 */
2815hterm.Terminal.prototype.onMouse = function(e) { };
2816
2817/**
rginda8e92a692012-05-20 19:37:20 -07002818 * React when focus changes.
2819 */
Rob Spies06533ba2014-04-24 11:20:37 -07002820hterm.Terminal.prototype.onFocusChange_ = function(focused) {
2821 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07002822 this.restyleCursor_();
Michael Kelly485ecd12014-06-09 11:41:56 -04002823 if (focused === true)
2824 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07002825};
2826
2827/**
rginda8ba33642011-12-14 12:31:31 -08002828 * React when the ScrollPort is scrolled.
2829 */
2830hterm.Terminal.prototype.onScroll_ = function() {
2831 this.scheduleSyncCursorPosition_();
2832};
2833
2834/**
rginda9846e2f2012-01-27 13:53:33 -08002835 * React when text is pasted into the scrollPort.
2836 */
2837hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07002838 var data = e.text.replace(/\n/mg, '\r');
Connor Hegartyf525ceb2014-09-03 13:36:03 -07002839 data = this.keyboard.encode(data);
Robert Gindaa063b202014-07-21 11:08:25 -07002840 if (this.options_.bracketedPaste)
2841 data = '\x1b[200~' + data + '\x1b[201~';
2842
2843 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08002844};
2845
2846/**
rgindaa09e7332012-08-17 12:49:51 -07002847 * React when the user tries to copy from the scrollPort.
2848 */
2849hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07002850 if (!this.useDefaultWindowCopy) {
2851 e.preventDefault();
2852 setTimeout(this.copySelectionToClipboard.bind(this), 0);
2853 }
rgindaa09e7332012-08-17 12:49:51 -07002854};
2855
2856/**
rginda8ba33642011-12-14 12:31:31 -08002857 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002858 *
2859 * Note: This function should not directly contain code that alters the internal
2860 * state of the terminal. That kind of code belongs in realizeWidth or
2861 * realizeHeight, so that it can be executed synchronously in the case of a
2862 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002863 */
2864hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08002865 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002866 this.scrollPort_.characterSize.width);
Rob Spiesf4e90e82015-01-28 12:10:13 -08002867 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
rginda35c456b2012-02-09 17:29:05 -08002868 this.scrollPort_.characterSize.height);
2869
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002870 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08002871 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002872 // gets removed from the document or during the initial load, and we can't
2873 // deal with that.
rginda35c456b2012-02-09 17:29:05 -08002874 return;
2875 }
2876
rgindaa8ba17d2012-08-15 14:41:10 -07002877 var isNewSize = (columnCount != this.screenSize.width ||
2878 rowCount != this.screenSize.height);
2879
2880 // We do this even if the size didn't change, just to be sure everything is
2881 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002882 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07002883 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07002884
2885 if (isNewSize)
2886 this.overlaySize();
2887
Robert Gindafb1be6a2013-12-11 11:56:22 -08002888 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07002889 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08002890};
2891
2892/**
2893 * Service the cursor blink timeout.
2894 */
2895hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07002896 if (!this.options_.cursorBlink) {
2897 delete this.timeouts_.cursorBlink;
2898 return;
2899 }
2900
Robert Ginda830583c2013-08-07 13:20:46 -07002901 if (this.cursorNode_.getAttribute('focus') == 'false' ||
2902 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08002903 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07002904 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
2905 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08002906 } else {
rginda87b86462011-12-14 13:48:03 -08002907 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07002908 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
2909 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08002910 }
2911};
David Reveman8f552492012-03-28 12:18:41 -04002912
2913/**
2914 * Set the scrollbar-visible mode bit.
2915 *
2916 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2917 * Otherwise it will not.
2918 *
2919 * Defaults to on.
2920 *
2921 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2922 */
2923hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2924 this.scrollPort_.setScrollbarVisible(state);
2925};
Michael Kelly485ecd12014-06-09 11:41:56 -04002926
2927/**
Rob Spies49039e52014-12-17 13:40:04 -08002928 * Set the scroll wheel move multiplier. This will affect how fast the page
2929 * scrolls on mousewheel events.
2930 *
2931 * Defaults to 1.
2932 *
2933 * @param {number} multiplier.
2934 */
2935hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
2936 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
2937};
2938
2939/**
Michael Kelly485ecd12014-06-09 11:41:56 -04002940 * Close all web notifications created by terminal bells.
2941 */
2942hterm.Terminal.prototype.closeBellNotifications_ = function() {
2943 this.bellNotificationList_.forEach(function(n) {
2944 n.close();
2945 });
2946 this.bellNotificationList_.length = 0;
2947};