blob: e2d08de98451230b4f7531e158f0c9efd65b609b [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 Gindaa8165692015-06-15 14:46:31 -0700416 'keybindings': function(v) {
417 terminal.keyboard.bindings.clear();
418
419 if (!v)
420 return;
421
422 if (!(v instanceof Object)) {
423 console.error('Error in keybindings preference: Expected object');
424 return;
425 }
426
427 try {
428 terminal.keyboard.bindings.addBindings(v);
429 } catch (ex) {
430 console.error('Error in keybindings preference: ' + ex);
431 }
432 },
433
Robert Ginda57f03b42012-09-13 11:02:48 -0700434 'max-string-sequence': function(v) {
435 terminal.vt.maxStringSequence = v;
436 },
rginda11057d52012-04-25 12:29:56 -0700437
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700438 'media-keys-are-fkeys': function(v) {
439 terminal.keyboard.mediaKeysAreFKeys = v;
440 },
441
Robert Ginda57f03b42012-09-13 11:02:48 -0700442 'meta-sends-escape': function(v) {
443 terminal.keyboard.metaSendsEscape = v;
444 },
rginda30f20f62012-04-05 16:36:19 -0700445
Robert Ginda57f03b42012-09-13 11:02:48 -0700446 'mouse-paste-button': function(v) {
447 terminal.syncMousePasteButton();
448 },
rgindaa8ba17d2012-08-15 14:41:10 -0700449
Robert Gindae76aa9f2014-03-14 12:29:12 -0700450 'page-keys-scroll': function(v) {
451 terminal.keyboard.pageKeysScroll = v;
452 },
453
Robert Ginda40932892012-12-10 17:26:40 -0800454 'pass-alt-number': function(v) {
455 if (v == null) {
456 var osx = window.navigator.userAgent.match(/Mac OS X/);
457
458 // Let Alt-1..9 pass to the browser (to control tab switching) on
459 // non-OS X systems, or if hterm is not opened in an app window.
460 v = (!osx && hterm.windowType != 'popup');
461 }
462
463 terminal.passAltNumber = v;
464 },
465
466 'pass-ctrl-number': function(v) {
467 if (v == null) {
468 var osx = window.navigator.userAgent.match(/Mac OS X/);
469
470 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
471 // non-OS X systems, or if hterm is not opened in an app window.
472 v = (!osx && hterm.windowType != 'popup');
473 }
474
475 terminal.passCtrlNumber = v;
476 },
477
478 'pass-meta-number': function(v) {
479 if (v == null) {
480 var osx = window.navigator.userAgent.match(/Mac OS X/);
481
482 // Let Meta-1..9 pass to the browser (to control tab switching) on
483 // OS X systems, or if hterm is not opened in an app window.
484 v = (osx && hterm.windowType != 'popup');
485 }
486
487 terminal.passMetaNumber = v;
488 },
489
Marius Schilder77857b32014-05-14 16:21:26 -0700490 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700491 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700492 },
493
Robert Ginda8cb7d902013-06-20 14:37:18 -0700494 'receive-encoding': function(v) {
495 if (!(/^(utf-8|raw)$/).test(v)) {
496 console.warn('Invalid value for "receive-encoding": ' + v);
497 v = 'utf-8';
498 }
499
500 terminal.vt.characterEncoding = v;
501 },
502
Robert Ginda57f03b42012-09-13 11:02:48 -0700503 'scroll-on-keystroke': function(v) {
504 terminal.scrollOnKeystroke_ = v;
505 },
rginda9f5222b2012-03-05 11:53:28 -0800506
Robert Ginda57f03b42012-09-13 11:02:48 -0700507 'scroll-on-output': function(v) {
508 terminal.scrollOnOutput_ = v;
509 },
rginda30f20f62012-04-05 16:36:19 -0700510
Robert Ginda57f03b42012-09-13 11:02:48 -0700511 'scrollbar-visible': function(v) {
512 terminal.setScrollbarVisible(v);
513 },
rginda9f5222b2012-03-05 11:53:28 -0800514
Rob Spies49039e52014-12-17 13:40:04 -0800515 'scroll-wheel-move-multiplier': function(v) {
516 terminal.setScrollWheelMoveMultipler(v);
517 },
518
Robert Ginda8cb7d902013-06-20 14:37:18 -0700519 'send-encoding': function(v) {
520 if (!(/^(utf-8|raw)$/).test(v)) {
521 console.warn('Invalid value for "send-encoding": ' + v);
522 v = 'utf-8';
523 }
524
525 terminal.keyboard.characterEncoding = v;
526 },
527
Robert Ginda57f03b42012-09-13 11:02:48 -0700528 'shift-insert-paste': function(v) {
529 terminal.keyboard.shiftInsertPaste = v;
530 },
rginda9f5222b2012-03-05 11:53:28 -0800531
Robert Gindae76aa9f2014-03-14 12:29:12 -0700532 'user-css': function(v) {
533 terminal.scrollPort_.setUserCss(v);
Robert Ginda57f03b42012-09-13 11:02:48 -0700534 }
535 });
rginda30f20f62012-04-05 16:36:19 -0700536
Robert Ginda57f03b42012-09-13 11:02:48 -0700537 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800538 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700539
540 if (opt_callback)
541 opt_callback();
542 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800543};
544
Rob Spies56953412014-04-28 14:09:47 -0700545
546/**
547 * Returns the preferences manager used for configuring this terminal.
548 */
549hterm.Terminal.prototype.getPrefs = function() {
550 return this.prefs_;
551};
552
Robert Gindaa063b202014-07-21 11:08:25 -0700553/**
554 * Enable or disable bracketed paste mode.
555 */
556hterm.Terminal.prototype.setBracketedPaste = function(state) {
557 this.options_.bracketedPaste = state;
558};
Rob Spies56953412014-04-28 14:09:47 -0700559
rginda8e92a692012-05-20 19:37:20 -0700560/**
561 * Set the color for the cursor.
562 *
563 * If you want this setting to persist, set it through prefs_, rather than
564 * with this method.
565 */
566hterm.Terminal.prototype.setCursorColor = function(color) {
Robert Ginda830583c2013-08-07 13:20:46 -0700567 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700568 this.cursorNode_.style.backgroundColor = color;
569 this.cursorNode_.style.borderColor = color;
570};
571
572/**
573 * Return the current cursor color as a string.
574 */
575hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700576 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700577};
578
579/**
rgindad5613292012-06-19 15:40:37 -0700580 * Enable or disable mouse based text selection in the terminal.
581 */
582hterm.Terminal.prototype.setSelectionEnabled = function(state) {
583 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700584};
585
586/**
rginda8e92a692012-05-20 19:37:20 -0700587 * Set the background color.
588 *
589 * If you want this setting to persist, set it through prefs_, rather than
590 * with this method.
591 */
592hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700593 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700594 this.primaryScreen_.textAttributes.setDefaults(
595 this.foregroundColor_, this.backgroundColor_);
596 this.alternateScreen_.textAttributes.setDefaults(
597 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700598 this.scrollPort_.setBackgroundColor(color);
599};
600
rginda9f5222b2012-03-05 11:53:28 -0800601/**
602 * Return the current terminal background color.
603 *
604 * Intended for use by other classes, so we don't have to expose the entire
605 * prefs_ object.
606 */
607hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700608 return this.backgroundColor_;
609};
610
611/**
612 * Set the foreground color.
613 *
614 * If you want this setting to persist, set it through prefs_, rather than
615 * with this method.
616 */
617hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700618 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700619 this.primaryScreen_.textAttributes.setDefaults(
620 this.foregroundColor_, this.backgroundColor_);
621 this.alternateScreen_.textAttributes.setDefaults(
622 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700623 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800624};
625
626/**
627 * Return the current terminal foreground color.
628 *
629 * Intended for use by other classes, so we don't have to expose the entire
630 * prefs_ object.
631 */
632hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700633 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800634};
635
636/**
rginda87b86462011-12-14 13:48:03 -0800637 * Create a new instance of a terminal command and run it with a given
638 * argument string.
639 *
640 * @param {function} commandClass The constructor for a terminal command.
641 * @param {string} argString The argument string to pass to the command.
642 */
643hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700644 var environment = this.prefs_.get('environment');
645 if (typeof environment != 'object' || environment == null)
646 environment = {};
647
rginda87b86462011-12-14 13:48:03 -0800648 var self = this;
649 this.command = new commandClass(
650 { argString: argString || '',
651 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700652 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800653 onExit: function(code) {
654 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800655 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700656 if (self.prefs_.get('close-on-exit'))
657 window.close();
rginda87b86462011-12-14 13:48:03 -0800658 }
659 });
660
rgindafeaf3142012-01-31 15:14:20 -0800661 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800662 this.command.run();
663};
664
665/**
rgindafeaf3142012-01-31 15:14:20 -0800666 * Returns true if the current screen is the primary screen, false otherwise.
667 */
668hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700669 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800670};
671
672/**
673 * Install the keyboard handler for this terminal.
674 *
675 * This will prevent the browser from seeing any keystrokes sent to the
676 * terminal.
677 */
678hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700679 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
rgindafeaf3142012-01-31 15:14:20 -0800680}
681
682/**
683 * Uninstall the keyboard handler for this terminal.
684 */
685hterm.Terminal.prototype.uninstallKeyboard = function() {
686 this.keyboard.installKeyboard(null);
687}
688
689/**
rginda35c456b2012-02-09 17:29:05 -0800690 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800691 *
692 * Call setFontSize(0) to reset to the default font size.
693 *
694 * This function does not modify the font-size preference.
695 *
696 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800697 */
698hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800699 if (px === 0)
700 px = this.prefs_.get('font-size');
701
rginda35c456b2012-02-09 17:29:05 -0800702 this.scrollPort_.setFontSize(px);
Ricky Liang48f05cb2013-12-31 23:35:29 +0800703 if (this.wcCssRule_) {
704 this.wcCssRule_.style.width = this.scrollPort_.characterSize.width * 2 +
705 'px';
706 }
rginda35c456b2012-02-09 17:29:05 -0800707};
708
709/**
710 * Get the current font size.
711 */
712hterm.Terminal.prototype.getFontSize = function() {
713 return this.scrollPort_.getFontSize();
714};
715
716/**
rginda8e92a692012-05-20 19:37:20 -0700717 * Get the current font family.
718 */
719hterm.Terminal.prototype.getFontFamily = function() {
720 return this.scrollPort_.getFontFamily();
721};
722
723/**
rginda35c456b2012-02-09 17:29:05 -0800724 * Set the CSS "font-family" for this terminal.
725 */
rginda9f5222b2012-03-05 11:53:28 -0800726hterm.Terminal.prototype.syncFontFamily = function() {
727 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
728 this.prefs_.get('font-smoothing'));
729 this.syncBoldSafeState();
730};
731
rginda4bba5e12012-06-20 16:15:30 -0700732/**
733 * Set this.mousePasteButton based on the mouse-paste-button pref,
734 * autodetecting if necessary.
735 */
736hterm.Terminal.prototype.syncMousePasteButton = function() {
737 var button = this.prefs_.get('mouse-paste-button');
738 if (typeof button == 'number') {
739 this.mousePasteButton = button;
740 return;
741 }
742
743 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
744 if (!ary || ary[2] == 'CrOS') {
745 this.mousePasteButton = 2;
746 } else {
747 this.mousePasteButton = 3;
748 }
749};
750
751/**
752 * Enable or disable bold based on the enable-bold pref, autodetecting if
753 * necessary.
754 */
rginda9f5222b2012-03-05 11:53:28 -0800755hterm.Terminal.prototype.syncBoldSafeState = function() {
756 var enableBold = this.prefs_.get('enable-bold');
757 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700758 this.primaryScreen_.textAttributes.enableBold = enableBold;
759 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800760 return;
761 }
762
rgindaf7521392012-02-28 17:20:34 -0800763 var normalSize = this.scrollPort_.measureCharacterSize();
764 var boldSize = this.scrollPort_.measureCharacterSize('bold');
765
766 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800767 if (!isBoldSafe) {
768 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700769 'from normal. Font family is: ' +
770 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800771 }
rginda9f5222b2012-03-05 11:53:28 -0800772
Robert Gindaed016262012-10-26 16:27:09 -0700773 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
774 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800775};
776
777/**
rginda87b86462011-12-14 13:48:03 -0800778 * Return a copy of the current cursor position.
779 *
780 * @return {hterm.RowCol} The RowCol object representing the current position.
781 */
782hterm.Terminal.prototype.saveCursor = function() {
783 return this.screen_.cursorPosition.clone();
784};
785
rgindaa19afe22012-01-25 15:40:22 -0800786hterm.Terminal.prototype.getTextAttributes = function() {
787 return this.screen_.textAttributes;
788};
789
rginda1a09aa02012-06-18 21:11:25 -0700790hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
791 this.screen_.textAttributes = textAttributes;
792};
793
rginda87b86462011-12-14 13:48:03 -0800794/**
rgindaf522ce02012-04-17 17:49:17 -0700795 * Return the current browser zoom factor applied to the terminal.
796 *
797 * @return {number} The current browser zoom factor.
798 */
799hterm.Terminal.prototype.getZoomFactor = function() {
800 return this.scrollPort_.characterSize.zoomFactor;
801};
802
803/**
rginda9846e2f2012-01-27 13:53:33 -0800804 * Change the title of this terminal's window.
805 */
806hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800807 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800808};
809
810/**
rginda87b86462011-12-14 13:48:03 -0800811 * Restore a previously saved cursor position.
812 *
813 * @param {hterm.RowCol} cursor The position to restore.
814 */
815hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700816 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
817 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800818 this.screen_.setCursorPosition(row, column);
819 if (cursor.column > column ||
820 cursor.column == column && cursor.overflow) {
821 this.screen_.cursorPosition.overflow = true;
822 }
rginda87b86462011-12-14 13:48:03 -0800823};
824
825/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400826 * Clear the cursor's overflow flag.
827 */
828hterm.Terminal.prototype.clearCursorOverflow = function() {
829 this.screen_.cursorPosition.overflow = false;
830};
831
832/**
Robert Ginda830583c2013-08-07 13:20:46 -0700833 * Sets the cursor shape
834 */
835hterm.Terminal.prototype.setCursorShape = function(shape) {
836 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -0800837 this.restyleCursor_();
Robert Ginda830583c2013-08-07 13:20:46 -0700838}
839
840/**
841 * Get the cursor shape
842 */
843hterm.Terminal.prototype.getCursorShape = function() {
844 return this.cursorShape_;
845}
846
847/**
rginda87b86462011-12-14 13:48:03 -0800848 * Set the width of the terminal, resizing the UI to match.
849 */
850hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800851 if (columnCount == null) {
852 this.div_.style.width = '100%';
853 return;
854 }
855
Robert Ginda26806d12014-07-24 13:44:07 -0700856 this.div_.style.width = Math.ceil(
857 this.scrollPort_.characterSize.width *
858 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400859 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800860 this.scheduleSyncCursorPosition_();
861};
rginda87b86462011-12-14 13:48:03 -0800862
rgindac9bc5502012-01-18 11:48:44 -0800863/**
rginda35c456b2012-02-09 17:29:05 -0800864 * Set the height of the terminal, resizing the UI to match.
865 */
866hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800867 if (rowCount == null) {
868 this.div_.style.height = '100%';
869 return;
870 }
871
rginda35c456b2012-02-09 17:29:05 -0800872 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700873 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800874 this.realizeSize_(this.screenSize.width, rowCount);
875 this.scheduleSyncCursorPosition_();
876};
877
878/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400879 * Deal with terminal size changes.
880 *
881 */
882hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
883 if (columnCount != this.screenSize.width)
884 this.realizeWidth_(columnCount);
885
886 if (rowCount != this.screenSize.height)
887 this.realizeHeight_(rowCount);
888
889 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -0700890 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400891};
892
893/**
rgindac9bc5502012-01-18 11:48:44 -0800894 * Deal with terminal width changes.
895 *
896 * This function does what needs to be done when the terminal width changes
897 * out from under us. It happens here rather than in onResize_() because this
898 * code may need to run synchronously to handle programmatic changes of
899 * terminal width.
900 *
901 * Relying on the browser to send us an async resize event means we may not be
902 * in the correct state yet when the next escape sequence hits.
903 */
904hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700905 if (columnCount <= 0)
906 throw new Error('Attempt to realize bad width: ' + columnCount);
907
rgindac9bc5502012-01-18 11:48:44 -0800908 var deltaColumns = columnCount - this.screen_.getWidth();
909
rginda87b86462011-12-14 13:48:03 -0800910 this.screenSize.width = columnCount;
911 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800912
913 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400914 if (this.defaultTabStops)
915 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800916 } else {
917 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400918 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800919 break;
920
921 this.tabStops_.pop();
922 }
923 }
924
925 this.screen_.setColumnCount(this.screenSize.width);
926};
927
928/**
929 * Deal with terminal height changes.
930 *
931 * This function does what needs to be done when the terminal height changes
932 * out from under us. It happens here rather than in onResize_() because this
933 * code may need to run synchronously to handle programmatic changes of
934 * terminal height.
935 *
936 * Relying on the browser to send us an async resize event means we may not be
937 * in the correct state yet when the next escape sequence hits.
938 */
939hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700940 if (rowCount <= 0)
941 throw new Error('Attempt to realize bad height: ' + rowCount);
942
rgindac9bc5502012-01-18 11:48:44 -0800943 var deltaRows = rowCount - this.screen_.getHeight();
944
945 this.screenSize.height = rowCount;
946
947 var cursor = this.saveCursor();
948
949 if (deltaRows < 0) {
950 // Screen got smaller.
951 deltaRows *= -1;
952 while (deltaRows) {
953 var lastRow = this.getRowCount() - 1;
954 if (lastRow - this.scrollbackRows_.length == cursor.row)
955 break;
956
957 if (this.getRowText(lastRow))
958 break;
959
960 this.screen_.popRow();
961 deltaRows--;
962 }
963
964 var ary = this.screen_.shiftRows(deltaRows);
965 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
966
967 // We just removed rows from the top of the screen, we need to update
968 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800969 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800970 } else if (deltaRows > 0) {
971 // Screen got larger.
972
973 if (deltaRows <= this.scrollbackRows_.length) {
974 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
975 var rows = this.scrollbackRows_.splice(
976 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
977 this.screen_.unshiftRows(rows);
978 deltaRows -= scrollbackCount;
979 cursor.row += scrollbackCount;
980 }
981
982 if (deltaRows)
983 this.appendRows_(deltaRows);
984 }
985
rginda35c456b2012-02-09 17:29:05 -0800986 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800987 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800988};
989
990/**
991 * Scroll the terminal to the top of the scrollback buffer.
992 */
993hterm.Terminal.prototype.scrollHome = function() {
994 this.scrollPort_.scrollRowToTop(0);
995};
996
997/**
998 * Scroll the terminal to the end.
999 */
1000hterm.Terminal.prototype.scrollEnd = function() {
1001 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1002};
1003
1004/**
1005 * Scroll the terminal one page up (minus one line) relative to the current
1006 * position.
1007 */
1008hterm.Terminal.prototype.scrollPageUp = function() {
1009 var i = this.scrollPort_.getTopRowIndex();
1010 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
1011};
1012
1013/**
1014 * Scroll the terminal one page down (minus one line) relative to the current
1015 * position.
1016 */
1017hterm.Terminal.prototype.scrollPageDown = function() {
1018 var i = this.scrollPort_.getTopRowIndex();
1019 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -08001020};
1021
rgindac9bc5502012-01-18 11:48:44 -08001022/**
Robert Ginda40932892012-12-10 17:26:40 -08001023 * Clear primary screen, secondary screen, and the scrollback buffer.
1024 */
1025hterm.Terminal.prototype.wipeContents = function() {
1026 this.scrollbackRows_.length = 0;
1027 this.scrollPort_.resetCache();
1028
1029 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
1030 var bottom = screen.getHeight();
1031 if (bottom > 0) {
1032 this.renumberRows_(0, bottom);
1033 this.clearHome(screen);
1034 }
1035 }.bind(this));
1036
1037 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001038 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001039};
1040
1041/**
rgindac9bc5502012-01-18 11:48:44 -08001042 * Full terminal reset.
1043 */
rginda87b86462011-12-14 13:48:03 -08001044hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -08001045 this.clearAllTabStops();
1046 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001047
1048 this.clearHome(this.primaryScreen_);
1049 this.primaryScreen_.textAttributes.reset();
1050
1051 this.clearHome(this.alternateScreen_);
1052 this.alternateScreen_.textAttributes.reset();
1053
rgindab8bc8932012-04-27 12:45:03 -07001054 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1055
Robert Ginda92e18102013-03-14 13:56:37 -07001056 this.vt.reset();
1057
rgindac9bc5502012-01-18 11:48:44 -08001058 this.softReset();
rginda87b86462011-12-14 13:48:03 -08001059};
1060
rgindac9bc5502012-01-18 11:48:44 -08001061/**
1062 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001063 *
1064 * Perform a soft reset to the default values listed in
1065 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001066 */
rginda0f5c0292012-01-13 11:00:13 -08001067hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -07001068 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001069 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001070
Brad Townb62dfdc2015-03-16 19:07:15 -07001071 // We show the cursor on soft reset but do not alter the blink state.
1072 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1073
rgindab8bc8932012-04-27 12:45:03 -07001074 // Xterm also resets the color palette on soft reset, even though it doesn't
1075 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -07001076 this.primaryScreen_.textAttributes.resetColorPalette();
1077 this.alternateScreen_.textAttributes.resetColorPalette();
1078
rgindab8bc8932012-04-27 12:45:03 -07001079 // The xterm man page explicitly says this will happen on soft reset.
1080 this.setVTScrollRegion(null, null);
1081
1082 // Xterm also shows the cursor on soft reset, but does not alter the blink
1083 // state.
rgindaa19afe22012-01-25 15:40:22 -08001084 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001085};
1086
rgindac9bc5502012-01-18 11:48:44 -08001087/**
1088 * Move the cursor forward to the next tab stop, or to the last column
1089 * if no more tab stops are set.
1090 */
1091hterm.Terminal.prototype.forwardTabStop = function() {
1092 var column = this.screen_.cursorPosition.column;
1093
1094 for (var i = 0; i < this.tabStops_.length; i++) {
1095 if (this.tabStops_[i] > column) {
1096 this.setCursorColumn(this.tabStops_[i]);
1097 return;
1098 }
1099 }
1100
David Benjamin66e954d2012-05-05 21:08:12 -04001101 // xterm does not clear the overflow flag on HT or CHT.
1102 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001103 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001104 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001105};
1106
rgindac9bc5502012-01-18 11:48:44 -08001107/**
1108 * Move the cursor backward to the previous tab stop, or to the first column
1109 * if no previous tab stops are set.
1110 */
1111hterm.Terminal.prototype.backwardTabStop = function() {
1112 var column = this.screen_.cursorPosition.column;
1113
1114 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1115 if (this.tabStops_[i] < column) {
1116 this.setCursorColumn(this.tabStops_[i]);
1117 return;
1118 }
1119 }
1120
1121 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001122};
1123
rgindac9bc5502012-01-18 11:48:44 -08001124/**
1125 * Set a tab stop at the given column.
1126 *
1127 * @param {int} column Zero based column.
1128 */
1129hterm.Terminal.prototype.setTabStop = function(column) {
1130 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1131 if (this.tabStops_[i] == column)
1132 return;
1133
1134 if (this.tabStops_[i] < column) {
1135 this.tabStops_.splice(i + 1, 0, column);
1136 return;
1137 }
1138 }
1139
1140 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001141};
1142
rgindac9bc5502012-01-18 11:48:44 -08001143/**
1144 * Clear the tab stop at the current cursor position.
1145 *
1146 * No effect if there is no tab stop at the current cursor position.
1147 */
1148hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1149 var column = this.screen_.cursorPosition.column;
1150
1151 var i = this.tabStops_.indexOf(column);
1152 if (i == -1)
1153 return;
1154
1155 this.tabStops_.splice(i, 1);
1156};
1157
1158/**
1159 * Clear all tab stops.
1160 */
1161hterm.Terminal.prototype.clearAllTabStops = function() {
1162 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001163 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001164};
1165
1166/**
1167 * Set up the default tab stops, starting from a given column.
1168 *
1169 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001170 * from the specified column, or 0 if no column is provided. It also flags
1171 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001172 *
1173 * This does not clear the existing tab stops first, use clearAllTabStops
1174 * for that.
1175 *
1176 * @param {int} opt_start Optional starting zero based starting column, useful
1177 * for filling out missing tab stops when the terminal is resized.
1178 */
1179hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1180 var start = opt_start || 0;
1181 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001182 // Round start up to a default tab stop.
1183 start = start - 1 - ((start - 1) % w) + w;
1184 for (var i = start; i < this.screenSize.width; i += w) {
1185 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001186 }
David Benjamin66e954d2012-05-05 21:08:12 -04001187
1188 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001189};
1190
rginda6d397402012-01-17 10:58:29 -08001191/**
rginda8ba33642011-12-14 12:31:31 -08001192 * Interpret a sequence of characters.
1193 *
1194 * Incomplete escape sequences are buffered until the next call.
1195 *
1196 * @param {string} str Sequence of characters to interpret or pass through.
1197 */
1198hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001199 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001200 this.scheduleSyncCursorPosition_();
1201};
1202
1203/**
1204 * Take over the given DIV for use as the terminal display.
1205 *
1206 * @param {HTMLDivElement} div The div to use as the terminal display.
1207 */
1208hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001209 this.div_ = div;
1210
rginda8ba33642011-12-14 12:31:31 -08001211 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001212 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001213 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1214 this.scrollPort_.setBackgroundPosition(
1215 this.prefs_.get('background-position'));
Robert Gindae76aa9f2014-03-14 12:29:12 -07001216 this.scrollPort_.setUserCss(this.prefs_.get('user-css'));
rginda30f20f62012-04-05 16:36:19 -07001217
rginda0918b652012-04-04 11:26:24 -07001218 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001219
rginda9f5222b2012-03-05 11:53:28 -08001220 this.setFontSize(this.prefs_.get('font-size'));
1221 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001222
David Reveman8f552492012-03-28 12:18:41 -04001223 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001224 this.setScrollWheelMoveMultipler(
1225 this.prefs_.get('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001226
rginda8ba33642011-12-14 12:31:31 -08001227 this.document_ = this.scrollPort_.getDocument();
1228
rginda4bba5e12012-06-20 16:15:30 -07001229 this.document_.body.oncontextmenu = function() { return false };
1230
1231 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001232 var screenNode = this.scrollPort_.getScreenNode();
1233 screenNode.addEventListener('mousedown', onMouse);
1234 screenNode.addEventListener('mouseup', onMouse);
1235 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001236 this.scrollPort_.onScrollWheel = onMouse;
1237
Toni Barzic0bfa8922013-11-22 11:18:35 -08001238 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001239 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001240 // Listen for mousedown events on the screenNode as in FF the focus
1241 // events don't bubble.
1242 screenNode.addEventListener('mousedown', function() {
1243 setTimeout(this.onFocusChange_.bind(this, true));
1244 }.bind(this));
1245
Toni Barzic0bfa8922013-11-22 11:18:35 -08001246 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001247 'blur', this.onFocusChange_.bind(this, false));
1248
1249 var style = this.document_.createElement('style');
1250 style.textContent =
1251 ('.cursor-node[focus="false"] {' +
1252 ' box-sizing: border-box;' +
1253 ' background-color: transparent !important;' +
1254 ' border-width: 2px;' +
1255 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001256 '}' +
1257 '.wc-node {' +
1258 ' display: inline-block;' +
1259 ' text-align: center;' +
1260 ' width: ' + this.scrollPort_.characterSize.width * 2 + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001261 '}');
1262 this.document_.head.appendChild(style);
1263
Ricky Liang48f05cb2013-12-31 23:35:29 +08001264 var styleSheets = this.document_.styleSheets;
1265 var cssRules = styleSheets[styleSheets.length - 1].cssRules;
1266 this.wcCssRule_ = cssRules[cssRules.length - 1];
1267
rginda8ba33642011-12-14 12:31:31 -08001268 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001269 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001270 this.cursorNode_.style.cssText =
1271 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001272 'top: -99px;' +
1273 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001274 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1275 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
Rob Spies06533ba2014-04-24 11:20:37 -07001276 '-webkit-transition: opacity, background-color 100ms linear;' +
1277 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001278
rginda8e92a692012-05-20 19:37:20 -07001279 this.setCursorColor(this.prefs_.get('cursor-color'));
Robert Gindafb1be6a2013-12-11 11:56:22 -08001280 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1281 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001282
rginda8ba33642011-12-14 12:31:31 -08001283 this.document_.body.appendChild(this.cursorNode_);
1284
rgindad5613292012-06-19 15:40:37 -07001285 // When 'enableMouseDragScroll' is off we reposition this element directly
1286 // under the mouse cursor after a click. This makes Chrome associate
1287 // subsequent mousemove events with the scroll-blocker. Since the
1288 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1289 // events do not cause the scrollport to scroll.
1290 //
1291 // It's a hack, but it's the cleanest way I could find.
1292 this.scrollBlockerNode_ = this.document_.createElement('div');
1293 this.scrollBlockerNode_.style.cssText =
1294 ('position: absolute;' +
1295 'top: -99px;' +
1296 'display: block;' +
1297 'width: 10px;' +
1298 'height: 10px;');
1299 this.document_.body.appendChild(this.scrollBlockerNode_);
1300
1301 var onMouse = this.onMouse_.bind(this);
1302 this.scrollPort_.onScrollWheel = onMouse;
1303 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1304 ].forEach(function(event) {
1305 this.scrollBlockerNode_.addEventListener(event, onMouse);
1306 this.cursorNode_.addEventListener(event, onMouse);
1307 this.document_.addEventListener(event, onMouse);
1308 }.bind(this));
1309
1310 this.cursorNode_.addEventListener('mousedown', function() {
1311 setTimeout(this.focus.bind(this));
1312 }.bind(this));
1313
rginda8ba33642011-12-14 12:31:31 -08001314 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001315
rginda87b86462011-12-14 13:48:03 -08001316 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001317 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001318};
1319
rginda0918b652012-04-04 11:26:24 -07001320/**
1321 * Return the HTML document that contains the terminal DOM nodes.
1322 */
rginda87b86462011-12-14 13:48:03 -08001323hterm.Terminal.prototype.getDocument = function() {
1324 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001325};
1326
1327/**
rginda0918b652012-04-04 11:26:24 -07001328 * Focus the terminal.
1329 */
1330hterm.Terminal.prototype.focus = function() {
1331 this.scrollPort_.focus();
1332};
1333
1334/**
rginda8ba33642011-12-14 12:31:31 -08001335 * Return the HTML Element for a given row index.
1336 *
1337 * This is a method from the RowProvider interface. The ScrollPort uses
1338 * it to fetch rows on demand as they are scrolled into view.
1339 *
1340 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1341 * pairs to conserve memory.
1342 *
1343 * @param {integer} index The zero-based row index, measured relative to the
1344 * start of the scrollback buffer. On-screen rows will always have the
1345 * largest indicies.
1346 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1347 */
1348hterm.Terminal.prototype.getRowNode = function(index) {
1349 if (index < this.scrollbackRows_.length)
1350 return this.scrollbackRows_[index];
1351
1352 var screenIndex = index - this.scrollbackRows_.length;
1353 return this.screen_.rowsArray[screenIndex];
1354};
1355
1356/**
1357 * Return the text content for a given range of rows.
1358 *
1359 * This is a method from the RowProvider interface. The ScrollPort uses
1360 * it to fetch text content on demand when the user attempts to copy their
1361 * selection to the clipboard.
1362 *
1363 * @param {integer} start The zero-based row index to start from, measured
1364 * relative to the start of the scrollback buffer. On-screen rows will
1365 * always have the largest indicies.
1366 * @param {integer} end The zero-based row index to end on, measured
1367 * relative to the start of the scrollback buffer.
1368 * @return {string} A single string containing the text value of the range of
1369 * rows. Lines will be newline delimited, with no trailing newline.
1370 */
1371hterm.Terminal.prototype.getRowsText = function(start, end) {
1372 var ary = [];
1373 for (var i = start; i < end; i++) {
1374 var node = this.getRowNode(i);
1375 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001376 if (i < end - 1 && !node.getAttribute('line-overflow'))
1377 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001378 }
1379
rgindaa09e7332012-08-17 12:49:51 -07001380 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001381};
1382
1383/**
1384 * Return the text content for a given row.
1385 *
1386 * This is a method from the RowProvider interface. The ScrollPort uses
1387 * it to fetch text content on demand when the user attempts to copy their
1388 * selection to the clipboard.
1389 *
1390 * @param {integer} index The zero-based row index to return, measured
1391 * relative to the start of the scrollback buffer. On-screen rows will
1392 * always have the largest indicies.
1393 * @return {string} A string containing the text value of the selected row.
1394 */
1395hterm.Terminal.prototype.getRowText = function(index) {
1396 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001397 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001398};
1399
1400/**
1401 * Return the total number of rows in the addressable screen and in the
1402 * scrollback buffer of this terminal.
1403 *
1404 * This is a method from the RowProvider interface. The ScrollPort uses
1405 * it to compute the size of the scrollbar.
1406 *
1407 * @return {integer} The number of rows in this terminal.
1408 */
1409hterm.Terminal.prototype.getRowCount = function() {
1410 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1411};
1412
1413/**
1414 * Create DOM nodes for new rows and append them to the end of the terminal.
1415 *
1416 * This is the only correct way to add a new DOM node for a row. Notice that
1417 * the new row is appended to the bottom of the list of rows, and does not
1418 * require renumbering (of the rowIndex property) of previous rows.
1419 *
1420 * If you think you want a new blank row somewhere in the middle of the
1421 * terminal, look into moveRows_().
1422 *
1423 * This method does not pay attention to vtScrollTop/Bottom, since you should
1424 * be using moveRows() in cases where they would matter.
1425 *
1426 * The cursor will be positioned at column 0 of the first inserted line.
1427 */
1428hterm.Terminal.prototype.appendRows_ = function(count) {
1429 var cursorRow = this.screen_.rowsArray.length;
1430 var offset = this.scrollbackRows_.length + cursorRow;
1431 for (var i = 0; i < count; i++) {
1432 var row = this.document_.createElement('x-row');
1433 row.appendChild(this.document_.createTextNode(''));
1434 row.rowIndex = offset + i;
1435 this.screen_.pushRow(row);
1436 }
1437
1438 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1439 if (extraRows > 0) {
1440 var ary = this.screen_.shiftRows(extraRows);
1441 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001442 if (this.scrollPort_.isScrolledEnd)
1443 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001444 }
1445
1446 if (cursorRow >= this.screen_.rowsArray.length)
1447 cursorRow = this.screen_.rowsArray.length - 1;
1448
rginda87b86462011-12-14 13:48:03 -08001449 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001450};
1451
1452/**
1453 * Relocate rows from one part of the addressable screen to another.
1454 *
1455 * This is used to recycle rows during VT scrolls (those which are driven
1456 * by VT commands, rather than by the user manipulating the scrollbar.)
1457 *
1458 * In this case, the blank lines scrolled into the scroll region are made of
1459 * the nodes we scrolled off. These have their rowIndex properties carefully
1460 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001461 */
1462hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1463 var ary = this.screen_.removeRows(fromIndex, count);
1464 this.screen_.insertRows(toIndex, ary);
1465
1466 var start, end;
1467 if (fromIndex < toIndex) {
1468 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001469 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001470 } else {
1471 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001472 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001473 }
1474
1475 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001476 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001477};
1478
1479/**
1480 * Renumber the rowIndex property of the given range of rows.
1481 *
1482 * The start and end indicies are relative to the screen, not the scrollback.
1483 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001484 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001485 * no need to renumber scrollback rows.
1486 */
Robert Ginda40932892012-12-10 17:26:40 -08001487hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1488 var screen = opt_screen || this.screen_;
1489
rginda8ba33642011-12-14 12:31:31 -08001490 var offset = this.scrollbackRows_.length;
1491 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001492 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001493 }
1494};
1495
1496/**
1497 * Print a string to the terminal.
1498 *
1499 * This respects the current insert and wraparound modes. It will add new lines
1500 * to the end of the terminal, scrolling off the top into the scrollback buffer
1501 * if necessary.
1502 *
1503 * The string is *not* parsed for escape codes. Use the interpret() method if
1504 * that's what you're after.
1505 *
1506 * @param{string} str The string to print.
1507 */
1508hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001509 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001510
Ricky Liang48f05cb2013-12-31 23:35:29 +08001511 var strWidth = lib.wc.strWidth(str);
1512
1513 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001514 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1515 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001516 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001517 }
rgindaa19afe22012-01-25 15:40:22 -08001518
Ricky Liang48f05cb2013-12-31 23:35:29 +08001519 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001520 var didOverflow = false;
1521 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001522
rgindaa9abdd82012-08-06 18:05:09 -07001523 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1524 didOverflow = true;
1525 count = this.screenSize.width - this.screen_.cursorPosition.column;
1526 }
rgindaa19afe22012-01-25 15:40:22 -08001527
rgindaa9abdd82012-08-06 18:05:09 -07001528 if (didOverflow && !this.options_.wraparound) {
1529 // If the string overflowed the line but wraparound is off, then the
1530 // last printed character should be the last of the string.
1531 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001532 substr = lib.wc.substr(str, startOffset, count - 1) +
1533 lib.wc.substr(str, strWidth - 1);
1534 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001535 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001536 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001537 }
rgindaa19afe22012-01-25 15:40:22 -08001538
Ricky Liang48f05cb2013-12-31 23:35:29 +08001539 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1540 for (var i = 0; i < tokens.length; i++) {
1541 if (tokens[i].wcNode)
1542 this.screen_.textAttributes.wcNode = true;
1543
1544 if (this.options_.insertMode) {
1545 this.screen_.insertString(tokens[i].str);
1546 } else {
1547 this.screen_.overwriteString(tokens[i].str);
1548 }
1549 this.screen_.textAttributes.wcNode = false;
rgindaa9abdd82012-08-06 18:05:09 -07001550 }
1551
1552 this.screen_.maybeClipCurrentRow();
1553 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001554 }
rginda8ba33642011-12-14 12:31:31 -08001555
1556 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001557
rginda9f5222b2012-03-05 11:53:28 -08001558 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001559 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001560};
1561
1562/**
rginda87b86462011-12-14 13:48:03 -08001563 * Set the VT scroll region.
1564 *
rginda87b86462011-12-14 13:48:03 -08001565 * This also resets the cursor position to the absolute (0, 0) position, since
1566 * that's what xterm appears to do.
1567 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001568 * Setting the scroll region to the full height of the terminal will clear
1569 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1570 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1571 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1572 * continue to work as most users would expect.
1573 *
rginda87b86462011-12-14 13:48:03 -08001574 * @param {integer} scrollTop The zero-based top of the scroll region.
1575 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1576 * inclusive.
1577 */
1578hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001579 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001580 this.vtScrollTop_ = null;
1581 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001582 } else {
1583 this.vtScrollTop_ = scrollTop;
1584 this.vtScrollBottom_ = scrollBottom;
1585 }
rginda87b86462011-12-14 13:48:03 -08001586};
1587
1588/**
rginda8ba33642011-12-14 12:31:31 -08001589 * Return the top row index according to the VT.
1590 *
1591 * This will return 0 unless the terminal has been told to restrict scrolling
1592 * to some lower row. It is used for some VT cursor positioning and scrolling
1593 * commands.
1594 *
1595 * @return {integer} The topmost row in the terminal's scroll region.
1596 */
1597hterm.Terminal.prototype.getVTScrollTop = function() {
1598 if (this.vtScrollTop_ != null)
1599 return this.vtScrollTop_;
1600
1601 return 0;
rginda87b86462011-12-14 13:48:03 -08001602};
rginda8ba33642011-12-14 12:31:31 -08001603
1604/**
1605 * Return the bottom row index according to the VT.
1606 *
1607 * This will return the height of the terminal unless the it has been told to
1608 * restrict scrolling to some higher row. It is used for some VT cursor
1609 * positioning and scrolling commands.
1610 *
1611 * @return {integer} The bottommost row in the terminal's scroll region.
1612 */
1613hterm.Terminal.prototype.getVTScrollBottom = function() {
1614 if (this.vtScrollBottom_ != null)
1615 return this.vtScrollBottom_;
1616
rginda87b86462011-12-14 13:48:03 -08001617 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001618}
1619
1620/**
1621 * Process a '\n' character.
1622 *
1623 * If the cursor is on the final row of the terminal this will append a new
1624 * blank row to the screen and scroll the topmost row into the scrollback
1625 * buffer.
1626 *
1627 * Otherwise, this moves the cursor to column zero of the next row.
1628 */
1629hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001630 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1631 this.screen_.rowsArray.length - 1);
1632
1633 if (this.vtScrollBottom_ != null) {
1634 // A VT Scroll region is active, we never append new rows.
1635 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1636 // We're at the end of the VT Scroll Region, perform a VT scroll.
1637 this.vtScrollUp(1);
1638 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1639 } else if (cursorAtEndOfScreen) {
1640 // We're at the end of the screen, the only thing to do is put the
1641 // cursor to column 0.
1642 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1643 } else {
1644 // Anywhere else, advance the cursor row, and reset the column.
1645 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1646 }
1647 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001648 // We're at the end of the screen. Append a new row to the terminal,
1649 // shifting the top row into the scrollback.
1650 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001651 } else {
rginda87b86462011-12-14 13:48:03 -08001652 // Anywhere else in the screen just moves the cursor.
1653 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001654 }
1655};
1656
1657/**
1658 * Like newLine(), except maintain the cursor column.
1659 */
1660hterm.Terminal.prototype.lineFeed = function() {
1661 var column = this.screen_.cursorPosition.column;
1662 this.newLine();
1663 this.setCursorColumn(column);
1664};
1665
1666/**
rginda87b86462011-12-14 13:48:03 -08001667 * If autoCarriageReturn is set then newLine(), else lineFeed().
1668 */
1669hterm.Terminal.prototype.formFeed = function() {
1670 if (this.options_.autoCarriageReturn) {
1671 this.newLine();
1672 } else {
1673 this.lineFeed();
1674 }
1675};
1676
1677/**
1678 * Move the cursor up one row, possibly inserting a blank line.
1679 *
1680 * The cursor column is not changed.
1681 */
1682hterm.Terminal.prototype.reverseLineFeed = function() {
1683 var scrollTop = this.getVTScrollTop();
1684 var currentRow = this.screen_.cursorPosition.row;
1685
1686 if (currentRow == scrollTop) {
1687 this.insertLines(1);
1688 } else {
1689 this.setAbsoluteCursorRow(currentRow - 1);
1690 }
1691};
1692
1693/**
rginda8ba33642011-12-14 12:31:31 -08001694 * Replace all characters to the left of the current cursor with the space
1695 * character.
1696 *
1697 * TODO(rginda): This should probably *remove* the characters (not just replace
1698 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001699 * position.
rginda8ba33642011-12-14 12:31:31 -08001700 */
1701hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001702 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001703 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001704 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001705 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001706};
1707
1708/**
David Benjamin684a9b72012-05-01 17:19:58 -04001709 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001710 *
1711 * The cursor position is unchanged.
1712 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001713 * If the current background color is not the default background color this
1714 * will insert spaces rather than delete. This is unfortunate because the
1715 * trailing space will affect text selection, but it's difficult to come up
1716 * with a way to style empty space that wouldn't trip up the hterm.Screen
1717 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001718 *
1719 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1720 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1721 * crbug.com/232390 for details.
rginda8ba33642011-12-14 12:31:31 -08001722 */
1723hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001724 if (this.screen_.cursorPosition.overflow)
1725 return;
1726
Robert Ginda7fd57082012-09-25 14:41:47 -07001727 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1728 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001729
1730 if (this.screen_.textAttributes.background ===
1731 this.screen_.textAttributes.DEFAULT_COLOR) {
1732 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08001733 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07001734 this.screen_.cursorPosition.column + count) {
1735 this.screen_.deleteChars(count);
1736 this.clearCursorOverflow();
1737 return;
1738 }
1739 }
1740
rginda87b86462011-12-14 13:48:03 -08001741 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001742 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001743 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001744 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001745};
1746
1747/**
1748 * Erase the current line.
1749 *
1750 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001751 */
1752hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001753 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001754 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001755 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001756 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001757};
1758
1759/**
David Benjamina08d78f2012-05-05 00:28:49 -04001760 * Erase all characters from the start of the screen to the current cursor
1761 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001762 *
1763 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001764 */
1765hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001766 var cursor = this.saveCursor();
1767
1768 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001769
David Benjamina08d78f2012-05-05 00:28:49 -04001770 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001771 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();
rginda8ba33642011-12-14 12:31:31 -08001777};
1778
1779/**
1780 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001781 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001782 *
1783 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001784 */
1785hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001786 var cursor = this.saveCursor();
1787
1788 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001789
David Benjamina08d78f2012-05-05 00:28:49 -04001790 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001791 for (var i = cursor.row + 1; i <= bottom; i++) {
1792 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001793 this.screen_.clearCursorRow();
1794 }
1795
rginda87b86462011-12-14 13:48:03 -08001796 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001797 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001798};
1799
1800/**
1801 * Fill the terminal with a given character.
1802 *
1803 * This methods does not respect the VT scroll region.
1804 *
1805 * @param {string} ch The character to use for the fill.
1806 */
1807hterm.Terminal.prototype.fill = function(ch) {
1808 var cursor = this.saveCursor();
1809
1810 this.setAbsoluteCursorPosition(0, 0);
1811 for (var row = 0; row < this.screenSize.height; row++) {
1812 for (var col = 0; col < this.screenSize.width; col++) {
1813 this.setAbsoluteCursorPosition(row, col);
1814 this.screen_.overwriteString(ch);
1815 }
1816 }
1817
1818 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001819};
1820
1821/**
rginda9ea433c2012-03-16 11:57:00 -07001822 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001823 *
rginda9ea433c2012-03-16 11:57:00 -07001824 * This does not respect the scroll region.
1825 *
1826 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1827 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001828 */
rginda9ea433c2012-03-16 11:57:00 -07001829hterm.Terminal.prototype.clearHome = function(opt_screen) {
1830 var screen = opt_screen || this.screen_;
1831 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001832
rginda11057d52012-04-25 12:29:56 -07001833 if (bottom == 0) {
1834 // Empty screen, nothing to do.
1835 return;
1836 }
1837
rgindae4d29232012-01-19 10:47:13 -08001838 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001839 screen.setCursorPosition(i, 0);
1840 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001841 }
1842
rginda9ea433c2012-03-16 11:57:00 -07001843 screen.setCursorPosition(0, 0);
1844};
1845
1846/**
1847 * Erase the entire display without changing the cursor position.
1848 *
1849 * The cursor position is unchanged. This does not respect the scroll
1850 * region.
1851 *
1852 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1853 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001854 */
1855hterm.Terminal.prototype.clear = function(opt_screen) {
1856 var screen = opt_screen || this.screen_;
1857 var cursor = screen.cursorPosition.clone();
1858 this.clearHome(screen);
1859 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001860};
1861
1862/**
1863 * VT command to insert lines at the current cursor row.
1864 *
1865 * This respects the current scroll region. Rows pushed off the bottom are
1866 * lost (they won't show up in the scrollback buffer).
1867 *
rginda8ba33642011-12-14 12:31:31 -08001868 * @param {integer} count The number of lines to insert.
1869 */
1870hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07001871 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08001872
1873 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07001874 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08001875
Robert Ginda579186b2012-09-26 11:40:04 -07001876 // The moveCount is the number of rows we need to relocate to make room for
1877 // the new row(s). The count is the distance to move them.
1878 var moveCount = bottom - cursorRow - count + 1;
1879 if (moveCount)
1880 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08001881
Robert Ginda579186b2012-09-26 11:40:04 -07001882 for (var i = count - 1; i >= 0; i--) {
1883 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001884 this.screen_.clearCursorRow();
1885 }
rginda8ba33642011-12-14 12:31:31 -08001886};
1887
1888/**
1889 * VT command to delete lines at the current cursor row.
1890 *
1891 * New rows are added to the bottom of scroll region to take their place. New
1892 * rows are strictly there to take up space and have no content or style.
1893 */
1894hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001895 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001896
rginda87b86462011-12-14 13:48:03 -08001897 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001898 var bottom = this.getVTScrollBottom();
1899
rginda87b86462011-12-14 13:48:03 -08001900 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001901 count = Math.min(count, maxCount);
1902
rginda87b86462011-12-14 13:48:03 -08001903 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001904 if (count != maxCount)
1905 this.moveRows_(top, count, moveStart);
1906
1907 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001908 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001909 this.screen_.clearCursorRow();
1910 }
1911
rginda87b86462011-12-14 13:48:03 -08001912 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001913 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001914};
1915
1916/**
1917 * Inserts the given number of spaces at the current cursor position.
1918 *
rginda87b86462011-12-14 13:48:03 -08001919 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001920 */
1921hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001922 var cursor = this.saveCursor();
1923
rgindacbbd7482012-06-13 15:06:16 -07001924 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001925 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001926 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001927
1928 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001929 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001930};
1931
1932/**
1933 * Forward-delete the specified number of characters starting at the cursor
1934 * position.
1935 *
1936 * @param {integer} count The number of characters to delete.
1937 */
1938hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001939 var deleted = this.screen_.deleteChars(count);
1940 if (deleted && !this.screen_.textAttributes.isDefault()) {
1941 var cursor = this.saveCursor();
1942 this.setCursorColumn(this.screenSize.width - deleted);
1943 this.screen_.insertString(lib.f.getWhitespace(deleted));
1944 this.restoreCursor(cursor);
1945 }
1946
David Benjamin54e8bf62012-06-01 22:31:40 -04001947 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001948};
1949
1950/**
1951 * Shift rows in the scroll region upwards by a given number of lines.
1952 *
1953 * New rows are inserted at the bottom of the scroll region to fill the
1954 * vacated rows. The new rows not filled out with the current text attributes.
1955 *
1956 * This function does not affect the scrollback rows at all. Rows shifted
1957 * off the top are lost.
1958 *
rginda87b86462011-12-14 13:48:03 -08001959 * The cursor position is not altered.
1960 *
rginda8ba33642011-12-14 12:31:31 -08001961 * @param {integer} count The number of rows to scroll.
1962 */
1963hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001964 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001965
rginda87b86462011-12-14 13:48:03 -08001966 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001967 this.deleteLines(count);
1968
rginda87b86462011-12-14 13:48:03 -08001969 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001970};
1971
1972/**
1973 * Shift rows below the cursor down by a given number of lines.
1974 *
1975 * This function respects the current scroll region.
1976 *
1977 * New rows are inserted at the top of the scroll region to fill the
1978 * vacated rows. The new rows not filled out with the current text attributes.
1979 *
1980 * This function does not affect the scrollback rows at all. Rows shifted
1981 * off the bottom are lost.
1982 *
1983 * @param {integer} count The number of rows to scroll.
1984 */
1985hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001986 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001987
rginda87b86462011-12-14 13:48:03 -08001988 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001989 this.insertLines(opt_count);
1990
rginda87b86462011-12-14 13:48:03 -08001991 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001992};
1993
rginda87b86462011-12-14 13:48:03 -08001994
rginda8ba33642011-12-14 12:31:31 -08001995/**
1996 * Set the cursor position.
1997 *
1998 * The cursor row is relative to the scroll region if the terminal has
1999 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2000 *
2001 * @param {integer} row The new zero-based cursor row.
2002 * @param {integer} row The new zero-based cursor column.
2003 */
2004hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2005 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002006 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002007 } else {
rginda87b86462011-12-14 13:48:03 -08002008 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002009 }
rginda87b86462011-12-14 13:48:03 -08002010};
rginda8ba33642011-12-14 12:31:31 -08002011
rginda87b86462011-12-14 13:48:03 -08002012hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2013 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002014 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2015 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002016 this.screen_.setCursorPosition(row, column);
2017};
2018
2019hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002020 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2021 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002022 this.screen_.setCursorPosition(row, column);
2023};
2024
2025/**
2026 * Set the cursor column.
2027 *
2028 * @param {integer} column The new zero-based cursor column.
2029 */
2030hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002031 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002032};
2033
2034/**
2035 * Return the cursor column.
2036 *
2037 * @return {integer} The zero-based cursor column.
2038 */
2039hterm.Terminal.prototype.getCursorColumn = function() {
2040 return this.screen_.cursorPosition.column;
2041};
2042
2043/**
2044 * Set the cursor row.
2045 *
2046 * The cursor row is relative to the scroll region if the terminal has
2047 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2048 *
2049 * @param {integer} row The new cursor row.
2050 */
rginda87b86462011-12-14 13:48:03 -08002051hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2052 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002053};
2054
2055/**
2056 * Return the cursor row.
2057 *
2058 * @return {integer} The zero-based cursor row.
2059 */
2060hterm.Terminal.prototype.getCursorRow = function(row) {
2061 return this.screen_.cursorPosition.row;
2062};
2063
2064/**
2065 * Request that the ScrollPort redraw itself soon.
2066 *
2067 * The redraw will happen asynchronously, soon after the call stack winds down.
2068 * Multiple calls will be coalesced into a single redraw.
2069 */
2070hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002071 if (this.timeouts_.redraw)
2072 return;
rginda8ba33642011-12-14 12:31:31 -08002073
2074 var self = this;
rginda87b86462011-12-14 13:48:03 -08002075 this.timeouts_.redraw = setTimeout(function() {
2076 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002077 self.scrollPort_.redraw_();
2078 }, 0);
2079};
2080
2081/**
2082 * Request that the ScrollPort be scrolled to the bottom.
2083 *
2084 * The scroll will happen asynchronously, soon after the call stack winds down.
2085 * Multiple calls will be coalesced into a single scroll.
2086 *
2087 * This affects the scrollbar position of the ScrollPort, and has nothing to
2088 * do with the VT scroll commands.
2089 */
2090hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2091 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002092 return;
rginda8ba33642011-12-14 12:31:31 -08002093
2094 var self = this;
2095 this.timeouts_.scrollDown = setTimeout(function() {
2096 delete self.timeouts_.scrollDown;
2097 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2098 }, 10);
2099};
2100
2101/**
2102 * Move the cursor up a specified number of rows.
2103 *
2104 * @param {integer} count The number of rows to move the cursor.
2105 */
2106hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002107 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002108};
2109
2110/**
2111 * Move the cursor down a specified number of rows.
2112 *
2113 * @param {integer} count The number of rows to move the cursor.
2114 */
2115hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002116 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002117 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2118 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2119 this.screenSize.height - 1);
2120
rgindacbbd7482012-06-13 15:06:16 -07002121 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002122 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002123 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002124};
2125
2126/**
2127 * Move the cursor left a specified number of columns.
2128 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002129 * If reverse wraparound mode is enabled and the previous row wrapped into
2130 * the current row then we back up through the wraparound as well.
2131 *
rginda8ba33642011-12-14 12:31:31 -08002132 * @param {integer} count The number of columns to move the cursor.
2133 */
2134hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002135 count = count || 1;
2136
2137 if (count < 1)
2138 return;
2139
2140 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002141 if (this.options_.reverseWraparound) {
2142 if (this.screen_.cursorPosition.overflow) {
2143 // If this cursor is in the right margin, consume one count to get it
2144 // back to the last column. This only applies when we're in reverse
2145 // wraparound mode.
2146 count--;
2147 this.clearCursorOverflow();
2148
2149 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002150 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002151 }
2152
Robert Gindabfb32622014-07-17 13:20:27 -07002153 var newRow = this.screen_.cursorPosition.row;
2154 var newColumn = currentColumn - count;
2155 if (newColumn < 0) {
2156 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2157 if (newRow < 0) {
2158 // xterm also wraps from row 0 to the last row.
2159 newRow = this.screenSize.height + newRow % this.screenSize.height;
2160 }
2161 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2162 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002163
Robert Gindabfb32622014-07-17 13:20:27 -07002164 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2165
2166 } else {
2167 var newColumn = Math.max(currentColumn - count, 0);
2168 this.setCursorColumn(newColumn);
2169 }
rginda8ba33642011-12-14 12:31:31 -08002170};
2171
2172/**
2173 * Move the cursor right a specified number of columns.
2174 *
2175 * @param {integer} count The number of columns to move the cursor.
2176 */
2177hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002178 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002179
2180 if (count < 1)
2181 return;
2182
rgindacbbd7482012-06-13 15:06:16 -07002183 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002184 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002185 this.setCursorColumn(column);
2186};
2187
2188/**
2189 * Reverse the foreground and background colors of the terminal.
2190 *
2191 * This only affects text that was drawn with no attributes.
2192 *
2193 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2194 * been drawn with attributes that happen to coincide with the default
2195 * 'no-attribute' colors. My guess is probably not.
2196 */
2197hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002198 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002199 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002200 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2201 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002202 } else {
rginda9f5222b2012-03-05 11:53:28 -08002203 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2204 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002205 }
2206};
2207
2208/**
rginda87b86462011-12-14 13:48:03 -08002209 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002210 *
2211 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002212 */
2213hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002214 this.cursorNode_.style.backgroundColor =
2215 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002216
2217 var self = this;
2218 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08002219 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08002220 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002221
Michael Kelly485ecd12014-06-09 11:41:56 -04002222 // bellSquelchTimeout_ affects both audio and notification bells.
2223 if (this.bellSquelchTimeout_)
2224 return;
2225
Robert Ginda92e18102013-03-14 13:56:37 -07002226 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002227 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002228 this.bellSequelchTimeout_ = setTimeout(function() {
2229 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002230 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002231 } else {
2232 delete this.bellSquelchTimeout_;
2233 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002234
2235 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
2236 var n = new Notification(
2237 lib.f.replaceVars(hterm.desktopNotificationTitle,
Robert Ginda348dc2b2014-06-24 14:42:23 -07002238 {'title': window.document.title || 'hterm'}));
Michael Kelly485ecd12014-06-09 11:41:56 -04002239 this.bellNotificationList_.push(n);
2240 // TODO: Should we try to raise the window here?
2241 n.onclick = function() { self.closeBellNotifications_(); };
2242 }
rginda87b86462011-12-14 13:48:03 -08002243};
2244
2245/**
rginda8ba33642011-12-14 12:31:31 -08002246 * Set the origin mode bit.
2247 *
2248 * If origin mode is on, certain VT cursor and scrolling commands measure their
2249 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2250 * to the top of the addressable screen.
2251 *
2252 * Defaults to off.
2253 *
2254 * @param {boolean} state True to set origin mode, false to unset.
2255 */
2256hterm.Terminal.prototype.setOriginMode = function(state) {
2257 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002258 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002259};
2260
2261/**
2262 * Set the insert mode bit.
2263 *
2264 * If insert mode is on, existing text beyond the cursor position will be
2265 * shifted right to make room for new text. Otherwise, new text overwrites
2266 * any existing text.
2267 *
2268 * Defaults to off.
2269 *
2270 * @param {boolean} state True to set insert mode, false to unset.
2271 */
2272hterm.Terminal.prototype.setInsertMode = function(state) {
2273 this.options_.insertMode = state;
2274};
2275
2276/**
rginda87b86462011-12-14 13:48:03 -08002277 * Set the auto carriage return bit.
2278 *
2279 * If auto carriage return is on then a formfeed character is interpreted
2280 * as a newline, otherwise it's the same as a linefeed. The difference boils
2281 * down to whether or not the cursor column is reset.
2282 */
2283hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2284 this.options_.autoCarriageReturn = state;
2285};
2286
2287/**
rginda8ba33642011-12-14 12:31:31 -08002288 * Set the wraparound mode bit.
2289 *
2290 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2291 * to the start of the following row. Otherwise, the cursor is clamped to the
2292 * end of the screen and attempts to write past it are ignored.
2293 *
2294 * Defaults to on.
2295 *
2296 * @param {boolean} state True to set wraparound mode, false to unset.
2297 */
2298hterm.Terminal.prototype.setWraparound = function(state) {
2299 this.options_.wraparound = state;
2300};
2301
2302/**
2303 * Set the reverse-wraparound mode bit.
2304 *
2305 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2306 * to the end of the previous row. Otherwise, the cursor is clamped to column
2307 * 0.
2308 *
2309 * Defaults to off.
2310 *
2311 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2312 */
2313hterm.Terminal.prototype.setReverseWraparound = function(state) {
2314 this.options_.reverseWraparound = state;
2315};
2316
2317/**
2318 * Selects between the primary and alternate screens.
2319 *
2320 * If alternate mode is on, the alternate screen is active. Otherwise the
2321 * primary screen is active.
2322 *
2323 * Swapping screens has no effect on the scrollback buffer.
2324 *
2325 * Each screen maintains its own cursor position.
2326 *
2327 * Defaults to off.
2328 *
2329 * @param {boolean} state True to set alternate mode, false to unset.
2330 */
2331hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002332 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002333 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2334
rginda35c456b2012-02-09 17:29:05 -08002335 if (this.screen_.rowsArray.length &&
2336 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2337 // If the screen changed sizes while we were away, our rowIndexes may
2338 // be incorrect.
2339 var offset = this.scrollbackRows_.length;
2340 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002341 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002342 ary[i].rowIndex = offset + i;
2343 }
2344 }
rginda8ba33642011-12-14 12:31:31 -08002345
rginda35c456b2012-02-09 17:29:05 -08002346 this.realizeWidth_(this.screenSize.width);
2347 this.realizeHeight_(this.screenSize.height);
2348 this.scrollPort_.syncScrollHeight();
2349 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002350
rginda6d397402012-01-17 10:58:29 -08002351 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002352 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002353};
2354
2355/**
2356 * Set the cursor-blink mode bit.
2357 *
2358 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2359 * a visible cursor does not blink.
2360 *
2361 * You should make sure to turn blinking off if you're going to dispose of a
2362 * terminal, otherwise you'll leak a timeout.
2363 *
2364 * Defaults to on.
2365 *
2366 * @param {boolean} state True to set cursor-blink mode, false to unset.
2367 */
2368hterm.Terminal.prototype.setCursorBlink = function(state) {
2369 this.options_.cursorBlink = state;
2370
2371 if (!state && this.timeouts_.cursorBlink) {
2372 clearTimeout(this.timeouts_.cursorBlink);
2373 delete this.timeouts_.cursorBlink;
2374 }
2375
2376 if (this.options_.cursorVisible)
2377 this.setCursorVisible(true);
2378};
2379
2380/**
2381 * Set the cursor-visible mode bit.
2382 *
2383 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2384 *
2385 * Defaults to on.
2386 *
2387 * @param {boolean} state True to set cursor-visible mode, false to unset.
2388 */
2389hterm.Terminal.prototype.setCursorVisible = function(state) {
2390 this.options_.cursorVisible = state;
2391
2392 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002393 if (this.timeouts_.cursorBlink) {
2394 clearTimeout(this.timeouts_.cursorBlink);
2395 delete this.timeouts_.cursorBlink;
2396 }
rginda87b86462011-12-14 13:48:03 -08002397 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002398 return;
2399 }
2400
rginda87b86462011-12-14 13:48:03 -08002401 this.syncCursorPosition_();
2402
2403 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002404
2405 if (this.options_.cursorBlink) {
2406 if (this.timeouts_.cursorBlink)
2407 return;
2408
Robert Gindaea2183e2014-07-17 09:51:51 -07002409 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002410 } else {
2411 if (this.timeouts_.cursorBlink) {
2412 clearTimeout(this.timeouts_.cursorBlink);
2413 delete this.timeouts_.cursorBlink;
2414 }
2415 }
2416};
2417
2418/**
rginda87b86462011-12-14 13:48:03 -08002419 * Synchronizes the visible cursor and document selection with the current
2420 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002421 */
2422hterm.Terminal.prototype.syncCursorPosition_ = function() {
2423 var topRowIndex = this.scrollPort_.getTopRowIndex();
2424 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2425 var cursorRowIndex = this.scrollbackRows_.length +
2426 this.screen_.cursorPosition.row;
2427
2428 if (cursorRowIndex > bottomRowIndex) {
2429 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002430 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002431 return;
2432 }
2433
Robert Gindab837c052014-08-11 11:17:51 -07002434 if (this.options_.cursorVisible &&
2435 this.cursorNode_.style.display == 'none') {
2436 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2437 this.cursorNode_.style.display = '';
2438 }
2439
2440
rginda8ba33642011-12-14 12:31:31 -08002441 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002442 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2443 'px';
2444 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2445 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002446
2447 this.cursorNode_.setAttribute('title',
2448 '(' + this.screen_.cursorPosition.row +
2449 ', ' + this.screen_.cursorPosition.column +
2450 ')');
2451
2452 // Update the caret for a11y purposes.
2453 var selection = this.document_.getSelection();
2454 if (selection && selection.isCollapsed)
2455 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002456};
2457
Robert Gindafb1be6a2013-12-11 11:56:22 -08002458/**
2459 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2460 * and character cell dimensions.
2461 */
Robert Ginda830583c2013-08-07 13:20:46 -07002462hterm.Terminal.prototype.restyleCursor_ = function() {
2463 var shape = this.cursorShape_;
2464
2465 if (this.cursorNode_.getAttribute('focus') == 'false') {
2466 // Always show a block cursor when unfocused.
2467 shape = hterm.Terminal.cursorShape.BLOCK;
2468 }
2469
2470 var style = this.cursorNode_.style;
2471
Robert Gindafb1be6a2013-12-11 11:56:22 -08002472 style.width = this.scrollPort_.characterSize.width + 'px';
2473
Robert Ginda830583c2013-08-07 13:20:46 -07002474 switch (shape) {
2475 case hterm.Terminal.cursorShape.BEAM:
2476 style.height = this.scrollPort_.characterSize.height + 'px';
2477 style.backgroundColor = 'transparent';
2478 style.borderBottomStyle = null;
2479 style.borderLeftStyle = 'solid';
2480 break;
2481
2482 case hterm.Terminal.cursorShape.UNDERLINE:
2483 style.height = this.scrollPort_.characterSize.baseline + 'px';
2484 style.backgroundColor = 'transparent';
2485 style.borderBottomStyle = 'solid';
2486 // correct the size to put it exactly at the baseline
2487 style.borderLeftStyle = null;
2488 break;
2489
2490 default:
2491 style.height = this.scrollPort_.characterSize.height + 'px';
2492 style.backgroundColor = this.cursorColor_;
2493 style.borderBottomStyle = null;
2494 style.borderLeftStyle = null;
2495 break;
2496 }
2497};
2498
rginda8ba33642011-12-14 12:31:31 -08002499/**
2500 * Synchronizes the visible cursor with the current cursor coordinates.
2501 *
2502 * The sync will happen asynchronously, soon after the call stack winds down.
2503 * Multiple calls will be coalesced into a single sync.
2504 */
2505hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2506 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002507 return;
rginda8ba33642011-12-14 12:31:31 -08002508
2509 var self = this;
2510 this.timeouts_.syncCursor = setTimeout(function() {
2511 self.syncCursorPosition_();
2512 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002513 }, 0);
2514};
2515
rgindacc2996c2012-02-24 14:59:31 -08002516/**
rgindaf522ce02012-04-17 17:49:17 -07002517 * Show or hide the zoom warning.
2518 *
2519 * The zoom warning is a message warning the user that their browser zoom must
2520 * be set to 100% in order for hterm to function properly.
2521 *
2522 * @param {boolean} state True to show the message, false to hide it.
2523 */
2524hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2525 if (!this.zoomWarningNode_) {
2526 if (!state)
2527 return;
2528
2529 this.zoomWarningNode_ = this.document_.createElement('div');
2530 this.zoomWarningNode_.style.cssText = (
2531 'color: black;' +
2532 'background-color: #ff2222;' +
2533 'font-size: large;' +
2534 'border-radius: 8px;' +
2535 'opacity: 0.75;' +
2536 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2537 'top: 0.5em;' +
2538 'right: 1.2em;' +
2539 'position: absolute;' +
2540 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002541 '-webkit-user-select: none;' +
2542 '-moz-text-size-adjust: none;' +
2543 '-moz-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002544 }
2545
Robert Gindab4839c22013-02-28 16:52:10 -08002546 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2547 hterm.zoomWarningMessage,
2548 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2549
rgindaf522ce02012-04-17 17:49:17 -07002550 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2551
2552 if (state) {
2553 if (!this.zoomWarningNode_.parentNode)
2554 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2555 } else if (this.zoomWarningNode_.parentNode) {
2556 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2557 }
2558};
2559
2560/**
rgindacc2996c2012-02-24 14:59:31 -08002561 * Show the terminal overlay for a given amount of time.
2562 *
2563 * The terminal overlay appears in inverse video in a large font, centered
2564 * over the terminal. You should probably keep the overlay message brief,
2565 * since it's in a large font and you probably aren't going to check the size
2566 * of the terminal first.
2567 *
2568 * @param {string} msg The text (not HTML) message to display in the overlay.
2569 * @param {number} opt_timeout The amount of time to wait before fading out
2570 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2571 * stay up forever (or until the next overlay).
2572 */
2573hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002574 if (!this.overlayNode_) {
2575 if (!this.div_)
2576 return;
2577
2578 this.overlayNode_ = this.document_.createElement('div');
2579 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002580 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002581 'font-size: xx-large;' +
2582 'opacity: 0.75;' +
2583 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2584 'position: absolute;' +
2585 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002586 '-webkit-transition: opacity 180ms ease-in;' +
2587 '-moz-user-select: none;' +
2588 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002589
2590 this.overlayNode_.addEventListener('mousedown', function(e) {
2591 e.preventDefault();
2592 e.stopPropagation();
2593 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002594 }
2595
rginda9f5222b2012-03-05 11:53:28 -08002596 this.overlayNode_.style.color = this.prefs_.get('background-color');
2597 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2598 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2599
rgindaf0090c92012-02-10 14:58:52 -08002600 this.overlayNode_.textContent = msg;
2601 this.overlayNode_.style.opacity = '0.75';
2602
2603 if (!this.overlayNode_.parentNode)
2604 this.div_.appendChild(this.overlayNode_);
2605
Robert Ginda97769282013-02-01 15:30:30 -08002606 var divSize = hterm.getClientSize(this.div_);
2607 var overlaySize = hterm.getClientSize(this.overlayNode_);
2608
Robert Ginda8a59f762014-07-23 11:29:55 -07002609 this.overlayNode_.style.top =
2610 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08002611 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07002612 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08002613
2614 var self = this;
2615
2616 if (this.overlayTimeout_)
2617 clearTimeout(this.overlayTimeout_);
2618
rgindacc2996c2012-02-24 14:59:31 -08002619 if (opt_timeout === null)
2620 return;
2621
rgindaf0090c92012-02-10 14:58:52 -08002622 this.overlayTimeout_ = setTimeout(function() {
2623 self.overlayNode_.style.opacity = '0';
Robert Ginda70926e42013-11-25 14:56:36 -08002624 self.overlayTimeout_ = setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002625 if (self.overlayNode_.parentNode)
2626 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002627 self.overlayTimeout_ = null;
2628 self.overlayNode_.style.opacity = '0.75';
2629 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002630 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002631};
2632
rginda4bba5e12012-06-20 16:15:30 -07002633/**
2634 * Paste from the system clipboard to the terminal.
2635 */
2636hterm.Terminal.prototype.paste = function() {
2637 hterm.pasteFromClipboard(this.document_);
2638};
2639
2640/**
2641 * Copy a string to the system clipboard.
2642 *
2643 * Note: If there is a selected range in the terminal, it'll be cleared.
2644 */
2645hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002646 if (this.prefs_.get('enable-clipboard-notice'))
2647 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2648
rgindaa09e7332012-08-17 12:49:51 -07002649 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002650 copySource.textContent = str;
2651 copySource.style.cssText = (
2652 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002653 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07002654 'position: absolute;' +
2655 'top: -99px');
2656
2657 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002658
rginda4bba5e12012-06-20 16:15:30 -07002659 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002660 var anchorNode = selection.anchorNode;
2661 var anchorOffset = selection.anchorOffset;
2662 var focusNode = selection.focusNode;
2663 var focusOffset = selection.focusOffset;
2664
rginda4bba5e12012-06-20 16:15:30 -07002665 selection.selectAllChildren(copySource);
2666
rgindaa09e7332012-08-17 12:49:51 -07002667 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002668
Rob Spies56953412014-04-28 14:09:47 -07002669 // IE doesn't support selection.extend. This means that the selection
2670 // won't return on IE.
Robert Ginda6f4f0aa2014-07-28 10:21:39 -07002671 if (selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07002672 selection.collapse(anchorNode, anchorOffset);
2673 selection.extend(focusNode, focusOffset);
2674 }
rgindafaa74742012-08-21 13:34:03 -07002675
rginda4bba5e12012-06-20 16:15:30 -07002676 copySource.parentNode.removeChild(copySource);
2677};
2678
rgindaa09e7332012-08-17 12:49:51 -07002679hterm.Terminal.prototype.getSelectionText = function() {
2680 var selection = this.scrollPort_.selection;
2681 selection.sync();
2682
2683 if (selection.isCollapsed)
2684 return null;
2685
2686
2687 // Start offset measures from the beginning of the line.
2688 var startOffset = selection.startOffset;
2689 var node = selection.startNode;
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 start on an x-row node, then it must be
2693 // somewhere inside the x-row. Add any characters from previous siblings
2694 // into the start 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.previousSibling) {
2702 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002703 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002704 }
rgindaa09e7332012-08-17 12:49:51 -07002705 }
2706
2707 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002708 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
2709 selection.endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002710 var node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002711
Robert Gindafdbb3f22012-09-06 20:23:06 -07002712 if (node.nodeName != 'X-ROW') {
2713 // If the selection doesn't end on an x-row node, then it must be
2714 // somewhere inside the x-row. Add any characters from following siblings
2715 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002716
2717 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2718 // If node is the text node in a styled span, move up to the span node.
2719 node = node.parentNode;
2720 }
2721
Robert Gindafdbb3f22012-09-06 20:23:06 -07002722 while (node.nextSibling) {
2723 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002724 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002725 }
rgindaa09e7332012-08-17 12:49:51 -07002726 }
2727
2728 var rv = this.getRowsText(selection.startRow.rowIndex,
2729 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002730 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002731};
2732
rginda4bba5e12012-06-20 16:15:30 -07002733/**
2734 * Copy the current selection to the system clipboard, then clear it after a
2735 * short delay.
2736 */
2737hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002738 var text = this.getSelectionText();
2739 if (text != null)
2740 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002741};
2742
rgindaf0090c92012-02-10 14:58:52 -08002743hterm.Terminal.prototype.overlaySize = function() {
2744 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2745};
2746
rginda87b86462011-12-14 13:48:03 -08002747/**
2748 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2749 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07002750 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08002751 */
2752hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002753 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002754 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2755
Robert Ginda8cb7d902013-06-20 14:37:18 -07002756 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08002757};
2758
2759/**
rgindad5613292012-06-19 15:40:37 -07002760 * Add the terminalRow and terminalColumn properties to mouse events and
2761 * then forward on to onMouse().
2762 *
2763 * The terminalRow and terminalColumn properties contain the (row, column)
2764 * coordinates for the mouse event.
2765 */
2766hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002767 if (e.processedByTerminalHandler_) {
2768 // We register our event handlers on the document, as well as the cursor
2769 // and the scroll blocker. Mouse events that occur on the cursor or
2770 // scroll blocker will also appear on the document, but we don't want to
2771 // process them twice.
2772 //
2773 // We can't just prevent bubbling because that has other side effects, so
2774 // we decorate the event object with this property instead.
2775 return;
2776 }
2777
2778 e.processedByTerminalHandler_ = true;
2779
Robert Gindaeda48db2014-07-17 09:25:30 -07002780 // One based row/column stored on the mouse event.
2781 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2782 this.scrollPort_.characterSize.height) + 1;
2783 e.terminalColumn = parseInt(e.clientX /
2784 this.scrollPort_.characterSize.width) + 1;
2785
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002786 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
2787 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07002788 return;
2789 }
2790
Robert Gindab837c052014-08-11 11:17:51 -07002791 if (this.options_.cursorVisible &&
2792 this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED) {
2793 // If the cursor is visible and we're not sending mouse events to the
2794 // host app, then we want to hide the terminal cursor when the mouse
2795 // cursor is over top. This keeps the terminal cursor from interfering
2796 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07002797 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
2798 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
2799 this.cursorNode_.style.display = 'none';
2800 } else if (this.cursorNode_.style.display == 'none') {
2801 this.cursorNode_.style.display = '';
2802 }
2803 }
rgindad5613292012-06-19 15:40:37 -07002804
Robert Ginda928cf632014-03-05 15:07:41 -08002805 if (e.type == 'mousedown') {
2806 if (e.altKey || this.vt.mouseReport == this.vt.MOUSE_REPORT_DISABLED) {
2807 // If VT mouse reporting is disabled, or has been defeated with
2808 // alt-mousedown, then the mouse will act on the local selection.
2809 this.reportMouseEvents_ = false;
2810 this.setSelectionEnabled(true);
2811 } else {
2812 // Otherwise we defer ownership of the mouse to the VT.
2813 this.reportMouseEvents_ = true;
Robert Ginda3ae37822014-05-15 13:05:35 -07002814 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08002815 this.setSelectionEnabled(false);
2816 e.preventDefault();
2817 }
2818 }
2819
2820 if (!this.reportMouseEvents_) {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002821 if (e.type == 'dblclick') {
2822 this.screen_.expandSelection(this.document_.getSelection());
2823 hterm.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07002824 }
2825
Robert Ginda928cf632014-03-05 15:07:41 -08002826 if (e.type == 'mousedown' && e.which == this.mousePasteButton)
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002827 this.paste();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002828
2829 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2830 !this.document_.getSelection().isCollapsed) {
2831 hterm.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002832 }
2833
2834 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
2835 this.scrollBlockerNode_.engaged) {
2836 // Disengage the scroll-blocker after one of these events.
2837 this.scrollBlockerNode_.engaged = false;
2838 this.scrollBlockerNode_.style.top = '-99px';
2839 }
2840
Robert Ginda928cf632014-03-05 15:07:41 -08002841 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002842 if (!this.scrollBlockerNode_.engaged) {
2843 if (e.type == 'mousedown') {
2844 // Move the scroll-blocker into place if we want to keep the scrollport
2845 // from scrolling.
2846 this.scrollBlockerNode_.engaged = true;
2847 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2848 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2849 } else if (e.type == 'mousemove') {
2850 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
2851 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07002852 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002853 e.preventDefault();
2854 }
2855 }
Robert Ginda928cf632014-03-05 15:07:41 -08002856
2857 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07002858 }
2859
Robert Ginda928cf632014-03-05 15:07:41 -08002860 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
2861 // Restore this on mouseup in case it was temporarily defeated with a
2862 // alt-mousedown. Only do this when the selection is empty so that
2863 // we don't immediately kill the users selection.
2864 this.reportMouseEvents_ = (this.vt.mouseReport !=
2865 this.vt.MOUSE_REPORT_DISABLED);
2866 }
rgindad5613292012-06-19 15:40:37 -07002867};
2868
2869/**
2870 * Clients should override this if they care to know about mouse events.
2871 *
2872 * The event parameter will be a normal DOM mouse click event with additional
2873 * 'terminalRow' and 'terminalColumn' properties.
2874 */
2875hterm.Terminal.prototype.onMouse = function(e) { };
2876
2877/**
rginda8e92a692012-05-20 19:37:20 -07002878 * React when focus changes.
2879 */
Rob Spies06533ba2014-04-24 11:20:37 -07002880hterm.Terminal.prototype.onFocusChange_ = function(focused) {
2881 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07002882 this.restyleCursor_();
Michael Kelly485ecd12014-06-09 11:41:56 -04002883 if (focused === true)
2884 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07002885};
2886
2887/**
rginda8ba33642011-12-14 12:31:31 -08002888 * React when the ScrollPort is scrolled.
2889 */
2890hterm.Terminal.prototype.onScroll_ = function() {
2891 this.scheduleSyncCursorPosition_();
2892};
2893
2894/**
rginda9846e2f2012-01-27 13:53:33 -08002895 * React when text is pasted into the scrollPort.
2896 */
2897hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07002898 var data = e.text.replace(/\n/mg, '\r');
Connor Hegartyf525ceb2014-09-03 13:36:03 -07002899 data = this.keyboard.encode(data);
Robert Gindaa063b202014-07-21 11:08:25 -07002900 if (this.options_.bracketedPaste)
2901 data = '\x1b[200~' + data + '\x1b[201~';
2902
2903 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08002904};
2905
2906/**
rgindaa09e7332012-08-17 12:49:51 -07002907 * React when the user tries to copy from the scrollPort.
2908 */
2909hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07002910 if (!this.useDefaultWindowCopy) {
2911 e.preventDefault();
2912 setTimeout(this.copySelectionToClipboard.bind(this), 0);
2913 }
rgindaa09e7332012-08-17 12:49:51 -07002914};
2915
2916/**
rginda8ba33642011-12-14 12:31:31 -08002917 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002918 *
2919 * Note: This function should not directly contain code that alters the internal
2920 * state of the terminal. That kind of code belongs in realizeWidth or
2921 * realizeHeight, so that it can be executed synchronously in the case of a
2922 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002923 */
2924hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08002925 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002926 this.scrollPort_.characterSize.width);
Rob Spiesf4e90e82015-01-28 12:10:13 -08002927 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
rginda35c456b2012-02-09 17:29:05 -08002928 this.scrollPort_.characterSize.height);
2929
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002930 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08002931 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002932 // gets removed from the document or during the initial load, and we can't
2933 // deal with that.
rginda35c456b2012-02-09 17:29:05 -08002934 return;
2935 }
2936
rgindaa8ba17d2012-08-15 14:41:10 -07002937 var isNewSize = (columnCount != this.screenSize.width ||
2938 rowCount != this.screenSize.height);
2939
2940 // We do this even if the size didn't change, just to be sure everything is
2941 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002942 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07002943 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07002944
2945 if (isNewSize)
2946 this.overlaySize();
2947
Robert Gindafb1be6a2013-12-11 11:56:22 -08002948 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07002949 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08002950};
2951
2952/**
2953 * Service the cursor blink timeout.
2954 */
2955hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07002956 if (!this.options_.cursorBlink) {
2957 delete this.timeouts_.cursorBlink;
2958 return;
2959 }
2960
Robert Ginda830583c2013-08-07 13:20:46 -07002961 if (this.cursorNode_.getAttribute('focus') == 'false' ||
2962 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08002963 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07002964 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
2965 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08002966 } else {
rginda87b86462011-12-14 13:48:03 -08002967 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07002968 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
2969 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08002970 }
2971};
David Reveman8f552492012-03-28 12:18:41 -04002972
2973/**
2974 * Set the scrollbar-visible mode bit.
2975 *
2976 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2977 * Otherwise it will not.
2978 *
2979 * Defaults to on.
2980 *
2981 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2982 */
2983hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2984 this.scrollPort_.setScrollbarVisible(state);
2985};
Michael Kelly485ecd12014-06-09 11:41:56 -04002986
2987/**
Rob Spies49039e52014-12-17 13:40:04 -08002988 * Set the scroll wheel move multiplier. This will affect how fast the page
2989 * scrolls on mousewheel events.
2990 *
2991 * Defaults to 1.
2992 *
2993 * @param {number} multiplier.
2994 */
2995hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
2996 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
2997};
2998
2999/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003000 * Close all web notifications created by terminal bells.
3001 */
3002hterm.Terminal.prototype.closeBellNotifications_ = function() {
3003 this.bellNotificationList_.forEach(function(n) {
3004 n.close();
3005 });
3006 this.bellNotificationList_.length = 0;
3007};