blob: 4cdd4ae1b69e665ae877fb34fc70cc2478dbba70 [file] [log] [blame]
rginda87b86462011-12-14 13:48:03 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rginda8ba33642011-12-14 12:31:31 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rgindacbbd7482012-06-13 15:06:16 -07005'use strict';
6
Masaya Suzuki273aa982014-05-31 07:25:55 +09007lib.rtdep('lib.colors', 'lib.PreferenceManager', 'lib.resource', 'lib.wc',
Rob Spiesf4e90e82015-01-28 12:10:13 -08008 'lib.f', 'hterm.Keyboard', 'hterm.Options', 'hterm.PreferenceManager',
Ricky Liang48f05cb2013-12-31 23:35:29 +08009 'hterm.Screen', 'hterm.ScrollPort', 'hterm.Size',
10 'hterm.TextAttributes', 'hterm.VT');
rgindacbbd7482012-06-13 15:06:16 -070011
rginda8ba33642011-12-14 12:31:31 -080012/**
13 * Constructor for the Terminal class.
14 *
15 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
16 * classes to provide the complete terminal functionality.
17 *
18 * There are a number of lower-level Terminal methods that can be called
19 * directly to manipulate the cursor, text, scroll region, and other terminal
20 * attributes. However, the primary method is interpret(), which parses VT
21 * escape sequences and invokes the appropriate Terminal methods.
22 *
23 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
24 *
25 * TODO(rginda): Eventually we're going to need to support characters which are
26 * displayed twice as wide as standard latin characters. This is to support
27 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080028 *
Robert Ginda57f03b42012-09-13 11:02:48 -070029 * @param {string} opt_profileId Optional preference profile name. If not
rginda9f5222b2012-03-05 11:53:28 -080030 * provided, defaults to 'default'.
rginda8ba33642011-12-14 12:31:31 -080031 */
Robert Ginda57f03b42012-09-13 11:02:48 -070032hterm.Terminal = function(opt_profileId) {
33 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080034
rginda8ba33642011-12-14 12:31:31 -080035 // Two screen instances.
36 this.primaryScreen_ = new hterm.Screen();
37 this.alternateScreen_ = new hterm.Screen();
38
39 // The "current" screen.
40 this.screen_ = this.primaryScreen_;
41
rginda8ba33642011-12-14 12:31:31 -080042 // The local notion of the screen size. ScreenBuffers also have a size which
43 // indicates their present size. During size changes, the two may disagree.
44 // Also, the inactive screen's size is not altered until it is made the active
45 // screen.
46 this.screenSize = new hterm.Size(0, 0);
47
rginda8ba33642011-12-14 12:31:31 -080048 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080049 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080050 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
51 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080052 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070053 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080054
rginda87b86462011-12-14 13:48:03 -080055 // The div that contains this terminal.
56 this.div_ = null;
57
rgindac9bc5502012-01-18 11:48:44 -080058 // The document that contains the scrollPort. Defaulted to the global
59 // document here so that the terminal is functional even if it hasn't been
60 // inserted into a document yet, but re-set in decorate().
61 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080062
rginda8ba33642011-12-14 12:31:31 -080063 // The rows that have scrolled off screen and are no longer addressable.
64 this.scrollbackRows_ = [];
65
rgindac9bc5502012-01-18 11:48:44 -080066 // Saved tab stops.
67 this.tabStops_ = [];
68
David Benjamin66e954d2012-05-05 21:08:12 -040069 // Keep track of whether default tab stops have been erased; after a TBC
70 // clears all tab stops, defaults aren't restored on resize until a reset.
71 this.defaultTabStops = true;
72
rginda8ba33642011-12-14 12:31:31 -080073 // The VT's notion of the top and bottom rows. Used during some VT
74 // cursor positioning and scrolling commands.
75 this.vtScrollTop_ = null;
76 this.vtScrollBottom_ = null;
77
78 // The DIV element for the visible cursor.
79 this.cursorNode_ = null;
80
Robert Ginda830583c2013-08-07 13:20:46 -070081 // The current cursor shape of the terminal.
82 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
83
84 // The current color of the cursor.
85 this.cursorColor_ = null;
86
Robert Gindaea2183e2014-07-17 09:51:51 -070087 // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded.
88 this.cursorBlinkCycle_ = [100, 100];
89
90 // Pre-bound onCursorBlink_ handler, so we don't have to do this for each
91 // cursor on/off servicing.
92 this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
93
rginda9f5222b2012-03-05 11:53:28 -080094 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070095 // each output and keystroke. They are initialized by the preference manager.
Robert Ginda8cb7d902013-06-20 14:37:18 -070096 this.backgroundColor_ = null;
97 this.foregroundColor_ = null;
Robert Ginda57f03b42012-09-13 11:02:48 -070098 this.scrollOnOutput_ = null;
99 this.scrollOnKeystroke_ = null;
rginda9f5222b2012-03-05 11:53:28 -0800100
Robert Ginda928cf632014-03-05 15:07:41 -0800101 // True if we should send mouse events to the vt, false if we want them
102 // to manage the local text selection.
103 this.reportMouseEvents_ = false;
104
rgindaf0090c92012-02-10 14:58:52 -0800105 // Terminal bell sound.
106 this.bellAudio_ = this.document_.createElement('audio');
rgindaf0090c92012-02-10 14:58:52 -0800107 this.bellAudio_.setAttribute('preload', 'auto');
108
Michael Kelly485ecd12014-06-09 11:41:56 -0400109 // All terminal bell notifications that have been generated (not necessarily
110 // shown).
111 this.bellNotificationList_ = [];
112
113 // Whether we have permission to display notifications.
114 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400115
rginda6d397402012-01-17 10:58:29 -0800116 // Cursor position and attributes saved with DECSC.
117 this.savedOptions_ = {};
118
rginda8ba33642011-12-14 12:31:31 -0800119 // The current mode bits for the terminal.
120 this.options_ = new hterm.Options();
121
122 // Timeouts we might need to clear.
123 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800124
125 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800126 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800127
rgindafeaf3142012-01-31 15:14:20 -0800128 // The keyboard hander.
129 this.keyboard = new hterm.Keyboard(this);
130
rginda87b86462011-12-14 13:48:03 -0800131 // General IO interface that can be given to third parties without exposing
132 // the entire terminal object.
133 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800134
rgindad5613292012-06-19 15:40:37 -0700135 // True if mouse-click-drag should scroll the terminal.
136 this.enableMouseDragScroll = true;
137
Robert Ginda57f03b42012-09-13 11:02:48 -0700138 this.copyOnSelect = null;
rginda4bba5e12012-06-20 16:15:30 -0700139 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700140
Rob Spies0bec09b2014-06-06 15:58:09 -0700141 // Whether to use the default window copy behaviour.
142 this.useDefaultWindowCopy = false;
143
144 this.clearSelectionAfterCopy = true;
145
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400146 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800147 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700148
149 this.setProfile(opt_profileId || 'default',
150 function() { this.onTerminalReady() }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800151};
152
153/**
Robert Ginda830583c2013-08-07 13:20:46 -0700154 * Possible cursor shapes.
155 */
156hterm.Terminal.cursorShape = {
157 BLOCK: 'BLOCK',
158 BEAM: 'BEAM',
159 UNDERLINE: 'UNDERLINE'
160};
161
162/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700163 * Clients should override this to be notified when the terminal is ready
164 * for use.
165 *
166 * The terminal initialization is asynchronous, and shouldn't be used before
167 * this method is called.
168 */
169hterm.Terminal.prototype.onTerminalReady = function() { };
170
171/**
rginda35c456b2012-02-09 17:29:05 -0800172 * Default tab with of 8 to match xterm.
173 */
174hterm.Terminal.prototype.tabWidth = 8;
175
176/**
rginda9f5222b2012-03-05 11:53:28 -0800177 * Select a preference profile.
178 *
179 * This will load the terminal preferences for the given profile name and
180 * associate subsequent preference changes with the new preference profile.
181 *
182 * @param {string} newName The name of the preference profile. Forward slash
183 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700184 * @param {function} opt_callback Optional callback to invoke when the profile
185 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800186 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700187hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
188 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800189
Robert Ginda57f03b42012-09-13 11:02:48 -0700190 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800191
Robert Ginda57f03b42012-09-13 11:02:48 -0700192 if (this.prefs_)
193 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800194
Robert Ginda57f03b42012-09-13 11:02:48 -0700195 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
196 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800197 'alt-gr-mode': function(v) {
198 if (v == null) {
199 if (navigator.language.toLowerCase() == 'en-us') {
200 v = 'none';
201 } else {
202 v = 'right-alt';
203 }
204 } else if (typeof v == 'string') {
205 v = v.toLowerCase();
206 } else {
207 v = 'none';
208 }
209
210 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v))
211 v = 'none';
212
213 terminal.keyboard.altGrMode = v;
214 },
215
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700216 'alt-backspace-is-meta-backspace': function(v) {
217 terminal.keyboard.altBackspaceIsMetaBackspace = v;
218 },
219
Robert Ginda57f03b42012-09-13 11:02:48 -0700220 'alt-is-meta': function(v) {
221 terminal.keyboard.altIsMeta = v;
222 },
223
224 'alt-sends-what': function(v) {
225 if (!/^(escape|8-bit|browser-key)$/.test(v))
226 v = 'escape';
227
228 terminal.keyboard.altSendsWhat = v;
229 },
230
231 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800232 var ary = v.match(/^lib-resource:(\S+)/);
233 if (ary) {
234 terminal.bellAudio_.setAttribute('src',
235 lib.resource.getDataUrl(ary[1]));
236 } else {
237 terminal.bellAudio_.setAttribute('src', v);
238 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700239 },
240
Michael Kelly485ecd12014-06-09 11:41:56 -0400241 'desktop-notification-bell': function(v) {
242 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700243 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400244 Notification.permission === 'granted';
245 if (!terminal.desktopNotificationBell_) {
246 // Note: We don't call Notification.requestPermission here because
247 // Chrome requires the call be the result of a user action (such as an
248 // onclick handler), and pref listeners are run asynchronously.
249 //
250 // A way of working around this would be to display a dialog in the
251 // terminal with a "click-to-request-permission" button.
252 console.warn('desktop-notification-bell is true but we do not have ' +
253 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400254 }
255 } else {
256 terminal.desktopNotificationBell_ = false;
257 }
258 },
259
Robert Ginda57f03b42012-09-13 11:02:48 -0700260 'background-color': function(v) {
261 terminal.setBackgroundColor(v);
262 },
263
264 'background-image': function(v) {
265 terminal.scrollPort_.setBackgroundImage(v);
266 },
267
268 'background-size': function(v) {
269 terminal.scrollPort_.setBackgroundSize(v);
270 },
271
272 'background-position': function(v) {
273 terminal.scrollPort_.setBackgroundPosition(v);
274 },
275
276 'backspace-sends-backspace': function(v) {
277 terminal.keyboard.backspaceSendsBackspace = v;
278 },
279
Brad Town18654b62015-03-12 00:27:45 -0700280 'character-map-overrides': function(v) {
281 if (!(v == null || v instanceof Object)) {
282 console.warn('Preference character-map-modifications is not an ' +
283 'object: ' + v);
284 return;
285 }
286
287 for (var code in v) {
288 var glmap = hterm.VT.CharacterMap.maps[code].glmap;
289 for (var received in v[code]) {
290 glmap[received] = v[code][received];
291 }
292 hterm.VT.CharacterMap.maps[code].reset(glmap);
293 }
294 },
295
Robert Ginda57f03b42012-09-13 11:02:48 -0700296 'cursor-blink': function(v) {
297 terminal.setCursorBlink(!!v);
298 },
299
Robert Gindaea2183e2014-07-17 09:51:51 -0700300 'cursor-blink-cycle': function(v) {
301 if (v instanceof Array &&
302 typeof v[0] == 'number' &&
303 typeof v[1] == 'number') {
304 terminal.cursorBlinkCycle_ = v;
305 } else if (typeof v == 'number') {
306 terminal.cursorBlinkCycle_ = [v, v];
307 } else {
308 // Fast blink indicates an error.
309 terminal.cursorBlinkCycle_ = [100, 100];
310 }
311 },
312
Robert Ginda57f03b42012-09-13 11:02:48 -0700313 'cursor-color': function(v) {
314 terminal.setCursorColor(v);
315 },
316
317 'color-palette-overrides': function(v) {
318 if (!(v == null || v instanceof Object || v instanceof Array)) {
319 console.warn('Preference color-palette-overrides is not an array or ' +
320 'object: ' + v);
321 return;
rginda9f5222b2012-03-05 11:53:28 -0800322 }
rginda9f5222b2012-03-05 11:53:28 -0800323
Robert Ginda57f03b42012-09-13 11:02:48 -0700324 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700325
Robert Ginda57f03b42012-09-13 11:02:48 -0700326 if (v) {
327 for (var key in v) {
328 var i = parseInt(key);
329 if (isNaN(i) || i < 0 || i > 255) {
330 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
331 continue;
332 }
333
334 if (v[i]) {
335 var rgb = lib.colors.normalizeCSS(v[i]);
336 if (rgb)
337 lib.colors.colorPalette[i] = rgb;
338 }
339 }
rginda30f20f62012-04-05 16:36:19 -0700340 }
rginda30f20f62012-04-05 16:36:19 -0700341
Robert Ginda57f03b42012-09-13 11:02:48 -0700342 terminal.primaryScreen_.textAttributes.resetColorPalette()
343 terminal.alternateScreen_.textAttributes.resetColorPalette();
344 },
rginda30f20f62012-04-05 16:36:19 -0700345
Robert Ginda57f03b42012-09-13 11:02:48 -0700346 'copy-on-select': function(v) {
347 terminal.copyOnSelect = !!v;
348 },
rginda9f5222b2012-03-05 11:53:28 -0800349
Rob Spies0bec09b2014-06-06 15:58:09 -0700350 'use-default-window-copy': function(v) {
351 terminal.useDefaultWindowCopy = !!v;
352 },
353
354 'clear-selection-after-copy': function(v) {
355 terminal.clearSelectionAfterCopy = !!v;
356 },
357
Robert Ginda7e5e9522014-03-14 12:23:58 -0700358 'ctrl-plus-minus-zero-zoom': function(v) {
359 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
360 },
361
Robert Gindafb5a3f92014-05-13 14:12:00 -0700362 'ctrl-c-copy': function(v) {
363 terminal.keyboard.ctrlCCopy = v;
364 },
365
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100366 'ctrl-v-paste': function(v) {
367 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700368 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100369 },
370
Masaya Suzuki273aa982014-05-31 07:25:55 +0900371 'east-asian-ambiguous-as-two-column': function(v) {
372 lib.wc.regardCjkAmbiguous = v;
373 },
374
Robert Ginda57f03b42012-09-13 11:02:48 -0700375 'enable-8-bit-control': function(v) {
376 terminal.vt.enable8BitControl = !!v;
377 },
rginda30f20f62012-04-05 16:36:19 -0700378
Robert Ginda57f03b42012-09-13 11:02:48 -0700379 'enable-bold': function(v) {
380 terminal.syncBoldSafeState();
381 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400382
Robert Ginda3e278d72014-03-25 13:18:51 -0700383 'enable-bold-as-bright': function(v) {
384 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
385 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
386 },
387
Robert Ginda57f03b42012-09-13 11:02:48 -0700388 'enable-clipboard-write': function(v) {
389 terminal.vt.enableClipboardWrite = !!v;
390 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400391
Robert Ginda3755e752013-05-31 13:34:09 -0700392 'enable-dec12': function(v) {
393 terminal.vt.enableDec12 = !!v;
394 },
395
Robert Ginda57f03b42012-09-13 11:02:48 -0700396 'font-family': function(v) {
397 terminal.syncFontFamily();
398 },
rginda30f20f62012-04-05 16:36:19 -0700399
Robert Ginda57f03b42012-09-13 11:02:48 -0700400 'font-size': function(v) {
401 terminal.setFontSize(v);
402 },
rginda9875d902012-08-20 16:21:57 -0700403
Robert Ginda57f03b42012-09-13 11:02:48 -0700404 'font-smoothing': function(v) {
405 terminal.syncFontFamily();
406 },
rgindade84e382012-04-20 15:39:31 -0700407
Robert Ginda57f03b42012-09-13 11:02:48 -0700408 'foreground-color': function(v) {
409 terminal.setForegroundColor(v);
410 },
rginda30f20f62012-04-05 16:36:19 -0700411
Robert Ginda57f03b42012-09-13 11:02:48 -0700412 'home-keys-scroll': function(v) {
413 terminal.keyboard.homeKeysScroll = v;
414 },
rginda4bba5e12012-06-20 16:15:30 -0700415
Robert Ginda57f03b42012-09-13 11:02:48 -0700416 'max-string-sequence': function(v) {
417 terminal.vt.maxStringSequence = v;
418 },
rginda11057d52012-04-25 12:29:56 -0700419
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700420 'media-keys-are-fkeys': function(v) {
421 terminal.keyboard.mediaKeysAreFKeys = v;
422 },
423
Robert Ginda57f03b42012-09-13 11:02:48 -0700424 'meta-sends-escape': function(v) {
425 terminal.keyboard.metaSendsEscape = v;
426 },
rginda30f20f62012-04-05 16:36:19 -0700427
Robert Ginda57f03b42012-09-13 11:02:48 -0700428 'mouse-paste-button': function(v) {
429 terminal.syncMousePasteButton();
430 },
rgindaa8ba17d2012-08-15 14:41:10 -0700431
Robert Gindae76aa9f2014-03-14 12:29:12 -0700432 'page-keys-scroll': function(v) {
433 terminal.keyboard.pageKeysScroll = v;
434 },
435
Robert Ginda40932892012-12-10 17:26:40 -0800436 'pass-alt-number': function(v) {
437 if (v == null) {
438 var osx = window.navigator.userAgent.match(/Mac OS X/);
439
440 // Let Alt-1..9 pass to the browser (to control tab switching) on
441 // non-OS X systems, or if hterm is not opened in an app window.
442 v = (!osx && hterm.windowType != 'popup');
443 }
444
445 terminal.passAltNumber = v;
446 },
447
448 'pass-ctrl-number': function(v) {
449 if (v == null) {
450 var osx = window.navigator.userAgent.match(/Mac OS X/);
451
452 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
453 // non-OS X systems, or if hterm is not opened in an app window.
454 v = (!osx && hterm.windowType != 'popup');
455 }
456
457 terminal.passCtrlNumber = v;
458 },
459
460 'pass-meta-number': function(v) {
461 if (v == null) {
462 var osx = window.navigator.userAgent.match(/Mac OS X/);
463
464 // Let Meta-1..9 pass to the browser (to control tab switching) on
465 // OS X systems, or if hterm is not opened in an app window.
466 v = (osx && hterm.windowType != 'popup');
467 }
468
469 terminal.passMetaNumber = v;
470 },
471
Marius Schilder77857b32014-05-14 16:21:26 -0700472 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700473 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700474 },
475
Robert Ginda8cb7d902013-06-20 14:37:18 -0700476 'receive-encoding': function(v) {
477 if (!(/^(utf-8|raw)$/).test(v)) {
478 console.warn('Invalid value for "receive-encoding": ' + v);
479 v = 'utf-8';
480 }
481
482 terminal.vt.characterEncoding = v;
483 },
484
Robert Ginda57f03b42012-09-13 11:02:48 -0700485 'scroll-on-keystroke': function(v) {
486 terminal.scrollOnKeystroke_ = v;
487 },
rginda9f5222b2012-03-05 11:53:28 -0800488
Robert Ginda57f03b42012-09-13 11:02:48 -0700489 'scroll-on-output': function(v) {
490 terminal.scrollOnOutput_ = v;
491 },
rginda30f20f62012-04-05 16:36:19 -0700492
Robert Ginda57f03b42012-09-13 11:02:48 -0700493 'scrollbar-visible': function(v) {
494 terminal.setScrollbarVisible(v);
495 },
rginda9f5222b2012-03-05 11:53:28 -0800496
Rob Spies49039e52014-12-17 13:40:04 -0800497 'scroll-wheel-move-multiplier': function(v) {
498 terminal.setScrollWheelMoveMultipler(v);
499 },
500
Robert Ginda8cb7d902013-06-20 14:37:18 -0700501 'send-encoding': function(v) {
502 if (!(/^(utf-8|raw)$/).test(v)) {
503 console.warn('Invalid value for "send-encoding": ' + v);
504 v = 'utf-8';
505 }
506
507 terminal.keyboard.characterEncoding = v;
508 },
509
Robert Ginda57f03b42012-09-13 11:02:48 -0700510 'shift-insert-paste': function(v) {
511 terminal.keyboard.shiftInsertPaste = v;
512 },
rginda9f5222b2012-03-05 11:53:28 -0800513
Robert Gindae76aa9f2014-03-14 12:29:12 -0700514 'user-css': function(v) {
515 terminal.scrollPort_.setUserCss(v);
Robert Ginda57f03b42012-09-13 11:02:48 -0700516 }
517 });
rginda30f20f62012-04-05 16:36:19 -0700518
Robert Ginda57f03b42012-09-13 11:02:48 -0700519 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800520 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700521
522 if (opt_callback)
523 opt_callback();
524 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800525};
526
Rob Spies56953412014-04-28 14:09:47 -0700527
528/**
529 * Returns the preferences manager used for configuring this terminal.
530 */
531hterm.Terminal.prototype.getPrefs = function() {
532 return this.prefs_;
533};
534
Robert Gindaa063b202014-07-21 11:08:25 -0700535/**
536 * Enable or disable bracketed paste mode.
537 */
538hterm.Terminal.prototype.setBracketedPaste = function(state) {
539 this.options_.bracketedPaste = state;
540};
Rob Spies56953412014-04-28 14:09:47 -0700541
rginda8e92a692012-05-20 19:37:20 -0700542/**
543 * Set the color for the cursor.
544 *
545 * If you want this setting to persist, set it through prefs_, rather than
546 * with this method.
547 */
548hterm.Terminal.prototype.setCursorColor = function(color) {
Robert Ginda830583c2013-08-07 13:20:46 -0700549 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700550 this.cursorNode_.style.backgroundColor = color;
551 this.cursorNode_.style.borderColor = color;
552};
553
554/**
555 * Return the current cursor color as a string.
556 */
557hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700558 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700559};
560
561/**
rgindad5613292012-06-19 15:40:37 -0700562 * Enable or disable mouse based text selection in the terminal.
563 */
564hterm.Terminal.prototype.setSelectionEnabled = function(state) {
565 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700566};
567
568/**
rginda8e92a692012-05-20 19:37:20 -0700569 * Set the background color.
570 *
571 * If you want this setting to persist, set it through prefs_, rather than
572 * with this method.
573 */
574hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700575 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700576 this.primaryScreen_.textAttributes.setDefaults(
577 this.foregroundColor_, this.backgroundColor_);
578 this.alternateScreen_.textAttributes.setDefaults(
579 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700580 this.scrollPort_.setBackgroundColor(color);
581};
582
rginda9f5222b2012-03-05 11:53:28 -0800583/**
584 * Return the current terminal background color.
585 *
586 * Intended for use by other classes, so we don't have to expose the entire
587 * prefs_ object.
588 */
589hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700590 return this.backgroundColor_;
591};
592
593/**
594 * Set the foreground color.
595 *
596 * If you want this setting to persist, set it through prefs_, rather than
597 * with this method.
598 */
599hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700600 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700601 this.primaryScreen_.textAttributes.setDefaults(
602 this.foregroundColor_, this.backgroundColor_);
603 this.alternateScreen_.textAttributes.setDefaults(
604 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700605 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800606};
607
608/**
609 * Return the current terminal foreground color.
610 *
611 * Intended for use by other classes, so we don't have to expose the entire
612 * prefs_ object.
613 */
614hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700615 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800616};
617
618/**
rginda87b86462011-12-14 13:48:03 -0800619 * Create a new instance of a terminal command and run it with a given
620 * argument string.
621 *
622 * @param {function} commandClass The constructor for a terminal command.
623 * @param {string} argString The argument string to pass to the command.
624 */
625hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700626 var environment = this.prefs_.get('environment');
627 if (typeof environment != 'object' || environment == null)
628 environment = {};
629
rginda87b86462011-12-14 13:48:03 -0800630 var self = this;
631 this.command = new commandClass(
632 { argString: argString || '',
633 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700634 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800635 onExit: function(code) {
636 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800637 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700638 if (self.prefs_.get('close-on-exit'))
639 window.close();
rginda87b86462011-12-14 13:48:03 -0800640 }
641 });
642
rgindafeaf3142012-01-31 15:14:20 -0800643 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800644 this.command.run();
645};
646
647/**
rgindafeaf3142012-01-31 15:14:20 -0800648 * Returns true if the current screen is the primary screen, false otherwise.
649 */
650hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700651 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800652};
653
654/**
655 * Install the keyboard handler for this terminal.
656 *
657 * This will prevent the browser from seeing any keystrokes sent to the
658 * terminal.
659 */
660hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700661 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
rgindafeaf3142012-01-31 15:14:20 -0800662}
663
664/**
665 * Uninstall the keyboard handler for this terminal.
666 */
667hterm.Terminal.prototype.uninstallKeyboard = function() {
668 this.keyboard.installKeyboard(null);
669}
670
671/**
rginda35c456b2012-02-09 17:29:05 -0800672 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800673 *
674 * Call setFontSize(0) to reset to the default font size.
675 *
676 * This function does not modify the font-size preference.
677 *
678 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800679 */
680hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800681 if (px === 0)
682 px = this.prefs_.get('font-size');
683
rginda35c456b2012-02-09 17:29:05 -0800684 this.scrollPort_.setFontSize(px);
Ricky Liang48f05cb2013-12-31 23:35:29 +0800685 if (this.wcCssRule_) {
686 this.wcCssRule_.style.width = this.scrollPort_.characterSize.width * 2 +
687 'px';
688 }
rginda35c456b2012-02-09 17:29:05 -0800689};
690
691/**
692 * Get the current font size.
693 */
694hterm.Terminal.prototype.getFontSize = function() {
695 return this.scrollPort_.getFontSize();
696};
697
698/**
rginda8e92a692012-05-20 19:37:20 -0700699 * Get the current font family.
700 */
701hterm.Terminal.prototype.getFontFamily = function() {
702 return this.scrollPort_.getFontFamily();
703};
704
705/**
rginda35c456b2012-02-09 17:29:05 -0800706 * Set the CSS "font-family" for this terminal.
707 */
rginda9f5222b2012-03-05 11:53:28 -0800708hterm.Terminal.prototype.syncFontFamily = function() {
709 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
710 this.prefs_.get('font-smoothing'));
711 this.syncBoldSafeState();
712};
713
rginda4bba5e12012-06-20 16:15:30 -0700714/**
715 * Set this.mousePasteButton based on the mouse-paste-button pref,
716 * autodetecting if necessary.
717 */
718hterm.Terminal.prototype.syncMousePasteButton = function() {
719 var button = this.prefs_.get('mouse-paste-button');
720 if (typeof button == 'number') {
721 this.mousePasteButton = button;
722 return;
723 }
724
725 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
726 if (!ary || ary[2] == 'CrOS') {
727 this.mousePasteButton = 2;
728 } else {
729 this.mousePasteButton = 3;
730 }
731};
732
733/**
734 * Enable or disable bold based on the enable-bold pref, autodetecting if
735 * necessary.
736 */
rginda9f5222b2012-03-05 11:53:28 -0800737hterm.Terminal.prototype.syncBoldSafeState = function() {
738 var enableBold = this.prefs_.get('enable-bold');
739 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700740 this.primaryScreen_.textAttributes.enableBold = enableBold;
741 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800742 return;
743 }
744
rgindaf7521392012-02-28 17:20:34 -0800745 var normalSize = this.scrollPort_.measureCharacterSize();
746 var boldSize = this.scrollPort_.measureCharacterSize('bold');
747
748 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800749 if (!isBoldSafe) {
750 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700751 'from normal. Font family is: ' +
752 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800753 }
rginda9f5222b2012-03-05 11:53:28 -0800754
Robert Gindaed016262012-10-26 16:27:09 -0700755 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
756 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800757};
758
759/**
rginda87b86462011-12-14 13:48:03 -0800760 * Return a copy of the current cursor position.
761 *
762 * @return {hterm.RowCol} The RowCol object representing the current position.
763 */
764hterm.Terminal.prototype.saveCursor = function() {
765 return this.screen_.cursorPosition.clone();
766};
767
rgindaa19afe22012-01-25 15:40:22 -0800768hterm.Terminal.prototype.getTextAttributes = function() {
769 return this.screen_.textAttributes;
770};
771
rginda1a09aa02012-06-18 21:11:25 -0700772hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
773 this.screen_.textAttributes = textAttributes;
774};
775
rginda87b86462011-12-14 13:48:03 -0800776/**
rgindaf522ce02012-04-17 17:49:17 -0700777 * Return the current browser zoom factor applied to the terminal.
778 *
779 * @return {number} The current browser zoom factor.
780 */
781hterm.Terminal.prototype.getZoomFactor = function() {
782 return this.scrollPort_.characterSize.zoomFactor;
783};
784
785/**
rginda9846e2f2012-01-27 13:53:33 -0800786 * Change the title of this terminal's window.
787 */
788hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800789 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800790};
791
792/**
rginda87b86462011-12-14 13:48:03 -0800793 * Restore a previously saved cursor position.
794 *
795 * @param {hterm.RowCol} cursor The position to restore.
796 */
797hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700798 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
799 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800800 this.screen_.setCursorPosition(row, column);
801 if (cursor.column > column ||
802 cursor.column == column && cursor.overflow) {
803 this.screen_.cursorPosition.overflow = true;
804 }
rginda87b86462011-12-14 13:48:03 -0800805};
806
807/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400808 * Clear the cursor's overflow flag.
809 */
810hterm.Terminal.prototype.clearCursorOverflow = function() {
811 this.screen_.cursorPosition.overflow = false;
812};
813
814/**
Robert Ginda830583c2013-08-07 13:20:46 -0700815 * Sets the cursor shape
816 */
817hterm.Terminal.prototype.setCursorShape = function(shape) {
818 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -0800819 this.restyleCursor_();
Robert Ginda830583c2013-08-07 13:20:46 -0700820}
821
822/**
823 * Get the cursor shape
824 */
825hterm.Terminal.prototype.getCursorShape = function() {
826 return this.cursorShape_;
827}
828
829/**
rginda87b86462011-12-14 13:48:03 -0800830 * Set the width of the terminal, resizing the UI to match.
831 */
832hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800833 if (columnCount == null) {
834 this.div_.style.width = '100%';
835 return;
836 }
837
Robert Ginda26806d12014-07-24 13:44:07 -0700838 this.div_.style.width = Math.ceil(
839 this.scrollPort_.characterSize.width *
840 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400841 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800842 this.scheduleSyncCursorPosition_();
843};
rginda87b86462011-12-14 13:48:03 -0800844
rgindac9bc5502012-01-18 11:48:44 -0800845/**
rginda35c456b2012-02-09 17:29:05 -0800846 * Set the height of the terminal, resizing the UI to match.
847 */
848hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800849 if (rowCount == null) {
850 this.div_.style.height = '100%';
851 return;
852 }
853
rginda35c456b2012-02-09 17:29:05 -0800854 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700855 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800856 this.realizeSize_(this.screenSize.width, rowCount);
857 this.scheduleSyncCursorPosition_();
858};
859
860/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400861 * Deal with terminal size changes.
862 *
863 */
864hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
865 if (columnCount != this.screenSize.width)
866 this.realizeWidth_(columnCount);
867
868 if (rowCount != this.screenSize.height)
869 this.realizeHeight_(rowCount);
870
871 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -0700872 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400873};
874
875/**
rgindac9bc5502012-01-18 11:48:44 -0800876 * Deal with terminal width changes.
877 *
878 * This function does what needs to be done when the terminal width changes
879 * out from under us. It happens here rather than in onResize_() because this
880 * code may need to run synchronously to handle programmatic changes of
881 * terminal width.
882 *
883 * Relying on the browser to send us an async resize event means we may not be
884 * in the correct state yet when the next escape sequence hits.
885 */
886hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700887 if (columnCount <= 0)
888 throw new Error('Attempt to realize bad width: ' + columnCount);
889
rgindac9bc5502012-01-18 11:48:44 -0800890 var deltaColumns = columnCount - this.screen_.getWidth();
891
rginda87b86462011-12-14 13:48:03 -0800892 this.screenSize.width = columnCount;
893 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800894
895 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400896 if (this.defaultTabStops)
897 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800898 } else {
899 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400900 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800901 break;
902
903 this.tabStops_.pop();
904 }
905 }
906
907 this.screen_.setColumnCount(this.screenSize.width);
908};
909
910/**
911 * Deal with terminal height changes.
912 *
913 * This function does what needs to be done when the terminal height changes
914 * out from under us. It happens here rather than in onResize_() because this
915 * code may need to run synchronously to handle programmatic changes of
916 * terminal height.
917 *
918 * Relying on the browser to send us an async resize event means we may not be
919 * in the correct state yet when the next escape sequence hits.
920 */
921hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700922 if (rowCount <= 0)
923 throw new Error('Attempt to realize bad height: ' + rowCount);
924
rgindac9bc5502012-01-18 11:48:44 -0800925 var deltaRows = rowCount - this.screen_.getHeight();
926
927 this.screenSize.height = rowCount;
928
929 var cursor = this.saveCursor();
930
931 if (deltaRows < 0) {
932 // Screen got smaller.
933 deltaRows *= -1;
934 while (deltaRows) {
935 var lastRow = this.getRowCount() - 1;
936 if (lastRow - this.scrollbackRows_.length == cursor.row)
937 break;
938
939 if (this.getRowText(lastRow))
940 break;
941
942 this.screen_.popRow();
943 deltaRows--;
944 }
945
946 var ary = this.screen_.shiftRows(deltaRows);
947 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
948
949 // We just removed rows from the top of the screen, we need to update
950 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800951 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800952 } else if (deltaRows > 0) {
953 // Screen got larger.
954
955 if (deltaRows <= this.scrollbackRows_.length) {
956 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
957 var rows = this.scrollbackRows_.splice(
958 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
959 this.screen_.unshiftRows(rows);
960 deltaRows -= scrollbackCount;
961 cursor.row += scrollbackCount;
962 }
963
964 if (deltaRows)
965 this.appendRows_(deltaRows);
966 }
967
rginda35c456b2012-02-09 17:29:05 -0800968 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800969 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800970};
971
972/**
973 * Scroll the terminal to the top of the scrollback buffer.
974 */
975hterm.Terminal.prototype.scrollHome = function() {
976 this.scrollPort_.scrollRowToTop(0);
977};
978
979/**
980 * Scroll the terminal to the end.
981 */
982hterm.Terminal.prototype.scrollEnd = function() {
983 this.scrollPort_.scrollRowToBottom(this.getRowCount());
984};
985
986/**
987 * Scroll the terminal one page up (minus one line) relative to the current
988 * position.
989 */
990hterm.Terminal.prototype.scrollPageUp = function() {
991 var i = this.scrollPort_.getTopRowIndex();
992 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
993};
994
995/**
996 * Scroll the terminal one page down (minus one line) relative to the current
997 * position.
998 */
999hterm.Terminal.prototype.scrollPageDown = function() {
1000 var i = this.scrollPort_.getTopRowIndex();
1001 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -08001002};
1003
rgindac9bc5502012-01-18 11:48:44 -08001004/**
Robert Ginda40932892012-12-10 17:26:40 -08001005 * Clear primary screen, secondary screen, and the scrollback buffer.
1006 */
1007hterm.Terminal.prototype.wipeContents = function() {
1008 this.scrollbackRows_.length = 0;
1009 this.scrollPort_.resetCache();
1010
1011 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
1012 var bottom = screen.getHeight();
1013 if (bottom > 0) {
1014 this.renumberRows_(0, bottom);
1015 this.clearHome(screen);
1016 }
1017 }.bind(this));
1018
1019 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001020 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001021};
1022
1023/**
rgindac9bc5502012-01-18 11:48:44 -08001024 * Full terminal reset.
1025 */
rginda87b86462011-12-14 13:48:03 -08001026hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -08001027 this.clearAllTabStops();
1028 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001029
1030 this.clearHome(this.primaryScreen_);
1031 this.primaryScreen_.textAttributes.reset();
1032
1033 this.clearHome(this.alternateScreen_);
1034 this.alternateScreen_.textAttributes.reset();
1035
rgindab8bc8932012-04-27 12:45:03 -07001036 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1037
Robert Ginda92e18102013-03-14 13:56:37 -07001038 this.vt.reset();
1039
rgindac9bc5502012-01-18 11:48:44 -08001040 this.softReset();
rginda87b86462011-12-14 13:48:03 -08001041};
1042
rgindac9bc5502012-01-18 11:48:44 -08001043/**
1044 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001045 *
1046 * Perform a soft reset to the default values listed in
1047 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001048 */
rginda0f5c0292012-01-13 11:00:13 -08001049hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -07001050 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001051 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001052
rgindab8bc8932012-04-27 12:45:03 -07001053 // Xterm also resets the color palette on soft reset, even though it doesn't
1054 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -07001055 this.primaryScreen_.textAttributes.resetColorPalette();
1056 this.alternateScreen_.textAttributes.resetColorPalette();
1057
rgindab8bc8932012-04-27 12:45:03 -07001058 // The xterm man page explicitly says this will happen on soft reset.
1059 this.setVTScrollRegion(null, null);
1060
1061 // Xterm also shows the cursor on soft reset, but does not alter the blink
1062 // state.
rgindaa19afe22012-01-25 15:40:22 -08001063 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001064};
1065
rgindac9bc5502012-01-18 11:48:44 -08001066/**
1067 * Move the cursor forward to the next tab stop, or to the last column
1068 * if no more tab stops are set.
1069 */
1070hterm.Terminal.prototype.forwardTabStop = function() {
1071 var column = this.screen_.cursorPosition.column;
1072
1073 for (var i = 0; i < this.tabStops_.length; i++) {
1074 if (this.tabStops_[i] > column) {
1075 this.setCursorColumn(this.tabStops_[i]);
1076 return;
1077 }
1078 }
1079
David Benjamin66e954d2012-05-05 21:08:12 -04001080 // xterm does not clear the overflow flag on HT or CHT.
1081 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001082 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001083 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001084};
1085
rgindac9bc5502012-01-18 11:48:44 -08001086/**
1087 * Move the cursor backward to the previous tab stop, or to the first column
1088 * if no previous tab stops are set.
1089 */
1090hterm.Terminal.prototype.backwardTabStop = function() {
1091 var column = this.screen_.cursorPosition.column;
1092
1093 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1094 if (this.tabStops_[i] < column) {
1095 this.setCursorColumn(this.tabStops_[i]);
1096 return;
1097 }
1098 }
1099
1100 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001101};
1102
rgindac9bc5502012-01-18 11:48:44 -08001103/**
1104 * Set a tab stop at the given column.
1105 *
1106 * @param {int} column Zero based column.
1107 */
1108hterm.Terminal.prototype.setTabStop = function(column) {
1109 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1110 if (this.tabStops_[i] == column)
1111 return;
1112
1113 if (this.tabStops_[i] < column) {
1114 this.tabStops_.splice(i + 1, 0, column);
1115 return;
1116 }
1117 }
1118
1119 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001120};
1121
rgindac9bc5502012-01-18 11:48:44 -08001122/**
1123 * Clear the tab stop at the current cursor position.
1124 *
1125 * No effect if there is no tab stop at the current cursor position.
1126 */
1127hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1128 var column = this.screen_.cursorPosition.column;
1129
1130 var i = this.tabStops_.indexOf(column);
1131 if (i == -1)
1132 return;
1133
1134 this.tabStops_.splice(i, 1);
1135};
1136
1137/**
1138 * Clear all tab stops.
1139 */
1140hterm.Terminal.prototype.clearAllTabStops = function() {
1141 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001142 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001143};
1144
1145/**
1146 * Set up the default tab stops, starting from a given column.
1147 *
1148 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001149 * from the specified column, or 0 if no column is provided. It also flags
1150 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001151 *
1152 * This does not clear the existing tab stops first, use clearAllTabStops
1153 * for that.
1154 *
1155 * @param {int} opt_start Optional starting zero based starting column, useful
1156 * for filling out missing tab stops when the terminal is resized.
1157 */
1158hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1159 var start = opt_start || 0;
1160 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001161 // Round start up to a default tab stop.
1162 start = start - 1 - ((start - 1) % w) + w;
1163 for (var i = start; i < this.screenSize.width; i += w) {
1164 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001165 }
David Benjamin66e954d2012-05-05 21:08:12 -04001166
1167 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001168};
1169
rginda6d397402012-01-17 10:58:29 -08001170/**
rginda8ba33642011-12-14 12:31:31 -08001171 * Interpret a sequence of characters.
1172 *
1173 * Incomplete escape sequences are buffered until the next call.
1174 *
1175 * @param {string} str Sequence of characters to interpret or pass through.
1176 */
1177hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001178 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001179 this.scheduleSyncCursorPosition_();
1180};
1181
1182/**
1183 * Take over the given DIV for use as the terminal display.
1184 *
1185 * @param {HTMLDivElement} div The div to use as the terminal display.
1186 */
1187hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001188 this.div_ = div;
1189
rginda8ba33642011-12-14 12:31:31 -08001190 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001191 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001192 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1193 this.scrollPort_.setBackgroundPosition(
1194 this.prefs_.get('background-position'));
Robert Gindae76aa9f2014-03-14 12:29:12 -07001195 this.scrollPort_.setUserCss(this.prefs_.get('user-css'));
rginda30f20f62012-04-05 16:36:19 -07001196
rginda0918b652012-04-04 11:26:24 -07001197 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001198
rginda9f5222b2012-03-05 11:53:28 -08001199 this.setFontSize(this.prefs_.get('font-size'));
1200 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001201
David Reveman8f552492012-03-28 12:18:41 -04001202 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001203 this.setScrollWheelMoveMultipler(
1204 this.prefs_.get('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001205
rginda8ba33642011-12-14 12:31:31 -08001206 this.document_ = this.scrollPort_.getDocument();
1207
rginda4bba5e12012-06-20 16:15:30 -07001208 this.document_.body.oncontextmenu = function() { return false };
1209
1210 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001211 var screenNode = this.scrollPort_.getScreenNode();
1212 screenNode.addEventListener('mousedown', onMouse);
1213 screenNode.addEventListener('mouseup', onMouse);
1214 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001215 this.scrollPort_.onScrollWheel = onMouse;
1216
Toni Barzic0bfa8922013-11-22 11:18:35 -08001217 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001218 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001219 // Listen for mousedown events on the screenNode as in FF the focus
1220 // events don't bubble.
1221 screenNode.addEventListener('mousedown', function() {
1222 setTimeout(this.onFocusChange_.bind(this, true));
1223 }.bind(this));
1224
Toni Barzic0bfa8922013-11-22 11:18:35 -08001225 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001226 'blur', this.onFocusChange_.bind(this, false));
1227
1228 var style = this.document_.createElement('style');
1229 style.textContent =
1230 ('.cursor-node[focus="false"] {' +
1231 ' box-sizing: border-box;' +
1232 ' background-color: transparent !important;' +
1233 ' border-width: 2px;' +
1234 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001235 '}' +
1236 '.wc-node {' +
1237 ' display: inline-block;' +
1238 ' text-align: center;' +
1239 ' width: ' + this.scrollPort_.characterSize.width * 2 + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001240 '}');
1241 this.document_.head.appendChild(style);
1242
Ricky Liang48f05cb2013-12-31 23:35:29 +08001243 var styleSheets = this.document_.styleSheets;
1244 var cssRules = styleSheets[styleSheets.length - 1].cssRules;
1245 this.wcCssRule_ = cssRules[cssRules.length - 1];
1246
rginda8ba33642011-12-14 12:31:31 -08001247 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001248 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001249 this.cursorNode_.style.cssText =
1250 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001251 'top: -99px;' +
1252 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001253 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1254 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
Rob Spies06533ba2014-04-24 11:20:37 -07001255 '-webkit-transition: opacity, background-color 100ms linear;' +
1256 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001257
rginda8e92a692012-05-20 19:37:20 -07001258 this.setCursorColor(this.prefs_.get('cursor-color'));
Robert Gindafb1be6a2013-12-11 11:56:22 -08001259 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1260 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001261
rginda8ba33642011-12-14 12:31:31 -08001262 this.document_.body.appendChild(this.cursorNode_);
1263
rgindad5613292012-06-19 15:40:37 -07001264 // When 'enableMouseDragScroll' is off we reposition this element directly
1265 // under the mouse cursor after a click. This makes Chrome associate
1266 // subsequent mousemove events with the scroll-blocker. Since the
1267 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1268 // events do not cause the scrollport to scroll.
1269 //
1270 // It's a hack, but it's the cleanest way I could find.
1271 this.scrollBlockerNode_ = this.document_.createElement('div');
1272 this.scrollBlockerNode_.style.cssText =
1273 ('position: absolute;' +
1274 'top: -99px;' +
1275 'display: block;' +
1276 'width: 10px;' +
1277 'height: 10px;');
1278 this.document_.body.appendChild(this.scrollBlockerNode_);
1279
1280 var onMouse = this.onMouse_.bind(this);
1281 this.scrollPort_.onScrollWheel = onMouse;
1282 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1283 ].forEach(function(event) {
1284 this.scrollBlockerNode_.addEventListener(event, onMouse);
1285 this.cursorNode_.addEventListener(event, onMouse);
1286 this.document_.addEventListener(event, onMouse);
1287 }.bind(this));
1288
1289 this.cursorNode_.addEventListener('mousedown', function() {
1290 setTimeout(this.focus.bind(this));
1291 }.bind(this));
1292
rginda8ba33642011-12-14 12:31:31 -08001293 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001294
rginda87b86462011-12-14 13:48:03 -08001295 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001296 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001297};
1298
rginda0918b652012-04-04 11:26:24 -07001299/**
1300 * Return the HTML document that contains the terminal DOM nodes.
1301 */
rginda87b86462011-12-14 13:48:03 -08001302hterm.Terminal.prototype.getDocument = function() {
1303 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001304};
1305
1306/**
rginda0918b652012-04-04 11:26:24 -07001307 * Focus the terminal.
1308 */
1309hterm.Terminal.prototype.focus = function() {
1310 this.scrollPort_.focus();
1311};
1312
1313/**
rginda8ba33642011-12-14 12:31:31 -08001314 * Return the HTML Element for a given row index.
1315 *
1316 * This is a method from the RowProvider interface. The ScrollPort uses
1317 * it to fetch rows on demand as they are scrolled into view.
1318 *
1319 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1320 * pairs to conserve memory.
1321 *
1322 * @param {integer} index The zero-based row index, measured relative to the
1323 * start of the scrollback buffer. On-screen rows will always have the
1324 * largest indicies.
1325 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1326 */
1327hterm.Terminal.prototype.getRowNode = function(index) {
1328 if (index < this.scrollbackRows_.length)
1329 return this.scrollbackRows_[index];
1330
1331 var screenIndex = index - this.scrollbackRows_.length;
1332 return this.screen_.rowsArray[screenIndex];
1333};
1334
1335/**
1336 * Return the text content for a given range of rows.
1337 *
1338 * This is a method from the RowProvider interface. The ScrollPort uses
1339 * it to fetch text content on demand when the user attempts to copy their
1340 * selection to the clipboard.
1341 *
1342 * @param {integer} start The zero-based row index to start from, measured
1343 * relative to the start of the scrollback buffer. On-screen rows will
1344 * always have the largest indicies.
1345 * @param {integer} end The zero-based row index to end on, measured
1346 * relative to the start of the scrollback buffer.
1347 * @return {string} A single string containing the text value of the range of
1348 * rows. Lines will be newline delimited, with no trailing newline.
1349 */
1350hterm.Terminal.prototype.getRowsText = function(start, end) {
1351 var ary = [];
1352 for (var i = start; i < end; i++) {
1353 var node = this.getRowNode(i);
1354 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001355 if (i < end - 1 && !node.getAttribute('line-overflow'))
1356 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001357 }
1358
rgindaa09e7332012-08-17 12:49:51 -07001359 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001360};
1361
1362/**
1363 * Return the text content for a given row.
1364 *
1365 * This is a method from the RowProvider interface. The ScrollPort uses
1366 * it to fetch text content on demand when the user attempts to copy their
1367 * selection to the clipboard.
1368 *
1369 * @param {integer} index The zero-based row index to return, measured
1370 * relative to the start of the scrollback buffer. On-screen rows will
1371 * always have the largest indicies.
1372 * @return {string} A string containing the text value of the selected row.
1373 */
1374hterm.Terminal.prototype.getRowText = function(index) {
1375 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001376 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001377};
1378
1379/**
1380 * Return the total number of rows in the addressable screen and in the
1381 * scrollback buffer of this terminal.
1382 *
1383 * This is a method from the RowProvider interface. The ScrollPort uses
1384 * it to compute the size of the scrollbar.
1385 *
1386 * @return {integer} The number of rows in this terminal.
1387 */
1388hterm.Terminal.prototype.getRowCount = function() {
1389 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1390};
1391
1392/**
1393 * Create DOM nodes for new rows and append them to the end of the terminal.
1394 *
1395 * This is the only correct way to add a new DOM node for a row. Notice that
1396 * the new row is appended to the bottom of the list of rows, and does not
1397 * require renumbering (of the rowIndex property) of previous rows.
1398 *
1399 * If you think you want a new blank row somewhere in the middle of the
1400 * terminal, look into moveRows_().
1401 *
1402 * This method does not pay attention to vtScrollTop/Bottom, since you should
1403 * be using moveRows() in cases where they would matter.
1404 *
1405 * The cursor will be positioned at column 0 of the first inserted line.
1406 */
1407hterm.Terminal.prototype.appendRows_ = function(count) {
1408 var cursorRow = this.screen_.rowsArray.length;
1409 var offset = this.scrollbackRows_.length + cursorRow;
1410 for (var i = 0; i < count; i++) {
1411 var row = this.document_.createElement('x-row');
1412 row.appendChild(this.document_.createTextNode(''));
1413 row.rowIndex = offset + i;
1414 this.screen_.pushRow(row);
1415 }
1416
1417 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1418 if (extraRows > 0) {
1419 var ary = this.screen_.shiftRows(extraRows);
1420 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001421 if (this.scrollPort_.isScrolledEnd)
1422 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001423 }
1424
1425 if (cursorRow >= this.screen_.rowsArray.length)
1426 cursorRow = this.screen_.rowsArray.length - 1;
1427
rginda87b86462011-12-14 13:48:03 -08001428 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001429};
1430
1431/**
1432 * Relocate rows from one part of the addressable screen to another.
1433 *
1434 * This is used to recycle rows during VT scrolls (those which are driven
1435 * by VT commands, rather than by the user manipulating the scrollbar.)
1436 *
1437 * In this case, the blank lines scrolled into the scroll region are made of
1438 * the nodes we scrolled off. These have their rowIndex properties carefully
1439 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001440 */
1441hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1442 var ary = this.screen_.removeRows(fromIndex, count);
1443 this.screen_.insertRows(toIndex, ary);
1444
1445 var start, end;
1446 if (fromIndex < toIndex) {
1447 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001448 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001449 } else {
1450 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001451 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001452 }
1453
1454 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001455 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001456};
1457
1458/**
1459 * Renumber the rowIndex property of the given range of rows.
1460 *
1461 * The start and end indicies are relative to the screen, not the scrollback.
1462 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001463 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001464 * no need to renumber scrollback rows.
1465 */
Robert Ginda40932892012-12-10 17:26:40 -08001466hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1467 var screen = opt_screen || this.screen_;
1468
rginda8ba33642011-12-14 12:31:31 -08001469 var offset = this.scrollbackRows_.length;
1470 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001471 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001472 }
1473};
1474
1475/**
1476 * Print a string to the terminal.
1477 *
1478 * This respects the current insert and wraparound modes. It will add new lines
1479 * to the end of the terminal, scrolling off the top into the scrollback buffer
1480 * if necessary.
1481 *
1482 * The string is *not* parsed for escape codes. Use the interpret() method if
1483 * that's what you're after.
1484 *
1485 * @param{string} str The string to print.
1486 */
1487hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001488 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001489
Ricky Liang48f05cb2013-12-31 23:35:29 +08001490 var strWidth = lib.wc.strWidth(str);
1491
1492 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001493 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1494 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001495 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001496 }
rgindaa19afe22012-01-25 15:40:22 -08001497
Ricky Liang48f05cb2013-12-31 23:35:29 +08001498 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001499 var didOverflow = false;
1500 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001501
rgindaa9abdd82012-08-06 18:05:09 -07001502 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1503 didOverflow = true;
1504 count = this.screenSize.width - this.screen_.cursorPosition.column;
1505 }
rgindaa19afe22012-01-25 15:40:22 -08001506
rgindaa9abdd82012-08-06 18:05:09 -07001507 if (didOverflow && !this.options_.wraparound) {
1508 // If the string overflowed the line but wraparound is off, then the
1509 // last printed character should be the last of the string.
1510 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001511 substr = lib.wc.substr(str, startOffset, count - 1) +
1512 lib.wc.substr(str, strWidth - 1);
1513 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001514 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001515 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001516 }
rgindaa19afe22012-01-25 15:40:22 -08001517
Ricky Liang48f05cb2013-12-31 23:35:29 +08001518 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1519 for (var i = 0; i < tokens.length; i++) {
1520 if (tokens[i].wcNode)
1521 this.screen_.textAttributes.wcNode = true;
1522
1523 if (this.options_.insertMode) {
1524 this.screen_.insertString(tokens[i].str);
1525 } else {
1526 this.screen_.overwriteString(tokens[i].str);
1527 }
1528 this.screen_.textAttributes.wcNode = false;
rgindaa9abdd82012-08-06 18:05:09 -07001529 }
1530
1531 this.screen_.maybeClipCurrentRow();
1532 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001533 }
rginda8ba33642011-12-14 12:31:31 -08001534
1535 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001536
rginda9f5222b2012-03-05 11:53:28 -08001537 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001538 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001539};
1540
1541/**
rginda87b86462011-12-14 13:48:03 -08001542 * Set the VT scroll region.
1543 *
rginda87b86462011-12-14 13:48:03 -08001544 * This also resets the cursor position to the absolute (0, 0) position, since
1545 * that's what xterm appears to do.
1546 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001547 * Setting the scroll region to the full height of the terminal will clear
1548 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1549 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1550 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1551 * continue to work as most users would expect.
1552 *
rginda87b86462011-12-14 13:48:03 -08001553 * @param {integer} scrollTop The zero-based top of the scroll region.
1554 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1555 * inclusive.
1556 */
1557hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001558 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001559 this.vtScrollTop_ = null;
1560 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001561 } else {
1562 this.vtScrollTop_ = scrollTop;
1563 this.vtScrollBottom_ = scrollBottom;
1564 }
rginda87b86462011-12-14 13:48:03 -08001565};
1566
1567/**
rginda8ba33642011-12-14 12:31:31 -08001568 * Return the top row index according to the VT.
1569 *
1570 * This will return 0 unless the terminal has been told to restrict scrolling
1571 * to some lower row. It is used for some VT cursor positioning and scrolling
1572 * commands.
1573 *
1574 * @return {integer} The topmost row in the terminal's scroll region.
1575 */
1576hterm.Terminal.prototype.getVTScrollTop = function() {
1577 if (this.vtScrollTop_ != null)
1578 return this.vtScrollTop_;
1579
1580 return 0;
rginda87b86462011-12-14 13:48:03 -08001581};
rginda8ba33642011-12-14 12:31:31 -08001582
1583/**
1584 * Return the bottom row index according to the VT.
1585 *
1586 * This will return the height of the terminal unless the it has been told to
1587 * restrict scrolling to some higher row. It is used for some VT cursor
1588 * positioning and scrolling commands.
1589 *
1590 * @return {integer} The bottommost row in the terminal's scroll region.
1591 */
1592hterm.Terminal.prototype.getVTScrollBottom = function() {
1593 if (this.vtScrollBottom_ != null)
1594 return this.vtScrollBottom_;
1595
rginda87b86462011-12-14 13:48:03 -08001596 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001597}
1598
1599/**
1600 * Process a '\n' character.
1601 *
1602 * If the cursor is on the final row of the terminal this will append a new
1603 * blank row to the screen and scroll the topmost row into the scrollback
1604 * buffer.
1605 *
1606 * Otherwise, this moves the cursor to column zero of the next row.
1607 */
1608hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001609 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1610 this.screen_.rowsArray.length - 1);
1611
1612 if (this.vtScrollBottom_ != null) {
1613 // A VT Scroll region is active, we never append new rows.
1614 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1615 // We're at the end of the VT Scroll Region, perform a VT scroll.
1616 this.vtScrollUp(1);
1617 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1618 } else if (cursorAtEndOfScreen) {
1619 // We're at the end of the screen, the only thing to do is put the
1620 // cursor to column 0.
1621 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1622 } else {
1623 // Anywhere else, advance the cursor row, and reset the column.
1624 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1625 }
1626 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001627 // We're at the end of the screen. Append a new row to the terminal,
1628 // shifting the top row into the scrollback.
1629 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001630 } else {
rginda87b86462011-12-14 13:48:03 -08001631 // Anywhere else in the screen just moves the cursor.
1632 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001633 }
1634};
1635
1636/**
1637 * Like newLine(), except maintain the cursor column.
1638 */
1639hterm.Terminal.prototype.lineFeed = function() {
1640 var column = this.screen_.cursorPosition.column;
1641 this.newLine();
1642 this.setCursorColumn(column);
1643};
1644
1645/**
rginda87b86462011-12-14 13:48:03 -08001646 * If autoCarriageReturn is set then newLine(), else lineFeed().
1647 */
1648hterm.Terminal.prototype.formFeed = function() {
1649 if (this.options_.autoCarriageReturn) {
1650 this.newLine();
1651 } else {
1652 this.lineFeed();
1653 }
1654};
1655
1656/**
1657 * Move the cursor up one row, possibly inserting a blank line.
1658 *
1659 * The cursor column is not changed.
1660 */
1661hterm.Terminal.prototype.reverseLineFeed = function() {
1662 var scrollTop = this.getVTScrollTop();
1663 var currentRow = this.screen_.cursorPosition.row;
1664
1665 if (currentRow == scrollTop) {
1666 this.insertLines(1);
1667 } else {
1668 this.setAbsoluteCursorRow(currentRow - 1);
1669 }
1670};
1671
1672/**
rginda8ba33642011-12-14 12:31:31 -08001673 * Replace all characters to the left of the current cursor with the space
1674 * character.
1675 *
1676 * TODO(rginda): This should probably *remove* the characters (not just replace
1677 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001678 * position.
rginda8ba33642011-12-14 12:31:31 -08001679 */
1680hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001681 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001682 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001683 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001684 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001685};
1686
1687/**
David Benjamin684a9b72012-05-01 17:19:58 -04001688 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001689 *
1690 * The cursor position is unchanged.
1691 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001692 * If the current background color is not the default background color this
1693 * will insert spaces rather than delete. This is unfortunate because the
1694 * trailing space will affect text selection, but it's difficult to come up
1695 * with a way to style empty space that wouldn't trip up the hterm.Screen
1696 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001697 *
1698 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1699 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1700 * crbug.com/232390 for details.
rginda8ba33642011-12-14 12:31:31 -08001701 */
1702hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001703 if (this.screen_.cursorPosition.overflow)
1704 return;
1705
Robert Ginda7fd57082012-09-25 14:41:47 -07001706 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1707 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001708
1709 if (this.screen_.textAttributes.background ===
1710 this.screen_.textAttributes.DEFAULT_COLOR) {
1711 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08001712 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07001713 this.screen_.cursorPosition.column + count) {
1714 this.screen_.deleteChars(count);
1715 this.clearCursorOverflow();
1716 return;
1717 }
1718 }
1719
rginda87b86462011-12-14 13:48:03 -08001720 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001721 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001722 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001723 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001724};
1725
1726/**
1727 * Erase the current line.
1728 *
1729 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001730 */
1731hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001732 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001733 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001734 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001735 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001736};
1737
1738/**
David Benjamina08d78f2012-05-05 00:28:49 -04001739 * Erase all characters from the start of the screen to the current cursor
1740 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001741 *
1742 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001743 */
1744hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001745 var cursor = this.saveCursor();
1746
1747 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001748
David Benjamina08d78f2012-05-05 00:28:49 -04001749 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001750 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001751 this.screen_.clearCursorRow();
1752 }
1753
rginda87b86462011-12-14 13:48:03 -08001754 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001755 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001756};
1757
1758/**
1759 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001760 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001761 *
1762 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001763 */
1764hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001765 var cursor = this.saveCursor();
1766
1767 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001768
David Benjamina08d78f2012-05-05 00:28:49 -04001769 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001770 for (var i = cursor.row + 1; i <= bottom; i++) {
1771 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001772 this.screen_.clearCursorRow();
1773 }
1774
rginda87b86462011-12-14 13:48:03 -08001775 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001776 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001777};
1778
1779/**
1780 * Fill the terminal with a given character.
1781 *
1782 * This methods does not respect the VT scroll region.
1783 *
1784 * @param {string} ch The character to use for the fill.
1785 */
1786hterm.Terminal.prototype.fill = function(ch) {
1787 var cursor = this.saveCursor();
1788
1789 this.setAbsoluteCursorPosition(0, 0);
1790 for (var row = 0; row < this.screenSize.height; row++) {
1791 for (var col = 0; col < this.screenSize.width; col++) {
1792 this.setAbsoluteCursorPosition(row, col);
1793 this.screen_.overwriteString(ch);
1794 }
1795 }
1796
1797 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001798};
1799
1800/**
rginda9ea433c2012-03-16 11:57:00 -07001801 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001802 *
rginda9ea433c2012-03-16 11:57:00 -07001803 * This does not respect the scroll region.
1804 *
1805 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1806 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001807 */
rginda9ea433c2012-03-16 11:57:00 -07001808hterm.Terminal.prototype.clearHome = function(opt_screen) {
1809 var screen = opt_screen || this.screen_;
1810 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001811
rginda11057d52012-04-25 12:29:56 -07001812 if (bottom == 0) {
1813 // Empty screen, nothing to do.
1814 return;
1815 }
1816
rgindae4d29232012-01-19 10:47:13 -08001817 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001818 screen.setCursorPosition(i, 0);
1819 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001820 }
1821
rginda9ea433c2012-03-16 11:57:00 -07001822 screen.setCursorPosition(0, 0);
1823};
1824
1825/**
1826 * Erase the entire display without changing the cursor position.
1827 *
1828 * The cursor position is unchanged. This does not respect the scroll
1829 * region.
1830 *
1831 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1832 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001833 */
1834hterm.Terminal.prototype.clear = function(opt_screen) {
1835 var screen = opt_screen || this.screen_;
1836 var cursor = screen.cursorPosition.clone();
1837 this.clearHome(screen);
1838 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001839};
1840
1841/**
1842 * VT command to insert lines at the current cursor row.
1843 *
1844 * This respects the current scroll region. Rows pushed off the bottom are
1845 * lost (they won't show up in the scrollback buffer).
1846 *
rginda8ba33642011-12-14 12:31:31 -08001847 * @param {integer} count The number of lines to insert.
1848 */
1849hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07001850 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08001851
1852 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07001853 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08001854
Robert Ginda579186b2012-09-26 11:40:04 -07001855 // The moveCount is the number of rows we need to relocate to make room for
1856 // the new row(s). The count is the distance to move them.
1857 var moveCount = bottom - cursorRow - count + 1;
1858 if (moveCount)
1859 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08001860
Robert Ginda579186b2012-09-26 11:40:04 -07001861 for (var i = count - 1; i >= 0; i--) {
1862 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001863 this.screen_.clearCursorRow();
1864 }
rginda8ba33642011-12-14 12:31:31 -08001865};
1866
1867/**
1868 * VT command to delete lines at the current cursor row.
1869 *
1870 * New rows are added to the bottom of scroll region to take their place. New
1871 * rows are strictly there to take up space and have no content or style.
1872 */
1873hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001874 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001875
rginda87b86462011-12-14 13:48:03 -08001876 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001877 var bottom = this.getVTScrollBottom();
1878
rginda87b86462011-12-14 13:48:03 -08001879 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001880 count = Math.min(count, maxCount);
1881
rginda87b86462011-12-14 13:48:03 -08001882 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001883 if (count != maxCount)
1884 this.moveRows_(top, count, moveStart);
1885
1886 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001887 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001888 this.screen_.clearCursorRow();
1889 }
1890
rginda87b86462011-12-14 13:48:03 -08001891 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001892 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001893};
1894
1895/**
1896 * Inserts the given number of spaces at the current cursor position.
1897 *
rginda87b86462011-12-14 13:48:03 -08001898 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001899 */
1900hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001901 var cursor = this.saveCursor();
1902
rgindacbbd7482012-06-13 15:06:16 -07001903 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001904 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001905 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001906
1907 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001908 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001909};
1910
1911/**
1912 * Forward-delete the specified number of characters starting at the cursor
1913 * position.
1914 *
1915 * @param {integer} count The number of characters to delete.
1916 */
1917hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001918 var deleted = this.screen_.deleteChars(count);
1919 if (deleted && !this.screen_.textAttributes.isDefault()) {
1920 var cursor = this.saveCursor();
1921 this.setCursorColumn(this.screenSize.width - deleted);
1922 this.screen_.insertString(lib.f.getWhitespace(deleted));
1923 this.restoreCursor(cursor);
1924 }
1925
David Benjamin54e8bf62012-06-01 22:31:40 -04001926 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001927};
1928
1929/**
1930 * Shift rows in the scroll region upwards by a given number of lines.
1931 *
1932 * New rows are inserted at the bottom of the scroll region to fill the
1933 * vacated rows. The new rows not filled out with the current text attributes.
1934 *
1935 * This function does not affect the scrollback rows at all. Rows shifted
1936 * off the top are lost.
1937 *
rginda87b86462011-12-14 13:48:03 -08001938 * The cursor position is not altered.
1939 *
rginda8ba33642011-12-14 12:31:31 -08001940 * @param {integer} count The number of rows to scroll.
1941 */
1942hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001943 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001944
rginda87b86462011-12-14 13:48:03 -08001945 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001946 this.deleteLines(count);
1947
rginda87b86462011-12-14 13:48:03 -08001948 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001949};
1950
1951/**
1952 * Shift rows below the cursor down by a given number of lines.
1953 *
1954 * This function respects the current scroll region.
1955 *
1956 * New rows are inserted at the top of the scroll region to fill the
1957 * vacated rows. The new rows not filled out with the current text attributes.
1958 *
1959 * This function does not affect the scrollback rows at all. Rows shifted
1960 * off the bottom are lost.
1961 *
1962 * @param {integer} count The number of rows to scroll.
1963 */
1964hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001965 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001966
rginda87b86462011-12-14 13:48:03 -08001967 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001968 this.insertLines(opt_count);
1969
rginda87b86462011-12-14 13:48:03 -08001970 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001971};
1972
rginda87b86462011-12-14 13:48:03 -08001973
rginda8ba33642011-12-14 12:31:31 -08001974/**
1975 * Set the cursor position.
1976 *
1977 * The cursor row is relative to the scroll region if the terminal has
1978 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1979 *
1980 * @param {integer} row The new zero-based cursor row.
1981 * @param {integer} row The new zero-based cursor column.
1982 */
1983hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1984 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001985 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001986 } else {
rginda87b86462011-12-14 13:48:03 -08001987 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001988 }
rginda87b86462011-12-14 13:48:03 -08001989};
rginda8ba33642011-12-14 12:31:31 -08001990
rginda87b86462011-12-14 13:48:03 -08001991hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1992 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07001993 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
1994 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001995 this.screen_.setCursorPosition(row, column);
1996};
1997
1998hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07001999 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2000 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002001 this.screen_.setCursorPosition(row, column);
2002};
2003
2004/**
2005 * Set the cursor column.
2006 *
2007 * @param {integer} column The new zero-based cursor column.
2008 */
2009hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002010 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002011};
2012
2013/**
2014 * Return the cursor column.
2015 *
2016 * @return {integer} The zero-based cursor column.
2017 */
2018hterm.Terminal.prototype.getCursorColumn = function() {
2019 return this.screen_.cursorPosition.column;
2020};
2021
2022/**
2023 * Set the cursor row.
2024 *
2025 * The cursor row is relative to the scroll region if the terminal has
2026 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2027 *
2028 * @param {integer} row The new cursor row.
2029 */
rginda87b86462011-12-14 13:48:03 -08002030hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2031 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002032};
2033
2034/**
2035 * Return the cursor row.
2036 *
2037 * @return {integer} The zero-based cursor row.
2038 */
2039hterm.Terminal.prototype.getCursorRow = function(row) {
2040 return this.screen_.cursorPosition.row;
2041};
2042
2043/**
2044 * Request that the ScrollPort redraw itself soon.
2045 *
2046 * The redraw will happen asynchronously, soon after the call stack winds down.
2047 * Multiple calls will be coalesced into a single redraw.
2048 */
2049hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002050 if (this.timeouts_.redraw)
2051 return;
rginda8ba33642011-12-14 12:31:31 -08002052
2053 var self = this;
rginda87b86462011-12-14 13:48:03 -08002054 this.timeouts_.redraw = setTimeout(function() {
2055 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002056 self.scrollPort_.redraw_();
2057 }, 0);
2058};
2059
2060/**
2061 * Request that the ScrollPort be scrolled to the bottom.
2062 *
2063 * The scroll will happen asynchronously, soon after the call stack winds down.
2064 * Multiple calls will be coalesced into a single scroll.
2065 *
2066 * This affects the scrollbar position of the ScrollPort, and has nothing to
2067 * do with the VT scroll commands.
2068 */
2069hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2070 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002071 return;
rginda8ba33642011-12-14 12:31:31 -08002072
2073 var self = this;
2074 this.timeouts_.scrollDown = setTimeout(function() {
2075 delete self.timeouts_.scrollDown;
2076 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2077 }, 10);
2078};
2079
2080/**
2081 * Move the cursor up a specified number of rows.
2082 *
2083 * @param {integer} count The number of rows to move the cursor.
2084 */
2085hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002086 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002087};
2088
2089/**
2090 * Move the cursor down a specified number of rows.
2091 *
2092 * @param {integer} count The number of rows to move the cursor.
2093 */
2094hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002095 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002096 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2097 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2098 this.screenSize.height - 1);
2099
rgindacbbd7482012-06-13 15:06:16 -07002100 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002101 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002102 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002103};
2104
2105/**
2106 * Move the cursor left a specified number of columns.
2107 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002108 * If reverse wraparound mode is enabled and the previous row wrapped into
2109 * the current row then we back up through the wraparound as well.
2110 *
rginda8ba33642011-12-14 12:31:31 -08002111 * @param {integer} count The number of columns to move the cursor.
2112 */
2113hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002114 count = count || 1;
2115
2116 if (count < 1)
2117 return;
2118
2119 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002120 if (this.options_.reverseWraparound) {
2121 if (this.screen_.cursorPosition.overflow) {
2122 // If this cursor is in the right margin, consume one count to get it
2123 // back to the last column. This only applies when we're in reverse
2124 // wraparound mode.
2125 count--;
2126 this.clearCursorOverflow();
2127
2128 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002129 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002130 }
2131
Robert Gindabfb32622014-07-17 13:20:27 -07002132 var newRow = this.screen_.cursorPosition.row;
2133 var newColumn = currentColumn - count;
2134 if (newColumn < 0) {
2135 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2136 if (newRow < 0) {
2137 // xterm also wraps from row 0 to the last row.
2138 newRow = this.screenSize.height + newRow % this.screenSize.height;
2139 }
2140 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2141 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002142
Robert Gindabfb32622014-07-17 13:20:27 -07002143 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2144
2145 } else {
2146 var newColumn = Math.max(currentColumn - count, 0);
2147 this.setCursorColumn(newColumn);
2148 }
rginda8ba33642011-12-14 12:31:31 -08002149};
2150
2151/**
2152 * Move the cursor right a specified number of columns.
2153 *
2154 * @param {integer} count The number of columns to move the cursor.
2155 */
2156hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002157 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002158
2159 if (count < 1)
2160 return;
2161
rgindacbbd7482012-06-13 15:06:16 -07002162 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002163 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002164 this.setCursorColumn(column);
2165};
2166
2167/**
2168 * Reverse the foreground and background colors of the terminal.
2169 *
2170 * This only affects text that was drawn with no attributes.
2171 *
2172 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2173 * been drawn with attributes that happen to coincide with the default
2174 * 'no-attribute' colors. My guess is probably not.
2175 */
2176hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002177 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002178 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002179 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2180 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002181 } else {
rginda9f5222b2012-03-05 11:53:28 -08002182 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2183 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002184 }
2185};
2186
2187/**
rginda87b86462011-12-14 13:48:03 -08002188 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002189 *
2190 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002191 */
2192hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002193 this.cursorNode_.style.backgroundColor =
2194 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002195
2196 var self = this;
2197 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08002198 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08002199 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002200
Michael Kelly485ecd12014-06-09 11:41:56 -04002201 // bellSquelchTimeout_ affects both audio and notification bells.
2202 if (this.bellSquelchTimeout_)
2203 return;
2204
Robert Ginda92e18102013-03-14 13:56:37 -07002205 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002206 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002207 this.bellSequelchTimeout_ = setTimeout(function() {
2208 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002209 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002210 } else {
2211 delete this.bellSquelchTimeout_;
2212 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002213
2214 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
2215 var n = new Notification(
2216 lib.f.replaceVars(hterm.desktopNotificationTitle,
Robert Ginda348dc2b2014-06-24 14:42:23 -07002217 {'title': window.document.title || 'hterm'}));
Michael Kelly485ecd12014-06-09 11:41:56 -04002218 this.bellNotificationList_.push(n);
2219 // TODO: Should we try to raise the window here?
2220 n.onclick = function() { self.closeBellNotifications_(); };
2221 }
rginda87b86462011-12-14 13:48:03 -08002222};
2223
2224/**
rginda8ba33642011-12-14 12:31:31 -08002225 * Set the origin mode bit.
2226 *
2227 * If origin mode is on, certain VT cursor and scrolling commands measure their
2228 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2229 * to the top of the addressable screen.
2230 *
2231 * Defaults to off.
2232 *
2233 * @param {boolean} state True to set origin mode, false to unset.
2234 */
2235hterm.Terminal.prototype.setOriginMode = function(state) {
2236 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002237 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002238};
2239
2240/**
2241 * Set the insert mode bit.
2242 *
2243 * If insert mode is on, existing text beyond the cursor position will be
2244 * shifted right to make room for new text. Otherwise, new text overwrites
2245 * any existing text.
2246 *
2247 * Defaults to off.
2248 *
2249 * @param {boolean} state True to set insert mode, false to unset.
2250 */
2251hterm.Terminal.prototype.setInsertMode = function(state) {
2252 this.options_.insertMode = state;
2253};
2254
2255/**
rginda87b86462011-12-14 13:48:03 -08002256 * Set the auto carriage return bit.
2257 *
2258 * If auto carriage return is on then a formfeed character is interpreted
2259 * as a newline, otherwise it's the same as a linefeed. The difference boils
2260 * down to whether or not the cursor column is reset.
2261 */
2262hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2263 this.options_.autoCarriageReturn = state;
2264};
2265
2266/**
rginda8ba33642011-12-14 12:31:31 -08002267 * Set the wraparound mode bit.
2268 *
2269 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2270 * to the start of the following row. Otherwise, the cursor is clamped to the
2271 * end of the screen and attempts to write past it are ignored.
2272 *
2273 * Defaults to on.
2274 *
2275 * @param {boolean} state True to set wraparound mode, false to unset.
2276 */
2277hterm.Terminal.prototype.setWraparound = function(state) {
2278 this.options_.wraparound = state;
2279};
2280
2281/**
2282 * Set the reverse-wraparound mode bit.
2283 *
2284 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2285 * to the end of the previous row. Otherwise, the cursor is clamped to column
2286 * 0.
2287 *
2288 * Defaults to off.
2289 *
2290 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2291 */
2292hterm.Terminal.prototype.setReverseWraparound = function(state) {
2293 this.options_.reverseWraparound = state;
2294};
2295
2296/**
2297 * Selects between the primary and alternate screens.
2298 *
2299 * If alternate mode is on, the alternate screen is active. Otherwise the
2300 * primary screen is active.
2301 *
2302 * Swapping screens has no effect on the scrollback buffer.
2303 *
2304 * Each screen maintains its own cursor position.
2305 *
2306 * Defaults to off.
2307 *
2308 * @param {boolean} state True to set alternate mode, false to unset.
2309 */
2310hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002311 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002312 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2313
rginda35c456b2012-02-09 17:29:05 -08002314 if (this.screen_.rowsArray.length &&
2315 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2316 // If the screen changed sizes while we were away, our rowIndexes may
2317 // be incorrect.
2318 var offset = this.scrollbackRows_.length;
2319 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002320 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002321 ary[i].rowIndex = offset + i;
2322 }
2323 }
rginda8ba33642011-12-14 12:31:31 -08002324
rginda35c456b2012-02-09 17:29:05 -08002325 this.realizeWidth_(this.screenSize.width);
2326 this.realizeHeight_(this.screenSize.height);
2327 this.scrollPort_.syncScrollHeight();
2328 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002329
rginda6d397402012-01-17 10:58:29 -08002330 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002331 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002332};
2333
2334/**
2335 * Set the cursor-blink mode bit.
2336 *
2337 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2338 * a visible cursor does not blink.
2339 *
2340 * You should make sure to turn blinking off if you're going to dispose of a
2341 * terminal, otherwise you'll leak a timeout.
2342 *
2343 * Defaults to on.
2344 *
2345 * @param {boolean} state True to set cursor-blink mode, false to unset.
2346 */
2347hterm.Terminal.prototype.setCursorBlink = function(state) {
2348 this.options_.cursorBlink = state;
2349
2350 if (!state && this.timeouts_.cursorBlink) {
2351 clearTimeout(this.timeouts_.cursorBlink);
2352 delete this.timeouts_.cursorBlink;
2353 }
2354
2355 if (this.options_.cursorVisible)
2356 this.setCursorVisible(true);
2357};
2358
2359/**
2360 * Set the cursor-visible mode bit.
2361 *
2362 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2363 *
2364 * Defaults to on.
2365 *
2366 * @param {boolean} state True to set cursor-visible mode, false to unset.
2367 */
2368hterm.Terminal.prototype.setCursorVisible = function(state) {
2369 this.options_.cursorVisible = state;
2370
2371 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002372 if (this.timeouts_.cursorBlink) {
2373 clearTimeout(this.timeouts_.cursorBlink);
2374 delete this.timeouts_.cursorBlink;
2375 }
rginda87b86462011-12-14 13:48:03 -08002376 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002377 return;
2378 }
2379
rginda87b86462011-12-14 13:48:03 -08002380 this.syncCursorPosition_();
2381
2382 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002383
2384 if (this.options_.cursorBlink) {
2385 if (this.timeouts_.cursorBlink)
2386 return;
2387
Robert Gindaea2183e2014-07-17 09:51:51 -07002388 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002389 } else {
2390 if (this.timeouts_.cursorBlink) {
2391 clearTimeout(this.timeouts_.cursorBlink);
2392 delete this.timeouts_.cursorBlink;
2393 }
2394 }
2395};
2396
2397/**
rginda87b86462011-12-14 13:48:03 -08002398 * Synchronizes the visible cursor and document selection with the current
2399 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002400 */
2401hterm.Terminal.prototype.syncCursorPosition_ = function() {
2402 var topRowIndex = this.scrollPort_.getTopRowIndex();
2403 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2404 var cursorRowIndex = this.scrollbackRows_.length +
2405 this.screen_.cursorPosition.row;
2406
2407 if (cursorRowIndex > bottomRowIndex) {
2408 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002409 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002410 return;
2411 }
2412
Robert Gindab837c052014-08-11 11:17:51 -07002413 if (this.options_.cursorVisible &&
2414 this.cursorNode_.style.display == 'none') {
2415 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2416 this.cursorNode_.style.display = '';
2417 }
2418
2419
rginda8ba33642011-12-14 12:31:31 -08002420 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002421 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2422 'px';
2423 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2424 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002425
2426 this.cursorNode_.setAttribute('title',
2427 '(' + this.screen_.cursorPosition.row +
2428 ', ' + this.screen_.cursorPosition.column +
2429 ')');
2430
2431 // Update the caret for a11y purposes.
2432 var selection = this.document_.getSelection();
2433 if (selection && selection.isCollapsed)
2434 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002435};
2436
Robert Gindafb1be6a2013-12-11 11:56:22 -08002437/**
2438 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2439 * and character cell dimensions.
2440 */
Robert Ginda830583c2013-08-07 13:20:46 -07002441hterm.Terminal.prototype.restyleCursor_ = function() {
2442 var shape = this.cursorShape_;
2443
2444 if (this.cursorNode_.getAttribute('focus') == 'false') {
2445 // Always show a block cursor when unfocused.
2446 shape = hterm.Terminal.cursorShape.BLOCK;
2447 }
2448
2449 var style = this.cursorNode_.style;
2450
Robert Gindafb1be6a2013-12-11 11:56:22 -08002451 style.width = this.scrollPort_.characterSize.width + 'px';
2452
Robert Ginda830583c2013-08-07 13:20:46 -07002453 switch (shape) {
2454 case hterm.Terminal.cursorShape.BEAM:
2455 style.height = this.scrollPort_.characterSize.height + 'px';
2456 style.backgroundColor = 'transparent';
2457 style.borderBottomStyle = null;
2458 style.borderLeftStyle = 'solid';
2459 break;
2460
2461 case hterm.Terminal.cursorShape.UNDERLINE:
2462 style.height = this.scrollPort_.characterSize.baseline + 'px';
2463 style.backgroundColor = 'transparent';
2464 style.borderBottomStyle = 'solid';
2465 // correct the size to put it exactly at the baseline
2466 style.borderLeftStyle = null;
2467 break;
2468
2469 default:
2470 style.height = this.scrollPort_.characterSize.height + 'px';
2471 style.backgroundColor = this.cursorColor_;
2472 style.borderBottomStyle = null;
2473 style.borderLeftStyle = null;
2474 break;
2475 }
2476};
2477
rginda8ba33642011-12-14 12:31:31 -08002478/**
2479 * Synchronizes the visible cursor with the current cursor coordinates.
2480 *
2481 * The sync will happen asynchronously, soon after the call stack winds down.
2482 * Multiple calls will be coalesced into a single sync.
2483 */
2484hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2485 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002486 return;
rginda8ba33642011-12-14 12:31:31 -08002487
2488 var self = this;
2489 this.timeouts_.syncCursor = setTimeout(function() {
2490 self.syncCursorPosition_();
2491 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002492 }, 0);
2493};
2494
rgindacc2996c2012-02-24 14:59:31 -08002495/**
rgindaf522ce02012-04-17 17:49:17 -07002496 * Show or hide the zoom warning.
2497 *
2498 * The zoom warning is a message warning the user that their browser zoom must
2499 * be set to 100% in order for hterm to function properly.
2500 *
2501 * @param {boolean} state True to show the message, false to hide it.
2502 */
2503hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2504 if (!this.zoomWarningNode_) {
2505 if (!state)
2506 return;
2507
2508 this.zoomWarningNode_ = this.document_.createElement('div');
2509 this.zoomWarningNode_.style.cssText = (
2510 'color: black;' +
2511 'background-color: #ff2222;' +
2512 'font-size: large;' +
2513 'border-radius: 8px;' +
2514 'opacity: 0.75;' +
2515 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2516 'top: 0.5em;' +
2517 'right: 1.2em;' +
2518 'position: absolute;' +
2519 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002520 '-webkit-user-select: none;' +
2521 '-moz-text-size-adjust: none;' +
2522 '-moz-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002523 }
2524
Robert Gindab4839c22013-02-28 16:52:10 -08002525 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2526 hterm.zoomWarningMessage,
2527 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2528
rgindaf522ce02012-04-17 17:49:17 -07002529 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2530
2531 if (state) {
2532 if (!this.zoomWarningNode_.parentNode)
2533 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2534 } else if (this.zoomWarningNode_.parentNode) {
2535 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2536 }
2537};
2538
2539/**
rgindacc2996c2012-02-24 14:59:31 -08002540 * Show the terminal overlay for a given amount of time.
2541 *
2542 * The terminal overlay appears in inverse video in a large font, centered
2543 * over the terminal. You should probably keep the overlay message brief,
2544 * since it's in a large font and you probably aren't going to check the size
2545 * of the terminal first.
2546 *
2547 * @param {string} msg The text (not HTML) message to display in the overlay.
2548 * @param {number} opt_timeout The amount of time to wait before fading out
2549 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2550 * stay up forever (or until the next overlay).
2551 */
2552hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002553 if (!this.overlayNode_) {
2554 if (!this.div_)
2555 return;
2556
2557 this.overlayNode_ = this.document_.createElement('div');
2558 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002559 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002560 'font-size: xx-large;' +
2561 'opacity: 0.75;' +
2562 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2563 'position: absolute;' +
2564 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002565 '-webkit-transition: opacity 180ms ease-in;' +
2566 '-moz-user-select: none;' +
2567 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002568
2569 this.overlayNode_.addEventListener('mousedown', function(e) {
2570 e.preventDefault();
2571 e.stopPropagation();
2572 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002573 }
2574
rginda9f5222b2012-03-05 11:53:28 -08002575 this.overlayNode_.style.color = this.prefs_.get('background-color');
2576 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2577 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2578
rgindaf0090c92012-02-10 14:58:52 -08002579 this.overlayNode_.textContent = msg;
2580 this.overlayNode_.style.opacity = '0.75';
2581
2582 if (!this.overlayNode_.parentNode)
2583 this.div_.appendChild(this.overlayNode_);
2584
Robert Ginda97769282013-02-01 15:30:30 -08002585 var divSize = hterm.getClientSize(this.div_);
2586 var overlaySize = hterm.getClientSize(this.overlayNode_);
2587
Robert Ginda8a59f762014-07-23 11:29:55 -07002588 this.overlayNode_.style.top =
2589 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08002590 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07002591 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08002592
2593 var self = this;
2594
2595 if (this.overlayTimeout_)
2596 clearTimeout(this.overlayTimeout_);
2597
rgindacc2996c2012-02-24 14:59:31 -08002598 if (opt_timeout === null)
2599 return;
2600
rgindaf0090c92012-02-10 14:58:52 -08002601 this.overlayTimeout_ = setTimeout(function() {
2602 self.overlayNode_.style.opacity = '0';
Robert Ginda70926e42013-11-25 14:56:36 -08002603 self.overlayTimeout_ = setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002604 if (self.overlayNode_.parentNode)
2605 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002606 self.overlayTimeout_ = null;
2607 self.overlayNode_.style.opacity = '0.75';
2608 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002609 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002610};
2611
rginda4bba5e12012-06-20 16:15:30 -07002612/**
2613 * Paste from the system clipboard to the terminal.
2614 */
2615hterm.Terminal.prototype.paste = function() {
2616 hterm.pasteFromClipboard(this.document_);
2617};
2618
2619/**
2620 * Copy a string to the system clipboard.
2621 *
2622 * Note: If there is a selected range in the terminal, it'll be cleared.
2623 */
2624hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002625 if (this.prefs_.get('enable-clipboard-notice'))
2626 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2627
rgindaa09e7332012-08-17 12:49:51 -07002628 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002629 copySource.textContent = str;
2630 copySource.style.cssText = (
2631 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002632 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07002633 'position: absolute;' +
2634 'top: -99px');
2635
2636 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002637
rginda4bba5e12012-06-20 16:15:30 -07002638 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002639 var anchorNode = selection.anchorNode;
2640 var anchorOffset = selection.anchorOffset;
2641 var focusNode = selection.focusNode;
2642 var focusOffset = selection.focusOffset;
2643
rginda4bba5e12012-06-20 16:15:30 -07002644 selection.selectAllChildren(copySource);
2645
rgindaa09e7332012-08-17 12:49:51 -07002646 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002647
Rob Spies56953412014-04-28 14:09:47 -07002648 // IE doesn't support selection.extend. This means that the selection
2649 // won't return on IE.
Robert Ginda6f4f0aa2014-07-28 10:21:39 -07002650 if (selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07002651 selection.collapse(anchorNode, anchorOffset);
2652 selection.extend(focusNode, focusOffset);
2653 }
rgindafaa74742012-08-21 13:34:03 -07002654
rginda4bba5e12012-06-20 16:15:30 -07002655 copySource.parentNode.removeChild(copySource);
2656};
2657
rgindaa09e7332012-08-17 12:49:51 -07002658hterm.Terminal.prototype.getSelectionText = function() {
2659 var selection = this.scrollPort_.selection;
2660 selection.sync();
2661
2662 if (selection.isCollapsed)
2663 return null;
2664
2665
2666 // Start offset measures from the beginning of the line.
2667 var startOffset = selection.startOffset;
2668 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002669
Robert Gindafdbb3f22012-09-06 20:23:06 -07002670 if (node.nodeName != 'X-ROW') {
2671 // If the selection doesn't start on an x-row node, then it must be
2672 // somewhere inside the x-row. Add any characters from previous siblings
2673 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002674
2675 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2676 // If node is the text node in a styled span, move up to the span node.
2677 node = node.parentNode;
2678 }
2679
Robert Gindafdbb3f22012-09-06 20:23:06 -07002680 while (node.previousSibling) {
2681 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002682 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002683 }
rgindaa09e7332012-08-17 12:49:51 -07002684 }
2685
2686 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002687 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
2688 selection.endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002689 var node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002690
Robert Gindafdbb3f22012-09-06 20:23:06 -07002691 if (node.nodeName != 'X-ROW') {
2692 // If the selection doesn't end on an x-row node, then it must be
2693 // somewhere inside the x-row. Add any characters from following siblings
2694 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002695
2696 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2697 // If node is the text node in a styled span, move up to the span node.
2698 node = node.parentNode;
2699 }
2700
Robert Gindafdbb3f22012-09-06 20:23:06 -07002701 while (node.nextSibling) {
2702 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002703 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002704 }
rgindaa09e7332012-08-17 12:49:51 -07002705 }
2706
2707 var rv = this.getRowsText(selection.startRow.rowIndex,
2708 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002709 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002710};
2711
rginda4bba5e12012-06-20 16:15:30 -07002712/**
2713 * Copy the current selection to the system clipboard, then clear it after a
2714 * short delay.
2715 */
2716hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002717 var text = this.getSelectionText();
2718 if (text != null)
2719 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002720};
2721
rgindaf0090c92012-02-10 14:58:52 -08002722hterm.Terminal.prototype.overlaySize = function() {
2723 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2724};
2725
rginda87b86462011-12-14 13:48:03 -08002726/**
2727 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2728 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07002729 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08002730 */
2731hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002732 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002733 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2734
Robert Ginda8cb7d902013-06-20 14:37:18 -07002735 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08002736};
2737
2738/**
rgindad5613292012-06-19 15:40:37 -07002739 * Add the terminalRow and terminalColumn properties to mouse events and
2740 * then forward on to onMouse().
2741 *
2742 * The terminalRow and terminalColumn properties contain the (row, column)
2743 * coordinates for the mouse event.
2744 */
2745hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002746 if (e.processedByTerminalHandler_) {
2747 // We register our event handlers on the document, as well as the cursor
2748 // and the scroll blocker. Mouse events that occur on the cursor or
2749 // scroll blocker will also appear on the document, but we don't want to
2750 // process them twice.
2751 //
2752 // We can't just prevent bubbling because that has other side effects, so
2753 // we decorate the event object with this property instead.
2754 return;
2755 }
2756
2757 e.processedByTerminalHandler_ = true;
2758
Robert Gindaeda48db2014-07-17 09:25:30 -07002759 // One based row/column stored on the mouse event.
2760 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2761 this.scrollPort_.characterSize.height) + 1;
2762 e.terminalColumn = parseInt(e.clientX /
2763 this.scrollPort_.characterSize.width) + 1;
2764
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002765 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
2766 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07002767 return;
2768 }
2769
Robert Gindab837c052014-08-11 11:17:51 -07002770 if (this.options_.cursorVisible &&
2771 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED) {
2772 // If the cursor is visible and we're not sending mouse events to the
2773 // host app, then we want to hide the terminal cursor when the mouse
2774 // cursor is over top. This keeps the terminal cursor from interfering
2775 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07002776 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
2777 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
2778 this.cursorNode_.style.display = 'none';
2779 } else if (this.cursorNode_.style.display == 'none') {
2780 this.cursorNode_.style.display = '';
2781 }
2782 }
rgindad5613292012-06-19 15:40:37 -07002783
Robert Ginda928cf632014-03-05 15:07:41 -08002784 if (e.type == 'mousedown') {
2785 if (e.altKey || this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED) {
2786 // If VT mouse reporting is disabled, or has been defeated with
2787 // alt-mousedown, then the mouse will act on the local selection.
2788 this.reportMouseEvents_ = false;
2789 this.setSelectionEnabled(true);
2790 } else {
2791 // Otherwise we defer ownership of the mouse to the VT.
2792 this.reportMouseEvents_ = true;
Robert Ginda3ae37822014-05-15 13:05:35 -07002793 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08002794 this.setSelectionEnabled(false);
2795 e.preventDefault();
2796 }
2797 }
2798
2799 if (!this.reportMouseEvents_) {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002800 if (e.type == 'dblclick') {
2801 this.screen_.expandSelection(this.document_.getSelection());
2802 hterm.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07002803 }
2804
Robert Ginda928cf632014-03-05 15:07:41 -08002805 if (e.type == 'mousedown' && e.which == this.mousePasteButton)
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002806 this.paste();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002807
2808 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2809 !this.document_.getSelection().isCollapsed) {
2810 hterm.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002811 }
2812
2813 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
2814 this.scrollBlockerNode_.engaged) {
2815 // Disengage the scroll-blocker after one of these events.
2816 this.scrollBlockerNode_.engaged = false;
2817 this.scrollBlockerNode_.style.top = '-99px';
2818 }
2819
Robert Ginda928cf632014-03-05 15:07:41 -08002820 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002821 if (!this.scrollBlockerNode_.engaged) {
2822 if (e.type == 'mousedown') {
2823 // Move the scroll-blocker into place if we want to keep the scrollport
2824 // from scrolling.
2825 this.scrollBlockerNode_.engaged = true;
2826 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2827 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2828 } else if (e.type == 'mousemove') {
2829 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
2830 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07002831 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002832 e.preventDefault();
2833 }
2834 }
Robert Ginda928cf632014-03-05 15:07:41 -08002835
2836 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07002837 }
2838
Robert Ginda928cf632014-03-05 15:07:41 -08002839 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
2840 // Restore this on mouseup in case it was temporarily defeated with a
2841 // alt-mousedown. Only do this when the selection is empty so that
2842 // we don't immediately kill the users selection.
2843 this.reportMouseEvents_ = (this.vt.mouseReport !=
2844 this.vt.MOUSE_REPORT_DISABLED);
2845 }
rgindad5613292012-06-19 15:40:37 -07002846};
2847
2848/**
2849 * Clients should override this if they care to know about mouse events.
2850 *
2851 * The event parameter will be a normal DOM mouse click event with additional
2852 * 'terminalRow' and 'terminalColumn' properties.
2853 */
2854hterm.Terminal.prototype.onMouse = function(e) { };
2855
2856/**
rginda8e92a692012-05-20 19:37:20 -07002857 * React when focus changes.
2858 */
Rob Spies06533ba2014-04-24 11:20:37 -07002859hterm.Terminal.prototype.onFocusChange_ = function(focused) {
2860 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07002861 this.restyleCursor_();
Michael Kelly485ecd12014-06-09 11:41:56 -04002862 if (focused === true)
2863 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07002864};
2865
2866/**
rginda8ba33642011-12-14 12:31:31 -08002867 * React when the ScrollPort is scrolled.
2868 */
2869hterm.Terminal.prototype.onScroll_ = function() {
2870 this.scheduleSyncCursorPosition_();
2871};
2872
2873/**
rginda9846e2f2012-01-27 13:53:33 -08002874 * React when text is pasted into the scrollPort.
2875 */
2876hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07002877 var data = e.text.replace(/\n/mg, '\r');
Connor Hegartyf525ceb2014-09-03 13:36:03 -07002878 data = this.keyboard.encode(data);
Robert Gindaa063b202014-07-21 11:08:25 -07002879 if (this.options_.bracketedPaste)
2880 data = '\x1b[200~' + data + '\x1b[201~';
2881
2882 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08002883};
2884
2885/**
rgindaa09e7332012-08-17 12:49:51 -07002886 * React when the user tries to copy from the scrollPort.
2887 */
2888hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07002889 if (!this.useDefaultWindowCopy) {
2890 e.preventDefault();
2891 setTimeout(this.copySelectionToClipboard.bind(this), 0);
2892 }
rgindaa09e7332012-08-17 12:49:51 -07002893};
2894
2895/**
rginda8ba33642011-12-14 12:31:31 -08002896 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002897 *
2898 * Note: This function should not directly contain code that alters the internal
2899 * state of the terminal. That kind of code belongs in realizeWidth or
2900 * realizeHeight, so that it can be executed synchronously in the case of a
2901 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002902 */
2903hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08002904 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002905 this.scrollPort_.characterSize.width);
Rob Spiesf4e90e82015-01-28 12:10:13 -08002906 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
rginda35c456b2012-02-09 17:29:05 -08002907 this.scrollPort_.characterSize.height);
2908
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002909 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08002910 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002911 // gets removed from the document or during the initial load, and we can't
2912 // deal with that.
rginda35c456b2012-02-09 17:29:05 -08002913 return;
2914 }
2915
rgindaa8ba17d2012-08-15 14:41:10 -07002916 var isNewSize = (columnCount != this.screenSize.width ||
2917 rowCount != this.screenSize.height);
2918
2919 // We do this even if the size didn't change, just to be sure everything is
2920 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002921 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07002922 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07002923
2924 if (isNewSize)
2925 this.overlaySize();
2926
Robert Gindafb1be6a2013-12-11 11:56:22 -08002927 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07002928 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08002929};
2930
2931/**
2932 * Service the cursor blink timeout.
2933 */
2934hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07002935 if (!this.options_.cursorBlink) {
2936 delete this.timeouts_.cursorBlink;
2937 return;
2938 }
2939
Robert Ginda830583c2013-08-07 13:20:46 -07002940 if (this.cursorNode_.getAttribute('focus') == 'false' ||
2941 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08002942 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07002943 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
2944 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08002945 } else {
rginda87b86462011-12-14 13:48:03 -08002946 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07002947 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
2948 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08002949 }
2950};
David Reveman8f552492012-03-28 12:18:41 -04002951
2952/**
2953 * Set the scrollbar-visible mode bit.
2954 *
2955 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2956 * Otherwise it will not.
2957 *
2958 * Defaults to on.
2959 *
2960 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2961 */
2962hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2963 this.scrollPort_.setScrollbarVisible(state);
2964};
Michael Kelly485ecd12014-06-09 11:41:56 -04002965
2966/**
Rob Spies49039e52014-12-17 13:40:04 -08002967 * Set the scroll wheel move multiplier. This will affect how fast the page
2968 * scrolls on mousewheel events.
2969 *
2970 * Defaults to 1.
2971 *
2972 * @param {number} multiplier.
2973 */
2974hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
2975 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
2976};
2977
2978/**
Michael Kelly485ecd12014-06-09 11:41:56 -04002979 * Close all web notifications created by terminal bells.
2980 */
2981hterm.Terminal.prototype.closeBellNotifications_ = function() {
2982 this.bellNotificationList_.forEach(function(n) {
2983 n.close();
2984 });
2985 this.bellNotificationList_.length = 0;
2986};