blob: dcad5bbbd58adf4335a8083c58d7f5dcca476c8d [file] [log] [blame]
rginda87b86462011-12-14 13:48:03 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rginda8ba33642011-12-14 12:31:31 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rgindacbbd7482012-06-13 15:06:16 -07005'use strict';
6
Masaya Suzuki273aa982014-05-31 07:25:55 +09007lib.rtdep('lib.colors', 'lib.PreferenceManager', 'lib.resource', 'lib.wc',
Rob Spiesf4e90e82015-01-28 12:10:13 -08008 'lib.f', 'hterm.Keyboard', 'hterm.Options', 'hterm.PreferenceManager',
Ricky Liang48f05cb2013-12-31 23:35:29 +08009 'hterm.Screen', 'hterm.ScrollPort', 'hterm.Size',
10 'hterm.TextAttributes', 'hterm.VT');
rgindacbbd7482012-06-13 15:06:16 -070011
rginda8ba33642011-12-14 12:31:31 -080012/**
13 * Constructor for the Terminal class.
14 *
15 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
16 * classes to provide the complete terminal functionality.
17 *
18 * There are a number of lower-level Terminal methods that can be called
19 * directly to manipulate the cursor, text, scroll region, and other terminal
20 * attributes. However, the primary method is interpret(), which parses VT
21 * escape sequences and invokes the appropriate Terminal methods.
22 *
23 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
24 *
25 * TODO(rginda): Eventually we're going to need to support characters which are
26 * displayed twice as wide as standard latin characters. This is to support
27 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080028 *
Robert Ginda57f03b42012-09-13 11:02:48 -070029 * @param {string} opt_profileId Optional preference profile name. If not
rginda9f5222b2012-03-05 11:53:28 -080030 * provided, defaults to 'default'.
rginda8ba33642011-12-14 12:31:31 -080031 */
Robert Ginda57f03b42012-09-13 11:02:48 -070032hterm.Terminal = function(opt_profileId) {
33 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080034
rginda8ba33642011-12-14 12:31:31 -080035 // Two screen instances.
36 this.primaryScreen_ = new hterm.Screen();
37 this.alternateScreen_ = new hterm.Screen();
38
39 // The "current" screen.
40 this.screen_ = this.primaryScreen_;
41
rginda8ba33642011-12-14 12:31:31 -080042 // The local notion of the screen size. ScreenBuffers also have a size which
43 // indicates their present size. During size changes, the two may disagree.
44 // Also, the inactive screen's size is not altered until it is made the active
45 // screen.
46 this.screenSize = new hterm.Size(0, 0);
47
rginda8ba33642011-12-14 12:31:31 -080048 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080049 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080050 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
51 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080052 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070053 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080054
rginda87b86462011-12-14 13:48:03 -080055 // The div that contains this terminal.
56 this.div_ = null;
57
rgindac9bc5502012-01-18 11:48:44 -080058 // The document that contains the scrollPort. Defaulted to the global
59 // document here so that the terminal is functional even if it hasn't been
60 // inserted into a document yet, but re-set in decorate().
61 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080062
rginda8ba33642011-12-14 12:31:31 -080063 // The rows that have scrolled off screen and are no longer addressable.
64 this.scrollbackRows_ = [];
65
rgindac9bc5502012-01-18 11:48:44 -080066 // Saved tab stops.
67 this.tabStops_ = [];
68
David Benjamin66e954d2012-05-05 21:08:12 -040069 // Keep track of whether default tab stops have been erased; after a TBC
70 // clears all tab stops, defaults aren't restored on resize until a reset.
71 this.defaultTabStops = true;
72
rginda8ba33642011-12-14 12:31:31 -080073 // The VT's notion of the top and bottom rows. Used during some VT
74 // cursor positioning and scrolling commands.
75 this.vtScrollTop_ = null;
76 this.vtScrollBottom_ = null;
77
78 // The DIV element for the visible cursor.
79 this.cursorNode_ = null;
80
Robert Ginda830583c2013-08-07 13:20:46 -070081 // The current cursor shape of the terminal.
82 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
83
84 // The current color of the cursor.
85 this.cursorColor_ = null;
86
Robert Gindaea2183e2014-07-17 09:51:51 -070087 // Cursor blink on/off cycle in ms, overwritten by prefs once they're loaded.
88 this.cursorBlinkCycle_ = [100, 100];
89
90 // Pre-bound onCursorBlink_ handler, so we don't have to do this for each
91 // cursor on/off servicing.
92 this.myOnCursorBlink_ = this.onCursorBlink_.bind(this);
93
rginda9f5222b2012-03-05 11:53:28 -080094 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070095 // each output and keystroke. They are initialized by the preference manager.
Robert Ginda8cb7d902013-06-20 14:37:18 -070096 this.backgroundColor_ = null;
97 this.foregroundColor_ = null;
Robert Ginda57f03b42012-09-13 11:02:48 -070098 this.scrollOnOutput_ = null;
99 this.scrollOnKeystroke_ = null;
rginda9f5222b2012-03-05 11:53:28 -0800100
Robert Ginda6aec7eb2015-06-16 10:31:30 -0700101 // True if we should override mouse event reporting to allow local selection.
102 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -0800103
rgindaf0090c92012-02-10 14:58:52 -0800104 // Terminal bell sound.
105 this.bellAudio_ = this.document_.createElement('audio');
rgindaf0090c92012-02-10 14:58:52 -0800106 this.bellAudio_.setAttribute('preload', 'auto');
107
Michael Kelly485ecd12014-06-09 11:41:56 -0400108 // All terminal bell notifications that have been generated (not necessarily
109 // shown).
110 this.bellNotificationList_ = [];
111
112 // Whether we have permission to display notifications.
113 this.desktopNotificationBell_ = false;
Michael Kelly485ecd12014-06-09 11:41:56 -0400114
rginda6d397402012-01-17 10:58:29 -0800115 // Cursor position and attributes saved with DECSC.
116 this.savedOptions_ = {};
117
rginda8ba33642011-12-14 12:31:31 -0800118 // The current mode bits for the terminal.
119 this.options_ = new hterm.Options();
120
121 // Timeouts we might need to clear.
122 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800123
124 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800125 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800126
rgindafeaf3142012-01-31 15:14:20 -0800127 // The keyboard hander.
128 this.keyboard = new hterm.Keyboard(this);
129
rginda87b86462011-12-14 13:48:03 -0800130 // General IO interface that can be given to third parties without exposing
131 // the entire terminal object.
132 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800133
rgindad5613292012-06-19 15:40:37 -0700134 // True if mouse-click-drag should scroll the terminal.
135 this.enableMouseDragScroll = true;
136
Robert Ginda57f03b42012-09-13 11:02:48 -0700137 this.copyOnSelect = null;
rginda4bba5e12012-06-20 16:15:30 -0700138 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700139
Rob Spies0bec09b2014-06-06 15:58:09 -0700140 // Whether to use the default window copy behaviour.
141 this.useDefaultWindowCopy = false;
142
143 this.clearSelectionAfterCopy = true;
144
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400145 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800146 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700147
148 this.setProfile(opt_profileId || 'default',
149 function() { this.onTerminalReady() }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800150};
151
152/**
Robert Ginda830583c2013-08-07 13:20:46 -0700153 * Possible cursor shapes.
154 */
155hterm.Terminal.cursorShape = {
156 BLOCK: 'BLOCK',
157 BEAM: 'BEAM',
158 UNDERLINE: 'UNDERLINE'
159};
160
161/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700162 * Clients should override this to be notified when the terminal is ready
163 * for use.
164 *
165 * The terminal initialization is asynchronous, and shouldn't be used before
166 * this method is called.
167 */
168hterm.Terminal.prototype.onTerminalReady = function() { };
169
170/**
rginda35c456b2012-02-09 17:29:05 -0800171 * Default tab with of 8 to match xterm.
172 */
173hterm.Terminal.prototype.tabWidth = 8;
174
175/**
rginda9f5222b2012-03-05 11:53:28 -0800176 * Select a preference profile.
177 *
178 * This will load the terminal preferences for the given profile name and
179 * associate subsequent preference changes with the new preference profile.
180 *
181 * @param {string} newName The name of the preference profile. Forward slash
182 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700183 * @param {function} opt_callback Optional callback to invoke when the profile
184 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800185 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700186hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
187 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800188
Robert Ginda57f03b42012-09-13 11:02:48 -0700189 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800190
Robert Ginda57f03b42012-09-13 11:02:48 -0700191 if (this.prefs_)
192 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800193
Robert Ginda57f03b42012-09-13 11:02:48 -0700194 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
195 this.prefs_.addObservers(null, {
Robert Ginda034ffa72015-02-26 14:02:37 -0800196 'alt-gr-mode': function(v) {
197 if (v == null) {
198 if (navigator.language.toLowerCase() == 'en-us') {
199 v = 'none';
200 } else {
201 v = 'right-alt';
202 }
203 } else if (typeof v == 'string') {
204 v = v.toLowerCase();
205 } else {
206 v = 'none';
207 }
208
209 if (!/^(none|ctrl-alt|left-alt|right-alt)$/.test(v))
210 v = 'none';
211
212 terminal.keyboard.altGrMode = v;
213 },
214
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700215 'alt-backspace-is-meta-backspace': function(v) {
216 terminal.keyboard.altBackspaceIsMetaBackspace = v;
217 },
218
Robert Ginda57f03b42012-09-13 11:02:48 -0700219 'alt-is-meta': function(v) {
220 terminal.keyboard.altIsMeta = v;
221 },
222
223 'alt-sends-what': function(v) {
224 if (!/^(escape|8-bit|browser-key)$/.test(v))
225 v = 'escape';
226
227 terminal.keyboard.altSendsWhat = v;
228 },
229
230 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800231 var ary = v.match(/^lib-resource:(\S+)/);
232 if (ary) {
233 terminal.bellAudio_.setAttribute('src',
234 lib.resource.getDataUrl(ary[1]));
235 } else {
236 terminal.bellAudio_.setAttribute('src', v);
237 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700238 },
239
Michael Kelly485ecd12014-06-09 11:41:56 -0400240 'desktop-notification-bell': function(v) {
241 if (v && Notification) {
Robert Ginda348dc2b2014-06-24 14:42:23 -0700242 terminal.desktopNotificationBell_ =
Michael Kellyb8067862014-06-26 12:59:47 -0400243 Notification.permission === 'granted';
244 if (!terminal.desktopNotificationBell_) {
245 // Note: We don't call Notification.requestPermission here because
246 // Chrome requires the call be the result of a user action (such as an
247 // onclick handler), and pref listeners are run asynchronously.
248 //
249 // A way of working around this would be to display a dialog in the
250 // terminal with a "click-to-request-permission" button.
251 console.warn('desktop-notification-bell is true but we do not have ' +
252 'permission to display notifications.');
Michael Kelly485ecd12014-06-09 11:41:56 -0400253 }
254 } else {
255 terminal.desktopNotificationBell_ = false;
256 }
257 },
258
Robert Ginda57f03b42012-09-13 11:02:48 -0700259 'background-color': function(v) {
260 terminal.setBackgroundColor(v);
261 },
262
263 'background-image': function(v) {
264 terminal.scrollPort_.setBackgroundImage(v);
265 },
266
267 'background-size': function(v) {
268 terminal.scrollPort_.setBackgroundSize(v);
269 },
270
271 'background-position': function(v) {
272 terminal.scrollPort_.setBackgroundPosition(v);
273 },
274
275 'backspace-sends-backspace': function(v) {
276 terminal.keyboard.backspaceSendsBackspace = v;
277 },
278
Brad Town18654b62015-03-12 00:27:45 -0700279 'character-map-overrides': function(v) {
280 if (!(v == null || v instanceof Object)) {
281 console.warn('Preference character-map-modifications is not an ' +
282 'object: ' + v);
283 return;
284 }
285
286 for (var code in v) {
287 var glmap = hterm.VT.CharacterMap.maps[code].glmap;
288 for (var received in v[code]) {
289 glmap[received] = v[code][received];
290 }
291 hterm.VT.CharacterMap.maps[code].reset(glmap);
292 }
293 },
294
Robert Ginda57f03b42012-09-13 11:02:48 -0700295 'cursor-blink': function(v) {
296 terminal.setCursorBlink(!!v);
297 },
298
Robert Gindaea2183e2014-07-17 09:51:51 -0700299 'cursor-blink-cycle': function(v) {
300 if (v instanceof Array &&
301 typeof v[0] == 'number' &&
302 typeof v[1] == 'number') {
303 terminal.cursorBlinkCycle_ = v;
304 } else if (typeof v == 'number') {
305 terminal.cursorBlinkCycle_ = [v, v];
306 } else {
307 // Fast blink indicates an error.
308 terminal.cursorBlinkCycle_ = [100, 100];
309 }
310 },
311
Robert Ginda57f03b42012-09-13 11:02:48 -0700312 'cursor-color': function(v) {
313 terminal.setCursorColor(v);
314 },
315
316 'color-palette-overrides': function(v) {
317 if (!(v == null || v instanceof Object || v instanceof Array)) {
318 console.warn('Preference color-palette-overrides is not an array or ' +
319 'object: ' + v);
320 return;
rginda9f5222b2012-03-05 11:53:28 -0800321 }
rginda9f5222b2012-03-05 11:53:28 -0800322
Robert Ginda57f03b42012-09-13 11:02:48 -0700323 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700324
Robert Ginda57f03b42012-09-13 11:02:48 -0700325 if (v) {
326 for (var key in v) {
327 var i = parseInt(key);
328 if (isNaN(i) || i < 0 || i > 255) {
329 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
330 continue;
331 }
332
333 if (v[i]) {
334 var rgb = lib.colors.normalizeCSS(v[i]);
335 if (rgb)
336 lib.colors.colorPalette[i] = rgb;
337 }
338 }
rginda30f20f62012-04-05 16:36:19 -0700339 }
rginda30f20f62012-04-05 16:36:19 -0700340
Robert Ginda57f03b42012-09-13 11:02:48 -0700341 terminal.primaryScreen_.textAttributes.resetColorPalette()
342 terminal.alternateScreen_.textAttributes.resetColorPalette();
343 },
rginda30f20f62012-04-05 16:36:19 -0700344
Robert Ginda57f03b42012-09-13 11:02:48 -0700345 'copy-on-select': function(v) {
346 terminal.copyOnSelect = !!v;
347 },
rginda9f5222b2012-03-05 11:53:28 -0800348
Rob Spies0bec09b2014-06-06 15:58:09 -0700349 'use-default-window-copy': function(v) {
350 terminal.useDefaultWindowCopy = !!v;
351 },
352
353 'clear-selection-after-copy': function(v) {
354 terminal.clearSelectionAfterCopy = !!v;
355 },
356
Robert Ginda7e5e9522014-03-14 12:23:58 -0700357 'ctrl-plus-minus-zero-zoom': function(v) {
358 terminal.keyboard.ctrlPlusMinusZeroZoom = v;
359 },
360
Robert Gindafb5a3f92014-05-13 14:12:00 -0700361 'ctrl-c-copy': function(v) {
362 terminal.keyboard.ctrlCCopy = v;
363 },
364
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100365 'ctrl-v-paste': function(v) {
366 terminal.keyboard.ctrlVPaste = v;
Rob Spiese52e1842014-07-10 15:32:51 -0700367 terminal.scrollPort_.setCtrlVPaste(v);
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100368 },
369
Masaya Suzuki273aa982014-05-31 07:25:55 +0900370 'east-asian-ambiguous-as-two-column': function(v) {
371 lib.wc.regardCjkAmbiguous = v;
372 },
373
Robert Ginda57f03b42012-09-13 11:02:48 -0700374 'enable-8-bit-control': function(v) {
375 terminal.vt.enable8BitControl = !!v;
376 },
rginda30f20f62012-04-05 16:36:19 -0700377
Robert Ginda57f03b42012-09-13 11:02:48 -0700378 'enable-bold': function(v) {
379 terminal.syncBoldSafeState();
380 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400381
Robert Ginda3e278d72014-03-25 13:18:51 -0700382 'enable-bold-as-bright': function(v) {
383 terminal.primaryScreen_.textAttributes.enableBoldAsBright = !!v;
384 terminal.alternateScreen_.textAttributes.enableBoldAsBright = !!v;
385 },
386
Robert Ginda57f03b42012-09-13 11:02:48 -0700387 'enable-clipboard-write': function(v) {
388 terminal.vt.enableClipboardWrite = !!v;
389 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400390
Robert Ginda3755e752013-05-31 13:34:09 -0700391 'enable-dec12': function(v) {
392 terminal.vt.enableDec12 = !!v;
393 },
394
Robert Ginda57f03b42012-09-13 11:02:48 -0700395 'font-family': function(v) {
396 terminal.syncFontFamily();
397 },
rginda30f20f62012-04-05 16:36:19 -0700398
Robert Ginda57f03b42012-09-13 11:02:48 -0700399 'font-size': function(v) {
400 terminal.setFontSize(v);
401 },
rginda9875d902012-08-20 16:21:57 -0700402
Robert Ginda57f03b42012-09-13 11:02:48 -0700403 'font-smoothing': function(v) {
404 terminal.syncFontFamily();
405 },
rgindade84e382012-04-20 15:39:31 -0700406
Robert Ginda57f03b42012-09-13 11:02:48 -0700407 'foreground-color': function(v) {
408 terminal.setForegroundColor(v);
409 },
rginda30f20f62012-04-05 16:36:19 -0700410
Robert Ginda57f03b42012-09-13 11:02:48 -0700411 'home-keys-scroll': function(v) {
412 terminal.keyboard.homeKeysScroll = v;
413 },
rginda4bba5e12012-06-20 16:15:30 -0700414
Robert Gindaa8165692015-06-15 14:46:31 -0700415 'keybindings': function(v) {
416 terminal.keyboard.bindings.clear();
417
418 if (!v)
419 return;
420
421 if (!(v instanceof Object)) {
422 console.error('Error in keybindings preference: Expected object');
423 return;
424 }
425
426 try {
427 terminal.keyboard.bindings.addBindings(v);
428 } catch (ex) {
429 console.error('Error in keybindings preference: ' + ex);
430 }
431 },
432
Robert Ginda57f03b42012-09-13 11:02:48 -0700433 'max-string-sequence': function(v) {
434 terminal.vt.maxStringSequence = v;
435 },
rginda11057d52012-04-25 12:29:56 -0700436
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700437 'media-keys-are-fkeys': function(v) {
438 terminal.keyboard.mediaKeysAreFKeys = v;
439 },
440
Robert Ginda57f03b42012-09-13 11:02:48 -0700441 'meta-sends-escape': function(v) {
442 terminal.keyboard.metaSendsEscape = v;
443 },
rginda30f20f62012-04-05 16:36:19 -0700444
Robert Ginda57f03b42012-09-13 11:02:48 -0700445 'mouse-paste-button': function(v) {
446 terminal.syncMousePasteButton();
447 },
rgindaa8ba17d2012-08-15 14:41:10 -0700448
Robert Gindae76aa9f2014-03-14 12:29:12 -0700449 'page-keys-scroll': function(v) {
450 terminal.keyboard.pageKeysScroll = v;
451 },
452
Robert Ginda40932892012-12-10 17:26:40 -0800453 'pass-alt-number': function(v) {
454 if (v == null) {
455 var osx = window.navigator.userAgent.match(/Mac OS X/);
456
457 // Let Alt-1..9 pass to the browser (to control tab switching) on
458 // non-OS X systems, or if hterm is not opened in an app window.
459 v = (!osx && hterm.windowType != 'popup');
460 }
461
462 terminal.passAltNumber = v;
463 },
464
465 'pass-ctrl-number': function(v) {
466 if (v == null) {
467 var osx = window.navigator.userAgent.match(/Mac OS X/);
468
469 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
470 // non-OS X systems, or if hterm is not opened in an app window.
471 v = (!osx && hterm.windowType != 'popup');
472 }
473
474 terminal.passCtrlNumber = v;
475 },
476
477 'pass-meta-number': function(v) {
478 if (v == null) {
479 var osx = window.navigator.userAgent.match(/Mac OS X/);
480
481 // Let Meta-1..9 pass to the browser (to control tab switching) on
482 // OS X systems, or if hterm is not opened in an app window.
483 v = (osx && hterm.windowType != 'popup');
484 }
485
486 terminal.passMetaNumber = v;
487 },
488
Marius Schilder77857b32014-05-14 16:21:26 -0700489 'pass-meta-v': function(v) {
Marius Schilder1a567812014-05-15 20:30:02 -0700490 terminal.keyboard.passMetaV = v;
Marius Schilder77857b32014-05-14 16:21:26 -0700491 },
492
Robert Ginda8cb7d902013-06-20 14:37:18 -0700493 'receive-encoding': function(v) {
494 if (!(/^(utf-8|raw)$/).test(v)) {
495 console.warn('Invalid value for "receive-encoding": ' + v);
496 v = 'utf-8';
497 }
498
499 terminal.vt.characterEncoding = v;
500 },
501
Robert Ginda57f03b42012-09-13 11:02:48 -0700502 'scroll-on-keystroke': function(v) {
503 terminal.scrollOnKeystroke_ = v;
504 },
rginda9f5222b2012-03-05 11:53:28 -0800505
Robert Ginda57f03b42012-09-13 11:02:48 -0700506 'scroll-on-output': function(v) {
507 terminal.scrollOnOutput_ = v;
508 },
rginda30f20f62012-04-05 16:36:19 -0700509
Robert Ginda57f03b42012-09-13 11:02:48 -0700510 'scrollbar-visible': function(v) {
511 terminal.setScrollbarVisible(v);
512 },
rginda9f5222b2012-03-05 11:53:28 -0800513
Rob Spies49039e52014-12-17 13:40:04 -0800514 'scroll-wheel-move-multiplier': function(v) {
515 terminal.setScrollWheelMoveMultipler(v);
516 },
517
Robert Ginda8cb7d902013-06-20 14:37:18 -0700518 'send-encoding': function(v) {
519 if (!(/^(utf-8|raw)$/).test(v)) {
520 console.warn('Invalid value for "send-encoding": ' + v);
521 v = 'utf-8';
522 }
523
524 terminal.keyboard.characterEncoding = v;
525 },
526
Robert Ginda57f03b42012-09-13 11:02:48 -0700527 'shift-insert-paste': function(v) {
528 terminal.keyboard.shiftInsertPaste = v;
529 },
rginda9f5222b2012-03-05 11:53:28 -0800530
Robert Gindae76aa9f2014-03-14 12:29:12 -0700531 'user-css': function(v) {
532 terminal.scrollPort_.setUserCss(v);
Robert Ginda57f03b42012-09-13 11:02:48 -0700533 }
534 });
rginda30f20f62012-04-05 16:36:19 -0700535
Robert Ginda57f03b42012-09-13 11:02:48 -0700536 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800537 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700538
539 if (opt_callback)
540 opt_callback();
541 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800542};
543
Rob Spies56953412014-04-28 14:09:47 -0700544
545/**
546 * Returns the preferences manager used for configuring this terminal.
547 */
548hterm.Terminal.prototype.getPrefs = function() {
549 return this.prefs_;
550};
551
Robert Gindaa063b202014-07-21 11:08:25 -0700552/**
553 * Enable or disable bracketed paste mode.
554 */
555hterm.Terminal.prototype.setBracketedPaste = function(state) {
556 this.options_.bracketedPaste = state;
557};
Rob Spies56953412014-04-28 14:09:47 -0700558
rginda8e92a692012-05-20 19:37:20 -0700559/**
560 * Set the color for the cursor.
561 *
562 * If you want this setting to persist, set it through prefs_, rather than
563 * with this method.
564 */
565hterm.Terminal.prototype.setCursorColor = function(color) {
Robert Ginda830583c2013-08-07 13:20:46 -0700566 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700567 this.cursorNode_.style.backgroundColor = color;
568 this.cursorNode_.style.borderColor = color;
569};
570
571/**
572 * Return the current cursor color as a string.
573 */
574hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700575 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700576};
577
578/**
rgindad5613292012-06-19 15:40:37 -0700579 * Enable or disable mouse based text selection in the terminal.
580 */
581hterm.Terminal.prototype.setSelectionEnabled = function(state) {
582 this.enableMouseDragScroll = state;
rgindad5613292012-06-19 15:40:37 -0700583};
584
585/**
rginda8e92a692012-05-20 19:37:20 -0700586 * Set the background color.
587 *
588 * If you want this setting to persist, set it through prefs_, rather than
589 * with this method.
590 */
591hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700592 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700593 this.primaryScreen_.textAttributes.setDefaults(
594 this.foregroundColor_, this.backgroundColor_);
595 this.alternateScreen_.textAttributes.setDefaults(
596 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700597 this.scrollPort_.setBackgroundColor(color);
598};
599
rginda9f5222b2012-03-05 11:53:28 -0800600/**
601 * Return the current terminal background color.
602 *
603 * Intended for use by other classes, so we don't have to expose the entire
604 * prefs_ object.
605 */
606hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700607 return this.backgroundColor_;
608};
609
610/**
611 * Set the foreground color.
612 *
613 * If you want this setting to persist, set it through prefs_, rather than
614 * with this method.
615 */
616hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700617 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700618 this.primaryScreen_.textAttributes.setDefaults(
619 this.foregroundColor_, this.backgroundColor_);
620 this.alternateScreen_.textAttributes.setDefaults(
621 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700622 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800623};
624
625/**
626 * Return the current terminal foreground color.
627 *
628 * Intended for use by other classes, so we don't have to expose the entire
629 * prefs_ object.
630 */
631hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700632 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800633};
634
635/**
rginda87b86462011-12-14 13:48:03 -0800636 * Create a new instance of a terminal command and run it with a given
637 * argument string.
638 *
639 * @param {function} commandClass The constructor for a terminal command.
640 * @param {string} argString The argument string to pass to the command.
641 */
642hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700643 var environment = this.prefs_.get('environment');
644 if (typeof environment != 'object' || environment == null)
645 environment = {};
646
rginda87b86462011-12-14 13:48:03 -0800647 var self = this;
648 this.command = new commandClass(
649 { argString: argString || '',
650 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700651 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800652 onExit: function(code) {
653 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800654 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700655 if (self.prefs_.get('close-on-exit'))
656 window.close();
rginda87b86462011-12-14 13:48:03 -0800657 }
658 });
659
rgindafeaf3142012-01-31 15:14:20 -0800660 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800661 this.command.run();
662};
663
664/**
rgindafeaf3142012-01-31 15:14:20 -0800665 * Returns true if the current screen is the primary screen, false otherwise.
666 */
667hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700668 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800669};
670
671/**
672 * Install the keyboard handler for this terminal.
673 *
674 * This will prevent the browser from seeing any keystrokes sent to the
675 * terminal.
676 */
677hterm.Terminal.prototype.installKeyboard = function() {
Rob Spies06533ba2014-04-24 11:20:37 -0700678 this.keyboard.installKeyboard(this.scrollPort_.getDocument().body);
rgindafeaf3142012-01-31 15:14:20 -0800679}
680
681/**
682 * Uninstall the keyboard handler for this terminal.
683 */
684hterm.Terminal.prototype.uninstallKeyboard = function() {
685 this.keyboard.installKeyboard(null);
686}
687
688/**
rginda35c456b2012-02-09 17:29:05 -0800689 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800690 *
691 * Call setFontSize(0) to reset to the default font size.
692 *
693 * This function does not modify the font-size preference.
694 *
695 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800696 */
697hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800698 if (px === 0)
699 px = this.prefs_.get('font-size');
700
rginda35c456b2012-02-09 17:29:05 -0800701 this.scrollPort_.setFontSize(px);
Ricky Liang48f05cb2013-12-31 23:35:29 +0800702 if (this.wcCssRule_) {
703 this.wcCssRule_.style.width = this.scrollPort_.characterSize.width * 2 +
704 'px';
705 }
rginda35c456b2012-02-09 17:29:05 -0800706};
707
708/**
709 * Get the current font size.
710 */
711hterm.Terminal.prototype.getFontSize = function() {
712 return this.scrollPort_.getFontSize();
713};
714
715/**
rginda8e92a692012-05-20 19:37:20 -0700716 * Get the current font family.
717 */
718hterm.Terminal.prototype.getFontFamily = function() {
719 return this.scrollPort_.getFontFamily();
720};
721
722/**
rginda35c456b2012-02-09 17:29:05 -0800723 * Set the CSS "font-family" for this terminal.
724 */
rginda9f5222b2012-03-05 11:53:28 -0800725hterm.Terminal.prototype.syncFontFamily = function() {
726 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
727 this.prefs_.get('font-smoothing'));
728 this.syncBoldSafeState();
729};
730
rginda4bba5e12012-06-20 16:15:30 -0700731/**
732 * Set this.mousePasteButton based on the mouse-paste-button pref,
733 * autodetecting if necessary.
734 */
735hterm.Terminal.prototype.syncMousePasteButton = function() {
736 var button = this.prefs_.get('mouse-paste-button');
737 if (typeof button == 'number') {
738 this.mousePasteButton = button;
739 return;
740 }
741
742 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
743 if (!ary || ary[2] == 'CrOS') {
744 this.mousePasteButton = 2;
745 } else {
746 this.mousePasteButton = 3;
747 }
748};
749
750/**
751 * Enable or disable bold based on the enable-bold pref, autodetecting if
752 * necessary.
753 */
rginda9f5222b2012-03-05 11:53:28 -0800754hterm.Terminal.prototype.syncBoldSafeState = function() {
755 var enableBold = this.prefs_.get('enable-bold');
756 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700757 this.primaryScreen_.textAttributes.enableBold = enableBold;
758 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800759 return;
760 }
761
rgindaf7521392012-02-28 17:20:34 -0800762 var normalSize = this.scrollPort_.measureCharacterSize();
763 var boldSize = this.scrollPort_.measureCharacterSize('bold');
764
765 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800766 if (!isBoldSafe) {
767 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700768 'from normal. Font family is: ' +
769 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800770 }
rginda9f5222b2012-03-05 11:53:28 -0800771
Robert Gindaed016262012-10-26 16:27:09 -0700772 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
773 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800774};
775
776/**
rginda87b86462011-12-14 13:48:03 -0800777 * Return a copy of the current cursor position.
778 *
779 * @return {hterm.RowCol} The RowCol object representing the current position.
780 */
781hterm.Terminal.prototype.saveCursor = function() {
782 return this.screen_.cursorPosition.clone();
783};
784
rgindaa19afe22012-01-25 15:40:22 -0800785hterm.Terminal.prototype.getTextAttributes = function() {
786 return this.screen_.textAttributes;
787};
788
rginda1a09aa02012-06-18 21:11:25 -0700789hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
790 this.screen_.textAttributes = textAttributes;
791};
792
rginda87b86462011-12-14 13:48:03 -0800793/**
rgindaf522ce02012-04-17 17:49:17 -0700794 * Return the current browser zoom factor applied to the terminal.
795 *
796 * @return {number} The current browser zoom factor.
797 */
798hterm.Terminal.prototype.getZoomFactor = function() {
799 return this.scrollPort_.characterSize.zoomFactor;
800};
801
802/**
rginda9846e2f2012-01-27 13:53:33 -0800803 * Change the title of this terminal's window.
804 */
805hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800806 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800807};
808
809/**
rginda87b86462011-12-14 13:48:03 -0800810 * Restore a previously saved cursor position.
811 *
812 * @param {hterm.RowCol} cursor The position to restore.
813 */
814hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700815 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
816 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800817 this.screen_.setCursorPosition(row, column);
818 if (cursor.column > column ||
819 cursor.column == column && cursor.overflow) {
820 this.screen_.cursorPosition.overflow = true;
821 }
rginda87b86462011-12-14 13:48:03 -0800822};
823
824/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400825 * Clear the cursor's overflow flag.
826 */
827hterm.Terminal.prototype.clearCursorOverflow = function() {
828 this.screen_.cursorPosition.overflow = false;
829};
830
831/**
Robert Ginda830583c2013-08-07 13:20:46 -0700832 * Sets the cursor shape
833 */
834hterm.Terminal.prototype.setCursorShape = function(shape) {
835 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -0800836 this.restyleCursor_();
Robert Ginda830583c2013-08-07 13:20:46 -0700837}
838
839/**
840 * Get the cursor shape
841 */
842hterm.Terminal.prototype.getCursorShape = function() {
843 return this.cursorShape_;
844}
845
846/**
rginda87b86462011-12-14 13:48:03 -0800847 * Set the width of the terminal, resizing the UI to match.
848 */
849hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800850 if (columnCount == null) {
851 this.div_.style.width = '100%';
852 return;
853 }
854
Robert Ginda26806d12014-07-24 13:44:07 -0700855 this.div_.style.width = Math.ceil(
856 this.scrollPort_.characterSize.width *
857 columnCount + this.scrollPort_.currentScrollbarWidthPx) + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400858 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800859 this.scheduleSyncCursorPosition_();
860};
rginda87b86462011-12-14 13:48:03 -0800861
rgindac9bc5502012-01-18 11:48:44 -0800862/**
rginda35c456b2012-02-09 17:29:05 -0800863 * Set the height of the terminal, resizing the UI to match.
864 */
865hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800866 if (rowCount == null) {
867 this.div_.style.height = '100%';
868 return;
869 }
870
rginda35c456b2012-02-09 17:29:05 -0800871 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700872 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800873 this.realizeSize_(this.screenSize.width, rowCount);
874 this.scheduleSyncCursorPosition_();
875};
876
877/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400878 * Deal with terminal size changes.
879 *
880 */
881hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
882 if (columnCount != this.screenSize.width)
883 this.realizeWidth_(columnCount);
884
885 if (rowCount != this.screenSize.height)
886 this.realizeHeight_(rowCount);
887
888 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -0700889 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400890};
891
892/**
rgindac9bc5502012-01-18 11:48:44 -0800893 * Deal with terminal width changes.
894 *
895 * This function does what needs to be done when the terminal width changes
896 * out from under us. It happens here rather than in onResize_() because this
897 * code may need to run synchronously to handle programmatic changes of
898 * terminal width.
899 *
900 * Relying on the browser to send us an async resize event means we may not be
901 * in the correct state yet when the next escape sequence hits.
902 */
903hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700904 if (columnCount <= 0)
905 throw new Error('Attempt to realize bad width: ' + columnCount);
906
rgindac9bc5502012-01-18 11:48:44 -0800907 var deltaColumns = columnCount - this.screen_.getWidth();
908
rginda87b86462011-12-14 13:48:03 -0800909 this.screenSize.width = columnCount;
910 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800911
912 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400913 if (this.defaultTabStops)
914 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800915 } else {
916 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400917 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800918 break;
919
920 this.tabStops_.pop();
921 }
922 }
923
924 this.screen_.setColumnCount(this.screenSize.width);
925};
926
927/**
928 * Deal with terminal height changes.
929 *
930 * This function does what needs to be done when the terminal height changes
931 * out from under us. It happens here rather than in onResize_() because this
932 * code may need to run synchronously to handle programmatic changes of
933 * terminal height.
934 *
935 * Relying on the browser to send us an async resize event means we may not be
936 * in the correct state yet when the next escape sequence hits.
937 */
938hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700939 if (rowCount <= 0)
940 throw new Error('Attempt to realize bad height: ' + rowCount);
941
rgindac9bc5502012-01-18 11:48:44 -0800942 var deltaRows = rowCount - this.screen_.getHeight();
943
944 this.screenSize.height = rowCount;
945
946 var cursor = this.saveCursor();
947
948 if (deltaRows < 0) {
949 // Screen got smaller.
950 deltaRows *= -1;
951 while (deltaRows) {
952 var lastRow = this.getRowCount() - 1;
953 if (lastRow - this.scrollbackRows_.length == cursor.row)
954 break;
955
956 if (this.getRowText(lastRow))
957 break;
958
959 this.screen_.popRow();
960 deltaRows--;
961 }
962
963 var ary = this.screen_.shiftRows(deltaRows);
964 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
965
966 // We just removed rows from the top of the screen, we need to update
967 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800968 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800969 } else if (deltaRows > 0) {
970 // Screen got larger.
971
972 if (deltaRows <= this.scrollbackRows_.length) {
973 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
974 var rows = this.scrollbackRows_.splice(
975 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
976 this.screen_.unshiftRows(rows);
977 deltaRows -= scrollbackCount;
978 cursor.row += scrollbackCount;
979 }
980
981 if (deltaRows)
982 this.appendRows_(deltaRows);
983 }
984
rginda35c456b2012-02-09 17:29:05 -0800985 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800986 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800987};
988
989/**
990 * Scroll the terminal to the top of the scrollback buffer.
991 */
992hterm.Terminal.prototype.scrollHome = function() {
993 this.scrollPort_.scrollRowToTop(0);
994};
995
996/**
997 * Scroll the terminal to the end.
998 */
999hterm.Terminal.prototype.scrollEnd = function() {
1000 this.scrollPort_.scrollRowToBottom(this.getRowCount());
1001};
1002
1003/**
1004 * Scroll the terminal one page up (minus one line) relative to the current
1005 * position.
1006 */
1007hterm.Terminal.prototype.scrollPageUp = function() {
1008 var i = this.scrollPort_.getTopRowIndex();
1009 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
1010};
1011
1012/**
1013 * Scroll the terminal one page down (minus one line) relative to the current
1014 * position.
1015 */
1016hterm.Terminal.prototype.scrollPageDown = function() {
1017 var i = this.scrollPort_.getTopRowIndex();
1018 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -08001019};
1020
rgindac9bc5502012-01-18 11:48:44 -08001021/**
Robert Ginda40932892012-12-10 17:26:40 -08001022 * Clear primary screen, secondary screen, and the scrollback buffer.
1023 */
1024hterm.Terminal.prototype.wipeContents = function() {
1025 this.scrollbackRows_.length = 0;
1026 this.scrollPort_.resetCache();
1027
1028 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
1029 var bottom = screen.getHeight();
1030 if (bottom > 0) {
1031 this.renumberRows_(0, bottom);
1032 this.clearHome(screen);
1033 }
1034 }.bind(this));
1035
1036 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -07001037 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -08001038};
1039
1040/**
rgindac9bc5502012-01-18 11:48:44 -08001041 * Full terminal reset.
1042 */
rginda87b86462011-12-14 13:48:03 -08001043hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -08001044 this.clearAllTabStops();
1045 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -07001046
1047 this.clearHome(this.primaryScreen_);
1048 this.primaryScreen_.textAttributes.reset();
1049
1050 this.clearHome(this.alternateScreen_);
1051 this.alternateScreen_.textAttributes.reset();
1052
rgindab8bc8932012-04-27 12:45:03 -07001053 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1054
Robert Ginda92e18102013-03-14 13:56:37 -07001055 this.vt.reset();
1056
rgindac9bc5502012-01-18 11:48:44 -08001057 this.softReset();
rginda87b86462011-12-14 13:48:03 -08001058};
1059
rgindac9bc5502012-01-18 11:48:44 -08001060/**
1061 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -07001062 *
1063 * Perform a soft reset to the default values listed in
1064 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -08001065 */
rginda0f5c0292012-01-13 11:00:13 -08001066hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -07001067 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -08001068 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -07001069
Brad Townb62dfdc2015-03-16 19:07:15 -07001070 // We show the cursor on soft reset but do not alter the blink state.
1071 this.options_.cursorBlink = !!this.timeouts_.cursorBlink;
1072
rgindab8bc8932012-04-27 12:45:03 -07001073 // Xterm also resets the color palette on soft reset, even though it doesn't
1074 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -07001075 this.primaryScreen_.textAttributes.resetColorPalette();
1076 this.alternateScreen_.textAttributes.resetColorPalette();
1077
rgindab8bc8932012-04-27 12:45:03 -07001078 // The xterm man page explicitly says this will happen on soft reset.
1079 this.setVTScrollRegion(null, null);
1080
1081 // Xterm also shows the cursor on soft reset, but does not alter the blink
1082 // state.
rgindaa19afe22012-01-25 15:40:22 -08001083 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -08001084};
1085
rgindac9bc5502012-01-18 11:48:44 -08001086/**
1087 * Move the cursor forward to the next tab stop, or to the last column
1088 * if no more tab stops are set.
1089 */
1090hterm.Terminal.prototype.forwardTabStop = function() {
1091 var column = this.screen_.cursorPosition.column;
1092
1093 for (var i = 0; i < this.tabStops_.length; i++) {
1094 if (this.tabStops_[i] > column) {
1095 this.setCursorColumn(this.tabStops_[i]);
1096 return;
1097 }
1098 }
1099
David Benjamin66e954d2012-05-05 21:08:12 -04001100 // xterm does not clear the overflow flag on HT or CHT.
1101 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -08001102 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -04001103 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -08001104};
1105
rgindac9bc5502012-01-18 11:48:44 -08001106/**
1107 * Move the cursor backward to the previous tab stop, or to the first column
1108 * if no previous tab stops are set.
1109 */
1110hterm.Terminal.prototype.backwardTabStop = function() {
1111 var column = this.screen_.cursorPosition.column;
1112
1113 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1114 if (this.tabStops_[i] < column) {
1115 this.setCursorColumn(this.tabStops_[i]);
1116 return;
1117 }
1118 }
1119
1120 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -08001121};
1122
rgindac9bc5502012-01-18 11:48:44 -08001123/**
1124 * Set a tab stop at the given column.
1125 *
1126 * @param {int} column Zero based column.
1127 */
1128hterm.Terminal.prototype.setTabStop = function(column) {
1129 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
1130 if (this.tabStops_[i] == column)
1131 return;
1132
1133 if (this.tabStops_[i] < column) {
1134 this.tabStops_.splice(i + 1, 0, column);
1135 return;
1136 }
1137 }
1138
1139 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -08001140};
1141
rgindac9bc5502012-01-18 11:48:44 -08001142/**
1143 * Clear the tab stop at the current cursor position.
1144 *
1145 * No effect if there is no tab stop at the current cursor position.
1146 */
1147hterm.Terminal.prototype.clearTabStopAtCursor = function() {
1148 var column = this.screen_.cursorPosition.column;
1149
1150 var i = this.tabStops_.indexOf(column);
1151 if (i == -1)
1152 return;
1153
1154 this.tabStops_.splice(i, 1);
1155};
1156
1157/**
1158 * Clear all tab stops.
1159 */
1160hterm.Terminal.prototype.clearAllTabStops = function() {
1161 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -04001162 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -08001163};
1164
1165/**
1166 * Set up the default tab stops, starting from a given column.
1167 *
1168 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001169 * from the specified column, or 0 if no column is provided. It also flags
1170 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001171 *
1172 * This does not clear the existing tab stops first, use clearAllTabStops
1173 * for that.
1174 *
1175 * @param {int} opt_start Optional starting zero based starting column, useful
1176 * for filling out missing tab stops when the terminal is resized.
1177 */
1178hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1179 var start = opt_start || 0;
1180 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001181 // Round start up to a default tab stop.
1182 start = start - 1 - ((start - 1) % w) + w;
1183 for (var i = start; i < this.screenSize.width; i += w) {
1184 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001185 }
David Benjamin66e954d2012-05-05 21:08:12 -04001186
1187 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001188};
1189
rginda6d397402012-01-17 10:58:29 -08001190/**
rginda8ba33642011-12-14 12:31:31 -08001191 * Interpret a sequence of characters.
1192 *
1193 * Incomplete escape sequences are buffered until the next call.
1194 *
1195 * @param {string} str Sequence of characters to interpret or pass through.
1196 */
1197hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001198 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001199 this.scheduleSyncCursorPosition_();
1200};
1201
1202/**
1203 * Take over the given DIV for use as the terminal display.
1204 *
1205 * @param {HTMLDivElement} div The div to use as the terminal display.
1206 */
1207hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001208 this.div_ = div;
1209
rginda8ba33642011-12-14 12:31:31 -08001210 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001211 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001212 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1213 this.scrollPort_.setBackgroundPosition(
1214 this.prefs_.get('background-position'));
Robert Gindae76aa9f2014-03-14 12:29:12 -07001215 this.scrollPort_.setUserCss(this.prefs_.get('user-css'));
rginda30f20f62012-04-05 16:36:19 -07001216
rginda0918b652012-04-04 11:26:24 -07001217 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001218
rginda9f5222b2012-03-05 11:53:28 -08001219 this.setFontSize(this.prefs_.get('font-size'));
1220 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001221
David Reveman8f552492012-03-28 12:18:41 -04001222 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
Rob Spies49039e52014-12-17 13:40:04 -08001223 this.setScrollWheelMoveMultipler(
1224 this.prefs_.get('scroll-wheel-move-multiplier'));
David Reveman8f552492012-03-28 12:18:41 -04001225
rginda8ba33642011-12-14 12:31:31 -08001226 this.document_ = this.scrollPort_.getDocument();
1227
rginda4bba5e12012-06-20 16:15:30 -07001228 this.document_.body.oncontextmenu = function() { return false };
1229
1230 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001231 var screenNode = this.scrollPort_.getScreenNode();
1232 screenNode.addEventListener('mousedown', onMouse);
1233 screenNode.addEventListener('mouseup', onMouse);
1234 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001235 this.scrollPort_.onScrollWheel = onMouse;
1236
Toni Barzic0bfa8922013-11-22 11:18:35 -08001237 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001238 'focus', this.onFocusChange_.bind(this, true));
Rob Spies06533ba2014-04-24 11:20:37 -07001239 // Listen for mousedown events on the screenNode as in FF the focus
1240 // events don't bubble.
1241 screenNode.addEventListener('mousedown', function() {
1242 setTimeout(this.onFocusChange_.bind(this, true));
1243 }.bind(this));
1244
Toni Barzic0bfa8922013-11-22 11:18:35 -08001245 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001246 'blur', this.onFocusChange_.bind(this, false));
1247
1248 var style = this.document_.createElement('style');
1249 style.textContent =
1250 ('.cursor-node[focus="false"] {' +
1251 ' box-sizing: border-box;' +
1252 ' background-color: transparent !important;' +
1253 ' border-width: 2px;' +
1254 ' border-style: solid;' +
Ricky Liang48f05cb2013-12-31 23:35:29 +08001255 '}' +
1256 '.wc-node {' +
1257 ' display: inline-block;' +
1258 ' text-align: center;' +
1259 ' width: ' + this.scrollPort_.characterSize.width * 2 + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001260 '}');
1261 this.document_.head.appendChild(style);
1262
Ricky Liang48f05cb2013-12-31 23:35:29 +08001263 var styleSheets = this.document_.styleSheets;
1264 var cssRules = styleSheets[styleSheets.length - 1].cssRules;
1265 this.wcCssRule_ = cssRules[cssRules.length - 1];
1266
rginda8ba33642011-12-14 12:31:31 -08001267 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001268 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001269 this.cursorNode_.style.cssText =
1270 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001271 'top: -99px;' +
1272 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001273 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1274 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
Rob Spies06533ba2014-04-24 11:20:37 -07001275 '-webkit-transition: opacity, background-color 100ms linear;' +
1276 '-moz-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001277
rginda8e92a692012-05-20 19:37:20 -07001278 this.setCursorColor(this.prefs_.get('cursor-color'));
Robert Gindafb1be6a2013-12-11 11:56:22 -08001279 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1280 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001281
rginda8ba33642011-12-14 12:31:31 -08001282 this.document_.body.appendChild(this.cursorNode_);
1283
rgindad5613292012-06-19 15:40:37 -07001284 // When 'enableMouseDragScroll' is off we reposition this element directly
1285 // under the mouse cursor after a click. This makes Chrome associate
1286 // subsequent mousemove events with the scroll-blocker. Since the
1287 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1288 // events do not cause the scrollport to scroll.
1289 //
1290 // It's a hack, but it's the cleanest way I could find.
1291 this.scrollBlockerNode_ = this.document_.createElement('div');
1292 this.scrollBlockerNode_.style.cssText =
1293 ('position: absolute;' +
1294 'top: -99px;' +
1295 'display: block;' +
1296 'width: 10px;' +
1297 'height: 10px;');
1298 this.document_.body.appendChild(this.scrollBlockerNode_);
1299
1300 var onMouse = this.onMouse_.bind(this);
1301 this.scrollPort_.onScrollWheel = onMouse;
1302 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1303 ].forEach(function(event) {
1304 this.scrollBlockerNode_.addEventListener(event, onMouse);
1305 this.cursorNode_.addEventListener(event, onMouse);
1306 this.document_.addEventListener(event, onMouse);
1307 }.bind(this));
1308
1309 this.cursorNode_.addEventListener('mousedown', function() {
1310 setTimeout(this.focus.bind(this));
1311 }.bind(this));
1312
rginda8ba33642011-12-14 12:31:31 -08001313 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001314
rginda87b86462011-12-14 13:48:03 -08001315 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001316 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001317};
1318
rginda0918b652012-04-04 11:26:24 -07001319/**
1320 * Return the HTML document that contains the terminal DOM nodes.
1321 */
rginda87b86462011-12-14 13:48:03 -08001322hterm.Terminal.prototype.getDocument = function() {
1323 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001324};
1325
1326/**
rginda0918b652012-04-04 11:26:24 -07001327 * Focus the terminal.
1328 */
1329hterm.Terminal.prototype.focus = function() {
1330 this.scrollPort_.focus();
1331};
1332
1333/**
rginda8ba33642011-12-14 12:31:31 -08001334 * Return the HTML Element for a given row index.
1335 *
1336 * This is a method from the RowProvider interface. The ScrollPort uses
1337 * it to fetch rows on demand as they are scrolled into view.
1338 *
1339 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1340 * pairs to conserve memory.
1341 *
1342 * @param {integer} index The zero-based row index, measured relative to the
1343 * start of the scrollback buffer. On-screen rows will always have the
1344 * largest indicies.
1345 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1346 */
1347hterm.Terminal.prototype.getRowNode = function(index) {
1348 if (index < this.scrollbackRows_.length)
1349 return this.scrollbackRows_[index];
1350
1351 var screenIndex = index - this.scrollbackRows_.length;
1352 return this.screen_.rowsArray[screenIndex];
1353};
1354
1355/**
1356 * Return the text content for a given range of rows.
1357 *
1358 * This is a method from the RowProvider interface. The ScrollPort uses
1359 * it to fetch text content on demand when the user attempts to copy their
1360 * selection to the clipboard.
1361 *
1362 * @param {integer} start The zero-based row index to start from, measured
1363 * relative to the start of the scrollback buffer. On-screen rows will
1364 * always have the largest indicies.
1365 * @param {integer} end The zero-based row index to end on, measured
1366 * relative to the start of the scrollback buffer.
1367 * @return {string} A single string containing the text value of the range of
1368 * rows. Lines will be newline delimited, with no trailing newline.
1369 */
1370hterm.Terminal.prototype.getRowsText = function(start, end) {
1371 var ary = [];
1372 for (var i = start; i < end; i++) {
1373 var node = this.getRowNode(i);
1374 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001375 if (i < end - 1 && !node.getAttribute('line-overflow'))
1376 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001377 }
1378
rgindaa09e7332012-08-17 12:49:51 -07001379 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001380};
1381
1382/**
1383 * Return the text content for a given row.
1384 *
1385 * This is a method from the RowProvider interface. The ScrollPort uses
1386 * it to fetch text content on demand when the user attempts to copy their
1387 * selection to the clipboard.
1388 *
1389 * @param {integer} index The zero-based row index to return, measured
1390 * relative to the start of the scrollback buffer. On-screen rows will
1391 * always have the largest indicies.
1392 * @return {string} A string containing the text value of the selected row.
1393 */
1394hterm.Terminal.prototype.getRowText = function(index) {
1395 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001396 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001397};
1398
1399/**
1400 * Return the total number of rows in the addressable screen and in the
1401 * scrollback buffer of this terminal.
1402 *
1403 * This is a method from the RowProvider interface. The ScrollPort uses
1404 * it to compute the size of the scrollbar.
1405 *
1406 * @return {integer} The number of rows in this terminal.
1407 */
1408hterm.Terminal.prototype.getRowCount = function() {
1409 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1410};
1411
1412/**
1413 * Create DOM nodes for new rows and append them to the end of the terminal.
1414 *
1415 * This is the only correct way to add a new DOM node for a row. Notice that
1416 * the new row is appended to the bottom of the list of rows, and does not
1417 * require renumbering (of the rowIndex property) of previous rows.
1418 *
1419 * If you think you want a new blank row somewhere in the middle of the
1420 * terminal, look into moveRows_().
1421 *
1422 * This method does not pay attention to vtScrollTop/Bottom, since you should
1423 * be using moveRows() in cases where they would matter.
1424 *
1425 * The cursor will be positioned at column 0 of the first inserted line.
1426 */
1427hterm.Terminal.prototype.appendRows_ = function(count) {
1428 var cursorRow = this.screen_.rowsArray.length;
1429 var offset = this.scrollbackRows_.length + cursorRow;
1430 for (var i = 0; i < count; i++) {
1431 var row = this.document_.createElement('x-row');
1432 row.appendChild(this.document_.createTextNode(''));
1433 row.rowIndex = offset + i;
1434 this.screen_.pushRow(row);
1435 }
1436
1437 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1438 if (extraRows > 0) {
1439 var ary = this.screen_.shiftRows(extraRows);
1440 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001441 if (this.scrollPort_.isScrolledEnd)
1442 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001443 }
1444
1445 if (cursorRow >= this.screen_.rowsArray.length)
1446 cursorRow = this.screen_.rowsArray.length - 1;
1447
rginda87b86462011-12-14 13:48:03 -08001448 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001449};
1450
1451/**
1452 * Relocate rows from one part of the addressable screen to another.
1453 *
1454 * This is used to recycle rows during VT scrolls (those which are driven
1455 * by VT commands, rather than by the user manipulating the scrollbar.)
1456 *
1457 * In this case, the blank lines scrolled into the scroll region are made of
1458 * the nodes we scrolled off. These have their rowIndex properties carefully
1459 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001460 */
1461hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1462 var ary = this.screen_.removeRows(fromIndex, count);
1463 this.screen_.insertRows(toIndex, ary);
1464
1465 var start, end;
1466 if (fromIndex < toIndex) {
1467 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001468 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001469 } else {
1470 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001471 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001472 }
1473
1474 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001475 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001476};
1477
1478/**
1479 * Renumber the rowIndex property of the given range of rows.
1480 *
1481 * The start and end indicies are relative to the screen, not the scrollback.
1482 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001483 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001484 * no need to renumber scrollback rows.
1485 */
Robert Ginda40932892012-12-10 17:26:40 -08001486hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1487 var screen = opt_screen || this.screen_;
1488
rginda8ba33642011-12-14 12:31:31 -08001489 var offset = this.scrollbackRows_.length;
1490 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001491 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001492 }
1493};
1494
1495/**
1496 * Print a string to the terminal.
1497 *
1498 * This respects the current insert and wraparound modes. It will add new lines
1499 * to the end of the terminal, scrolling off the top into the scrollback buffer
1500 * if necessary.
1501 *
1502 * The string is *not* parsed for escape codes. Use the interpret() method if
1503 * that's what you're after.
1504 *
1505 * @param{string} str The string to print.
1506 */
1507hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001508 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001509
Ricky Liang48f05cb2013-12-31 23:35:29 +08001510 var strWidth = lib.wc.strWidth(str);
1511
1512 while (startOffset < strWidth) {
rgindaa09e7332012-08-17 12:49:51 -07001513 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1514 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001515 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001516 }
rgindaa19afe22012-01-25 15:40:22 -08001517
Ricky Liang48f05cb2013-12-31 23:35:29 +08001518 var count = strWidth - startOffset;
rgindaa9abdd82012-08-06 18:05:09 -07001519 var didOverflow = false;
1520 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001521
rgindaa9abdd82012-08-06 18:05:09 -07001522 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1523 didOverflow = true;
1524 count = this.screenSize.width - this.screen_.cursorPosition.column;
1525 }
rgindaa19afe22012-01-25 15:40:22 -08001526
rgindaa9abdd82012-08-06 18:05:09 -07001527 if (didOverflow && !this.options_.wraparound) {
1528 // If the string overflowed the line but wraparound is off, then the
1529 // last printed character should be the last of the string.
1530 // TODO: This will add to our problems with multibyte UTF-16 characters.
Ricky Liang48f05cb2013-12-31 23:35:29 +08001531 substr = lib.wc.substr(str, startOffset, count - 1) +
1532 lib.wc.substr(str, strWidth - 1);
1533 count = strWidth;
rgindaa9abdd82012-08-06 18:05:09 -07001534 } else {
Ricky Liang48f05cb2013-12-31 23:35:29 +08001535 substr = lib.wc.substr(str, startOffset, count);
rgindaa9abdd82012-08-06 18:05:09 -07001536 }
rgindaa19afe22012-01-25 15:40:22 -08001537
Ricky Liang48f05cb2013-12-31 23:35:29 +08001538 var tokens = hterm.TextAttributes.splitWidecharString(substr);
1539 for (var i = 0; i < tokens.length; i++) {
1540 if (tokens[i].wcNode)
1541 this.screen_.textAttributes.wcNode = true;
1542
1543 if (this.options_.insertMode) {
1544 this.screen_.insertString(tokens[i].str);
1545 } else {
1546 this.screen_.overwriteString(tokens[i].str);
1547 }
1548 this.screen_.textAttributes.wcNode = false;
rgindaa9abdd82012-08-06 18:05:09 -07001549 }
1550
1551 this.screen_.maybeClipCurrentRow();
1552 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001553 }
rginda8ba33642011-12-14 12:31:31 -08001554
1555 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001556
rginda9f5222b2012-03-05 11:53:28 -08001557 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001558 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001559};
1560
1561/**
rginda87b86462011-12-14 13:48:03 -08001562 * Set the VT scroll region.
1563 *
rginda87b86462011-12-14 13:48:03 -08001564 * This also resets the cursor position to the absolute (0, 0) position, since
1565 * that's what xterm appears to do.
1566 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001567 * Setting the scroll region to the full height of the terminal will clear
1568 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1569 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1570 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1571 * continue to work as most users would expect.
1572 *
rginda87b86462011-12-14 13:48:03 -08001573 * @param {integer} scrollTop The zero-based top of the scroll region.
1574 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1575 * inclusive.
1576 */
1577hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001578 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001579 this.vtScrollTop_ = null;
1580 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001581 } else {
1582 this.vtScrollTop_ = scrollTop;
1583 this.vtScrollBottom_ = scrollBottom;
1584 }
rginda87b86462011-12-14 13:48:03 -08001585};
1586
1587/**
rginda8ba33642011-12-14 12:31:31 -08001588 * Return the top row index according to the VT.
1589 *
1590 * This will return 0 unless the terminal has been told to restrict scrolling
1591 * to some lower row. It is used for some VT cursor positioning and scrolling
1592 * commands.
1593 *
1594 * @return {integer} The topmost row in the terminal's scroll region.
1595 */
1596hterm.Terminal.prototype.getVTScrollTop = function() {
1597 if (this.vtScrollTop_ != null)
1598 return this.vtScrollTop_;
1599
1600 return 0;
rginda87b86462011-12-14 13:48:03 -08001601};
rginda8ba33642011-12-14 12:31:31 -08001602
1603/**
1604 * Return the bottom row index according to the VT.
1605 *
1606 * This will return the height of the terminal unless the it has been told to
1607 * restrict scrolling to some higher row. It is used for some VT cursor
1608 * positioning and scrolling commands.
1609 *
1610 * @return {integer} The bottommost row in the terminal's scroll region.
1611 */
1612hterm.Terminal.prototype.getVTScrollBottom = function() {
1613 if (this.vtScrollBottom_ != null)
1614 return this.vtScrollBottom_;
1615
rginda87b86462011-12-14 13:48:03 -08001616 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001617}
1618
1619/**
1620 * Process a '\n' character.
1621 *
1622 * If the cursor is on the final row of the terminal this will append a new
1623 * blank row to the screen and scroll the topmost row into the scrollback
1624 * buffer.
1625 *
1626 * Otherwise, this moves the cursor to column zero of the next row.
1627 */
1628hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001629 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1630 this.screen_.rowsArray.length - 1);
1631
1632 if (this.vtScrollBottom_ != null) {
1633 // A VT Scroll region is active, we never append new rows.
1634 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1635 // We're at the end of the VT Scroll Region, perform a VT scroll.
1636 this.vtScrollUp(1);
1637 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1638 } else if (cursorAtEndOfScreen) {
1639 // We're at the end of the screen, the only thing to do is put the
1640 // cursor to column 0.
1641 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1642 } else {
1643 // Anywhere else, advance the cursor row, and reset the column.
1644 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1645 }
1646 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001647 // We're at the end of the screen. Append a new row to the terminal,
1648 // shifting the top row into the scrollback.
1649 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001650 } else {
rginda87b86462011-12-14 13:48:03 -08001651 // Anywhere else in the screen just moves the cursor.
1652 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001653 }
1654};
1655
1656/**
1657 * Like newLine(), except maintain the cursor column.
1658 */
1659hterm.Terminal.prototype.lineFeed = function() {
1660 var column = this.screen_.cursorPosition.column;
1661 this.newLine();
1662 this.setCursorColumn(column);
1663};
1664
1665/**
rginda87b86462011-12-14 13:48:03 -08001666 * If autoCarriageReturn is set then newLine(), else lineFeed().
1667 */
1668hterm.Terminal.prototype.formFeed = function() {
1669 if (this.options_.autoCarriageReturn) {
1670 this.newLine();
1671 } else {
1672 this.lineFeed();
1673 }
1674};
1675
1676/**
1677 * Move the cursor up one row, possibly inserting a blank line.
1678 *
1679 * The cursor column is not changed.
1680 */
1681hterm.Terminal.prototype.reverseLineFeed = function() {
1682 var scrollTop = this.getVTScrollTop();
1683 var currentRow = this.screen_.cursorPosition.row;
1684
1685 if (currentRow == scrollTop) {
1686 this.insertLines(1);
1687 } else {
1688 this.setAbsoluteCursorRow(currentRow - 1);
1689 }
1690};
1691
1692/**
rginda8ba33642011-12-14 12:31:31 -08001693 * Replace all characters to the left of the current cursor with the space
1694 * character.
1695 *
1696 * TODO(rginda): This should probably *remove* the characters (not just replace
1697 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001698 * position.
rginda8ba33642011-12-14 12:31:31 -08001699 */
1700hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001701 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001702 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001703 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001704 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001705};
1706
1707/**
David Benjamin684a9b72012-05-01 17:19:58 -04001708 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001709 *
1710 * The cursor position is unchanged.
1711 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001712 * If the current background color is not the default background color this
1713 * will insert spaces rather than delete. This is unfortunate because the
1714 * trailing space will affect text selection, but it's difficult to come up
1715 * with a way to style empty space that wouldn't trip up the hterm.Screen
1716 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001717 *
1718 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1719 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1720 * crbug.com/232390 for details.
rginda8ba33642011-12-14 12:31:31 -08001721 */
1722hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001723 if (this.screen_.cursorPosition.overflow)
1724 return;
1725
Robert Ginda7fd57082012-09-25 14:41:47 -07001726 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1727 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001728
1729 if (this.screen_.textAttributes.background ===
1730 this.screen_.textAttributes.DEFAULT_COLOR) {
1731 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
Ricky Liang48f05cb2013-12-31 23:35:29 +08001732 if (hterm.TextAttributes.nodeWidth(cursorRow) <=
Robert Gindaf2547f12012-10-25 20:36:21 -07001733 this.screen_.cursorPosition.column + count) {
1734 this.screen_.deleteChars(count);
1735 this.clearCursorOverflow();
1736 return;
1737 }
1738 }
1739
rginda87b86462011-12-14 13:48:03 -08001740 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001741 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001742 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001743 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001744};
1745
1746/**
1747 * Erase the current line.
1748 *
1749 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001750 */
1751hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001752 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001753 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001754 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001755 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001756};
1757
1758/**
David Benjamina08d78f2012-05-05 00:28:49 -04001759 * Erase all characters from the start of the screen to the current cursor
1760 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001761 *
1762 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001763 */
1764hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001765 var cursor = this.saveCursor();
1766
1767 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001768
David Benjamina08d78f2012-05-05 00:28:49 -04001769 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001770 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001771 this.screen_.clearCursorRow();
1772 }
1773
rginda87b86462011-12-14 13:48:03 -08001774 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001775 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001776};
1777
1778/**
1779 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001780 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001781 *
1782 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001783 */
1784hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001785 var cursor = this.saveCursor();
1786
1787 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001788
David Benjamina08d78f2012-05-05 00:28:49 -04001789 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001790 for (var i = cursor.row + 1; i <= bottom; i++) {
1791 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001792 this.screen_.clearCursorRow();
1793 }
1794
rginda87b86462011-12-14 13:48:03 -08001795 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001796 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001797};
1798
1799/**
1800 * Fill the terminal with a given character.
1801 *
1802 * This methods does not respect the VT scroll region.
1803 *
1804 * @param {string} ch The character to use for the fill.
1805 */
1806hterm.Terminal.prototype.fill = function(ch) {
1807 var cursor = this.saveCursor();
1808
1809 this.setAbsoluteCursorPosition(0, 0);
1810 for (var row = 0; row < this.screenSize.height; row++) {
1811 for (var col = 0; col < this.screenSize.width; col++) {
1812 this.setAbsoluteCursorPosition(row, col);
1813 this.screen_.overwriteString(ch);
1814 }
1815 }
1816
1817 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001818};
1819
1820/**
rginda9ea433c2012-03-16 11:57:00 -07001821 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001822 *
rginda9ea433c2012-03-16 11:57:00 -07001823 * This does not respect the scroll region.
1824 *
1825 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1826 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001827 */
rginda9ea433c2012-03-16 11:57:00 -07001828hterm.Terminal.prototype.clearHome = function(opt_screen) {
1829 var screen = opt_screen || this.screen_;
1830 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001831
rginda11057d52012-04-25 12:29:56 -07001832 if (bottom == 0) {
1833 // Empty screen, nothing to do.
1834 return;
1835 }
1836
rgindae4d29232012-01-19 10:47:13 -08001837 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001838 screen.setCursorPosition(i, 0);
1839 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001840 }
1841
rginda9ea433c2012-03-16 11:57:00 -07001842 screen.setCursorPosition(0, 0);
1843};
1844
1845/**
1846 * Erase the entire display without changing the cursor position.
1847 *
1848 * The cursor position is unchanged. This does not respect the scroll
1849 * region.
1850 *
1851 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1852 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001853 */
1854hterm.Terminal.prototype.clear = function(opt_screen) {
1855 var screen = opt_screen || this.screen_;
1856 var cursor = screen.cursorPosition.clone();
1857 this.clearHome(screen);
1858 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001859};
1860
1861/**
1862 * VT command to insert lines at the current cursor row.
1863 *
1864 * This respects the current scroll region. Rows pushed off the bottom are
1865 * lost (they won't show up in the scrollback buffer).
1866 *
rginda8ba33642011-12-14 12:31:31 -08001867 * @param {integer} count The number of lines to insert.
1868 */
1869hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07001870 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08001871
1872 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07001873 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08001874
Robert Ginda579186b2012-09-26 11:40:04 -07001875 // The moveCount is the number of rows we need to relocate to make room for
1876 // the new row(s). The count is the distance to move them.
1877 var moveCount = bottom - cursorRow - count + 1;
1878 if (moveCount)
1879 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08001880
Robert Ginda579186b2012-09-26 11:40:04 -07001881 for (var i = count - 1; i >= 0; i--) {
1882 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001883 this.screen_.clearCursorRow();
1884 }
rginda8ba33642011-12-14 12:31:31 -08001885};
1886
1887/**
1888 * VT command to delete lines at the current cursor row.
1889 *
1890 * New rows are added to the bottom of scroll region to take their place. New
1891 * rows are strictly there to take up space and have no content or style.
1892 */
1893hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001894 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001895
rginda87b86462011-12-14 13:48:03 -08001896 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001897 var bottom = this.getVTScrollBottom();
1898
rginda87b86462011-12-14 13:48:03 -08001899 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001900 count = Math.min(count, maxCount);
1901
rginda87b86462011-12-14 13:48:03 -08001902 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001903 if (count != maxCount)
1904 this.moveRows_(top, count, moveStart);
1905
1906 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001907 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001908 this.screen_.clearCursorRow();
1909 }
1910
rginda87b86462011-12-14 13:48:03 -08001911 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001912 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001913};
1914
1915/**
1916 * Inserts the given number of spaces at the current cursor position.
1917 *
rginda87b86462011-12-14 13:48:03 -08001918 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001919 */
1920hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001921 var cursor = this.saveCursor();
1922
rgindacbbd7482012-06-13 15:06:16 -07001923 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001924 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001925 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001926
1927 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001928 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001929};
1930
1931/**
1932 * Forward-delete the specified number of characters starting at the cursor
1933 * position.
1934 *
1935 * @param {integer} count The number of characters to delete.
1936 */
1937hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001938 var deleted = this.screen_.deleteChars(count);
1939 if (deleted && !this.screen_.textAttributes.isDefault()) {
1940 var cursor = this.saveCursor();
1941 this.setCursorColumn(this.screenSize.width - deleted);
1942 this.screen_.insertString(lib.f.getWhitespace(deleted));
1943 this.restoreCursor(cursor);
1944 }
1945
David Benjamin54e8bf62012-06-01 22:31:40 -04001946 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001947};
1948
1949/**
1950 * Shift rows in the scroll region upwards by a given number of lines.
1951 *
1952 * New rows are inserted at the bottom of the scroll region to fill the
1953 * vacated rows. The new rows not filled out with the current text attributes.
1954 *
1955 * This function does not affect the scrollback rows at all. Rows shifted
1956 * off the top are lost.
1957 *
rginda87b86462011-12-14 13:48:03 -08001958 * The cursor position is not altered.
1959 *
rginda8ba33642011-12-14 12:31:31 -08001960 * @param {integer} count The number of rows to scroll.
1961 */
1962hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001963 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001964
rginda87b86462011-12-14 13:48:03 -08001965 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001966 this.deleteLines(count);
1967
rginda87b86462011-12-14 13:48:03 -08001968 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001969};
1970
1971/**
1972 * Shift rows below the cursor down by a given number of lines.
1973 *
1974 * This function respects the current scroll region.
1975 *
1976 * New rows are inserted at the top of the scroll region to fill the
1977 * vacated rows. The new rows not filled out with the current text attributes.
1978 *
1979 * This function does not affect the scrollback rows at all. Rows shifted
1980 * off the bottom are lost.
1981 *
1982 * @param {integer} count The number of rows to scroll.
1983 */
1984hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001985 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001986
rginda87b86462011-12-14 13:48:03 -08001987 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001988 this.insertLines(opt_count);
1989
rginda87b86462011-12-14 13:48:03 -08001990 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001991};
1992
rginda87b86462011-12-14 13:48:03 -08001993
rginda8ba33642011-12-14 12:31:31 -08001994/**
1995 * Set the cursor position.
1996 *
1997 * The cursor row is relative to the scroll region if the terminal has
1998 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1999 *
2000 * @param {integer} row The new zero-based cursor row.
2001 * @param {integer} row The new zero-based cursor column.
2002 */
2003hterm.Terminal.prototype.setCursorPosition = function(row, column) {
2004 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08002005 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002006 } else {
rginda87b86462011-12-14 13:48:03 -08002007 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08002008 }
rginda87b86462011-12-14 13:48:03 -08002009};
rginda8ba33642011-12-14 12:31:31 -08002010
rginda87b86462011-12-14 13:48:03 -08002011hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
2012 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07002013 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
2014 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08002015 this.screen_.setCursorPosition(row, column);
2016};
2017
2018hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07002019 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
2020 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002021 this.screen_.setCursorPosition(row, column);
2022};
2023
2024/**
2025 * Set the cursor column.
2026 *
2027 * @param {integer} column The new zero-based cursor column.
2028 */
2029hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08002030 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08002031};
2032
2033/**
2034 * Return the cursor column.
2035 *
2036 * @return {integer} The zero-based cursor column.
2037 */
2038hterm.Terminal.prototype.getCursorColumn = function() {
2039 return this.screen_.cursorPosition.column;
2040};
2041
2042/**
2043 * Set the cursor row.
2044 *
2045 * The cursor row is relative to the scroll region if the terminal has
2046 * 'origin mode' enabled, or relative to the addressable screen otherwise.
2047 *
2048 * @param {integer} row The new cursor row.
2049 */
rginda87b86462011-12-14 13:48:03 -08002050hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
2051 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08002052};
2053
2054/**
2055 * Return the cursor row.
2056 *
2057 * @return {integer} The zero-based cursor row.
2058 */
2059hterm.Terminal.prototype.getCursorRow = function(row) {
2060 return this.screen_.cursorPosition.row;
2061};
2062
2063/**
2064 * Request that the ScrollPort redraw itself soon.
2065 *
2066 * The redraw will happen asynchronously, soon after the call stack winds down.
2067 * Multiple calls will be coalesced into a single redraw.
2068 */
2069hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08002070 if (this.timeouts_.redraw)
2071 return;
rginda8ba33642011-12-14 12:31:31 -08002072
2073 var self = this;
rginda87b86462011-12-14 13:48:03 -08002074 this.timeouts_.redraw = setTimeout(function() {
2075 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08002076 self.scrollPort_.redraw_();
2077 }, 0);
2078};
2079
2080/**
2081 * Request that the ScrollPort be scrolled to the bottom.
2082 *
2083 * The scroll will happen asynchronously, soon after the call stack winds down.
2084 * Multiple calls will be coalesced into a single scroll.
2085 *
2086 * This affects the scrollbar position of the ScrollPort, and has nothing to
2087 * do with the VT scroll commands.
2088 */
2089hterm.Terminal.prototype.scheduleScrollDown_ = function() {
2090 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08002091 return;
rginda8ba33642011-12-14 12:31:31 -08002092
2093 var self = this;
2094 this.timeouts_.scrollDown = setTimeout(function() {
2095 delete self.timeouts_.scrollDown;
2096 self.scrollPort_.scrollRowToBottom(self.getRowCount());
2097 }, 10);
2098};
2099
2100/**
2101 * Move the cursor up a specified number of rows.
2102 *
2103 * @param {integer} count The number of rows to move the cursor.
2104 */
2105hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002106 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08002107};
2108
2109/**
2110 * Move the cursor down a specified number of rows.
2111 *
2112 * @param {integer} count The number of rows to move the cursor.
2113 */
2114hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002115 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08002116 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
2117 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
2118 this.screenSize.height - 1);
2119
rgindacbbd7482012-06-13 15:06:16 -07002120 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08002121 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08002122 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08002123};
2124
2125/**
2126 * Move the cursor left a specified number of columns.
2127 *
Robert Gindaaaba6132014-07-16 16:33:07 -07002128 * If reverse wraparound mode is enabled and the previous row wrapped into
2129 * the current row then we back up through the wraparound as well.
2130 *
rginda8ba33642011-12-14 12:31:31 -08002131 * @param {integer} count The number of columns to move the cursor.
2132 */
2133hterm.Terminal.prototype.cursorLeft = function(count) {
Robert Gindaaaba6132014-07-16 16:33:07 -07002134 count = count || 1;
2135
2136 if (count < 1)
2137 return;
2138
2139 var currentColumn = this.screen_.cursorPosition.column;
Robert Gindabfb32622014-07-17 13:20:27 -07002140 if (this.options_.reverseWraparound) {
2141 if (this.screen_.cursorPosition.overflow) {
2142 // If this cursor is in the right margin, consume one count to get it
2143 // back to the last column. This only applies when we're in reverse
2144 // wraparound mode.
2145 count--;
2146 this.clearCursorOverflow();
2147
2148 if (!count)
Robert Gindaaaba6132014-07-16 16:33:07 -07002149 return;
Robert Gindaaaba6132014-07-16 16:33:07 -07002150 }
2151
Robert Gindabfb32622014-07-17 13:20:27 -07002152 var newRow = this.screen_.cursorPosition.row;
2153 var newColumn = currentColumn - count;
2154 if (newColumn < 0) {
2155 newRow = newRow - Math.floor(count / this.screenSize.width) - 1;
2156 if (newRow < 0) {
2157 // xterm also wraps from row 0 to the last row.
2158 newRow = this.screenSize.height + newRow % this.screenSize.height;
2159 }
2160 newColumn = this.screenSize.width + newColumn % this.screenSize.width;
2161 }
Robert Gindaaaba6132014-07-16 16:33:07 -07002162
Robert Gindabfb32622014-07-17 13:20:27 -07002163 this.setCursorPosition(Math.max(newRow, 0), newColumn);
2164
2165 } else {
2166 var newColumn = Math.max(currentColumn - count, 0);
2167 this.setCursorColumn(newColumn);
2168 }
rginda8ba33642011-12-14 12:31:31 -08002169};
2170
2171/**
2172 * Move the cursor right a specified number of columns.
2173 *
2174 * @param {integer} count The number of columns to move the cursor.
2175 */
2176hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08002177 count = count || 1;
Robert Gindaaaba6132014-07-16 16:33:07 -07002178
2179 if (count < 1)
2180 return;
2181
rgindacbbd7482012-06-13 15:06:16 -07002182 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08002183 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08002184 this.setCursorColumn(column);
2185};
2186
2187/**
2188 * Reverse the foreground and background colors of the terminal.
2189 *
2190 * This only affects text that was drawn with no attributes.
2191 *
2192 * TODO(rginda): Test xterm to see if reverse is respected for text that has
2193 * been drawn with attributes that happen to coincide with the default
2194 * 'no-attribute' colors. My guess is probably not.
2195 */
2196hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08002197 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08002198 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08002199 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
2200 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08002201 } else {
rginda9f5222b2012-03-05 11:53:28 -08002202 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
2203 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08002204 }
2205};
2206
2207/**
rginda87b86462011-12-14 13:48:03 -08002208 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07002209 *
2210 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08002211 */
2212hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08002213 this.cursorNode_.style.backgroundColor =
2214 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08002215
2216 var self = this;
2217 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08002218 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08002219 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07002220
Michael Kelly485ecd12014-06-09 11:41:56 -04002221 // bellSquelchTimeout_ affects both audio and notification bells.
2222 if (this.bellSquelchTimeout_)
2223 return;
2224
Robert Ginda92e18102013-03-14 13:56:37 -07002225 if (this.bellAudio_.getAttribute('src')) {
Robert Ginda92e18102013-03-14 13:56:37 -07002226 this.bellAudio_.play();
Robert Ginda92e18102013-03-14 13:56:37 -07002227 this.bellSequelchTimeout_ = setTimeout(function() {
2228 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07002229 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07002230 } else {
2231 delete this.bellSquelchTimeout_;
2232 }
Michael Kelly485ecd12014-06-09 11:41:56 -04002233
2234 if (this.desktopNotificationBell_ && !this.document_.hasFocus()) {
2235 var n = new Notification(
2236 lib.f.replaceVars(hterm.desktopNotificationTitle,
Robert Ginda348dc2b2014-06-24 14:42:23 -07002237 {'title': window.document.title || 'hterm'}));
Michael Kelly485ecd12014-06-09 11:41:56 -04002238 this.bellNotificationList_.push(n);
2239 // TODO: Should we try to raise the window here?
2240 n.onclick = function() { self.closeBellNotifications_(); };
2241 }
rginda87b86462011-12-14 13:48:03 -08002242};
2243
2244/**
rginda8ba33642011-12-14 12:31:31 -08002245 * Set the origin mode bit.
2246 *
2247 * If origin mode is on, certain VT cursor and scrolling commands measure their
2248 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2249 * to the top of the addressable screen.
2250 *
2251 * Defaults to off.
2252 *
2253 * @param {boolean} state True to set origin mode, false to unset.
2254 */
2255hterm.Terminal.prototype.setOriginMode = function(state) {
2256 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002257 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002258};
2259
2260/**
2261 * Set the insert mode bit.
2262 *
2263 * If insert mode is on, existing text beyond the cursor position will be
2264 * shifted right to make room for new text. Otherwise, new text overwrites
2265 * any existing text.
2266 *
2267 * Defaults to off.
2268 *
2269 * @param {boolean} state True to set insert mode, false to unset.
2270 */
2271hterm.Terminal.prototype.setInsertMode = function(state) {
2272 this.options_.insertMode = state;
2273};
2274
2275/**
rginda87b86462011-12-14 13:48:03 -08002276 * Set the auto carriage return bit.
2277 *
2278 * If auto carriage return is on then a formfeed character is interpreted
2279 * as a newline, otherwise it's the same as a linefeed. The difference boils
2280 * down to whether or not the cursor column is reset.
2281 */
2282hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2283 this.options_.autoCarriageReturn = state;
2284};
2285
2286/**
rginda8ba33642011-12-14 12:31:31 -08002287 * Set the wraparound mode bit.
2288 *
2289 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2290 * to the start of the following row. Otherwise, the cursor is clamped to the
2291 * end of the screen and attempts to write past it are ignored.
2292 *
2293 * Defaults to on.
2294 *
2295 * @param {boolean} state True to set wraparound mode, false to unset.
2296 */
2297hterm.Terminal.prototype.setWraparound = function(state) {
2298 this.options_.wraparound = state;
2299};
2300
2301/**
2302 * Set the reverse-wraparound mode bit.
2303 *
2304 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2305 * to the end of the previous row. Otherwise, the cursor is clamped to column
2306 * 0.
2307 *
2308 * Defaults to off.
2309 *
2310 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2311 */
2312hterm.Terminal.prototype.setReverseWraparound = function(state) {
2313 this.options_.reverseWraparound = state;
2314};
2315
2316/**
2317 * Selects between the primary and alternate screens.
2318 *
2319 * If alternate mode is on, the alternate screen is active. Otherwise the
2320 * primary screen is active.
2321 *
2322 * Swapping screens has no effect on the scrollback buffer.
2323 *
2324 * Each screen maintains its own cursor position.
2325 *
2326 * Defaults to off.
2327 *
2328 * @param {boolean} state True to set alternate mode, false to unset.
2329 */
2330hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002331 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002332 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2333
rginda35c456b2012-02-09 17:29:05 -08002334 if (this.screen_.rowsArray.length &&
2335 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2336 // If the screen changed sizes while we were away, our rowIndexes may
2337 // be incorrect.
2338 var offset = this.scrollbackRows_.length;
2339 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002340 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002341 ary[i].rowIndex = offset + i;
2342 }
2343 }
rginda8ba33642011-12-14 12:31:31 -08002344
rginda35c456b2012-02-09 17:29:05 -08002345 this.realizeWidth_(this.screenSize.width);
2346 this.realizeHeight_(this.screenSize.height);
2347 this.scrollPort_.syncScrollHeight();
2348 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002349
rginda6d397402012-01-17 10:58:29 -08002350 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002351 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002352};
2353
2354/**
2355 * Set the cursor-blink mode bit.
2356 *
2357 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2358 * a visible cursor does not blink.
2359 *
2360 * You should make sure to turn blinking off if you're going to dispose of a
2361 * terminal, otherwise you'll leak a timeout.
2362 *
2363 * Defaults to on.
2364 *
2365 * @param {boolean} state True to set cursor-blink mode, false to unset.
2366 */
2367hterm.Terminal.prototype.setCursorBlink = function(state) {
2368 this.options_.cursorBlink = state;
2369
2370 if (!state && this.timeouts_.cursorBlink) {
2371 clearTimeout(this.timeouts_.cursorBlink);
2372 delete this.timeouts_.cursorBlink;
2373 }
2374
2375 if (this.options_.cursorVisible)
2376 this.setCursorVisible(true);
2377};
2378
2379/**
2380 * Set the cursor-visible mode bit.
2381 *
2382 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2383 *
2384 * Defaults to on.
2385 *
2386 * @param {boolean} state True to set cursor-visible mode, false to unset.
2387 */
2388hterm.Terminal.prototype.setCursorVisible = function(state) {
2389 this.options_.cursorVisible = state;
2390
2391 if (!state) {
Brad Town1c2afa82015-03-11 21:36:58 -07002392 if (this.timeouts_.cursorBlink) {
2393 clearTimeout(this.timeouts_.cursorBlink);
2394 delete this.timeouts_.cursorBlink;
2395 }
rginda87b86462011-12-14 13:48:03 -08002396 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002397 return;
2398 }
2399
rginda87b86462011-12-14 13:48:03 -08002400 this.syncCursorPosition_();
2401
2402 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002403
2404 if (this.options_.cursorBlink) {
2405 if (this.timeouts_.cursorBlink)
2406 return;
2407
Robert Gindaea2183e2014-07-17 09:51:51 -07002408 this.onCursorBlink_();
rginda8ba33642011-12-14 12:31:31 -08002409 } else {
2410 if (this.timeouts_.cursorBlink) {
2411 clearTimeout(this.timeouts_.cursorBlink);
2412 delete this.timeouts_.cursorBlink;
2413 }
2414 }
2415};
2416
2417/**
rginda87b86462011-12-14 13:48:03 -08002418 * Synchronizes the visible cursor and document selection with the current
2419 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002420 */
2421hterm.Terminal.prototype.syncCursorPosition_ = function() {
2422 var topRowIndex = this.scrollPort_.getTopRowIndex();
2423 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2424 var cursorRowIndex = this.scrollbackRows_.length +
2425 this.screen_.cursorPosition.row;
2426
2427 if (cursorRowIndex > bottomRowIndex) {
2428 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002429 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002430 return;
2431 }
2432
Robert Gindab837c052014-08-11 11:17:51 -07002433 if (this.options_.cursorVisible &&
2434 this.cursorNode_.style.display == 'none') {
2435 // Re-display the terminal cursor if it was hidden by the mouse cursor.
2436 this.cursorNode_.style.display = '';
2437 }
2438
2439
rginda8ba33642011-12-14 12:31:31 -08002440 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002441 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2442 'px';
2443 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2444 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002445
2446 this.cursorNode_.setAttribute('title',
2447 '(' + this.screen_.cursorPosition.row +
2448 ', ' + this.screen_.cursorPosition.column +
2449 ')');
2450
2451 // Update the caret for a11y purposes.
2452 var selection = this.document_.getSelection();
2453 if (selection && selection.isCollapsed)
2454 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002455};
2456
Robert Gindafb1be6a2013-12-11 11:56:22 -08002457/**
2458 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2459 * and character cell dimensions.
2460 */
Robert Ginda830583c2013-08-07 13:20:46 -07002461hterm.Terminal.prototype.restyleCursor_ = function() {
2462 var shape = this.cursorShape_;
2463
2464 if (this.cursorNode_.getAttribute('focus') == 'false') {
2465 // Always show a block cursor when unfocused.
2466 shape = hterm.Terminal.cursorShape.BLOCK;
2467 }
2468
2469 var style = this.cursorNode_.style;
2470
Robert Gindafb1be6a2013-12-11 11:56:22 -08002471 style.width = this.scrollPort_.characterSize.width + 'px';
2472
Robert Ginda830583c2013-08-07 13:20:46 -07002473 switch (shape) {
2474 case hterm.Terminal.cursorShape.BEAM:
2475 style.height = this.scrollPort_.characterSize.height + 'px';
2476 style.backgroundColor = 'transparent';
2477 style.borderBottomStyle = null;
2478 style.borderLeftStyle = 'solid';
2479 break;
2480
2481 case hterm.Terminal.cursorShape.UNDERLINE:
2482 style.height = this.scrollPort_.characterSize.baseline + 'px';
2483 style.backgroundColor = 'transparent';
2484 style.borderBottomStyle = 'solid';
2485 // correct the size to put it exactly at the baseline
2486 style.borderLeftStyle = null;
2487 break;
2488
2489 default:
2490 style.height = this.scrollPort_.characterSize.height + 'px';
2491 style.backgroundColor = this.cursorColor_;
2492 style.borderBottomStyle = null;
2493 style.borderLeftStyle = null;
2494 break;
2495 }
2496};
2497
rginda8ba33642011-12-14 12:31:31 -08002498/**
2499 * Synchronizes the visible cursor with the current cursor coordinates.
2500 *
2501 * The sync will happen asynchronously, soon after the call stack winds down.
2502 * Multiple calls will be coalesced into a single sync.
2503 */
2504hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2505 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002506 return;
rginda8ba33642011-12-14 12:31:31 -08002507
2508 var self = this;
2509 this.timeouts_.syncCursor = setTimeout(function() {
2510 self.syncCursorPosition_();
2511 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002512 }, 0);
2513};
2514
rgindacc2996c2012-02-24 14:59:31 -08002515/**
rgindaf522ce02012-04-17 17:49:17 -07002516 * Show or hide the zoom warning.
2517 *
2518 * The zoom warning is a message warning the user that their browser zoom must
2519 * be set to 100% in order for hterm to function properly.
2520 *
2521 * @param {boolean} state True to show the message, false to hide it.
2522 */
2523hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2524 if (!this.zoomWarningNode_) {
2525 if (!state)
2526 return;
2527
2528 this.zoomWarningNode_ = this.document_.createElement('div');
2529 this.zoomWarningNode_.style.cssText = (
2530 'color: black;' +
2531 'background-color: #ff2222;' +
2532 'font-size: large;' +
2533 'border-radius: 8px;' +
2534 'opacity: 0.75;' +
2535 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2536 'top: 0.5em;' +
2537 'right: 1.2em;' +
2538 'position: absolute;' +
2539 '-webkit-text-size-adjust: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002540 '-webkit-user-select: none;' +
2541 '-moz-text-size-adjust: none;' +
2542 '-moz-user-select: none;');
Mike Frysinger4c0c5e02016-03-12 23:11:25 -05002543
2544 this.zoomWarningNode_.addEventListener('click', function(e) {
2545 this.parentNode.removeChild(this);
2546 });
rgindaf522ce02012-04-17 17:49:17 -07002547 }
2548
Robert Gindab4839c22013-02-28 16:52:10 -08002549 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2550 hterm.zoomWarningMessage,
2551 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2552
rgindaf522ce02012-04-17 17:49:17 -07002553 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2554
2555 if (state) {
2556 if (!this.zoomWarningNode_.parentNode)
2557 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2558 } else if (this.zoomWarningNode_.parentNode) {
2559 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2560 }
2561};
2562
2563/**
rgindacc2996c2012-02-24 14:59:31 -08002564 * Show the terminal overlay for a given amount of time.
2565 *
2566 * The terminal overlay appears in inverse video in a large font, centered
2567 * over the terminal. You should probably keep the overlay message brief,
2568 * since it's in a large font and you probably aren't going to check the size
2569 * of the terminal first.
2570 *
2571 * @param {string} msg The text (not HTML) message to display in the overlay.
2572 * @param {number} opt_timeout The amount of time to wait before fading out
2573 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2574 * stay up forever (or until the next overlay).
2575 */
2576hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002577 if (!this.overlayNode_) {
2578 if (!this.div_)
2579 return;
2580
2581 this.overlayNode_ = this.document_.createElement('div');
2582 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002583 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002584 'font-size: xx-large;' +
2585 'opacity: 0.75;' +
2586 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2587 'position: absolute;' +
2588 '-webkit-user-select: none;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002589 '-webkit-transition: opacity 180ms ease-in;' +
2590 '-moz-user-select: none;' +
2591 '-moz-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002592
2593 this.overlayNode_.addEventListener('mousedown', function(e) {
2594 e.preventDefault();
2595 e.stopPropagation();
2596 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002597 }
2598
rginda9f5222b2012-03-05 11:53:28 -08002599 this.overlayNode_.style.color = this.prefs_.get('background-color');
2600 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2601 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2602
rgindaf0090c92012-02-10 14:58:52 -08002603 this.overlayNode_.textContent = msg;
2604 this.overlayNode_.style.opacity = '0.75';
2605
2606 if (!this.overlayNode_.parentNode)
2607 this.div_.appendChild(this.overlayNode_);
2608
Robert Ginda97769282013-02-01 15:30:30 -08002609 var divSize = hterm.getClientSize(this.div_);
2610 var overlaySize = hterm.getClientSize(this.overlayNode_);
2611
Robert Ginda8a59f762014-07-23 11:29:55 -07002612 this.overlayNode_.style.top =
2613 (divSize.height - overlaySize.height) / 2 + 'px';
Robert Ginda97769282013-02-01 15:30:30 -08002614 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
Robert Ginda8a59f762014-07-23 11:29:55 -07002615 this.scrollPort_.currentScrollbarWidthPx) / 2 + 'px';
rgindaf0090c92012-02-10 14:58:52 -08002616
2617 var self = this;
2618
2619 if (this.overlayTimeout_)
2620 clearTimeout(this.overlayTimeout_);
2621
rgindacc2996c2012-02-24 14:59:31 -08002622 if (opt_timeout === null)
2623 return;
2624
rgindaf0090c92012-02-10 14:58:52 -08002625 this.overlayTimeout_ = setTimeout(function() {
2626 self.overlayNode_.style.opacity = '0';
Robert Ginda70926e42013-11-25 14:56:36 -08002627 self.overlayTimeout_ = setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002628 if (self.overlayNode_.parentNode)
2629 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002630 self.overlayTimeout_ = null;
2631 self.overlayNode_.style.opacity = '0.75';
2632 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002633 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002634};
2635
rginda4bba5e12012-06-20 16:15:30 -07002636/**
2637 * Paste from the system clipboard to the terminal.
2638 */
2639hterm.Terminal.prototype.paste = function() {
2640 hterm.pasteFromClipboard(this.document_);
2641};
2642
2643/**
2644 * Copy a string to the system clipboard.
2645 *
2646 * Note: If there is a selected range in the terminal, it'll be cleared.
2647 */
2648hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda4e4d42c2014-03-04 14:07:23 -08002649 if (this.prefs_.get('enable-clipboard-notice'))
2650 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
2651
rgindaa09e7332012-08-17 12:49:51 -07002652 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002653 copySource.textContent = str;
2654 copySource.style.cssText = (
2655 '-webkit-user-select: text;' +
Rob Spies06533ba2014-04-24 11:20:37 -07002656 '-moz-user-select: text;' +
rginda4bba5e12012-06-20 16:15:30 -07002657 'position: absolute;' +
2658 'top: -99px');
2659
2660 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002661
rginda4bba5e12012-06-20 16:15:30 -07002662 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002663 var anchorNode = selection.anchorNode;
2664 var anchorOffset = selection.anchorOffset;
2665 var focusNode = selection.focusNode;
2666 var focusOffset = selection.focusOffset;
2667
rginda4bba5e12012-06-20 16:15:30 -07002668 selection.selectAllChildren(copySource);
2669
rgindaa09e7332012-08-17 12:49:51 -07002670 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002671
Rob Spies56953412014-04-28 14:09:47 -07002672 // IE doesn't support selection.extend. This means that the selection
2673 // won't return on IE.
Robert Ginda6f4f0aa2014-07-28 10:21:39 -07002674 if (selection.extend) {
Rob Spies56953412014-04-28 14:09:47 -07002675 selection.collapse(anchorNode, anchorOffset);
2676 selection.extend(focusNode, focusOffset);
2677 }
rgindafaa74742012-08-21 13:34:03 -07002678
rginda4bba5e12012-06-20 16:15:30 -07002679 copySource.parentNode.removeChild(copySource);
2680};
2681
rgindaa09e7332012-08-17 12:49:51 -07002682hterm.Terminal.prototype.getSelectionText = function() {
2683 var selection = this.scrollPort_.selection;
2684 selection.sync();
2685
2686 if (selection.isCollapsed)
2687 return null;
2688
2689
2690 // Start offset measures from the beginning of the line.
2691 var startOffset = selection.startOffset;
2692 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002693
Robert Gindafdbb3f22012-09-06 20:23:06 -07002694 if (node.nodeName != 'X-ROW') {
2695 // If the selection doesn't start on an x-row node, then it must be
2696 // somewhere inside the x-row. Add any characters from previous siblings
2697 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002698
2699 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2700 // If node is the text node in a styled span, move up to the span node.
2701 node = node.parentNode;
2702 }
2703
Robert Gindafdbb3f22012-09-06 20:23:06 -07002704 while (node.previousSibling) {
2705 node = node.previousSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002706 startOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002707 }
rgindaa09e7332012-08-17 12:49:51 -07002708 }
2709
2710 // End offset measures from the end of the line.
Ricky Liang48f05cb2013-12-31 23:35:29 +08002711 var endOffset = (hterm.TextAttributes.nodeWidth(selection.endNode) -
2712 selection.endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002713 var node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002714
Robert Gindafdbb3f22012-09-06 20:23:06 -07002715 if (node.nodeName != 'X-ROW') {
2716 // If the selection doesn't end on an x-row node, then it must be
2717 // somewhere inside the x-row. Add any characters from following siblings
2718 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002719
2720 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2721 // If node is the text node in a styled span, move up to the span node.
2722 node = node.parentNode;
2723 }
2724
Robert Gindafdbb3f22012-09-06 20:23:06 -07002725 while (node.nextSibling) {
2726 node = node.nextSibling;
Ricky Liang48f05cb2013-12-31 23:35:29 +08002727 endOffset += hterm.TextAttributes.nodeWidth(node);
Robert Gindafdbb3f22012-09-06 20:23:06 -07002728 }
rgindaa09e7332012-08-17 12:49:51 -07002729 }
2730
2731 var rv = this.getRowsText(selection.startRow.rowIndex,
2732 selection.endRow.rowIndex + 1);
Ricky Liang48f05cb2013-12-31 23:35:29 +08002733 return lib.wc.substring(rv, startOffset, lib.wc.strWidth(rv) - endOffset);
rgindaa09e7332012-08-17 12:49:51 -07002734};
2735
rginda4bba5e12012-06-20 16:15:30 -07002736/**
2737 * Copy the current selection to the system clipboard, then clear it after a
2738 * short delay.
2739 */
2740hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002741 var text = this.getSelectionText();
2742 if (text != null)
2743 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002744};
2745
rgindaf0090c92012-02-10 14:58:52 -08002746hterm.Terminal.prototype.overlaySize = function() {
2747 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2748};
2749
rginda87b86462011-12-14 13:48:03 -08002750/**
2751 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2752 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07002753 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08002754 */
2755hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002756 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002757 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2758
Robert Ginda8cb7d902013-06-20 14:37:18 -07002759 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08002760};
2761
2762/**
rgindad5613292012-06-19 15:40:37 -07002763 * Add the terminalRow and terminalColumn properties to mouse events and
2764 * then forward on to onMouse().
2765 *
2766 * The terminalRow and terminalColumn properties contain the (row, column)
2767 * coordinates for the mouse event.
2768 */
2769hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002770 if (e.processedByTerminalHandler_) {
2771 // We register our event handlers on the document, as well as the cursor
2772 // and the scroll blocker. Mouse events that occur on the cursor or
2773 // scroll blocker will also appear on the document, but we don't want to
2774 // process them twice.
2775 //
2776 // We can't just prevent bubbling because that has other side effects, so
2777 // we decorate the event object with this property instead.
2778 return;
2779 }
2780
Robert Ginda6aec7eb2015-06-16 10:31:30 -07002781 var reportMouseEvents = (!this.defeatMouseReports_ &&
2782 this.vt.mouseReport != this.vt.MOUSE_REPORT_DISABLED);
2783
rgindafaa74742012-08-21 13:34:03 -07002784 e.processedByTerminalHandler_ = true;
2785
Robert Gindaeda48db2014-07-17 09:25:30 -07002786 // One based row/column stored on the mouse event.
2787 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2788 this.scrollPort_.characterSize.height) + 1;
2789 e.terminalColumn = parseInt(e.clientX /
2790 this.scrollPort_.characterSize.width) + 1;
2791
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002792 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
2793 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07002794 return;
2795 }
2796
Robert Ginda6aec7eb2015-06-16 10:31:30 -07002797 if (this.options_.cursorVisible && !reportMouseEvents) {
Robert Gindab837c052014-08-11 11:17:51 -07002798 // If the cursor is visible and we're not sending mouse events to the
2799 // host app, then we want to hide the terminal cursor when the mouse
2800 // cursor is over top. This keeps the terminal cursor from interfering
2801 // with local text selection.
Robert Gindaeda48db2014-07-17 09:25:30 -07002802 if (e.terminalRow - 1 == this.screen_.cursorPosition.row &&
2803 e.terminalColumn - 1 == this.screen_.cursorPosition.column) {
2804 this.cursorNode_.style.display = 'none';
2805 } else if (this.cursorNode_.style.display == 'none') {
2806 this.cursorNode_.style.display = '';
2807 }
2808 }
rgindad5613292012-06-19 15:40:37 -07002809
Robert Ginda928cf632014-03-05 15:07:41 -08002810 if (e.type == 'mousedown') {
Robert Ginda6aec7eb2015-06-16 10:31:30 -07002811 if (e.altKey || !reportMouseEvents) {
Robert Ginda928cf632014-03-05 15:07:41 -08002812 // If VT mouse reporting is disabled, or has been defeated with
2813 // alt-mousedown, then the mouse will act on the local selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07002814 this.defeatMouseReports_ = true;
Robert Ginda928cf632014-03-05 15:07:41 -08002815 this.setSelectionEnabled(true);
2816 } else {
2817 // Otherwise we defer ownership of the mouse to the VT.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07002818 this.defeatMouseReports_ = false;
Robert Ginda3ae37822014-05-15 13:05:35 -07002819 this.document_.getSelection().collapseToEnd();
Robert Ginda928cf632014-03-05 15:07:41 -08002820 this.setSelectionEnabled(false);
2821 e.preventDefault();
2822 }
2823 }
2824
Robert Ginda6aec7eb2015-06-16 10:31:30 -07002825 if (!reportMouseEvents) {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002826 if (e.type == 'dblclick') {
2827 this.screen_.expandSelection(this.document_.getSelection());
2828 hterm.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07002829 }
2830
Robert Ginda928cf632014-03-05 15:07:41 -08002831 if (e.type == 'mousedown' && e.which == this.mousePasteButton)
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002832 this.paste();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002833
2834 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2835 !this.document_.getSelection().isCollapsed) {
2836 hterm.copySelectionToClipboard(this.document_);
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002837 }
2838
2839 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
2840 this.scrollBlockerNode_.engaged) {
2841 // Disengage the scroll-blocker after one of these events.
2842 this.scrollBlockerNode_.engaged = false;
2843 this.scrollBlockerNode_.style.top = '-99px';
2844 }
2845
Robert Ginda928cf632014-03-05 15:07:41 -08002846 } else /* if (this.reportMouseEvents) */ {
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002847 if (!this.scrollBlockerNode_.engaged) {
2848 if (e.type == 'mousedown') {
2849 // Move the scroll-blocker into place if we want to keep the scrollport
2850 // from scrolling.
2851 this.scrollBlockerNode_.engaged = true;
2852 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2853 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2854 } else if (e.type == 'mousemove') {
2855 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
2856 // in which case it's too late to engage the scroll-blocker.
Robert Ginda3ae37822014-05-15 13:05:35 -07002857 this.document_.getSelection().collapseToEnd();
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002858 e.preventDefault();
2859 }
2860 }
Robert Ginda928cf632014-03-05 15:07:41 -08002861
2862 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07002863 }
2864
Robert Ginda928cf632014-03-05 15:07:41 -08002865 if (e.type == 'mouseup' && this.document_.getSelection().isCollapsed) {
2866 // Restore this on mouseup in case it was temporarily defeated with a
2867 // alt-mousedown. Only do this when the selection is empty so that
2868 // we don't immediately kill the users selection.
Robert Ginda6aec7eb2015-06-16 10:31:30 -07002869 this.defeatMouseReports_ = false;
Robert Ginda928cf632014-03-05 15:07:41 -08002870 }
rgindad5613292012-06-19 15:40:37 -07002871};
2872
2873/**
2874 * Clients should override this if they care to know about mouse events.
2875 *
2876 * The event parameter will be a normal DOM mouse click event with additional
2877 * 'terminalRow' and 'terminalColumn' properties.
2878 */
2879hterm.Terminal.prototype.onMouse = function(e) { };
2880
2881/**
rginda8e92a692012-05-20 19:37:20 -07002882 * React when focus changes.
2883 */
Rob Spies06533ba2014-04-24 11:20:37 -07002884hterm.Terminal.prototype.onFocusChange_ = function(focused) {
2885 this.cursorNode_.setAttribute('focus', focused);
Robert Ginda830583c2013-08-07 13:20:46 -07002886 this.restyleCursor_();
Michael Kelly485ecd12014-06-09 11:41:56 -04002887 if (focused === true)
2888 this.closeBellNotifications_();
rginda8e92a692012-05-20 19:37:20 -07002889};
2890
2891/**
rginda8ba33642011-12-14 12:31:31 -08002892 * React when the ScrollPort is scrolled.
2893 */
2894hterm.Terminal.prototype.onScroll_ = function() {
2895 this.scheduleSyncCursorPosition_();
2896};
2897
2898/**
rginda9846e2f2012-01-27 13:53:33 -08002899 * React when text is pasted into the scrollPort.
2900 */
2901hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaa063b202014-07-21 11:08:25 -07002902 var data = e.text.replace(/\n/mg, '\r');
Connor Hegartyf525ceb2014-09-03 13:36:03 -07002903 data = this.keyboard.encode(data);
Robert Gindaa063b202014-07-21 11:08:25 -07002904 if (this.options_.bracketedPaste)
2905 data = '\x1b[200~' + data + '\x1b[201~';
2906
2907 this.io.sendString(data);
rginda9846e2f2012-01-27 13:53:33 -08002908};
2909
2910/**
rgindaa09e7332012-08-17 12:49:51 -07002911 * React when the user tries to copy from the scrollPort.
2912 */
2913hterm.Terminal.prototype.onCopy_ = function(e) {
Rob Spies0bec09b2014-06-06 15:58:09 -07002914 if (!this.useDefaultWindowCopy) {
2915 e.preventDefault();
2916 setTimeout(this.copySelectionToClipboard.bind(this), 0);
2917 }
rgindaa09e7332012-08-17 12:49:51 -07002918};
2919
2920/**
rginda8ba33642011-12-14 12:31:31 -08002921 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002922 *
2923 * Note: This function should not directly contain code that alters the internal
2924 * state of the terminal. That kind of code belongs in realizeWidth or
2925 * realizeHeight, so that it can be executed synchronously in the case of a
2926 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002927 */
2928hterm.Terminal.prototype.onResize_ = function() {
Robert Ginda19f61292014-03-04 14:07:57 -08002929 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
Rob Spies0be20252015-07-16 14:36:47 -07002930 this.scrollPort_.characterSize.width) || 0;
Rob Spiesf4e90e82015-01-28 12:10:13 -08002931 var rowCount = lib.f.smartFloorDivide(this.scrollPort_.getScreenHeight(),
Rob Spies0be20252015-07-16 14:36:47 -07002932 this.scrollPort_.characterSize.height) || 0;
rginda35c456b2012-02-09 17:29:05 -08002933
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002934 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08002935 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002936 // gets removed from the document or during the initial load, and we can't
2937 // deal with that.
Rob Spies0be20252015-07-16 14:36:47 -07002938 // This can also happen if called before the scrollPort calculates the
2939 // character size, meaning we dived by 0 above and default to 0 values.
rginda35c456b2012-02-09 17:29:05 -08002940 return;
2941 }
2942
rgindaa8ba17d2012-08-15 14:41:10 -07002943 var isNewSize = (columnCount != this.screenSize.width ||
2944 rowCount != this.screenSize.height);
2945
2946 // We do this even if the size didn't change, just to be sure everything is
2947 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002948 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07002949 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07002950
2951 if (isNewSize)
2952 this.overlaySize();
2953
Robert Gindafb1be6a2013-12-11 11:56:22 -08002954 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07002955 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08002956};
2957
2958/**
2959 * Service the cursor blink timeout.
2960 */
2961hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Gindaea2183e2014-07-17 09:51:51 -07002962 if (!this.options_.cursorBlink) {
2963 delete this.timeouts_.cursorBlink;
2964 return;
2965 }
2966
Robert Ginda830583c2013-08-07 13:20:46 -07002967 if (this.cursorNode_.getAttribute('focus') == 'false' ||
2968 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08002969 this.cursorNode_.style.opacity = '1';
Robert Gindaea2183e2014-07-17 09:51:51 -07002970 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
2971 this.cursorBlinkCycle_[0]);
rginda8ba33642011-12-14 12:31:31 -08002972 } else {
rginda87b86462011-12-14 13:48:03 -08002973 this.cursorNode_.style.opacity = '0';
Robert Gindaea2183e2014-07-17 09:51:51 -07002974 this.timeouts_.cursorBlink = setTimeout(this.myOnCursorBlink_,
2975 this.cursorBlinkCycle_[1]);
rginda8ba33642011-12-14 12:31:31 -08002976 }
2977};
David Reveman8f552492012-03-28 12:18:41 -04002978
2979/**
2980 * Set the scrollbar-visible mode bit.
2981 *
2982 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2983 * Otherwise it will not.
2984 *
2985 * Defaults to on.
2986 *
2987 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2988 */
2989hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2990 this.scrollPort_.setScrollbarVisible(state);
2991};
Michael Kelly485ecd12014-06-09 11:41:56 -04002992
2993/**
Rob Spies49039e52014-12-17 13:40:04 -08002994 * Set the scroll wheel move multiplier. This will affect how fast the page
2995 * scrolls on mousewheel events.
2996 *
2997 * Defaults to 1.
2998 *
2999 * @param {number} multiplier.
3000 */
3001hterm.Terminal.prototype.setScrollWheelMoveMultipler = function(multiplier) {
3002 this.scrollPort_.setScrollWheelMoveMultipler(multiplier);
3003};
3004
3005/**
Michael Kelly485ecd12014-06-09 11:41:56 -04003006 * Close all web notifications created by terminal bells.
3007 */
3008hterm.Terminal.prototype.closeBellNotifications_ = function() {
3009 this.bellNotificationList_.forEach(function(n) {
3010 n.close();
3011 });
3012 this.bellNotificationList_.length = 0;
3013};