blob: 703eadbfe3425673cdf5dcf9f8b68f1ea2d949b5 [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
Robert Gindab4839c22013-02-28 16:52:10 -08007lib.rtdep('lib.colors', 'lib.PreferenceManager', 'lib.resource',
Robert Ginda57f03b42012-09-13 11:02:48 -07008 'hterm.Keyboard', 'hterm.Options', 'hterm.PreferenceManager',
9 'hterm.Screen', 'hterm.ScrollPort', 'hterm.Size', 'hterm.VT');
rgindacbbd7482012-06-13 15:06:16 -070010
rginda8ba33642011-12-14 12:31:31 -080011/**
12 * Constructor for the Terminal class.
13 *
14 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
15 * classes to provide the complete terminal functionality.
16 *
17 * There are a number of lower-level Terminal methods that can be called
18 * directly to manipulate the cursor, text, scroll region, and other terminal
19 * attributes. However, the primary method is interpret(), which parses VT
20 * escape sequences and invokes the appropriate Terminal methods.
21 *
22 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
23 *
24 * TODO(rginda): Eventually we're going to need to support characters which are
25 * displayed twice as wide as standard latin characters. This is to support
26 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080027 *
Robert Ginda57f03b42012-09-13 11:02:48 -070028 * @param {string} opt_profileId Optional preference profile name. If not
rginda9f5222b2012-03-05 11:53:28 -080029 * provided, defaults to 'default'.
rginda8ba33642011-12-14 12:31:31 -080030 */
Robert Ginda57f03b42012-09-13 11:02:48 -070031hterm.Terminal = function(opt_profileId) {
32 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080033
rginda8ba33642011-12-14 12:31:31 -080034 // Two screen instances.
35 this.primaryScreen_ = new hterm.Screen();
36 this.alternateScreen_ = new hterm.Screen();
37
38 // The "current" screen.
39 this.screen_ = this.primaryScreen_;
40
rginda8ba33642011-12-14 12:31:31 -080041 // The local notion of the screen size. ScreenBuffers also have a size which
42 // indicates their present size. During size changes, the two may disagree.
43 // Also, the inactive screen's size is not altered until it is made the active
44 // screen.
45 this.screenSize = new hterm.Size(0, 0);
46
rginda8ba33642011-12-14 12:31:31 -080047 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080048 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080049 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
50 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080051 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070052 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080053
rginda87b86462011-12-14 13:48:03 -080054 // The div that contains this terminal.
55 this.div_ = null;
56
rgindac9bc5502012-01-18 11:48:44 -080057 // The document that contains the scrollPort. Defaulted to the global
58 // document here so that the terminal is functional even if it hasn't been
59 // inserted into a document yet, but re-set in decorate().
60 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080061
rginda8ba33642011-12-14 12:31:31 -080062 // The rows that have scrolled off screen and are no longer addressable.
63 this.scrollbackRows_ = [];
64
rgindac9bc5502012-01-18 11:48:44 -080065 // Saved tab stops.
66 this.tabStops_ = [];
67
David Benjamin66e954d2012-05-05 21:08:12 -040068 // Keep track of whether default tab stops have been erased; after a TBC
69 // clears all tab stops, defaults aren't restored on resize until a reset.
70 this.defaultTabStops = true;
71
rginda8ba33642011-12-14 12:31:31 -080072 // The VT's notion of the top and bottom rows. Used during some VT
73 // cursor positioning and scrolling commands.
74 this.vtScrollTop_ = null;
75 this.vtScrollBottom_ = null;
76
77 // The DIV element for the visible cursor.
78 this.cursorNode_ = null;
79
rginda9f5222b2012-03-05 11:53:28 -080080 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070081 // each output and keystroke. They are initialized by the preference manager.
Robert Ginda8cb7d902013-06-20 14:37:18 -070082 this.backgroundColor_ = null;
83 this.foregroundColor_ = null;
Robert Ginda57f03b42012-09-13 11:02:48 -070084 this.scrollOnOutput_ = null;
85 this.scrollOnKeystroke_ = null;
rginda9f5222b2012-03-05 11:53:28 -080086
rgindaf0090c92012-02-10 14:58:52 -080087 // Terminal bell sound.
88 this.bellAudio_ = this.document_.createElement('audio');
rgindaf0090c92012-02-10 14:58:52 -080089 this.bellAudio_.setAttribute('preload', 'auto');
90
rginda6d397402012-01-17 10:58:29 -080091 // Cursor position and attributes saved with DECSC.
92 this.savedOptions_ = {};
93
rginda8ba33642011-12-14 12:31:31 -080094 // The current mode bits for the terminal.
95 this.options_ = new hterm.Options();
96
97 // Timeouts we might need to clear.
98 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -080099
100 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800101 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800102
rgindafeaf3142012-01-31 15:14:20 -0800103 // The keyboard hander.
104 this.keyboard = new hterm.Keyboard(this);
105
rginda87b86462011-12-14 13:48:03 -0800106 // General IO interface that can be given to third parties without exposing
107 // the entire terminal object.
108 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800109
rgindad5613292012-06-19 15:40:37 -0700110 // True if mouse-click-drag should scroll the terminal.
111 this.enableMouseDragScroll = true;
112
Robert Ginda57f03b42012-09-13 11:02:48 -0700113 this.copyOnSelect = null;
rginda4bba5e12012-06-20 16:15:30 -0700114 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700115
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400116 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800117 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700118
119 this.setProfile(opt_profileId || 'default',
120 function() { this.onTerminalReady() }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800121};
122
123/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700124 * Clients should override this to be notified when the terminal is ready
125 * for use.
126 *
127 * The terminal initialization is asynchronous, and shouldn't be used before
128 * this method is called.
129 */
130hterm.Terminal.prototype.onTerminalReady = function() { };
131
132/**
rginda35c456b2012-02-09 17:29:05 -0800133 * Default tab with of 8 to match xterm.
134 */
135hterm.Terminal.prototype.tabWidth = 8;
136
137/**
rginda9f5222b2012-03-05 11:53:28 -0800138 * Select a preference profile.
139 *
140 * This will load the terminal preferences for the given profile name and
141 * associate subsequent preference changes with the new preference profile.
142 *
143 * @param {string} newName The name of the preference profile. Forward slash
144 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700145 * @param {function} opt_callback Optional callback to invoke when the profile
146 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800147 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700148hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
149 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800150
Robert Ginda57f03b42012-09-13 11:02:48 -0700151 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800152
Robert Ginda57f03b42012-09-13 11:02:48 -0700153 if (this.prefs_)
154 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800155
Robert Ginda57f03b42012-09-13 11:02:48 -0700156 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
157 this.prefs_.addObservers(null, {
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700158 'alt-backspace-is-meta-backspace': function(v) {
159 terminal.keyboard.altBackspaceIsMetaBackspace = v;
160 },
161
Robert Ginda57f03b42012-09-13 11:02:48 -0700162 'alt-is-meta': function(v) {
163 terminal.keyboard.altIsMeta = v;
164 },
165
166 'alt-sends-what': function(v) {
167 if (!/^(escape|8-bit|browser-key)$/.test(v))
168 v = 'escape';
169
170 terminal.keyboard.altSendsWhat = v;
171 },
172
173 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800174 var ary = v.match(/^lib-resource:(\S+)/);
175 if (ary) {
176 terminal.bellAudio_.setAttribute('src',
177 lib.resource.getDataUrl(ary[1]));
178 } else {
179 terminal.bellAudio_.setAttribute('src', v);
180 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700181 },
182
183 'background-color': function(v) {
184 terminal.setBackgroundColor(v);
185 },
186
187 'background-image': function(v) {
188 terminal.scrollPort_.setBackgroundImage(v);
189 },
190
191 'background-size': function(v) {
192 terminal.scrollPort_.setBackgroundSize(v);
193 },
194
195 'background-position': function(v) {
196 terminal.scrollPort_.setBackgroundPosition(v);
197 },
198
199 'backspace-sends-backspace': function(v) {
200 terminal.keyboard.backspaceSendsBackspace = v;
201 },
202
203 'cursor-blink': function(v) {
204 terminal.setCursorBlink(!!v);
205 },
206
207 'cursor-color': function(v) {
208 terminal.setCursorColor(v);
209 },
210
211 'color-palette-overrides': function(v) {
212 if (!(v == null || v instanceof Object || v instanceof Array)) {
213 console.warn('Preference color-palette-overrides is not an array or ' +
214 'object: ' + v);
215 return;
rginda9f5222b2012-03-05 11:53:28 -0800216 }
rginda9f5222b2012-03-05 11:53:28 -0800217
Robert Ginda57f03b42012-09-13 11:02:48 -0700218 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700219
Robert Ginda57f03b42012-09-13 11:02:48 -0700220 if (v) {
221 for (var key in v) {
222 var i = parseInt(key);
223 if (isNaN(i) || i < 0 || i > 255) {
224 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
225 continue;
226 }
227
228 if (v[i]) {
229 var rgb = lib.colors.normalizeCSS(v[i]);
230 if (rgb)
231 lib.colors.colorPalette[i] = rgb;
232 }
233 }
rginda30f20f62012-04-05 16:36:19 -0700234 }
rginda30f20f62012-04-05 16:36:19 -0700235
Robert Ginda57f03b42012-09-13 11:02:48 -0700236 terminal.primaryScreen_.textAttributes.resetColorPalette()
237 terminal.alternateScreen_.textAttributes.resetColorPalette();
238 },
rginda30f20f62012-04-05 16:36:19 -0700239
Robert Ginda57f03b42012-09-13 11:02:48 -0700240 'copy-on-select': function(v) {
241 terminal.copyOnSelect = !!v;
242 },
rginda9f5222b2012-03-05 11:53:28 -0800243
Robert Ginda57f03b42012-09-13 11:02:48 -0700244 'enable-8-bit-control': function(v) {
245 terminal.vt.enable8BitControl = !!v;
246 },
rginda30f20f62012-04-05 16:36:19 -0700247
Robert Ginda57f03b42012-09-13 11:02:48 -0700248 'enable-bold': function(v) {
249 terminal.syncBoldSafeState();
250 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400251
Robert Ginda57f03b42012-09-13 11:02:48 -0700252 'enable-clipboard-write': function(v) {
253 terminal.vt.enableClipboardWrite = !!v;
254 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400255
Robert Ginda3755e752013-05-31 13:34:09 -0700256 'enable-dec12': function(v) {
257 terminal.vt.enableDec12 = !!v;
258 },
259
Robert Ginda57f03b42012-09-13 11:02:48 -0700260 'font-family': function(v) {
261 terminal.syncFontFamily();
262 },
rginda30f20f62012-04-05 16:36:19 -0700263
Robert Ginda57f03b42012-09-13 11:02:48 -0700264 'font-size': function(v) {
265 terminal.setFontSize(v);
266 },
rginda9875d902012-08-20 16:21:57 -0700267
Robert Ginda57f03b42012-09-13 11:02:48 -0700268 'font-smoothing': function(v) {
269 terminal.syncFontFamily();
270 },
rgindade84e382012-04-20 15:39:31 -0700271
Robert Ginda57f03b42012-09-13 11:02:48 -0700272 'foreground-color': function(v) {
273 terminal.setForegroundColor(v);
274 },
rginda30f20f62012-04-05 16:36:19 -0700275
Robert Ginda57f03b42012-09-13 11:02:48 -0700276 'home-keys-scroll': function(v) {
277 terminal.keyboard.homeKeysScroll = v;
278 },
rginda4bba5e12012-06-20 16:15:30 -0700279
Robert Ginda57f03b42012-09-13 11:02:48 -0700280 'max-string-sequence': function(v) {
281 terminal.vt.maxStringSequence = v;
282 },
rginda11057d52012-04-25 12:29:56 -0700283
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700284 'media-keys-are-fkeys': function(v) {
285 terminal.keyboard.mediaKeysAreFKeys = v;
286 },
287
Robert Ginda57f03b42012-09-13 11:02:48 -0700288 'meta-sends-escape': function(v) {
289 terminal.keyboard.metaSendsEscape = v;
290 },
rginda30f20f62012-04-05 16:36:19 -0700291
Robert Ginda57f03b42012-09-13 11:02:48 -0700292 'mouse-cell-motion-trick': function(v) {
293 terminal.vt.setMouseCellMotionTrick(v);
294 },
Robert Ginda9fb38222012-09-11 14:19:12 -0700295
Robert Ginda57f03b42012-09-13 11:02:48 -0700296 'mouse-paste-button': function(v) {
297 terminal.syncMousePasteButton();
298 },
rgindaa8ba17d2012-08-15 14:41:10 -0700299
Robert Ginda40932892012-12-10 17:26:40 -0800300 'pass-alt-number': function(v) {
301 if (v == null) {
302 var osx = window.navigator.userAgent.match(/Mac OS X/);
303
304 // Let Alt-1..9 pass to the browser (to control tab switching) on
305 // non-OS X systems, or if hterm is not opened in an app window.
306 v = (!osx && hterm.windowType != 'popup');
307 }
308
309 terminal.passAltNumber = v;
310 },
311
312 'pass-ctrl-number': function(v) {
313 if (v == null) {
314 var osx = window.navigator.userAgent.match(/Mac OS X/);
315
316 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
317 // non-OS X systems, or if hterm is not opened in an app window.
318 v = (!osx && hterm.windowType != 'popup');
319 }
320
321 terminal.passCtrlNumber = v;
322 },
323
324 'pass-meta-number': function(v) {
325 if (v == null) {
326 var osx = window.navigator.userAgent.match(/Mac OS X/);
327
328 // Let Meta-1..9 pass to the browser (to control tab switching) on
329 // OS X systems, or if hterm is not opened in an app window.
330 v = (osx && hterm.windowType != 'popup');
331 }
332
333 terminal.passMetaNumber = v;
334 },
335
Robert Ginda8cb7d902013-06-20 14:37:18 -0700336 'receive-encoding': function(v) {
337 if (!(/^(utf-8|raw)$/).test(v)) {
338 console.warn('Invalid value for "receive-encoding": ' + v);
339 v = 'utf-8';
340 }
341
342 terminal.vt.characterEncoding = v;
343 },
344
Robert Ginda57f03b42012-09-13 11:02:48 -0700345 'scroll-on-keystroke': function(v) {
346 terminal.scrollOnKeystroke_ = v;
347 },
rginda9f5222b2012-03-05 11:53:28 -0800348
Robert Ginda57f03b42012-09-13 11:02:48 -0700349 'scroll-on-output': function(v) {
350 terminal.scrollOnOutput_ = v;
351 },
rginda30f20f62012-04-05 16:36:19 -0700352
Robert Ginda57f03b42012-09-13 11:02:48 -0700353 'scrollbar-visible': function(v) {
354 terminal.setScrollbarVisible(v);
355 },
rginda9f5222b2012-03-05 11:53:28 -0800356
Robert Ginda8cb7d902013-06-20 14:37:18 -0700357 'send-encoding': function(v) {
358 if (!(/^(utf-8|raw)$/).test(v)) {
359 console.warn('Invalid value for "send-encoding": ' + v);
360 v = 'utf-8';
361 }
362
363 terminal.keyboard.characterEncoding = v;
364 },
365
Robert Ginda57f03b42012-09-13 11:02:48 -0700366 'shift-insert-paste': function(v) {
367 terminal.keyboard.shiftInsertPaste = v;
368 },
rginda9f5222b2012-03-05 11:53:28 -0800369
Robert Ginda57f03b42012-09-13 11:02:48 -0700370 'page-keys-scroll': function(v) {
371 terminal.keyboard.pageKeysScroll = v;
372 }
373 });
rginda30f20f62012-04-05 16:36:19 -0700374
Robert Ginda57f03b42012-09-13 11:02:48 -0700375 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800376 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700377
378 if (opt_callback)
379 opt_callback();
380 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800381};
382
rginda8e92a692012-05-20 19:37:20 -0700383/**
384 * Set the color for the cursor.
385 *
386 * If you want this setting to persist, set it through prefs_, rather than
387 * with this method.
388 */
389hterm.Terminal.prototype.setCursorColor = function(color) {
390 this.cursorNode_.style.backgroundColor = color;
391 this.cursorNode_.style.borderColor = color;
392};
393
394/**
395 * Return the current cursor color as a string.
396 */
397hterm.Terminal.prototype.getCursorColor = function() {
398 return this.cursorNode_.style.backgroundColor;
399};
400
401/**
rgindad5613292012-06-19 15:40:37 -0700402 * Enable or disable mouse based text selection in the terminal.
403 */
404hterm.Terminal.prototype.setSelectionEnabled = function(state) {
405 this.enableMouseDragScroll = state;
406 this.scrollPort_.setSelectionEnabled(state);
407};
408
409/**
rginda8e92a692012-05-20 19:37:20 -0700410 * Set the background color.
411 *
412 * If you want this setting to persist, set it through prefs_, rather than
413 * with this method.
414 */
415hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700416 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700417 this.primaryScreen_.textAttributes.setDefaults(
418 this.foregroundColor_, this.backgroundColor_);
419 this.alternateScreen_.textAttributes.setDefaults(
420 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700421 this.scrollPort_.setBackgroundColor(color);
422};
423
rginda9f5222b2012-03-05 11:53:28 -0800424/**
425 * Return the current terminal background color.
426 *
427 * Intended for use by other classes, so we don't have to expose the entire
428 * prefs_ object.
429 */
430hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700431 return this.backgroundColor_;
432};
433
434/**
435 * Set the foreground color.
436 *
437 * If you want this setting to persist, set it through prefs_, rather than
438 * with this method.
439 */
440hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700441 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700442 this.primaryScreen_.textAttributes.setDefaults(
443 this.foregroundColor_, this.backgroundColor_);
444 this.alternateScreen_.textAttributes.setDefaults(
445 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700446 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800447};
448
449/**
450 * Return the current terminal foreground color.
451 *
452 * Intended for use by other classes, so we don't have to expose the entire
453 * prefs_ object.
454 */
455hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700456 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800457};
458
459/**
rginda87b86462011-12-14 13:48:03 -0800460 * Create a new instance of a terminal command and run it with a given
461 * argument string.
462 *
463 * @param {function} commandClass The constructor for a terminal command.
464 * @param {string} argString The argument string to pass to the command.
465 */
466hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700467 var environment = this.prefs_.get('environment');
468 if (typeof environment != 'object' || environment == null)
469 environment = {};
470
rginda87b86462011-12-14 13:48:03 -0800471 var self = this;
472 this.command = new commandClass(
473 { argString: argString || '',
474 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700475 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800476 onExit: function(code) {
477 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800478 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700479 if (self.prefs_.get('close-on-exit'))
480 window.close();
rginda87b86462011-12-14 13:48:03 -0800481 }
482 });
483
rgindafeaf3142012-01-31 15:14:20 -0800484 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800485 this.command.run();
486};
487
488/**
rgindafeaf3142012-01-31 15:14:20 -0800489 * Returns true if the current screen is the primary screen, false otherwise.
490 */
491hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700492 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800493};
494
495/**
496 * Install the keyboard handler for this terminal.
497 *
498 * This will prevent the browser from seeing any keystrokes sent to the
499 * terminal.
500 */
501hterm.Terminal.prototype.installKeyboard = function() {
502 this.keyboard.installKeyboard(this.document_.body.firstChild);
503}
504
505/**
506 * Uninstall the keyboard handler for this terminal.
507 */
508hterm.Terminal.prototype.uninstallKeyboard = function() {
509 this.keyboard.installKeyboard(null);
510}
511
512/**
rginda35c456b2012-02-09 17:29:05 -0800513 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800514 *
515 * Call setFontSize(0) to reset to the default font size.
516 *
517 * This function does not modify the font-size preference.
518 *
519 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800520 */
521hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800522 if (px === 0)
523 px = this.prefs_.get('font-size');
524
rginda35c456b2012-02-09 17:29:05 -0800525 this.scrollPort_.setFontSize(px);
526};
527
528/**
529 * Get the current font size.
530 */
531hterm.Terminal.prototype.getFontSize = function() {
532 return this.scrollPort_.getFontSize();
533};
534
535/**
rginda8e92a692012-05-20 19:37:20 -0700536 * Get the current font family.
537 */
538hterm.Terminal.prototype.getFontFamily = function() {
539 return this.scrollPort_.getFontFamily();
540};
541
542/**
rginda35c456b2012-02-09 17:29:05 -0800543 * Set the CSS "font-family" for this terminal.
544 */
rginda9f5222b2012-03-05 11:53:28 -0800545hterm.Terminal.prototype.syncFontFamily = function() {
546 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
547 this.prefs_.get('font-smoothing'));
548 this.syncBoldSafeState();
549};
550
rginda4bba5e12012-06-20 16:15:30 -0700551/**
552 * Set this.mousePasteButton based on the mouse-paste-button pref,
553 * autodetecting if necessary.
554 */
555hterm.Terminal.prototype.syncMousePasteButton = function() {
556 var button = this.prefs_.get('mouse-paste-button');
557 if (typeof button == 'number') {
558 this.mousePasteButton = button;
559 return;
560 }
561
562 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
563 if (!ary || ary[2] == 'CrOS') {
564 this.mousePasteButton = 2;
565 } else {
566 this.mousePasteButton = 3;
567 }
568};
569
570/**
571 * Enable or disable bold based on the enable-bold pref, autodetecting if
572 * necessary.
573 */
rginda9f5222b2012-03-05 11:53:28 -0800574hterm.Terminal.prototype.syncBoldSafeState = function() {
575 var enableBold = this.prefs_.get('enable-bold');
576 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700577 this.primaryScreen_.textAttributes.enableBold = enableBold;
578 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800579 return;
580 }
581
rgindaf7521392012-02-28 17:20:34 -0800582 var normalSize = this.scrollPort_.measureCharacterSize();
583 var boldSize = this.scrollPort_.measureCharacterSize('bold');
584
585 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800586 if (!isBoldSafe) {
587 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700588 'from normal. Font family is: ' +
589 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800590 }
rginda9f5222b2012-03-05 11:53:28 -0800591
Robert Gindaed016262012-10-26 16:27:09 -0700592 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
593 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800594};
595
596/**
rginda87b86462011-12-14 13:48:03 -0800597 * Return a copy of the current cursor position.
598 *
599 * @return {hterm.RowCol} The RowCol object representing the current position.
600 */
601hterm.Terminal.prototype.saveCursor = function() {
602 return this.screen_.cursorPosition.clone();
603};
604
rgindaa19afe22012-01-25 15:40:22 -0800605hterm.Terminal.prototype.getTextAttributes = function() {
606 return this.screen_.textAttributes;
607};
608
rginda1a09aa02012-06-18 21:11:25 -0700609hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
610 this.screen_.textAttributes = textAttributes;
611};
612
rginda87b86462011-12-14 13:48:03 -0800613/**
rgindaf522ce02012-04-17 17:49:17 -0700614 * Return the current browser zoom factor applied to the terminal.
615 *
616 * @return {number} The current browser zoom factor.
617 */
618hterm.Terminal.prototype.getZoomFactor = function() {
619 return this.scrollPort_.characterSize.zoomFactor;
620};
621
622/**
rginda9846e2f2012-01-27 13:53:33 -0800623 * Change the title of this terminal's window.
624 */
625hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800626 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800627};
628
629/**
rginda87b86462011-12-14 13:48:03 -0800630 * Restore a previously saved cursor position.
631 *
632 * @param {hterm.RowCol} cursor The position to restore.
633 */
634hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700635 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
636 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800637 this.screen_.setCursorPosition(row, column);
638 if (cursor.column > column ||
639 cursor.column == column && cursor.overflow) {
640 this.screen_.cursorPosition.overflow = true;
641 }
rginda87b86462011-12-14 13:48:03 -0800642};
643
644/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400645 * Clear the cursor's overflow flag.
646 */
647hterm.Terminal.prototype.clearCursorOverflow = function() {
648 this.screen_.cursorPosition.overflow = false;
649};
650
651/**
rginda87b86462011-12-14 13:48:03 -0800652 * Set the width of the terminal, resizing the UI to match.
653 */
654hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800655 if (columnCount == null) {
656 this.div_.style.width = '100%';
657 return;
658 }
659
rginda35c456b2012-02-09 17:29:05 -0800660 this.div_.style.width = this.scrollPort_.characterSize.width *
Robert Ginda97769282013-02-01 15:30:30 -0800661 columnCount + this.scrollPort_.currentScrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400662 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800663 this.scheduleSyncCursorPosition_();
664};
rginda87b86462011-12-14 13:48:03 -0800665
rgindac9bc5502012-01-18 11:48:44 -0800666/**
rginda35c456b2012-02-09 17:29:05 -0800667 * Set the height of the terminal, resizing the UI to match.
668 */
669hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800670 if (rowCount == null) {
671 this.div_.style.height = '100%';
672 return;
673 }
674
rginda35c456b2012-02-09 17:29:05 -0800675 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700676 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800677 this.realizeSize_(this.screenSize.width, rowCount);
678 this.scheduleSyncCursorPosition_();
679};
680
681/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400682 * Deal with terminal size changes.
683 *
684 */
685hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
686 if (columnCount != this.screenSize.width)
687 this.realizeWidth_(columnCount);
688
689 if (rowCount != this.screenSize.height)
690 this.realizeHeight_(rowCount);
691
692 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -0700693 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400694};
695
696/**
rgindac9bc5502012-01-18 11:48:44 -0800697 * Deal with terminal width changes.
698 *
699 * This function does what needs to be done when the terminal width changes
700 * out from under us. It happens here rather than in onResize_() because this
701 * code may need to run synchronously to handle programmatic changes of
702 * terminal width.
703 *
704 * Relying on the browser to send us an async resize event means we may not be
705 * in the correct state yet when the next escape sequence hits.
706 */
707hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700708 if (columnCount <= 0)
709 throw new Error('Attempt to realize bad width: ' + columnCount);
710
rgindac9bc5502012-01-18 11:48:44 -0800711 var deltaColumns = columnCount - this.screen_.getWidth();
712
rginda87b86462011-12-14 13:48:03 -0800713 this.screenSize.width = columnCount;
714 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800715
716 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400717 if (this.defaultTabStops)
718 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800719 } else {
720 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400721 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800722 break;
723
724 this.tabStops_.pop();
725 }
726 }
727
728 this.screen_.setColumnCount(this.screenSize.width);
729};
730
731/**
732 * Deal with terminal height changes.
733 *
734 * This function does what needs to be done when the terminal height changes
735 * out from under us. It happens here rather than in onResize_() because this
736 * code may need to run synchronously to handle programmatic changes of
737 * terminal height.
738 *
739 * Relying on the browser to send us an async resize event means we may not be
740 * in the correct state yet when the next escape sequence hits.
741 */
742hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700743 if (rowCount <= 0)
744 throw new Error('Attempt to realize bad height: ' + rowCount);
745
rgindac9bc5502012-01-18 11:48:44 -0800746 var deltaRows = rowCount - this.screen_.getHeight();
747
748 this.screenSize.height = rowCount;
749
750 var cursor = this.saveCursor();
751
752 if (deltaRows < 0) {
753 // Screen got smaller.
754 deltaRows *= -1;
755 while (deltaRows) {
756 var lastRow = this.getRowCount() - 1;
757 if (lastRow - this.scrollbackRows_.length == cursor.row)
758 break;
759
760 if (this.getRowText(lastRow))
761 break;
762
763 this.screen_.popRow();
764 deltaRows--;
765 }
766
767 var ary = this.screen_.shiftRows(deltaRows);
768 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
769
770 // We just removed rows from the top of the screen, we need to update
771 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800772 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800773 } else if (deltaRows > 0) {
774 // Screen got larger.
775
776 if (deltaRows <= this.scrollbackRows_.length) {
777 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
778 var rows = this.scrollbackRows_.splice(
779 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
780 this.screen_.unshiftRows(rows);
781 deltaRows -= scrollbackCount;
782 cursor.row += scrollbackCount;
783 }
784
785 if (deltaRows)
786 this.appendRows_(deltaRows);
787 }
788
rginda35c456b2012-02-09 17:29:05 -0800789 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800790 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800791};
792
793/**
794 * Scroll the terminal to the top of the scrollback buffer.
795 */
796hterm.Terminal.prototype.scrollHome = function() {
797 this.scrollPort_.scrollRowToTop(0);
798};
799
800/**
801 * Scroll the terminal to the end.
802 */
803hterm.Terminal.prototype.scrollEnd = function() {
804 this.scrollPort_.scrollRowToBottom(this.getRowCount());
805};
806
807/**
808 * Scroll the terminal one page up (minus one line) relative to the current
809 * position.
810 */
811hterm.Terminal.prototype.scrollPageUp = function() {
812 var i = this.scrollPort_.getTopRowIndex();
813 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
814};
815
816/**
817 * Scroll the terminal one page down (minus one line) relative to the current
818 * position.
819 */
820hterm.Terminal.prototype.scrollPageDown = function() {
821 var i = this.scrollPort_.getTopRowIndex();
822 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800823};
824
rgindac9bc5502012-01-18 11:48:44 -0800825/**
Robert Ginda40932892012-12-10 17:26:40 -0800826 * Clear primary screen, secondary screen, and the scrollback buffer.
827 */
828hterm.Terminal.prototype.wipeContents = function() {
829 this.scrollbackRows_.length = 0;
830 this.scrollPort_.resetCache();
831
832 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
833 var bottom = screen.getHeight();
834 if (bottom > 0) {
835 this.renumberRows_(0, bottom);
836 this.clearHome(screen);
837 }
838 }.bind(this));
839
840 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -0700841 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -0800842};
843
844/**
rgindac9bc5502012-01-18 11:48:44 -0800845 * Full terminal reset.
846 */
rginda87b86462011-12-14 13:48:03 -0800847hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800848 this.clearAllTabStops();
849 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -0700850
851 this.clearHome(this.primaryScreen_);
852 this.primaryScreen_.textAttributes.reset();
853
854 this.clearHome(this.alternateScreen_);
855 this.alternateScreen_.textAttributes.reset();
856
rgindab8bc8932012-04-27 12:45:03 -0700857 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
858
Robert Ginda92e18102013-03-14 13:56:37 -0700859 this.vt.reset();
860
rgindac9bc5502012-01-18 11:48:44 -0800861 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800862};
863
rgindac9bc5502012-01-18 11:48:44 -0800864/**
865 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -0700866 *
867 * Perform a soft reset to the default values listed in
868 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -0800869 */
rginda0f5c0292012-01-13 11:00:13 -0800870hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -0700871 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -0800872 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -0700873
rgindab8bc8932012-04-27 12:45:03 -0700874 // Xterm also resets the color palette on soft reset, even though it doesn't
875 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -0700876 this.primaryScreen_.textAttributes.resetColorPalette();
877 this.alternateScreen_.textAttributes.resetColorPalette();
878
rgindab8bc8932012-04-27 12:45:03 -0700879 // The xterm man page explicitly says this will happen on soft reset.
880 this.setVTScrollRegion(null, null);
881
882 // Xterm also shows the cursor on soft reset, but does not alter the blink
883 // state.
rgindaa19afe22012-01-25 15:40:22 -0800884 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -0800885};
886
rgindac9bc5502012-01-18 11:48:44 -0800887/**
888 * Move the cursor forward to the next tab stop, or to the last column
889 * if no more tab stops are set.
890 */
891hterm.Terminal.prototype.forwardTabStop = function() {
892 var column = this.screen_.cursorPosition.column;
893
894 for (var i = 0; i < this.tabStops_.length; i++) {
895 if (this.tabStops_[i] > column) {
896 this.setCursorColumn(this.tabStops_[i]);
897 return;
898 }
899 }
900
David Benjamin66e954d2012-05-05 21:08:12 -0400901 // xterm does not clear the overflow flag on HT or CHT.
902 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -0800903 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -0400904 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -0800905};
906
rgindac9bc5502012-01-18 11:48:44 -0800907/**
908 * Move the cursor backward to the previous tab stop, or to the first column
909 * if no previous tab stops are set.
910 */
911hterm.Terminal.prototype.backwardTabStop = function() {
912 var column = this.screen_.cursorPosition.column;
913
914 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
915 if (this.tabStops_[i] < column) {
916 this.setCursorColumn(this.tabStops_[i]);
917 return;
918 }
919 }
920
921 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800922};
923
rgindac9bc5502012-01-18 11:48:44 -0800924/**
925 * Set a tab stop at the given column.
926 *
927 * @param {int} column Zero based column.
928 */
929hterm.Terminal.prototype.setTabStop = function(column) {
930 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
931 if (this.tabStops_[i] == column)
932 return;
933
934 if (this.tabStops_[i] < column) {
935 this.tabStops_.splice(i + 1, 0, column);
936 return;
937 }
938 }
939
940 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800941};
942
rgindac9bc5502012-01-18 11:48:44 -0800943/**
944 * Clear the tab stop at the current cursor position.
945 *
946 * No effect if there is no tab stop at the current cursor position.
947 */
948hterm.Terminal.prototype.clearTabStopAtCursor = function() {
949 var column = this.screen_.cursorPosition.column;
950
951 var i = this.tabStops_.indexOf(column);
952 if (i == -1)
953 return;
954
955 this.tabStops_.splice(i, 1);
956};
957
958/**
959 * Clear all tab stops.
960 */
961hterm.Terminal.prototype.clearAllTabStops = function() {
962 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -0400963 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -0800964};
965
966/**
967 * Set up the default tab stops, starting from a given column.
968 *
969 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -0400970 * from the specified column, or 0 if no column is provided. It also flags
971 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -0800972 *
973 * This does not clear the existing tab stops first, use clearAllTabStops
974 * for that.
975 *
976 * @param {int} opt_start Optional starting zero based starting column, useful
977 * for filling out missing tab stops when the terminal is resized.
978 */
979hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
980 var start = opt_start || 0;
981 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -0400982 // Round start up to a default tab stop.
983 start = start - 1 - ((start - 1) % w) + w;
984 for (var i = start; i < this.screenSize.width; i += w) {
985 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -0800986 }
David Benjamin66e954d2012-05-05 21:08:12 -0400987
988 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -0800989};
990
rginda6d397402012-01-17 10:58:29 -0800991/**
rginda8ba33642011-12-14 12:31:31 -0800992 * Interpret a sequence of characters.
993 *
994 * Incomplete escape sequences are buffered until the next call.
995 *
996 * @param {string} str Sequence of characters to interpret or pass through.
997 */
998hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -0800999 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001000 this.scheduleSyncCursorPosition_();
1001};
1002
1003/**
1004 * Take over the given DIV for use as the terminal display.
1005 *
1006 * @param {HTMLDivElement} div The div to use as the terminal display.
1007 */
1008hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001009 this.div_ = div;
1010
rginda8ba33642011-12-14 12:31:31 -08001011 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001012 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001013 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1014 this.scrollPort_.setBackgroundPosition(
1015 this.prefs_.get('background-position'));
rginda30f20f62012-04-05 16:36:19 -07001016
rginda0918b652012-04-04 11:26:24 -07001017 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001018
rginda9f5222b2012-03-05 11:53:28 -08001019 this.setFontSize(this.prefs_.get('font-size'));
1020 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001021
David Reveman8f552492012-03-28 12:18:41 -04001022 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
1023
rginda8ba33642011-12-14 12:31:31 -08001024 this.document_ = this.scrollPort_.getDocument();
1025
rginda4bba5e12012-06-20 16:15:30 -07001026 this.document_.body.oncontextmenu = function() { return false };
1027
1028 var onMouse = this.onMouse_.bind(this);
1029 this.document_.body.firstChild.addEventListener('mousedown', onMouse);
1030 this.document_.body.firstChild.addEventListener('mouseup', onMouse);
1031 this.document_.body.firstChild.addEventListener('mousemove', onMouse);
1032 this.scrollPort_.onScrollWheel = onMouse;
1033
rginda8e92a692012-05-20 19:37:20 -07001034 this.document_.body.firstChild.addEventListener(
1035 'focus', this.onFocusChange_.bind(this, true));
1036 this.document_.body.firstChild.addEventListener(
1037 'blur', this.onFocusChange_.bind(this, false));
1038
1039 var style = this.document_.createElement('style');
1040 style.textContent =
1041 ('.cursor-node[focus="false"] {' +
1042 ' box-sizing: border-box;' +
1043 ' background-color: transparent !important;' +
1044 ' border-width: 2px;' +
1045 ' border-style: solid;' +
1046 '}');
1047 this.document_.head.appendChild(style);
1048
rginda8ba33642011-12-14 12:31:31 -08001049 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001050 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001051 this.cursorNode_.style.cssText =
1052 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001053 'top: -99px;' +
1054 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001055 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1056 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001057 '-webkit-transition: opacity, background-color 100ms linear;');
1058 this.setCursorColor(this.prefs_.get('cursor-color'));
rgindad5613292012-06-19 15:40:37 -07001059
rginda8ba33642011-12-14 12:31:31 -08001060 this.document_.body.appendChild(this.cursorNode_);
1061
rgindad5613292012-06-19 15:40:37 -07001062 // When 'enableMouseDragScroll' is off we reposition this element directly
1063 // under the mouse cursor after a click. This makes Chrome associate
1064 // subsequent mousemove events with the scroll-blocker. Since the
1065 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1066 // events do not cause the scrollport to scroll.
1067 //
1068 // It's a hack, but it's the cleanest way I could find.
1069 this.scrollBlockerNode_ = this.document_.createElement('div');
1070 this.scrollBlockerNode_.style.cssText =
1071 ('position: absolute;' +
1072 'top: -99px;' +
1073 'display: block;' +
1074 'width: 10px;' +
1075 'height: 10px;');
1076 this.document_.body.appendChild(this.scrollBlockerNode_);
1077
1078 var onMouse = this.onMouse_.bind(this);
1079 this.scrollPort_.onScrollWheel = onMouse;
1080 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1081 ].forEach(function(event) {
1082 this.scrollBlockerNode_.addEventListener(event, onMouse);
1083 this.cursorNode_.addEventListener(event, onMouse);
1084 this.document_.addEventListener(event, onMouse);
1085 }.bind(this));
1086
1087 this.cursorNode_.addEventListener('mousedown', function() {
1088 setTimeout(this.focus.bind(this));
1089 }.bind(this));
1090
rgindade84e382012-04-20 15:39:31 -07001091 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
rginda8ba33642011-12-14 12:31:31 -08001092 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001093
rginda87b86462011-12-14 13:48:03 -08001094 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001095 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001096};
1097
rginda0918b652012-04-04 11:26:24 -07001098/**
1099 * Return the HTML document that contains the terminal DOM nodes.
1100 */
rginda87b86462011-12-14 13:48:03 -08001101hterm.Terminal.prototype.getDocument = function() {
1102 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001103};
1104
1105/**
rginda0918b652012-04-04 11:26:24 -07001106 * Focus the terminal.
1107 */
1108hterm.Terminal.prototype.focus = function() {
1109 this.scrollPort_.focus();
1110};
1111
1112/**
rginda8ba33642011-12-14 12:31:31 -08001113 * Return the HTML Element for a given row index.
1114 *
1115 * This is a method from the RowProvider interface. The ScrollPort uses
1116 * it to fetch rows on demand as they are scrolled into view.
1117 *
1118 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1119 * pairs to conserve memory.
1120 *
1121 * @param {integer} index The zero-based row index, measured relative to the
1122 * start of the scrollback buffer. On-screen rows will always have the
1123 * largest indicies.
1124 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1125 */
1126hterm.Terminal.prototype.getRowNode = function(index) {
1127 if (index < this.scrollbackRows_.length)
1128 return this.scrollbackRows_[index];
1129
1130 var screenIndex = index - this.scrollbackRows_.length;
1131 return this.screen_.rowsArray[screenIndex];
1132};
1133
1134/**
1135 * Return the text content for a given range of rows.
1136 *
1137 * This is a method from the RowProvider interface. The ScrollPort uses
1138 * it to fetch text content on demand when the user attempts to copy their
1139 * selection to the clipboard.
1140 *
1141 * @param {integer} start The zero-based row index to start from, measured
1142 * relative to the start of the scrollback buffer. On-screen rows will
1143 * always have the largest indicies.
1144 * @param {integer} end The zero-based row index to end on, measured
1145 * relative to the start of the scrollback buffer.
1146 * @return {string} A single string containing the text value of the range of
1147 * rows. Lines will be newline delimited, with no trailing newline.
1148 */
1149hterm.Terminal.prototype.getRowsText = function(start, end) {
1150 var ary = [];
1151 for (var i = start; i < end; i++) {
1152 var node = this.getRowNode(i);
1153 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001154 if (i < end - 1 && !node.getAttribute('line-overflow'))
1155 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001156 }
1157
rgindaa09e7332012-08-17 12:49:51 -07001158 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001159};
1160
1161/**
1162 * Return the text content for a given row.
1163 *
1164 * This is a method from the RowProvider interface. The ScrollPort uses
1165 * it to fetch text content on demand when the user attempts to copy their
1166 * selection to the clipboard.
1167 *
1168 * @param {integer} index The zero-based row index to return, measured
1169 * relative to the start of the scrollback buffer. On-screen rows will
1170 * always have the largest indicies.
1171 * @return {string} A string containing the text value of the selected row.
1172 */
1173hterm.Terminal.prototype.getRowText = function(index) {
1174 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001175 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001176};
1177
1178/**
1179 * Return the total number of rows in the addressable screen and in the
1180 * scrollback buffer of this terminal.
1181 *
1182 * This is a method from the RowProvider interface. The ScrollPort uses
1183 * it to compute the size of the scrollbar.
1184 *
1185 * @return {integer} The number of rows in this terminal.
1186 */
1187hterm.Terminal.prototype.getRowCount = function() {
1188 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1189};
1190
1191/**
1192 * Create DOM nodes for new rows and append them to the end of the terminal.
1193 *
1194 * This is the only correct way to add a new DOM node for a row. Notice that
1195 * the new row is appended to the bottom of the list of rows, and does not
1196 * require renumbering (of the rowIndex property) of previous rows.
1197 *
1198 * If you think you want a new blank row somewhere in the middle of the
1199 * terminal, look into moveRows_().
1200 *
1201 * This method does not pay attention to vtScrollTop/Bottom, since you should
1202 * be using moveRows() in cases where they would matter.
1203 *
1204 * The cursor will be positioned at column 0 of the first inserted line.
1205 */
1206hterm.Terminal.prototype.appendRows_ = function(count) {
1207 var cursorRow = this.screen_.rowsArray.length;
1208 var offset = this.scrollbackRows_.length + cursorRow;
1209 for (var i = 0; i < count; i++) {
1210 var row = this.document_.createElement('x-row');
1211 row.appendChild(this.document_.createTextNode(''));
1212 row.rowIndex = offset + i;
1213 this.screen_.pushRow(row);
1214 }
1215
1216 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1217 if (extraRows > 0) {
1218 var ary = this.screen_.shiftRows(extraRows);
1219 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001220 if (this.scrollPort_.isScrolledEnd)
1221 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001222 }
1223
1224 if (cursorRow >= this.screen_.rowsArray.length)
1225 cursorRow = this.screen_.rowsArray.length - 1;
1226
rginda87b86462011-12-14 13:48:03 -08001227 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001228};
1229
1230/**
1231 * Relocate rows from one part of the addressable screen to another.
1232 *
1233 * This is used to recycle rows during VT scrolls (those which are driven
1234 * by VT commands, rather than by the user manipulating the scrollbar.)
1235 *
1236 * In this case, the blank lines scrolled into the scroll region are made of
1237 * the nodes we scrolled off. These have their rowIndex properties carefully
1238 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001239 */
1240hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1241 var ary = this.screen_.removeRows(fromIndex, count);
1242 this.screen_.insertRows(toIndex, ary);
1243
1244 var start, end;
1245 if (fromIndex < toIndex) {
1246 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001247 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001248 } else {
1249 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001250 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001251 }
1252
1253 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001254 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001255};
1256
1257/**
1258 * Renumber the rowIndex property of the given range of rows.
1259 *
1260 * The start and end indicies are relative to the screen, not the scrollback.
1261 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001262 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001263 * no need to renumber scrollback rows.
1264 */
Robert Ginda40932892012-12-10 17:26:40 -08001265hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1266 var screen = opt_screen || this.screen_;
1267
rginda8ba33642011-12-14 12:31:31 -08001268 var offset = this.scrollbackRows_.length;
1269 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001270 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001271 }
1272};
1273
1274/**
1275 * Print a string to the terminal.
1276 *
1277 * This respects the current insert and wraparound modes. It will add new lines
1278 * to the end of the terminal, scrolling off the top into the scrollback buffer
1279 * if necessary.
1280 *
1281 * The string is *not* parsed for escape codes. Use the interpret() method if
1282 * that's what you're after.
1283 *
1284 * @param{string} str The string to print.
1285 */
1286hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001287 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001288
rgindaa9abdd82012-08-06 18:05:09 -07001289 while (startOffset < str.length) {
rgindaa09e7332012-08-17 12:49:51 -07001290 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1291 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001292 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001293 }
rgindaa19afe22012-01-25 15:40:22 -08001294
rgindaa9abdd82012-08-06 18:05:09 -07001295 var count = str.length - startOffset;
1296 var didOverflow = false;
1297 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001298
rgindaa9abdd82012-08-06 18:05:09 -07001299 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1300 didOverflow = true;
1301 count = this.screenSize.width - this.screen_.cursorPosition.column;
1302 }
rgindaa19afe22012-01-25 15:40:22 -08001303
rgindaa9abdd82012-08-06 18:05:09 -07001304 if (didOverflow && !this.options_.wraparound) {
1305 // If the string overflowed the line but wraparound is off, then the
1306 // last printed character should be the last of the string.
1307 // TODO: This will add to our problems with multibyte UTF-16 characters.
1308 substr = str.substr(startOffset, count - 1) +
1309 str.substr(str.length - 1);
1310 count = str.length;
1311 } else {
1312 substr = str.substr(startOffset, count);
1313 }
rgindaa19afe22012-01-25 15:40:22 -08001314
rgindaa9abdd82012-08-06 18:05:09 -07001315 if (this.options_.insertMode) {
1316 this.screen_.insertString(substr);
1317 } else {
1318 this.screen_.overwriteString(substr);
1319 }
1320
1321 this.screen_.maybeClipCurrentRow();
1322 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001323 }
rginda8ba33642011-12-14 12:31:31 -08001324
1325 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001326
rginda9f5222b2012-03-05 11:53:28 -08001327 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001328 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001329};
1330
1331/**
rginda87b86462011-12-14 13:48:03 -08001332 * Set the VT scroll region.
1333 *
rginda87b86462011-12-14 13:48:03 -08001334 * This also resets the cursor position to the absolute (0, 0) position, since
1335 * that's what xterm appears to do.
1336 *
1337 * @param {integer} scrollTop The zero-based top of the scroll region.
1338 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1339 * inclusive.
1340 */
1341hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
1342 this.vtScrollTop_ = scrollTop;
1343 this.vtScrollBottom_ = scrollBottom;
rginda87b86462011-12-14 13:48:03 -08001344};
1345
1346/**
rginda8ba33642011-12-14 12:31:31 -08001347 * Return the top row index according to the VT.
1348 *
1349 * This will return 0 unless the terminal has been told to restrict scrolling
1350 * to some lower row. It is used for some VT cursor positioning and scrolling
1351 * commands.
1352 *
1353 * @return {integer} The topmost row in the terminal's scroll region.
1354 */
1355hterm.Terminal.prototype.getVTScrollTop = function() {
1356 if (this.vtScrollTop_ != null)
1357 return this.vtScrollTop_;
1358
1359 return 0;
rginda87b86462011-12-14 13:48:03 -08001360};
rginda8ba33642011-12-14 12:31:31 -08001361
1362/**
1363 * Return the bottom row index according to the VT.
1364 *
1365 * This will return the height of the terminal unless the it has been told to
1366 * restrict scrolling to some higher row. It is used for some VT cursor
1367 * positioning and scrolling commands.
1368 *
1369 * @return {integer} The bottommost row in the terminal's scroll region.
1370 */
1371hterm.Terminal.prototype.getVTScrollBottom = function() {
1372 if (this.vtScrollBottom_ != null)
1373 return this.vtScrollBottom_;
1374
rginda87b86462011-12-14 13:48:03 -08001375 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001376}
1377
1378/**
1379 * Process a '\n' character.
1380 *
1381 * If the cursor is on the final row of the terminal this will append a new
1382 * blank row to the screen and scroll the topmost row into the scrollback
1383 * buffer.
1384 *
1385 * Otherwise, this moves the cursor to column zero of the next row.
1386 */
1387hterm.Terminal.prototype.newLine = function() {
Robert Ginda1b06b372013-07-19 15:22:51 -07001388 if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
1389 // We're at the end of the VT Scroll Region. (It's possible we're also at
1390 // the end of the physical screen.) Perform a VT Scroll rather than create
1391 // a new terminal row
rginda87b86462011-12-14 13:48:03 -08001392 this.vtScrollUp(1);
1393 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
Robert Ginda1b06b372013-07-19 15:22:51 -07001394 } else if (
1395 this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
1396 // We're at the end of the screen. Append a new row to the terminal,
1397 // shifting the top row into the scrollback.
1398 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001399 } else {
rginda87b86462011-12-14 13:48:03 -08001400 // Anywhere else in the screen just moves the cursor.
1401 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001402 }
1403};
1404
1405/**
1406 * Like newLine(), except maintain the cursor column.
1407 */
1408hterm.Terminal.prototype.lineFeed = function() {
1409 var column = this.screen_.cursorPosition.column;
1410 this.newLine();
1411 this.setCursorColumn(column);
1412};
1413
1414/**
rginda87b86462011-12-14 13:48:03 -08001415 * If autoCarriageReturn is set then newLine(), else lineFeed().
1416 */
1417hterm.Terminal.prototype.formFeed = function() {
1418 if (this.options_.autoCarriageReturn) {
1419 this.newLine();
1420 } else {
1421 this.lineFeed();
1422 }
1423};
1424
1425/**
1426 * Move the cursor up one row, possibly inserting a blank line.
1427 *
1428 * The cursor column is not changed.
1429 */
1430hterm.Terminal.prototype.reverseLineFeed = function() {
1431 var scrollTop = this.getVTScrollTop();
1432 var currentRow = this.screen_.cursorPosition.row;
1433
1434 if (currentRow == scrollTop) {
1435 this.insertLines(1);
1436 } else {
1437 this.setAbsoluteCursorRow(currentRow - 1);
1438 }
1439};
1440
1441/**
rginda8ba33642011-12-14 12:31:31 -08001442 * Replace all characters to the left of the current cursor with the space
1443 * character.
1444 *
1445 * TODO(rginda): This should probably *remove* the characters (not just replace
1446 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001447 * position.
rginda8ba33642011-12-14 12:31:31 -08001448 */
1449hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001450 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001451 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001452 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001453 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001454};
1455
1456/**
David Benjamin684a9b72012-05-01 17:19:58 -04001457 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001458 *
1459 * The cursor position is unchanged.
1460 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001461 * If the current background color is not the default background color this
1462 * will insert spaces rather than delete. This is unfortunate because the
1463 * trailing space will affect text selection, but it's difficult to come up
1464 * with a way to style empty space that wouldn't trip up the hterm.Screen
1465 * code.
rginda8ba33642011-12-14 12:31:31 -08001466 */
1467hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001468 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1469 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001470
1471 if (this.screen_.textAttributes.background ===
1472 this.screen_.textAttributes.DEFAULT_COLOR) {
1473 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
1474 if (cursorRow.textContent.length <=
1475 this.screen_.cursorPosition.column + count) {
1476 this.screen_.deleteChars(count);
1477 this.clearCursorOverflow();
1478 return;
1479 }
1480 }
1481
rginda87b86462011-12-14 13:48:03 -08001482 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001483 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001484 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001485 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001486};
1487
1488/**
1489 * Erase the current line.
1490 *
1491 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001492 */
1493hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001494 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001495 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001496 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001497 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001498};
1499
1500/**
David Benjamina08d78f2012-05-05 00:28:49 -04001501 * Erase all characters from the start of the screen to the current cursor
1502 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001503 *
1504 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001505 */
1506hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001507 var cursor = this.saveCursor();
1508
1509 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001510
David Benjamina08d78f2012-05-05 00:28:49 -04001511 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001512 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001513 this.screen_.clearCursorRow();
1514 }
1515
rginda87b86462011-12-14 13:48:03 -08001516 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001517 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001518};
1519
1520/**
1521 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001522 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001523 *
1524 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001525 */
1526hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001527 var cursor = this.saveCursor();
1528
1529 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001530
David Benjamina08d78f2012-05-05 00:28:49 -04001531 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001532 for (var i = cursor.row + 1; i <= bottom; i++) {
1533 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001534 this.screen_.clearCursorRow();
1535 }
1536
rginda87b86462011-12-14 13:48:03 -08001537 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001538 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001539};
1540
1541/**
1542 * Fill the terminal with a given character.
1543 *
1544 * This methods does not respect the VT scroll region.
1545 *
1546 * @param {string} ch The character to use for the fill.
1547 */
1548hterm.Terminal.prototype.fill = function(ch) {
1549 var cursor = this.saveCursor();
1550
1551 this.setAbsoluteCursorPosition(0, 0);
1552 for (var row = 0; row < this.screenSize.height; row++) {
1553 for (var col = 0; col < this.screenSize.width; col++) {
1554 this.setAbsoluteCursorPosition(row, col);
1555 this.screen_.overwriteString(ch);
1556 }
1557 }
1558
1559 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001560};
1561
1562/**
rginda9ea433c2012-03-16 11:57:00 -07001563 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001564 *
rginda9ea433c2012-03-16 11:57:00 -07001565 * This does not respect the scroll region.
1566 *
1567 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1568 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001569 */
rginda9ea433c2012-03-16 11:57:00 -07001570hterm.Terminal.prototype.clearHome = function(opt_screen) {
1571 var screen = opt_screen || this.screen_;
1572 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001573
rginda11057d52012-04-25 12:29:56 -07001574 if (bottom == 0) {
1575 // Empty screen, nothing to do.
1576 return;
1577 }
1578
rgindae4d29232012-01-19 10:47:13 -08001579 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001580 screen.setCursorPosition(i, 0);
1581 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001582 }
1583
rginda9ea433c2012-03-16 11:57:00 -07001584 screen.setCursorPosition(0, 0);
1585};
1586
1587/**
1588 * Erase the entire display without changing the cursor position.
1589 *
1590 * The cursor position is unchanged. This does not respect the scroll
1591 * region.
1592 *
1593 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1594 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001595 */
1596hterm.Terminal.prototype.clear = function(opt_screen) {
1597 var screen = opt_screen || this.screen_;
1598 var cursor = screen.cursorPosition.clone();
1599 this.clearHome(screen);
1600 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001601};
1602
1603/**
1604 * VT command to insert lines at the current cursor row.
1605 *
1606 * This respects the current scroll region. Rows pushed off the bottom are
1607 * lost (they won't show up in the scrollback buffer).
1608 *
rginda8ba33642011-12-14 12:31:31 -08001609 * @param {integer} count The number of lines to insert.
1610 */
1611hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07001612 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08001613
1614 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07001615 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08001616
Robert Ginda579186b2012-09-26 11:40:04 -07001617 // The moveCount is the number of rows we need to relocate to make room for
1618 // the new row(s). The count is the distance to move them.
1619 var moveCount = bottom - cursorRow - count + 1;
1620 if (moveCount)
1621 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08001622
Robert Ginda579186b2012-09-26 11:40:04 -07001623 for (var i = count - 1; i >= 0; i--) {
1624 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001625 this.screen_.clearCursorRow();
1626 }
rginda8ba33642011-12-14 12:31:31 -08001627};
1628
1629/**
1630 * VT command to delete lines at the current cursor row.
1631 *
1632 * New rows are added to the bottom of scroll region to take their place. New
1633 * rows are strictly there to take up space and have no content or style.
1634 */
1635hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001636 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001637
rginda87b86462011-12-14 13:48:03 -08001638 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001639 var bottom = this.getVTScrollBottom();
1640
rginda87b86462011-12-14 13:48:03 -08001641 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001642 count = Math.min(count, maxCount);
1643
rginda87b86462011-12-14 13:48:03 -08001644 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001645 if (count != maxCount)
1646 this.moveRows_(top, count, moveStart);
1647
1648 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001649 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001650 this.screen_.clearCursorRow();
1651 }
1652
rginda87b86462011-12-14 13:48:03 -08001653 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001654 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001655};
1656
1657/**
1658 * Inserts the given number of spaces at the current cursor position.
1659 *
rginda87b86462011-12-14 13:48:03 -08001660 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001661 */
1662hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001663 var cursor = this.saveCursor();
1664
rgindacbbd7482012-06-13 15:06:16 -07001665 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001666 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001667 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001668
1669 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001670 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001671};
1672
1673/**
1674 * Forward-delete the specified number of characters starting at the cursor
1675 * position.
1676 *
1677 * @param {integer} count The number of characters to delete.
1678 */
1679hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001680 var deleted = this.screen_.deleteChars(count);
1681 if (deleted && !this.screen_.textAttributes.isDefault()) {
1682 var cursor = this.saveCursor();
1683 this.setCursorColumn(this.screenSize.width - deleted);
1684 this.screen_.insertString(lib.f.getWhitespace(deleted));
1685 this.restoreCursor(cursor);
1686 }
1687
David Benjamin54e8bf62012-06-01 22:31:40 -04001688 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001689};
1690
1691/**
1692 * Shift rows in the scroll region upwards by a given number of lines.
1693 *
1694 * New rows are inserted at the bottom of the scroll region to fill the
1695 * vacated rows. The new rows not filled out with the current text attributes.
1696 *
1697 * This function does not affect the scrollback rows at all. Rows shifted
1698 * off the top are lost.
1699 *
rginda87b86462011-12-14 13:48:03 -08001700 * The cursor position is not altered.
1701 *
rginda8ba33642011-12-14 12:31:31 -08001702 * @param {integer} count The number of rows to scroll.
1703 */
1704hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001705 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001706
rginda87b86462011-12-14 13:48:03 -08001707 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001708 this.deleteLines(count);
1709
rginda87b86462011-12-14 13:48:03 -08001710 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001711};
1712
1713/**
1714 * Shift rows below the cursor down by a given number of lines.
1715 *
1716 * This function respects the current scroll region.
1717 *
1718 * New rows are inserted at the top of the scroll region to fill the
1719 * vacated rows. The new rows not filled out with the current text attributes.
1720 *
1721 * This function does not affect the scrollback rows at all. Rows shifted
1722 * off the bottom are lost.
1723 *
1724 * @param {integer} count The number of rows to scroll.
1725 */
1726hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001727 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001728
rginda87b86462011-12-14 13:48:03 -08001729 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001730 this.insertLines(opt_count);
1731
rginda87b86462011-12-14 13:48:03 -08001732 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001733};
1734
rginda87b86462011-12-14 13:48:03 -08001735
rginda8ba33642011-12-14 12:31:31 -08001736/**
1737 * Set the cursor position.
1738 *
1739 * The cursor row is relative to the scroll region if the terminal has
1740 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1741 *
1742 * @param {integer} row The new zero-based cursor row.
1743 * @param {integer} row The new zero-based cursor column.
1744 */
1745hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1746 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001747 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001748 } else {
rginda87b86462011-12-14 13:48:03 -08001749 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001750 }
rginda87b86462011-12-14 13:48:03 -08001751};
rginda8ba33642011-12-14 12:31:31 -08001752
rginda87b86462011-12-14 13:48:03 -08001753hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1754 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07001755 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
1756 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001757 this.screen_.setCursorPosition(row, column);
1758};
1759
1760hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07001761 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
1762 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001763 this.screen_.setCursorPosition(row, column);
1764};
1765
1766/**
1767 * Set the cursor column.
1768 *
1769 * @param {integer} column The new zero-based cursor column.
1770 */
1771hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001772 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001773};
1774
1775/**
1776 * Return the cursor column.
1777 *
1778 * @return {integer} The zero-based cursor column.
1779 */
1780hterm.Terminal.prototype.getCursorColumn = function() {
1781 return this.screen_.cursorPosition.column;
1782};
1783
1784/**
1785 * Set the cursor row.
1786 *
1787 * The cursor row is relative to the scroll region if the terminal has
1788 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1789 *
1790 * @param {integer} row The new cursor row.
1791 */
rginda87b86462011-12-14 13:48:03 -08001792hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1793 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001794};
1795
1796/**
1797 * Return the cursor row.
1798 *
1799 * @return {integer} The zero-based cursor row.
1800 */
1801hterm.Terminal.prototype.getCursorRow = function(row) {
1802 return this.screen_.cursorPosition.row;
1803};
1804
1805/**
1806 * Request that the ScrollPort redraw itself soon.
1807 *
1808 * The redraw will happen asynchronously, soon after the call stack winds down.
1809 * Multiple calls will be coalesced into a single redraw.
1810 */
1811hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001812 if (this.timeouts_.redraw)
1813 return;
rginda8ba33642011-12-14 12:31:31 -08001814
1815 var self = this;
rginda87b86462011-12-14 13:48:03 -08001816 this.timeouts_.redraw = setTimeout(function() {
1817 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001818 self.scrollPort_.redraw_();
1819 }, 0);
1820};
1821
1822/**
1823 * Request that the ScrollPort be scrolled to the bottom.
1824 *
1825 * The scroll will happen asynchronously, soon after the call stack winds down.
1826 * Multiple calls will be coalesced into a single scroll.
1827 *
1828 * This affects the scrollbar position of the ScrollPort, and has nothing to
1829 * do with the VT scroll commands.
1830 */
1831hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1832 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001833 return;
rginda8ba33642011-12-14 12:31:31 -08001834
1835 var self = this;
1836 this.timeouts_.scrollDown = setTimeout(function() {
1837 delete self.timeouts_.scrollDown;
1838 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1839 }, 10);
1840};
1841
1842/**
1843 * Move the cursor up a specified number of rows.
1844 *
1845 * @param {integer} count The number of rows to move the cursor.
1846 */
1847hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001848 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001849};
1850
1851/**
1852 * Move the cursor down a specified number of rows.
1853 *
1854 * @param {integer} count The number of rows to move the cursor.
1855 */
1856hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001857 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001858 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1859 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1860 this.screenSize.height - 1);
1861
rgindacbbd7482012-06-13 15:06:16 -07001862 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08001863 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001864 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001865};
1866
1867/**
1868 * Move the cursor left a specified number of columns.
1869 *
1870 * @param {integer} count The number of columns to move the cursor.
1871 */
1872hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001873 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001874};
1875
1876/**
1877 * Move the cursor right a specified number of columns.
1878 *
1879 * @param {integer} count The number of columns to move the cursor.
1880 */
1881hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001882 count = count || 1;
rgindacbbd7482012-06-13 15:06:16 -07001883 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001884 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001885 this.setCursorColumn(column);
1886};
1887
1888/**
1889 * Reverse the foreground and background colors of the terminal.
1890 *
1891 * This only affects text that was drawn with no attributes.
1892 *
1893 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1894 * been drawn with attributes that happen to coincide with the default
1895 * 'no-attribute' colors. My guess is probably not.
1896 */
1897hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001898 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001899 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08001900 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
1901 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08001902 } else {
rginda9f5222b2012-03-05 11:53:28 -08001903 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
1904 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08001905 }
1906};
1907
1908/**
rginda87b86462011-12-14 13:48:03 -08001909 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07001910 *
1911 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08001912 */
1913hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08001914 this.cursorNode_.style.backgroundColor =
1915 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001916
1917 var self = this;
1918 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08001919 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08001920 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07001921
1922 if (this.bellAudio_.getAttribute('src')) {
Robert Gindaa6331372013-03-19 10:35:39 -07001923 if (this.bellSquelchTimeout_)
Robert Ginda92e18102013-03-14 13:56:37 -07001924 return;
1925
1926 this.bellAudio_.play();
1927
1928 this.bellSequelchTimeout_ = setTimeout(function() {
1929 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07001930 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07001931 } else {
1932 delete this.bellSquelchTimeout_;
1933 }
rginda87b86462011-12-14 13:48:03 -08001934};
1935
1936/**
rginda8ba33642011-12-14 12:31:31 -08001937 * Set the origin mode bit.
1938 *
1939 * If origin mode is on, certain VT cursor and scrolling commands measure their
1940 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1941 * to the top of the addressable screen.
1942 *
1943 * Defaults to off.
1944 *
1945 * @param {boolean} state True to set origin mode, false to unset.
1946 */
1947hterm.Terminal.prototype.setOriginMode = function(state) {
1948 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08001949 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08001950};
1951
1952/**
1953 * Set the insert mode bit.
1954 *
1955 * If insert mode is on, existing text beyond the cursor position will be
1956 * shifted right to make room for new text. Otherwise, new text overwrites
1957 * any existing text.
1958 *
1959 * Defaults to off.
1960 *
1961 * @param {boolean} state True to set insert mode, false to unset.
1962 */
1963hterm.Terminal.prototype.setInsertMode = function(state) {
1964 this.options_.insertMode = state;
1965};
1966
1967/**
rginda87b86462011-12-14 13:48:03 -08001968 * Set the auto carriage return bit.
1969 *
1970 * If auto carriage return is on then a formfeed character is interpreted
1971 * as a newline, otherwise it's the same as a linefeed. The difference boils
1972 * down to whether or not the cursor column is reset.
1973 */
1974hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1975 this.options_.autoCarriageReturn = state;
1976};
1977
1978/**
rginda8ba33642011-12-14 12:31:31 -08001979 * Set the wraparound mode bit.
1980 *
1981 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1982 * to the start of the following row. Otherwise, the cursor is clamped to the
1983 * end of the screen and attempts to write past it are ignored.
1984 *
1985 * Defaults to on.
1986 *
1987 * @param {boolean} state True to set wraparound mode, false to unset.
1988 */
1989hterm.Terminal.prototype.setWraparound = function(state) {
1990 this.options_.wraparound = state;
1991};
1992
1993/**
1994 * Set the reverse-wraparound mode bit.
1995 *
1996 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1997 * to the end of the previous row. Otherwise, the cursor is clamped to column
1998 * 0.
1999 *
2000 * Defaults to off.
2001 *
2002 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2003 */
2004hterm.Terminal.prototype.setReverseWraparound = function(state) {
2005 this.options_.reverseWraparound = state;
2006};
2007
2008/**
2009 * Selects between the primary and alternate screens.
2010 *
2011 * If alternate mode is on, the alternate screen is active. Otherwise the
2012 * primary screen is active.
2013 *
2014 * Swapping screens has no effect on the scrollback buffer.
2015 *
2016 * Each screen maintains its own cursor position.
2017 *
2018 * Defaults to off.
2019 *
2020 * @param {boolean} state True to set alternate mode, false to unset.
2021 */
2022hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002023 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002024 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2025
rginda35c456b2012-02-09 17:29:05 -08002026 if (this.screen_.rowsArray.length &&
2027 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2028 // If the screen changed sizes while we were away, our rowIndexes may
2029 // be incorrect.
2030 var offset = this.scrollbackRows_.length;
2031 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002032 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002033 ary[i].rowIndex = offset + i;
2034 }
2035 }
rginda8ba33642011-12-14 12:31:31 -08002036
rginda35c456b2012-02-09 17:29:05 -08002037 this.realizeWidth_(this.screenSize.width);
2038 this.realizeHeight_(this.screenSize.height);
2039 this.scrollPort_.syncScrollHeight();
2040 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002041
rginda6d397402012-01-17 10:58:29 -08002042 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002043 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002044};
2045
2046/**
2047 * Set the cursor-blink mode bit.
2048 *
2049 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2050 * a visible cursor does not blink.
2051 *
2052 * You should make sure to turn blinking off if you're going to dispose of a
2053 * terminal, otherwise you'll leak a timeout.
2054 *
2055 * Defaults to on.
2056 *
2057 * @param {boolean} state True to set cursor-blink mode, false to unset.
2058 */
2059hterm.Terminal.prototype.setCursorBlink = function(state) {
2060 this.options_.cursorBlink = state;
2061
2062 if (!state && this.timeouts_.cursorBlink) {
2063 clearTimeout(this.timeouts_.cursorBlink);
2064 delete this.timeouts_.cursorBlink;
2065 }
2066
2067 if (this.options_.cursorVisible)
2068 this.setCursorVisible(true);
2069};
2070
2071/**
2072 * Set the cursor-visible mode bit.
2073 *
2074 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2075 *
2076 * Defaults to on.
2077 *
2078 * @param {boolean} state True to set cursor-visible mode, false to unset.
2079 */
2080hterm.Terminal.prototype.setCursorVisible = function(state) {
2081 this.options_.cursorVisible = state;
2082
2083 if (!state) {
rginda87b86462011-12-14 13:48:03 -08002084 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002085 return;
2086 }
2087
rginda87b86462011-12-14 13:48:03 -08002088 this.syncCursorPosition_();
2089
2090 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002091
2092 if (this.options_.cursorBlink) {
2093 if (this.timeouts_.cursorBlink)
2094 return;
2095
2096 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
2097 500);
2098 } else {
2099 if (this.timeouts_.cursorBlink) {
2100 clearTimeout(this.timeouts_.cursorBlink);
2101 delete this.timeouts_.cursorBlink;
2102 }
2103 }
2104};
2105
2106/**
rginda87b86462011-12-14 13:48:03 -08002107 * Synchronizes the visible cursor and document selection with the current
2108 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002109 */
2110hterm.Terminal.prototype.syncCursorPosition_ = function() {
2111 var topRowIndex = this.scrollPort_.getTopRowIndex();
2112 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2113 var cursorRowIndex = this.scrollbackRows_.length +
2114 this.screen_.cursorPosition.row;
2115
2116 if (cursorRowIndex > bottomRowIndex) {
2117 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002118 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002119 return;
2120 }
2121
rginda35c456b2012-02-09 17:29:05 -08002122 this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px';
2123 this.cursorNode_.style.height = this.scrollPort_.characterSize.height + 'px';
2124
rginda8ba33642011-12-14 12:31:31 -08002125 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002126 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2127 'px';
2128 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2129 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002130
2131 this.cursorNode_.setAttribute('title',
2132 '(' + this.screen_.cursorPosition.row +
2133 ', ' + this.screen_.cursorPosition.column +
2134 ')');
2135
2136 // Update the caret for a11y purposes.
2137 var selection = this.document_.getSelection();
2138 if (selection && selection.isCollapsed)
2139 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002140};
2141
2142/**
2143 * Synchronizes the visible cursor with the current cursor coordinates.
2144 *
2145 * The sync will happen asynchronously, soon after the call stack winds down.
2146 * Multiple calls will be coalesced into a single sync.
2147 */
2148hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2149 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002150 return;
rginda8ba33642011-12-14 12:31:31 -08002151
2152 var self = this;
2153 this.timeouts_.syncCursor = setTimeout(function() {
2154 self.syncCursorPosition_();
2155 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002156 }, 0);
2157};
2158
rgindacc2996c2012-02-24 14:59:31 -08002159/**
rgindaf522ce02012-04-17 17:49:17 -07002160 * Show or hide the zoom warning.
2161 *
2162 * The zoom warning is a message warning the user that their browser zoom must
2163 * be set to 100% in order for hterm to function properly.
2164 *
2165 * @param {boolean} state True to show the message, false to hide it.
2166 */
2167hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2168 if (!this.zoomWarningNode_) {
2169 if (!state)
2170 return;
2171
2172 this.zoomWarningNode_ = this.document_.createElement('div');
2173 this.zoomWarningNode_.style.cssText = (
2174 'color: black;' +
2175 'background-color: #ff2222;' +
2176 'font-size: large;' +
2177 'border-radius: 8px;' +
2178 'opacity: 0.75;' +
2179 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2180 'top: 0.5em;' +
2181 'right: 1.2em;' +
2182 'position: absolute;' +
2183 '-webkit-text-size-adjust: none;' +
2184 '-webkit-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002185 }
2186
Robert Gindab4839c22013-02-28 16:52:10 -08002187 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2188 hterm.zoomWarningMessage,
2189 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2190
rgindaf522ce02012-04-17 17:49:17 -07002191 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2192
2193 if (state) {
2194 if (!this.zoomWarningNode_.parentNode)
2195 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2196 } else if (this.zoomWarningNode_.parentNode) {
2197 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2198 }
2199};
2200
2201/**
rgindacc2996c2012-02-24 14:59:31 -08002202 * Show the terminal overlay for a given amount of time.
2203 *
2204 * The terminal overlay appears in inverse video in a large font, centered
2205 * over the terminal. You should probably keep the overlay message brief,
2206 * since it's in a large font and you probably aren't going to check the size
2207 * of the terminal first.
2208 *
2209 * @param {string} msg The text (not HTML) message to display in the overlay.
2210 * @param {number} opt_timeout The amount of time to wait before fading out
2211 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2212 * stay up forever (or until the next overlay).
2213 */
2214hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002215 if (!this.overlayNode_) {
2216 if (!this.div_)
2217 return;
2218
2219 this.overlayNode_ = this.document_.createElement('div');
2220 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002221 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002222 'font-size: xx-large;' +
2223 'opacity: 0.75;' +
2224 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2225 'position: absolute;' +
2226 '-webkit-user-select: none;' +
2227 '-webkit-transition: opacity 180ms ease-in;');
2228 }
2229
rginda9f5222b2012-03-05 11:53:28 -08002230 this.overlayNode_.style.color = this.prefs_.get('background-color');
2231 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2232 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2233
rgindaf0090c92012-02-10 14:58:52 -08002234 this.overlayNode_.textContent = msg;
2235 this.overlayNode_.style.opacity = '0.75';
2236
2237 if (!this.overlayNode_.parentNode)
2238 this.div_.appendChild(this.overlayNode_);
2239
Robert Ginda97769282013-02-01 15:30:30 -08002240 var divSize = hterm.getClientSize(this.div_);
2241 var overlaySize = hterm.getClientSize(this.overlayNode_);
2242
2243 this.overlayNode_.style.top = (divSize.height - overlaySize.height) / 2;
2244 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
2245 this.scrollPort_.currentScrollbarWidthPx) / 2;
rgindaf0090c92012-02-10 14:58:52 -08002246
2247 var self = this;
2248
2249 if (this.overlayTimeout_)
2250 clearTimeout(this.overlayTimeout_);
2251
rgindacc2996c2012-02-24 14:59:31 -08002252 if (opt_timeout === null)
2253 return;
2254
rgindaf0090c92012-02-10 14:58:52 -08002255 this.overlayTimeout_ = setTimeout(function() {
2256 self.overlayNode_.style.opacity = '0';
2257 setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002258 if (self.overlayNode_.parentNode)
2259 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002260 self.overlayTimeout_ = null;
2261 self.overlayNode_.style.opacity = '0.75';
2262 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002263 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002264};
2265
rginda4bba5e12012-06-20 16:15:30 -07002266/**
2267 * Paste from the system clipboard to the terminal.
2268 */
2269hterm.Terminal.prototype.paste = function() {
2270 hterm.pasteFromClipboard(this.document_);
2271};
2272
2273/**
2274 * Copy a string to the system clipboard.
2275 *
2276 * Note: If there is a selected range in the terminal, it'll be cleared.
2277 */
2278hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda9fb38222012-09-11 14:19:12 -07002279 if (this.prefs_.get('enable-clipboard-notice'))
Robert Gindab4839c22013-02-28 16:52:10 -08002280 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
rgindaa09e7332012-08-17 12:49:51 -07002281
2282 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002283 copySource.textContent = str;
2284 copySource.style.cssText = (
2285 '-webkit-user-select: text;' +
2286 'position: absolute;' +
2287 'top: -99px');
2288
2289 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002290
rginda4bba5e12012-06-20 16:15:30 -07002291 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002292 var anchorNode = selection.anchorNode;
2293 var anchorOffset = selection.anchorOffset;
2294 var focusNode = selection.focusNode;
2295 var focusOffset = selection.focusOffset;
2296
rginda4bba5e12012-06-20 16:15:30 -07002297 selection.selectAllChildren(copySource);
2298
rgindaa09e7332012-08-17 12:49:51 -07002299 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002300
rgindafaa74742012-08-21 13:34:03 -07002301 selection.collapse(anchorNode, anchorOffset);
2302 selection.extend(focusNode, focusOffset);
2303
rginda4bba5e12012-06-20 16:15:30 -07002304 copySource.parentNode.removeChild(copySource);
2305};
2306
rgindaa09e7332012-08-17 12:49:51 -07002307hterm.Terminal.prototype.getSelectionText = function() {
2308 var selection = this.scrollPort_.selection;
2309 selection.sync();
2310
2311 if (selection.isCollapsed)
2312 return null;
2313
2314
2315 // Start offset measures from the beginning of the line.
2316 var startOffset = selection.startOffset;
2317 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002318
Robert Gindafdbb3f22012-09-06 20:23:06 -07002319 if (node.nodeName != 'X-ROW') {
2320 // If the selection doesn't start on an x-row node, then it must be
2321 // somewhere inside the x-row. Add any characters from previous siblings
2322 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002323
2324 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2325 // If node is the text node in a styled span, move up to the span node.
2326 node = node.parentNode;
2327 }
2328
Robert Gindafdbb3f22012-09-06 20:23:06 -07002329 while (node.previousSibling) {
2330 node = node.previousSibling;
2331 startOffset += node.textContent.length;
2332 }
rgindaa09e7332012-08-17 12:49:51 -07002333 }
2334
2335 // End offset measures from the end of the line.
2336 var endOffset = selection.endNode.textContent.length - selection.endOffset;
2337 var node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002338
Robert Gindafdbb3f22012-09-06 20:23:06 -07002339 if (node.nodeName != 'X-ROW') {
2340 // If the selection doesn't end on an x-row node, then it must be
2341 // somewhere inside the x-row. Add any characters from following siblings
2342 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002343
2344 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2345 // If node is the text node in a styled span, move up to the span node.
2346 node = node.parentNode;
2347 }
2348
Robert Gindafdbb3f22012-09-06 20:23:06 -07002349 while (node.nextSibling) {
2350 node = node.nextSibling;
2351 endOffset += node.textContent.length;
2352 }
rgindaa09e7332012-08-17 12:49:51 -07002353 }
2354
2355 var rv = this.getRowsText(selection.startRow.rowIndex,
2356 selection.endRow.rowIndex + 1);
2357 return rv.substring(startOffset, rv.length - endOffset);
2358};
2359
rginda4bba5e12012-06-20 16:15:30 -07002360/**
2361 * Copy the current selection to the system clipboard, then clear it after a
2362 * short delay.
2363 */
2364hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002365 var text = this.getSelectionText();
2366 if (text != null)
2367 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002368};
2369
rgindaf0090c92012-02-10 14:58:52 -08002370hterm.Terminal.prototype.overlaySize = function() {
2371 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2372};
2373
rginda87b86462011-12-14 13:48:03 -08002374/**
2375 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2376 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07002377 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08002378 */
2379hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002380 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002381 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2382
Robert Ginda8cb7d902013-06-20 14:37:18 -07002383 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08002384};
2385
2386/**
rgindad5613292012-06-19 15:40:37 -07002387 * Add the terminalRow and terminalColumn properties to mouse events and
2388 * then forward on to onMouse().
2389 *
2390 * The terminalRow and terminalColumn properties contain the (row, column)
2391 * coordinates for the mouse event.
2392 */
2393hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002394 if (e.processedByTerminalHandler_) {
2395 // We register our event handlers on the document, as well as the cursor
2396 // and the scroll blocker. Mouse events that occur on the cursor or
2397 // scroll blocker will also appear on the document, but we don't want to
2398 // process them twice.
2399 //
2400 // We can't just prevent bubbling because that has other side effects, so
2401 // we decorate the event object with this property instead.
2402 return;
2403 }
2404
2405 e.processedByTerminalHandler_ = true;
2406
rginda4bba5e12012-06-20 16:15:30 -07002407 if (e.type == 'mousedown' && e.which == this.mousePasteButton) {
2408 this.paste();
2409 return;
2410 }
2411
2412 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2413 !this.document_.getSelection().isCollapsed) {
rgindafaa74742012-08-21 13:34:03 -07002414 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002415 return;
2416 }
2417
rgindad5613292012-06-19 15:40:37 -07002418 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2419 this.scrollPort_.characterSize.height) + 1;
2420 e.terminalColumn = parseInt(e.clientX /
2421 this.scrollPort_.characterSize.width) + 1;
2422
2423 if (e.type == 'mousedown') {
2424 if (e.terminalColumn > this.screenSize.width) {
2425 // Mousedown in the scrollbar area.
2426 return;
2427 }
2428
2429 if (!this.enableMouseDragScroll) {
2430 // Move the scroll-blocker into place if we want to keep the scrollport
2431 // from scrolling.
2432 this.scrollBlockerNode_.engaged = true;
2433 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2434 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2435 }
2436 } else if (this.scrollBlockerNode_.engaged &&
2437 (e.type == 'mousemove' || e.type == 'mouseup')) {
2438 // Disengage the scroll-blocker after one of these events.
2439 this.scrollBlockerNode_.engaged = false;
2440 this.scrollBlockerNode_.style.top = '-99px';
2441 }
2442
rgindafaa74742012-08-21 13:34:03 -07002443 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07002444};
2445
2446/**
2447 * Clients should override this if they care to know about mouse events.
2448 *
2449 * The event parameter will be a normal DOM mouse click event with additional
2450 * 'terminalRow' and 'terminalColumn' properties.
2451 */
2452hterm.Terminal.prototype.onMouse = function(e) { };
2453
2454/**
rginda8e92a692012-05-20 19:37:20 -07002455 * React when focus changes.
2456 */
2457hterm.Terminal.prototype.onFocusChange_ = function(state) {
2458 this.cursorNode_.setAttribute('focus', state ? 'true' : 'false');
2459};
2460
2461/**
rginda8ba33642011-12-14 12:31:31 -08002462 * React when the ScrollPort is scrolled.
2463 */
2464hterm.Terminal.prototype.onScroll_ = function() {
2465 this.scheduleSyncCursorPosition_();
2466};
2467
2468/**
rginda9846e2f2012-01-27 13:53:33 -08002469 * React when text is pasted into the scrollPort.
2470 */
2471hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Ginda8cb7d902013-06-20 14:37:18 -07002472 this.onVTKeystroke(e.text.replace(/\n/mg, '\r'));
rginda9846e2f2012-01-27 13:53:33 -08002473};
2474
2475/**
rgindaa09e7332012-08-17 12:49:51 -07002476 * React when the user tries to copy from the scrollPort.
2477 */
2478hterm.Terminal.prototype.onCopy_ = function(e) {
2479 e.preventDefault();
rgindafaa74742012-08-21 13:34:03 -07002480 this.copySelectionToClipboard();
rgindaa09e7332012-08-17 12:49:51 -07002481};
2482
2483/**
rginda8ba33642011-12-14 12:31:31 -08002484 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002485 *
2486 * Note: This function should not directly contain code that alters the internal
2487 * state of the terminal. That kind of code belongs in realizeWidth or
2488 * realizeHeight, so that it can be executed synchronously in the case of a
2489 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002490 */
2491hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08002492 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002493 this.scrollPort_.characterSize.width);
2494 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
2495 this.scrollPort_.characterSize.height);
2496
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002497 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08002498 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002499 // gets removed from the document or during the initial load, and we can't
2500 // deal with that.
rginda35c456b2012-02-09 17:29:05 -08002501 return;
2502 }
2503
rgindaa8ba17d2012-08-15 14:41:10 -07002504 var isNewSize = (columnCount != this.screenSize.width ||
2505 rowCount != this.screenSize.height);
2506
2507 // We do this even if the size didn't change, just to be sure everything is
2508 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002509 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07002510 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07002511
2512 if (isNewSize)
2513 this.overlaySize();
2514
2515 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08002516};
2517
2518/**
2519 * Service the cursor blink timeout.
2520 */
2521hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08002522 if (this.cursorNode_.style.opacity == '0') {
2523 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002524 } else {
rginda87b86462011-12-14 13:48:03 -08002525 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002526 }
2527};
David Reveman8f552492012-03-28 12:18:41 -04002528
2529/**
2530 * Set the scrollbar-visible mode bit.
2531 *
2532 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2533 * Otherwise it will not.
2534 *
2535 * Defaults to on.
2536 *
2537 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2538 */
2539hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2540 this.scrollPort_.setScrollbarVisible(state);
2541};