blob: 71403399b408bb92d14c29e20f07ae8495821fb5 [file] [log] [blame]
rginda87b86462011-12-14 13:48:03 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rginda8ba33642011-12-14 12:31:31 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rgindacbbd7482012-06-13 15:06:16 -07005'use strict';
6
Robert Gindab4839c22013-02-28 16:52:10 -08007lib.rtdep('lib.colors', 'lib.PreferenceManager', 'lib.resource',
Robert Ginda57f03b42012-09-13 11:02:48 -07008 'hterm.Keyboard', 'hterm.Options', 'hterm.PreferenceManager',
9 'hterm.Screen', 'hterm.ScrollPort', 'hterm.Size', 'hterm.VT');
rgindacbbd7482012-06-13 15:06:16 -070010
rginda8ba33642011-12-14 12:31:31 -080011/**
12 * Constructor for the Terminal class.
13 *
14 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
15 * classes to provide the complete terminal functionality.
16 *
17 * There are a number of lower-level Terminal methods that can be called
18 * directly to manipulate the cursor, text, scroll region, and other terminal
19 * attributes. However, the primary method is interpret(), which parses VT
20 * escape sequences and invokes the appropriate Terminal methods.
21 *
22 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
23 *
24 * TODO(rginda): Eventually we're going to need to support characters which are
25 * displayed twice as wide as standard latin characters. This is to support
26 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080027 *
Robert Ginda57f03b42012-09-13 11:02:48 -070028 * @param {string} opt_profileId Optional preference profile name. If not
rginda9f5222b2012-03-05 11:53:28 -080029 * provided, defaults to 'default'.
rginda8ba33642011-12-14 12:31:31 -080030 */
Robert Ginda57f03b42012-09-13 11:02:48 -070031hterm.Terminal = function(opt_profileId) {
32 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080033
rginda8ba33642011-12-14 12:31:31 -080034 // Two screen instances.
35 this.primaryScreen_ = new hterm.Screen();
36 this.alternateScreen_ = new hterm.Screen();
37
38 // The "current" screen.
39 this.screen_ = this.primaryScreen_;
40
rginda8ba33642011-12-14 12:31:31 -080041 // The local notion of the screen size. ScreenBuffers also have a size which
42 // indicates their present size. During size changes, the two may disagree.
43 // Also, the inactive screen's size is not altered until it is made the active
44 // screen.
45 this.screenSize = new hterm.Size(0, 0);
46
rginda8ba33642011-12-14 12:31:31 -080047 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080048 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080049 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
50 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080051 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070052 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080053
rginda87b86462011-12-14 13:48:03 -080054 // The div that contains this terminal.
55 this.div_ = null;
56
rgindac9bc5502012-01-18 11:48:44 -080057 // The document that contains the scrollPort. Defaulted to the global
58 // document here so that the terminal is functional even if it hasn't been
59 // inserted into a document yet, but re-set in decorate().
60 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080061
rginda8ba33642011-12-14 12:31:31 -080062 // The rows that have scrolled off screen and are no longer addressable.
63 this.scrollbackRows_ = [];
64
rgindac9bc5502012-01-18 11:48:44 -080065 // Saved tab stops.
66 this.tabStops_ = [];
67
David Benjamin66e954d2012-05-05 21:08:12 -040068 // Keep track of whether default tab stops have been erased; after a TBC
69 // clears all tab stops, defaults aren't restored on resize until a reset.
70 this.defaultTabStops = true;
71
rginda8ba33642011-12-14 12:31:31 -080072 // The VT's notion of the top and bottom rows. Used during some VT
73 // cursor positioning and scrolling commands.
74 this.vtScrollTop_ = null;
75 this.vtScrollBottom_ = null;
76
77 // The DIV element for the visible cursor.
78 this.cursorNode_ = null;
79
rginda9f5222b2012-03-05 11:53:28 -080080 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070081 // each output and keystroke. They are initialized by the preference manager.
82 this.scrollOnOutput_ = null;
83 this.scrollOnKeystroke_ = null;
84 this.foregroundColor_ = null;
85 this.backgroundColor_ = null;
rginda9f5222b2012-03-05 11:53:28 -080086
rgindaf0090c92012-02-10 14:58:52 -080087 // Terminal bell sound.
88 this.bellAudio_ = this.document_.createElement('audio');
rgindaf0090c92012-02-10 14:58:52 -080089 this.bellAudio_.setAttribute('preload', 'auto');
90
rginda6d397402012-01-17 10:58:29 -080091 // Cursor position and attributes saved with DECSC.
92 this.savedOptions_ = {};
93
rginda8ba33642011-12-14 12:31:31 -080094 // The current mode bits for the terminal.
95 this.options_ = new hterm.Options();
96
97 // Timeouts we might need to clear.
98 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -080099
100 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800101 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800102
rgindafeaf3142012-01-31 15:14:20 -0800103 // The keyboard hander.
104 this.keyboard = new hterm.Keyboard(this);
105
rginda87b86462011-12-14 13:48:03 -0800106 // General IO interface that can be given to third parties without exposing
107 // the entire terminal object.
108 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800109
rgindad5613292012-06-19 15:40:37 -0700110 // True if mouse-click-drag should scroll the terminal.
111 this.enableMouseDragScroll = true;
112
Robert Ginda57f03b42012-09-13 11:02:48 -0700113 this.copyOnSelect = null;
rginda4bba5e12012-06-20 16:15:30 -0700114 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700115
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400116 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800117 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700118
119 this.setProfile(opt_profileId || 'default',
120 function() { this.onTerminalReady() }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800121};
122
123/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700124 * Clients should override this to be notified when the terminal is ready
125 * for use.
126 *
127 * The terminal initialization is asynchronous, and shouldn't be used before
128 * this method is called.
129 */
130hterm.Terminal.prototype.onTerminalReady = function() { };
131
132/**
rginda35c456b2012-02-09 17:29:05 -0800133 * Default tab with of 8 to match xterm.
134 */
135hterm.Terminal.prototype.tabWidth = 8;
136
137/**
rginda9f5222b2012-03-05 11:53:28 -0800138 * Select a preference profile.
139 *
140 * This will load the terminal preferences for the given profile name and
141 * associate subsequent preference changes with the new preference profile.
142 *
143 * @param {string} newName The name of the preference profile. Forward slash
144 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700145 * @param {function} opt_callback Optional callback to invoke when the profile
146 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800147 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700148hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
149 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800150
Robert Ginda57f03b42012-09-13 11:02:48 -0700151 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800152
Robert Ginda57f03b42012-09-13 11:02:48 -0700153 if (this.prefs_)
154 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800155
Robert Ginda57f03b42012-09-13 11:02:48 -0700156 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
157 this.prefs_.addObservers(null, {
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700158 'alt-backspace-is-meta-backspace': function(v) {
159 terminal.keyboard.altBackspaceIsMetaBackspace = v;
160 },
161
Robert Ginda57f03b42012-09-13 11:02:48 -0700162 'alt-is-meta': function(v) {
163 terminal.keyboard.altIsMeta = v;
164 },
165
166 'alt-sends-what': function(v) {
167 if (!/^(escape|8-bit|browser-key)$/.test(v))
168 v = 'escape';
169
170 terminal.keyboard.altSendsWhat = v;
171 },
172
173 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800174 var ary = v.match(/^lib-resource:(\S+)/);
175 if (ary) {
176 terminal.bellAudio_.setAttribute('src',
177 lib.resource.getDataUrl(ary[1]));
178 } else {
179 terminal.bellAudio_.setAttribute('src', v);
180 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700181 },
182
183 'background-color': function(v) {
184 terminal.setBackgroundColor(v);
185 },
186
187 'background-image': function(v) {
188 terminal.scrollPort_.setBackgroundImage(v);
189 },
190
191 'background-size': function(v) {
192 terminal.scrollPort_.setBackgroundSize(v);
193 },
194
195 'background-position': function(v) {
196 terminal.scrollPort_.setBackgroundPosition(v);
197 },
198
199 'backspace-sends-backspace': function(v) {
200 terminal.keyboard.backspaceSendsBackspace = v;
201 },
202
203 'cursor-blink': function(v) {
204 terminal.setCursorBlink(!!v);
205 },
206
207 'cursor-color': function(v) {
208 terminal.setCursorColor(v);
209 },
210
211 'color-palette-overrides': function(v) {
212 if (!(v == null || v instanceof Object || v instanceof Array)) {
213 console.warn('Preference color-palette-overrides is not an array or ' +
214 'object: ' + v);
215 return;
rginda9f5222b2012-03-05 11:53:28 -0800216 }
rginda9f5222b2012-03-05 11:53:28 -0800217
Robert Ginda57f03b42012-09-13 11:02:48 -0700218 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700219
Robert Ginda57f03b42012-09-13 11:02:48 -0700220 if (v) {
221 for (var key in v) {
222 var i = parseInt(key);
223 if (isNaN(i) || i < 0 || i > 255) {
224 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
225 continue;
226 }
227
228 if (v[i]) {
229 var rgb = lib.colors.normalizeCSS(v[i]);
230 if (rgb)
231 lib.colors.colorPalette[i] = rgb;
232 }
233 }
rginda30f20f62012-04-05 16:36:19 -0700234 }
rginda30f20f62012-04-05 16:36:19 -0700235
Robert Ginda57f03b42012-09-13 11:02:48 -0700236 terminal.primaryScreen_.textAttributes.resetColorPalette()
237 terminal.alternateScreen_.textAttributes.resetColorPalette();
238 },
rginda30f20f62012-04-05 16:36:19 -0700239
Robert Ginda57f03b42012-09-13 11:02:48 -0700240 'copy-on-select': function(v) {
241 terminal.copyOnSelect = !!v;
242 },
rginda9f5222b2012-03-05 11:53:28 -0800243
Robert Ginda57f03b42012-09-13 11:02:48 -0700244 'enable-8-bit-control': function(v) {
245 terminal.vt.enable8BitControl = !!v;
246 },
rginda30f20f62012-04-05 16:36:19 -0700247
Robert Ginda57f03b42012-09-13 11:02:48 -0700248 'enable-bold': function(v) {
249 terminal.syncBoldSafeState();
250 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400251
Robert Ginda57f03b42012-09-13 11:02:48 -0700252 'enable-clipboard-write': function(v) {
253 terminal.vt.enableClipboardWrite = !!v;
254 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400255
Robert Ginda57f03b42012-09-13 11:02:48 -0700256 'font-family': function(v) {
257 terminal.syncFontFamily();
258 },
rginda30f20f62012-04-05 16:36:19 -0700259
Robert Ginda57f03b42012-09-13 11:02:48 -0700260 'font-size': function(v) {
261 terminal.setFontSize(v);
262 },
rginda9875d902012-08-20 16:21:57 -0700263
Robert Ginda57f03b42012-09-13 11:02:48 -0700264 'font-smoothing': function(v) {
265 terminal.syncFontFamily();
266 },
rgindade84e382012-04-20 15:39:31 -0700267
Robert Ginda57f03b42012-09-13 11:02:48 -0700268 'foreground-color': function(v) {
269 terminal.setForegroundColor(v);
270 },
rginda30f20f62012-04-05 16:36:19 -0700271
Robert Ginda57f03b42012-09-13 11:02:48 -0700272 'home-keys-scroll': function(v) {
273 terminal.keyboard.homeKeysScroll = v;
274 },
rginda4bba5e12012-06-20 16:15:30 -0700275
Robert Ginda57f03b42012-09-13 11:02:48 -0700276 'max-string-sequence': function(v) {
277 terminal.vt.maxStringSequence = v;
278 },
rginda11057d52012-04-25 12:29:56 -0700279
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700280 'media-keys-are-fkeys': function(v) {
281 terminal.keyboard.mediaKeysAreFKeys = v;
282 },
283
Robert Ginda57f03b42012-09-13 11:02:48 -0700284 'meta-sends-escape': function(v) {
285 terminal.keyboard.metaSendsEscape = v;
286 },
rginda30f20f62012-04-05 16:36:19 -0700287
Robert Ginda57f03b42012-09-13 11:02:48 -0700288 'mouse-cell-motion-trick': function(v) {
289 terminal.vt.setMouseCellMotionTrick(v);
290 },
Robert Ginda9fb38222012-09-11 14:19:12 -0700291
Robert Ginda57f03b42012-09-13 11:02:48 -0700292 'mouse-paste-button': function(v) {
293 terminal.syncMousePasteButton();
294 },
rgindaa8ba17d2012-08-15 14:41:10 -0700295
Robert Ginda40932892012-12-10 17:26:40 -0800296 'pass-alt-number': function(v) {
297 if (v == null) {
298 var osx = window.navigator.userAgent.match(/Mac OS X/);
299
300 // Let Alt-1..9 pass to the browser (to control tab switching) on
301 // non-OS X systems, or if hterm is not opened in an app window.
302 v = (!osx && hterm.windowType != 'popup');
303 }
304
305 terminal.passAltNumber = v;
306 },
307
308 'pass-ctrl-number': function(v) {
309 if (v == null) {
310 var osx = window.navigator.userAgent.match(/Mac OS X/);
311
312 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
313 // non-OS X systems, or if hterm is not opened in an app window.
314 v = (!osx && hterm.windowType != 'popup');
315 }
316
317 terminal.passCtrlNumber = v;
318 },
319
320 'pass-meta-number': function(v) {
321 if (v == null) {
322 var osx = window.navigator.userAgent.match(/Mac OS X/);
323
324 // Let Meta-1..9 pass to the browser (to control tab switching) on
325 // OS X systems, or if hterm is not opened in an app window.
326 v = (osx && hterm.windowType != 'popup');
327 }
328
329 terminal.passMetaNumber = v;
330 },
331
Robert Ginda57f03b42012-09-13 11:02:48 -0700332 'scroll-on-keystroke': function(v) {
333 terminal.scrollOnKeystroke_ = v;
334 },
rginda9f5222b2012-03-05 11:53:28 -0800335
Robert Ginda57f03b42012-09-13 11:02:48 -0700336 'scroll-on-output': function(v) {
337 terminal.scrollOnOutput_ = v;
338 },
rginda30f20f62012-04-05 16:36:19 -0700339
Robert Ginda57f03b42012-09-13 11:02:48 -0700340 'scrollbar-visible': function(v) {
341 terminal.setScrollbarVisible(v);
342 },
rginda9f5222b2012-03-05 11:53:28 -0800343
Robert Ginda57f03b42012-09-13 11:02:48 -0700344 'shift-insert-paste': function(v) {
345 terminal.keyboard.shiftInsertPaste = v;
346 },
rginda9f5222b2012-03-05 11:53:28 -0800347
Robert Ginda57f03b42012-09-13 11:02:48 -0700348 'page-keys-scroll': function(v) {
349 terminal.keyboard.pageKeysScroll = v;
350 }
351 });
rginda30f20f62012-04-05 16:36:19 -0700352
Robert Ginda57f03b42012-09-13 11:02:48 -0700353 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800354 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700355
356 if (opt_callback)
357 opt_callback();
358 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800359};
360
rginda8e92a692012-05-20 19:37:20 -0700361
362/**
363 * Set the color for the cursor.
364 *
365 * If you want this setting to persist, set it through prefs_, rather than
366 * with this method.
367 */
368hterm.Terminal.prototype.setCursorColor = function(color) {
369 this.cursorNode_.style.backgroundColor = color;
370 this.cursorNode_.style.borderColor = color;
371};
372
373/**
374 * Return the current cursor color as a string.
375 */
376hterm.Terminal.prototype.getCursorColor = function() {
377 return this.cursorNode_.style.backgroundColor;
378};
379
380/**
rgindad5613292012-06-19 15:40:37 -0700381 * Enable or disable mouse based text selection in the terminal.
382 */
383hterm.Terminal.prototype.setSelectionEnabled = function(state) {
384 this.enableMouseDragScroll = state;
385 this.scrollPort_.setSelectionEnabled(state);
386};
387
388/**
rginda8e92a692012-05-20 19:37:20 -0700389 * Set the background color.
390 *
391 * If you want this setting to persist, set it through prefs_, rather than
392 * with this method.
393 */
394hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700395 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700396 this.primaryScreen_.textAttributes.setDefaults(
397 this.foregroundColor_, this.backgroundColor_);
398 this.alternateScreen_.textAttributes.setDefaults(
399 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700400 this.scrollPort_.setBackgroundColor(color);
401};
402
rginda9f5222b2012-03-05 11:53:28 -0800403/**
404 * Return the current terminal background color.
405 *
406 * Intended for use by other classes, so we don't have to expose the entire
407 * prefs_ object.
408 */
409hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700410 return this.backgroundColor_;
411};
412
413/**
414 * Set the foreground color.
415 *
416 * If you want this setting to persist, set it through prefs_, rather than
417 * with this method.
418 */
419hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700420 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700421 this.primaryScreen_.textAttributes.setDefaults(
422 this.foregroundColor_, this.backgroundColor_);
423 this.alternateScreen_.textAttributes.setDefaults(
424 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700425 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800426};
427
428/**
429 * Return the current terminal foreground color.
430 *
431 * Intended for use by other classes, so we don't have to expose the entire
432 * prefs_ object.
433 */
434hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700435 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800436};
437
438/**
rginda87b86462011-12-14 13:48:03 -0800439 * Create a new instance of a terminal command and run it with a given
440 * argument string.
441 *
442 * @param {function} commandClass The constructor for a terminal command.
443 * @param {string} argString The argument string to pass to the command.
444 */
445hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700446 var environment = this.prefs_.get('environment');
447 if (typeof environment != 'object' || environment == null)
448 environment = {};
449
rginda87b86462011-12-14 13:48:03 -0800450 var self = this;
451 this.command = new commandClass(
452 { argString: argString || '',
453 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700454 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800455 onExit: function(code) {
456 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800457 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700458 if (self.prefs_.get('close-on-exit'))
459 window.close();
rginda87b86462011-12-14 13:48:03 -0800460 }
461 });
462
rgindafeaf3142012-01-31 15:14:20 -0800463 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800464 this.command.run();
465};
466
467/**
rgindafeaf3142012-01-31 15:14:20 -0800468 * Returns true if the current screen is the primary screen, false otherwise.
469 */
470hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700471 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800472};
473
474/**
475 * Install the keyboard handler for this terminal.
476 *
477 * This will prevent the browser from seeing any keystrokes sent to the
478 * terminal.
479 */
480hterm.Terminal.prototype.installKeyboard = function() {
481 this.keyboard.installKeyboard(this.document_.body.firstChild);
482}
483
484/**
485 * Uninstall the keyboard handler for this terminal.
486 */
487hterm.Terminal.prototype.uninstallKeyboard = function() {
488 this.keyboard.installKeyboard(null);
489}
490
491/**
rginda35c456b2012-02-09 17:29:05 -0800492 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800493 *
494 * Call setFontSize(0) to reset to the default font size.
495 *
496 * This function does not modify the font-size preference.
497 *
498 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800499 */
500hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800501 if (px === 0)
502 px = this.prefs_.get('font-size');
503
rginda35c456b2012-02-09 17:29:05 -0800504 this.scrollPort_.setFontSize(px);
505};
506
507/**
508 * Get the current font size.
509 */
510hterm.Terminal.prototype.getFontSize = function() {
511 return this.scrollPort_.getFontSize();
512};
513
514/**
rginda8e92a692012-05-20 19:37:20 -0700515 * Get the current font family.
516 */
517hterm.Terminal.prototype.getFontFamily = function() {
518 return this.scrollPort_.getFontFamily();
519};
520
521/**
rginda35c456b2012-02-09 17:29:05 -0800522 * Set the CSS "font-family" for this terminal.
523 */
rginda9f5222b2012-03-05 11:53:28 -0800524hterm.Terminal.prototype.syncFontFamily = function() {
525 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
526 this.prefs_.get('font-smoothing'));
527 this.syncBoldSafeState();
528};
529
rginda4bba5e12012-06-20 16:15:30 -0700530/**
531 * Set this.mousePasteButton based on the mouse-paste-button pref,
532 * autodetecting if necessary.
533 */
534hterm.Terminal.prototype.syncMousePasteButton = function() {
535 var button = this.prefs_.get('mouse-paste-button');
536 if (typeof button == 'number') {
537 this.mousePasteButton = button;
538 return;
539 }
540
541 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
542 if (!ary || ary[2] == 'CrOS') {
543 this.mousePasteButton = 2;
544 } else {
545 this.mousePasteButton = 3;
546 }
547};
548
549/**
550 * Enable or disable bold based on the enable-bold pref, autodetecting if
551 * necessary.
552 */
rginda9f5222b2012-03-05 11:53:28 -0800553hterm.Terminal.prototype.syncBoldSafeState = function() {
554 var enableBold = this.prefs_.get('enable-bold');
555 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700556 this.primaryScreen_.textAttributes.enableBold = enableBold;
557 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800558 return;
559 }
560
rgindaf7521392012-02-28 17:20:34 -0800561 var normalSize = this.scrollPort_.measureCharacterSize();
562 var boldSize = this.scrollPort_.measureCharacterSize('bold');
563
564 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800565 if (!isBoldSafe) {
566 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700567 'from normal. Font family is: ' +
568 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800569 }
rginda9f5222b2012-03-05 11:53:28 -0800570
Robert Gindaed016262012-10-26 16:27:09 -0700571 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
572 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800573};
574
575/**
rginda87b86462011-12-14 13:48:03 -0800576 * Return a copy of the current cursor position.
577 *
578 * @return {hterm.RowCol} The RowCol object representing the current position.
579 */
580hterm.Terminal.prototype.saveCursor = function() {
581 return this.screen_.cursorPosition.clone();
582};
583
rgindaa19afe22012-01-25 15:40:22 -0800584hterm.Terminal.prototype.getTextAttributes = function() {
585 return this.screen_.textAttributes;
586};
587
rginda1a09aa02012-06-18 21:11:25 -0700588hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
589 this.screen_.textAttributes = textAttributes;
590};
591
rginda87b86462011-12-14 13:48:03 -0800592/**
rgindaf522ce02012-04-17 17:49:17 -0700593 * Return the current browser zoom factor applied to the terminal.
594 *
595 * @return {number} The current browser zoom factor.
596 */
597hterm.Terminal.prototype.getZoomFactor = function() {
598 return this.scrollPort_.characterSize.zoomFactor;
599};
600
601/**
rginda9846e2f2012-01-27 13:53:33 -0800602 * Change the title of this terminal's window.
603 */
604hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800605 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800606};
607
608/**
rginda87b86462011-12-14 13:48:03 -0800609 * Restore a previously saved cursor position.
610 *
611 * @param {hterm.RowCol} cursor The position to restore.
612 */
613hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700614 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
615 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800616 this.screen_.setCursorPosition(row, column);
617 if (cursor.column > column ||
618 cursor.column == column && cursor.overflow) {
619 this.screen_.cursorPosition.overflow = true;
620 }
rginda87b86462011-12-14 13:48:03 -0800621};
622
623/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400624 * Clear the cursor's overflow flag.
625 */
626hterm.Terminal.prototype.clearCursorOverflow = function() {
627 this.screen_.cursorPosition.overflow = false;
628};
629
630/**
rginda87b86462011-12-14 13:48:03 -0800631 * Set the width of the terminal, resizing the UI to match.
632 */
633hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800634 if (columnCount == null) {
635 this.div_.style.width = '100%';
636 return;
637 }
638
rginda35c456b2012-02-09 17:29:05 -0800639 this.div_.style.width = this.scrollPort_.characterSize.width *
Robert Ginda97769282013-02-01 15:30:30 -0800640 columnCount + this.scrollPort_.currentScrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400641 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800642 this.scheduleSyncCursorPosition_();
643};
rginda87b86462011-12-14 13:48:03 -0800644
rgindac9bc5502012-01-18 11:48:44 -0800645/**
rginda35c456b2012-02-09 17:29:05 -0800646 * Set the height of the terminal, resizing the UI to match.
647 */
648hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800649 if (rowCount == null) {
650 this.div_.style.height = '100%';
651 return;
652 }
653
rginda35c456b2012-02-09 17:29:05 -0800654 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700655 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800656 this.realizeSize_(this.screenSize.width, rowCount);
657 this.scheduleSyncCursorPosition_();
658};
659
660/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400661 * Deal with terminal size changes.
662 *
663 */
664hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
665 if (columnCount != this.screenSize.width)
666 this.realizeWidth_(columnCount);
667
668 if (rowCount != this.screenSize.height)
669 this.realizeHeight_(rowCount);
670
671 // Send new terminal size to plugin.
672 this.io.onTerminalResize(columnCount, rowCount);
673};
674
675/**
rgindac9bc5502012-01-18 11:48:44 -0800676 * Deal with terminal width changes.
677 *
678 * This function does what needs to be done when the terminal width changes
679 * out from under us. It happens here rather than in onResize_() because this
680 * code may need to run synchronously to handle programmatic changes of
681 * terminal width.
682 *
683 * Relying on the browser to send us an async resize event means we may not be
684 * in the correct state yet when the next escape sequence hits.
685 */
686hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700687 if (columnCount <= 0)
688 throw new Error('Attempt to realize bad width: ' + columnCount);
689
rgindac9bc5502012-01-18 11:48:44 -0800690 var deltaColumns = columnCount - this.screen_.getWidth();
691
rginda87b86462011-12-14 13:48:03 -0800692 this.screenSize.width = columnCount;
693 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800694
695 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400696 if (this.defaultTabStops)
697 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800698 } else {
699 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400700 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800701 break;
702
703 this.tabStops_.pop();
704 }
705 }
706
707 this.screen_.setColumnCount(this.screenSize.width);
708};
709
710/**
711 * Deal with terminal height changes.
712 *
713 * This function does what needs to be done when the terminal height changes
714 * out from under us. It happens here rather than in onResize_() because this
715 * code may need to run synchronously to handle programmatic changes of
716 * terminal height.
717 *
718 * Relying on the browser to send us an async resize event means we may not be
719 * in the correct state yet when the next escape sequence hits.
720 */
721hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700722 if (rowCount <= 0)
723 throw new Error('Attempt to realize bad height: ' + rowCount);
724
rgindac9bc5502012-01-18 11:48:44 -0800725 var deltaRows = rowCount - this.screen_.getHeight();
726
727 this.screenSize.height = rowCount;
728
729 var cursor = this.saveCursor();
730
731 if (deltaRows < 0) {
732 // Screen got smaller.
733 deltaRows *= -1;
734 while (deltaRows) {
735 var lastRow = this.getRowCount() - 1;
736 if (lastRow - this.scrollbackRows_.length == cursor.row)
737 break;
738
739 if (this.getRowText(lastRow))
740 break;
741
742 this.screen_.popRow();
743 deltaRows--;
744 }
745
746 var ary = this.screen_.shiftRows(deltaRows);
747 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
748
749 // We just removed rows from the top of the screen, we need to update
750 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800751 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800752 } else if (deltaRows > 0) {
753 // Screen got larger.
754
755 if (deltaRows <= this.scrollbackRows_.length) {
756 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
757 var rows = this.scrollbackRows_.splice(
758 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
759 this.screen_.unshiftRows(rows);
760 deltaRows -= scrollbackCount;
761 cursor.row += scrollbackCount;
762 }
763
764 if (deltaRows)
765 this.appendRows_(deltaRows);
766 }
767
rginda35c456b2012-02-09 17:29:05 -0800768 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800769 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800770};
771
772/**
773 * Scroll the terminal to the top of the scrollback buffer.
774 */
775hterm.Terminal.prototype.scrollHome = function() {
776 this.scrollPort_.scrollRowToTop(0);
777};
778
779/**
780 * Scroll the terminal to the end.
781 */
782hterm.Terminal.prototype.scrollEnd = function() {
783 this.scrollPort_.scrollRowToBottom(this.getRowCount());
784};
785
786/**
787 * Scroll the terminal one page up (minus one line) relative to the current
788 * position.
789 */
790hterm.Terminal.prototype.scrollPageUp = function() {
791 var i = this.scrollPort_.getTopRowIndex();
792 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
793};
794
795/**
796 * Scroll the terminal one page down (minus one line) relative to the current
797 * position.
798 */
799hterm.Terminal.prototype.scrollPageDown = function() {
800 var i = this.scrollPort_.getTopRowIndex();
801 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800802};
803
rgindac9bc5502012-01-18 11:48:44 -0800804/**
Robert Ginda40932892012-12-10 17:26:40 -0800805 * Clear primary screen, secondary screen, and the scrollback buffer.
806 */
807hterm.Terminal.prototype.wipeContents = function() {
808 this.scrollbackRows_.length = 0;
809 this.scrollPort_.resetCache();
810
811 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
812 var bottom = screen.getHeight();
813 if (bottom > 0) {
814 this.renumberRows_(0, bottom);
815 this.clearHome(screen);
816 }
817 }.bind(this));
818
819 this.syncCursorPosition_();
820};
821
822/**
rgindac9bc5502012-01-18 11:48:44 -0800823 * Full terminal reset.
824 */
rginda87b86462011-12-14 13:48:03 -0800825hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800826 this.clearAllTabStops();
827 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -0700828
829 this.clearHome(this.primaryScreen_);
830 this.primaryScreen_.textAttributes.reset();
831
832 this.clearHome(this.alternateScreen_);
833 this.alternateScreen_.textAttributes.reset();
834
rgindab8bc8932012-04-27 12:45:03 -0700835 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
836
Robert Ginda92e18102013-03-14 13:56:37 -0700837 this.vt.reset();
838
rgindac9bc5502012-01-18 11:48:44 -0800839 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800840};
841
rgindac9bc5502012-01-18 11:48:44 -0800842/**
843 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -0700844 *
845 * Perform a soft reset to the default values listed in
846 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -0800847 */
rginda0f5c0292012-01-13 11:00:13 -0800848hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -0700849 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -0800850 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -0700851
rgindab8bc8932012-04-27 12:45:03 -0700852 // Xterm also resets the color palette on soft reset, even though it doesn't
853 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -0700854 this.primaryScreen_.textAttributes.resetColorPalette();
855 this.alternateScreen_.textAttributes.resetColorPalette();
856
rgindab8bc8932012-04-27 12:45:03 -0700857 // The xterm man page explicitly says this will happen on soft reset.
858 this.setVTScrollRegion(null, null);
859
860 // Xterm also shows the cursor on soft reset, but does not alter the blink
861 // state.
rgindaa19afe22012-01-25 15:40:22 -0800862 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -0800863};
864
rgindac9bc5502012-01-18 11:48:44 -0800865/**
866 * Move the cursor forward to the next tab stop, or to the last column
867 * if no more tab stops are set.
868 */
869hterm.Terminal.prototype.forwardTabStop = function() {
870 var column = this.screen_.cursorPosition.column;
871
872 for (var i = 0; i < this.tabStops_.length; i++) {
873 if (this.tabStops_[i] > column) {
874 this.setCursorColumn(this.tabStops_[i]);
875 return;
876 }
877 }
878
David Benjamin66e954d2012-05-05 21:08:12 -0400879 // xterm does not clear the overflow flag on HT or CHT.
880 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -0800881 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -0400882 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -0800883};
884
rgindac9bc5502012-01-18 11:48:44 -0800885/**
886 * Move the cursor backward to the previous tab stop, or to the first column
887 * if no previous tab stops are set.
888 */
889hterm.Terminal.prototype.backwardTabStop = function() {
890 var column = this.screen_.cursorPosition.column;
891
892 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
893 if (this.tabStops_[i] < column) {
894 this.setCursorColumn(this.tabStops_[i]);
895 return;
896 }
897 }
898
899 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800900};
901
rgindac9bc5502012-01-18 11:48:44 -0800902/**
903 * Set a tab stop at the given column.
904 *
905 * @param {int} column Zero based column.
906 */
907hterm.Terminal.prototype.setTabStop = function(column) {
908 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
909 if (this.tabStops_[i] == column)
910 return;
911
912 if (this.tabStops_[i] < column) {
913 this.tabStops_.splice(i + 1, 0, column);
914 return;
915 }
916 }
917
918 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800919};
920
rgindac9bc5502012-01-18 11:48:44 -0800921/**
922 * Clear the tab stop at the current cursor position.
923 *
924 * No effect if there is no tab stop at the current cursor position.
925 */
926hterm.Terminal.prototype.clearTabStopAtCursor = function() {
927 var column = this.screen_.cursorPosition.column;
928
929 var i = this.tabStops_.indexOf(column);
930 if (i == -1)
931 return;
932
933 this.tabStops_.splice(i, 1);
934};
935
936/**
937 * Clear all tab stops.
938 */
939hterm.Terminal.prototype.clearAllTabStops = function() {
940 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -0400941 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -0800942};
943
944/**
945 * Set up the default tab stops, starting from a given column.
946 *
947 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -0400948 * from the specified column, or 0 if no column is provided. It also flags
949 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -0800950 *
951 * This does not clear the existing tab stops first, use clearAllTabStops
952 * for that.
953 *
954 * @param {int} opt_start Optional starting zero based starting column, useful
955 * for filling out missing tab stops when the terminal is resized.
956 */
957hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
958 var start = opt_start || 0;
959 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -0400960 // Round start up to a default tab stop.
961 start = start - 1 - ((start - 1) % w) + w;
962 for (var i = start; i < this.screenSize.width; i += w) {
963 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -0800964 }
David Benjamin66e954d2012-05-05 21:08:12 -0400965
966 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -0800967};
968
rginda6d397402012-01-17 10:58:29 -0800969/**
rginda8ba33642011-12-14 12:31:31 -0800970 * Interpret a sequence of characters.
971 *
972 * Incomplete escape sequences are buffered until the next call.
973 *
974 * @param {string} str Sequence of characters to interpret or pass through.
975 */
976hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -0800977 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -0800978 this.scheduleSyncCursorPosition_();
979};
980
981/**
982 * Take over the given DIV for use as the terminal display.
983 *
984 * @param {HTMLDivElement} div The div to use as the terminal display.
985 */
986hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -0800987 this.div_ = div;
988
rginda8ba33642011-12-14 12:31:31 -0800989 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -0700990 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -0400991 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
992 this.scrollPort_.setBackgroundPosition(
993 this.prefs_.get('background-position'));
rginda30f20f62012-04-05 16:36:19 -0700994
rginda0918b652012-04-04 11:26:24 -0700995 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -0800996
rginda9f5222b2012-03-05 11:53:28 -0800997 this.setFontSize(this.prefs_.get('font-size'));
998 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -0800999
David Reveman8f552492012-03-28 12:18:41 -04001000 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
1001
rginda8ba33642011-12-14 12:31:31 -08001002 this.document_ = this.scrollPort_.getDocument();
1003
rginda4bba5e12012-06-20 16:15:30 -07001004 this.document_.body.oncontextmenu = function() { return false };
1005
1006 var onMouse = this.onMouse_.bind(this);
1007 this.document_.body.firstChild.addEventListener('mousedown', onMouse);
1008 this.document_.body.firstChild.addEventListener('mouseup', onMouse);
1009 this.document_.body.firstChild.addEventListener('mousemove', onMouse);
1010 this.scrollPort_.onScrollWheel = onMouse;
1011
rginda8e92a692012-05-20 19:37:20 -07001012 this.document_.body.firstChild.addEventListener(
1013 'focus', this.onFocusChange_.bind(this, true));
1014 this.document_.body.firstChild.addEventListener(
1015 'blur', this.onFocusChange_.bind(this, false));
1016
1017 var style = this.document_.createElement('style');
1018 style.textContent =
1019 ('.cursor-node[focus="false"] {' +
1020 ' box-sizing: border-box;' +
1021 ' background-color: transparent !important;' +
1022 ' border-width: 2px;' +
1023 ' border-style: solid;' +
1024 '}');
1025 this.document_.head.appendChild(style);
1026
rginda8ba33642011-12-14 12:31:31 -08001027 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001028 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001029 this.cursorNode_.style.cssText =
1030 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001031 'top: -99px;' +
1032 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001033 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1034 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001035 '-webkit-transition: opacity, background-color 100ms linear;');
1036 this.setCursorColor(this.prefs_.get('cursor-color'));
rgindad5613292012-06-19 15:40:37 -07001037
rginda8ba33642011-12-14 12:31:31 -08001038 this.document_.body.appendChild(this.cursorNode_);
1039
rgindad5613292012-06-19 15:40:37 -07001040 // When 'enableMouseDragScroll' is off we reposition this element directly
1041 // under the mouse cursor after a click. This makes Chrome associate
1042 // subsequent mousemove events with the scroll-blocker. Since the
1043 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1044 // events do not cause the scrollport to scroll.
1045 //
1046 // It's a hack, but it's the cleanest way I could find.
1047 this.scrollBlockerNode_ = this.document_.createElement('div');
1048 this.scrollBlockerNode_.style.cssText =
1049 ('position: absolute;' +
1050 'top: -99px;' +
1051 'display: block;' +
1052 'width: 10px;' +
1053 'height: 10px;');
1054 this.document_.body.appendChild(this.scrollBlockerNode_);
1055
1056 var onMouse = this.onMouse_.bind(this);
1057 this.scrollPort_.onScrollWheel = onMouse;
1058 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1059 ].forEach(function(event) {
1060 this.scrollBlockerNode_.addEventListener(event, onMouse);
1061 this.cursorNode_.addEventListener(event, onMouse);
1062 this.document_.addEventListener(event, onMouse);
1063 }.bind(this));
1064
1065 this.cursorNode_.addEventListener('mousedown', function() {
1066 setTimeout(this.focus.bind(this));
1067 }.bind(this));
1068
rgindade84e382012-04-20 15:39:31 -07001069 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
rginda8ba33642011-12-14 12:31:31 -08001070 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001071
rginda87b86462011-12-14 13:48:03 -08001072 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001073 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001074};
1075
rginda0918b652012-04-04 11:26:24 -07001076/**
1077 * Return the HTML document that contains the terminal DOM nodes.
1078 */
rginda87b86462011-12-14 13:48:03 -08001079hterm.Terminal.prototype.getDocument = function() {
1080 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001081};
1082
1083/**
rginda0918b652012-04-04 11:26:24 -07001084 * Focus the terminal.
1085 */
1086hterm.Terminal.prototype.focus = function() {
1087 this.scrollPort_.focus();
1088};
1089
1090/**
rginda8ba33642011-12-14 12:31:31 -08001091 * Return the HTML Element for a given row index.
1092 *
1093 * This is a method from the RowProvider interface. The ScrollPort uses
1094 * it to fetch rows on demand as they are scrolled into view.
1095 *
1096 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1097 * pairs to conserve memory.
1098 *
1099 * @param {integer} index The zero-based row index, measured relative to the
1100 * start of the scrollback buffer. On-screen rows will always have the
1101 * largest indicies.
1102 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1103 */
1104hterm.Terminal.prototype.getRowNode = function(index) {
1105 if (index < this.scrollbackRows_.length)
1106 return this.scrollbackRows_[index];
1107
1108 var screenIndex = index - this.scrollbackRows_.length;
1109 return this.screen_.rowsArray[screenIndex];
1110};
1111
1112/**
1113 * Return the text content for a given range of rows.
1114 *
1115 * This is a method from the RowProvider interface. The ScrollPort uses
1116 * it to fetch text content on demand when the user attempts to copy their
1117 * selection to the clipboard.
1118 *
1119 * @param {integer} start The zero-based row index to start from, measured
1120 * relative to the start of the scrollback buffer. On-screen rows will
1121 * always have the largest indicies.
1122 * @param {integer} end The zero-based row index to end on, measured
1123 * relative to the start of the scrollback buffer.
1124 * @return {string} A single string containing the text value of the range of
1125 * rows. Lines will be newline delimited, with no trailing newline.
1126 */
1127hterm.Terminal.prototype.getRowsText = function(start, end) {
1128 var ary = [];
1129 for (var i = start; i < end; i++) {
1130 var node = this.getRowNode(i);
1131 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001132 if (i < end - 1 && !node.getAttribute('line-overflow'))
1133 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001134 }
1135
rgindaa09e7332012-08-17 12:49:51 -07001136 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001137};
1138
1139/**
1140 * Return the text content for a given row.
1141 *
1142 * This is a method from the RowProvider interface. The ScrollPort uses
1143 * it to fetch text content on demand when the user attempts to copy their
1144 * selection to the clipboard.
1145 *
1146 * @param {integer} index The zero-based row index to return, measured
1147 * relative to the start of the scrollback buffer. On-screen rows will
1148 * always have the largest indicies.
1149 * @return {string} A string containing the text value of the selected row.
1150 */
1151hterm.Terminal.prototype.getRowText = function(index) {
1152 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001153 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001154};
1155
1156/**
1157 * Return the total number of rows in the addressable screen and in the
1158 * scrollback buffer of this terminal.
1159 *
1160 * This is a method from the RowProvider interface. The ScrollPort uses
1161 * it to compute the size of the scrollbar.
1162 *
1163 * @return {integer} The number of rows in this terminal.
1164 */
1165hterm.Terminal.prototype.getRowCount = function() {
1166 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1167};
1168
1169/**
1170 * Create DOM nodes for new rows and append them to the end of the terminal.
1171 *
1172 * This is the only correct way to add a new DOM node for a row. Notice that
1173 * the new row is appended to the bottom of the list of rows, and does not
1174 * require renumbering (of the rowIndex property) of previous rows.
1175 *
1176 * If you think you want a new blank row somewhere in the middle of the
1177 * terminal, look into moveRows_().
1178 *
1179 * This method does not pay attention to vtScrollTop/Bottom, since you should
1180 * be using moveRows() in cases where they would matter.
1181 *
1182 * The cursor will be positioned at column 0 of the first inserted line.
1183 */
1184hterm.Terminal.prototype.appendRows_ = function(count) {
1185 var cursorRow = this.screen_.rowsArray.length;
1186 var offset = this.scrollbackRows_.length + cursorRow;
1187 for (var i = 0; i < count; i++) {
1188 var row = this.document_.createElement('x-row');
1189 row.appendChild(this.document_.createTextNode(''));
1190 row.rowIndex = offset + i;
1191 this.screen_.pushRow(row);
1192 }
1193
1194 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1195 if (extraRows > 0) {
1196 var ary = this.screen_.shiftRows(extraRows);
1197 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001198 if (this.scrollPort_.isScrolledEnd)
1199 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001200 }
1201
1202 if (cursorRow >= this.screen_.rowsArray.length)
1203 cursorRow = this.screen_.rowsArray.length - 1;
1204
rginda87b86462011-12-14 13:48:03 -08001205 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001206};
1207
1208/**
1209 * Relocate rows from one part of the addressable screen to another.
1210 *
1211 * This is used to recycle rows during VT scrolls (those which are driven
1212 * by VT commands, rather than by the user manipulating the scrollbar.)
1213 *
1214 * In this case, the blank lines scrolled into the scroll region are made of
1215 * the nodes we scrolled off. These have their rowIndex properties carefully
1216 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001217 */
1218hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1219 var ary = this.screen_.removeRows(fromIndex, count);
1220 this.screen_.insertRows(toIndex, ary);
1221
1222 var start, end;
1223 if (fromIndex < toIndex) {
1224 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001225 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001226 } else {
1227 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001228 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001229 }
1230
1231 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001232 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001233};
1234
1235/**
1236 * Renumber the rowIndex property of the given range of rows.
1237 *
1238 * The start and end indicies are relative to the screen, not the scrollback.
1239 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001240 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001241 * no need to renumber scrollback rows.
1242 */
Robert Ginda40932892012-12-10 17:26:40 -08001243hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1244 var screen = opt_screen || this.screen_;
1245
rginda8ba33642011-12-14 12:31:31 -08001246 var offset = this.scrollbackRows_.length;
1247 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001248 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001249 }
1250};
1251
1252/**
1253 * Print a string to the terminal.
1254 *
1255 * This respects the current insert and wraparound modes. It will add new lines
1256 * to the end of the terminal, scrolling off the top into the scrollback buffer
1257 * if necessary.
1258 *
1259 * The string is *not* parsed for escape codes. Use the interpret() method if
1260 * that's what you're after.
1261 *
1262 * @param{string} str The string to print.
1263 */
1264hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001265 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001266
rgindaa9abdd82012-08-06 18:05:09 -07001267 while (startOffset < str.length) {
rgindaa09e7332012-08-17 12:49:51 -07001268 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1269 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001270 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001271 }
rgindaa19afe22012-01-25 15:40:22 -08001272
rgindaa9abdd82012-08-06 18:05:09 -07001273 var count = str.length - startOffset;
1274 var didOverflow = false;
1275 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001276
rgindaa9abdd82012-08-06 18:05:09 -07001277 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1278 didOverflow = true;
1279 count = this.screenSize.width - this.screen_.cursorPosition.column;
1280 }
rgindaa19afe22012-01-25 15:40:22 -08001281
rgindaa9abdd82012-08-06 18:05:09 -07001282 if (didOverflow && !this.options_.wraparound) {
1283 // If the string overflowed the line but wraparound is off, then the
1284 // last printed character should be the last of the string.
1285 // TODO: This will add to our problems with multibyte UTF-16 characters.
1286 substr = str.substr(startOffset, count - 1) +
1287 str.substr(str.length - 1);
1288 count = str.length;
1289 } else {
1290 substr = str.substr(startOffset, count);
1291 }
rgindaa19afe22012-01-25 15:40:22 -08001292
rgindaa9abdd82012-08-06 18:05:09 -07001293 if (this.options_.insertMode) {
1294 this.screen_.insertString(substr);
1295 } else {
1296 this.screen_.overwriteString(substr);
1297 }
1298
1299 this.screen_.maybeClipCurrentRow();
1300 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001301 }
rginda8ba33642011-12-14 12:31:31 -08001302
1303 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001304
rginda9f5222b2012-03-05 11:53:28 -08001305 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001306 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001307};
1308
1309/**
rginda87b86462011-12-14 13:48:03 -08001310 * Set the VT scroll region.
1311 *
rginda87b86462011-12-14 13:48:03 -08001312 * This also resets the cursor position to the absolute (0, 0) position, since
1313 * that's what xterm appears to do.
1314 *
1315 * @param {integer} scrollTop The zero-based top of the scroll region.
1316 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1317 * inclusive.
1318 */
1319hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
1320 this.vtScrollTop_ = scrollTop;
1321 this.vtScrollBottom_ = scrollBottom;
rginda87b86462011-12-14 13:48:03 -08001322};
1323
1324/**
rginda8ba33642011-12-14 12:31:31 -08001325 * Return the top row index according to the VT.
1326 *
1327 * This will return 0 unless the terminal has been told to restrict scrolling
1328 * to some lower row. It is used for some VT cursor positioning and scrolling
1329 * commands.
1330 *
1331 * @return {integer} The topmost row in the terminal's scroll region.
1332 */
1333hterm.Terminal.prototype.getVTScrollTop = function() {
1334 if (this.vtScrollTop_ != null)
1335 return this.vtScrollTop_;
1336
1337 return 0;
rginda87b86462011-12-14 13:48:03 -08001338};
rginda8ba33642011-12-14 12:31:31 -08001339
1340/**
1341 * Return the bottom row index according to the VT.
1342 *
1343 * This will return the height of the terminal unless the it has been told to
1344 * restrict scrolling to some higher row. It is used for some VT cursor
1345 * positioning and scrolling commands.
1346 *
1347 * @return {integer} The bottommost row in the terminal's scroll region.
1348 */
1349hterm.Terminal.prototype.getVTScrollBottom = function() {
1350 if (this.vtScrollBottom_ != null)
1351 return this.vtScrollBottom_;
1352
rginda87b86462011-12-14 13:48:03 -08001353 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001354}
1355
1356/**
1357 * Process a '\n' character.
1358 *
1359 * If the cursor is on the final row of the terminal this will append a new
1360 * blank row to the screen and scroll the topmost row into the scrollback
1361 * buffer.
1362 *
1363 * Otherwise, this moves the cursor to column zero of the next row.
1364 */
1365hterm.Terminal.prototype.newLine = function() {
1366 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -08001367 // If we're at the end of the screen we need to append a new line and
1368 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -08001369 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -08001370 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
1371 // End of the scroll region does not affect the scrollback buffer.
1372 this.vtScrollUp(1);
1373 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -08001374 } else {
rginda87b86462011-12-14 13:48:03 -08001375 // Anywhere else in the screen just moves the cursor.
1376 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001377 }
1378};
1379
1380/**
1381 * Like newLine(), except maintain the cursor column.
1382 */
1383hterm.Terminal.prototype.lineFeed = function() {
1384 var column = this.screen_.cursorPosition.column;
1385 this.newLine();
1386 this.setCursorColumn(column);
1387};
1388
1389/**
rginda87b86462011-12-14 13:48:03 -08001390 * If autoCarriageReturn is set then newLine(), else lineFeed().
1391 */
1392hterm.Terminal.prototype.formFeed = function() {
1393 if (this.options_.autoCarriageReturn) {
1394 this.newLine();
1395 } else {
1396 this.lineFeed();
1397 }
1398};
1399
1400/**
1401 * Move the cursor up one row, possibly inserting a blank line.
1402 *
1403 * The cursor column is not changed.
1404 */
1405hterm.Terminal.prototype.reverseLineFeed = function() {
1406 var scrollTop = this.getVTScrollTop();
1407 var currentRow = this.screen_.cursorPosition.row;
1408
1409 if (currentRow == scrollTop) {
1410 this.insertLines(1);
1411 } else {
1412 this.setAbsoluteCursorRow(currentRow - 1);
1413 }
1414};
1415
1416/**
rginda8ba33642011-12-14 12:31:31 -08001417 * Replace all characters to the left of the current cursor with the space
1418 * character.
1419 *
1420 * TODO(rginda): This should probably *remove* the characters (not just replace
1421 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001422 * position.
rginda8ba33642011-12-14 12:31:31 -08001423 */
1424hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001425 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001426 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001427 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001428 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001429};
1430
1431/**
David Benjamin684a9b72012-05-01 17:19:58 -04001432 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001433 *
1434 * The cursor position is unchanged.
1435 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001436 * If the current background color is not the default background color this
1437 * will insert spaces rather than delete. This is unfortunate because the
1438 * trailing space will affect text selection, but it's difficult to come up
1439 * with a way to style empty space that wouldn't trip up the hterm.Screen
1440 * code.
rginda8ba33642011-12-14 12:31:31 -08001441 */
1442hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001443 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1444 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001445
1446 if (this.screen_.textAttributes.background ===
1447 this.screen_.textAttributes.DEFAULT_COLOR) {
1448 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
1449 if (cursorRow.textContent.length <=
1450 this.screen_.cursorPosition.column + count) {
1451 this.screen_.deleteChars(count);
1452 this.clearCursorOverflow();
1453 return;
1454 }
1455 }
1456
rginda87b86462011-12-14 13:48:03 -08001457 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001458 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001459 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001460 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001461};
1462
1463/**
1464 * Erase the current line.
1465 *
1466 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001467 */
1468hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001469 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001470 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001471 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001472 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001473};
1474
1475/**
David Benjamina08d78f2012-05-05 00:28:49 -04001476 * Erase all characters from the start of the screen to the current cursor
1477 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001478 *
1479 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001480 */
1481hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001482 var cursor = this.saveCursor();
1483
1484 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001485
David Benjamina08d78f2012-05-05 00:28:49 -04001486 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001487 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001488 this.screen_.clearCursorRow();
1489 }
1490
rginda87b86462011-12-14 13:48:03 -08001491 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001492 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001493};
1494
1495/**
1496 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001497 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001498 *
1499 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001500 */
1501hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001502 var cursor = this.saveCursor();
1503
1504 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001505
David Benjamina08d78f2012-05-05 00:28:49 -04001506 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001507 for (var i = cursor.row + 1; i <= bottom; i++) {
1508 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001509 this.screen_.clearCursorRow();
1510 }
1511
rginda87b86462011-12-14 13:48:03 -08001512 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001513 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001514};
1515
1516/**
1517 * Fill the terminal with a given character.
1518 *
1519 * This methods does not respect the VT scroll region.
1520 *
1521 * @param {string} ch The character to use for the fill.
1522 */
1523hterm.Terminal.prototype.fill = function(ch) {
1524 var cursor = this.saveCursor();
1525
1526 this.setAbsoluteCursorPosition(0, 0);
1527 for (var row = 0; row < this.screenSize.height; row++) {
1528 for (var col = 0; col < this.screenSize.width; col++) {
1529 this.setAbsoluteCursorPosition(row, col);
1530 this.screen_.overwriteString(ch);
1531 }
1532 }
1533
1534 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001535};
1536
1537/**
rginda9ea433c2012-03-16 11:57:00 -07001538 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001539 *
rginda9ea433c2012-03-16 11:57:00 -07001540 * This does not respect the scroll region.
1541 *
1542 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1543 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001544 */
rginda9ea433c2012-03-16 11:57:00 -07001545hterm.Terminal.prototype.clearHome = function(opt_screen) {
1546 var screen = opt_screen || this.screen_;
1547 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001548
rginda11057d52012-04-25 12:29:56 -07001549 if (bottom == 0) {
1550 // Empty screen, nothing to do.
1551 return;
1552 }
1553
rgindae4d29232012-01-19 10:47:13 -08001554 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001555 screen.setCursorPosition(i, 0);
1556 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001557 }
1558
rginda9ea433c2012-03-16 11:57:00 -07001559 screen.setCursorPosition(0, 0);
1560};
1561
1562/**
1563 * Erase the entire display without changing the cursor position.
1564 *
1565 * The cursor position is unchanged. This does not respect the scroll
1566 * region.
1567 *
1568 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1569 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001570 */
1571hterm.Terminal.prototype.clear = function(opt_screen) {
1572 var screen = opt_screen || this.screen_;
1573 var cursor = screen.cursorPosition.clone();
1574 this.clearHome(screen);
1575 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001576};
1577
1578/**
1579 * VT command to insert lines at the current cursor row.
1580 *
1581 * This respects the current scroll region. Rows pushed off the bottom are
1582 * lost (they won't show up in the scrollback buffer).
1583 *
rginda8ba33642011-12-14 12:31:31 -08001584 * @param {integer} count The number of lines to insert.
1585 */
1586hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07001587 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08001588
1589 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07001590 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08001591
Robert Ginda579186b2012-09-26 11:40:04 -07001592 // The moveCount is the number of rows we need to relocate to make room for
1593 // the new row(s). The count is the distance to move them.
1594 var moveCount = bottom - cursorRow - count + 1;
1595 if (moveCount)
1596 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08001597
Robert Ginda579186b2012-09-26 11:40:04 -07001598 for (var i = count - 1; i >= 0; i--) {
1599 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001600 this.screen_.clearCursorRow();
1601 }
rginda8ba33642011-12-14 12:31:31 -08001602};
1603
1604/**
1605 * VT command to delete lines at the current cursor row.
1606 *
1607 * New rows are added to the bottom of scroll region to take their place. New
1608 * rows are strictly there to take up space and have no content or style.
1609 */
1610hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001611 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001612
rginda87b86462011-12-14 13:48:03 -08001613 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001614 var bottom = this.getVTScrollBottom();
1615
rginda87b86462011-12-14 13:48:03 -08001616 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001617 count = Math.min(count, maxCount);
1618
rginda87b86462011-12-14 13:48:03 -08001619 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001620 if (count != maxCount)
1621 this.moveRows_(top, count, moveStart);
1622
1623 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001624 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001625 this.screen_.clearCursorRow();
1626 }
1627
rginda87b86462011-12-14 13:48:03 -08001628 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001629 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001630};
1631
1632/**
1633 * Inserts the given number of spaces at the current cursor position.
1634 *
rginda87b86462011-12-14 13:48:03 -08001635 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001636 */
1637hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001638 var cursor = this.saveCursor();
1639
rgindacbbd7482012-06-13 15:06:16 -07001640 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001641 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001642 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001643
1644 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001645 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001646};
1647
1648/**
1649 * Forward-delete the specified number of characters starting at the cursor
1650 * position.
1651 *
1652 * @param {integer} count The number of characters to delete.
1653 */
1654hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001655 var deleted = this.screen_.deleteChars(count);
1656 if (deleted && !this.screen_.textAttributes.isDefault()) {
1657 var cursor = this.saveCursor();
1658 this.setCursorColumn(this.screenSize.width - deleted);
1659 this.screen_.insertString(lib.f.getWhitespace(deleted));
1660 this.restoreCursor(cursor);
1661 }
1662
David Benjamin54e8bf62012-06-01 22:31:40 -04001663 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001664};
1665
1666/**
1667 * Shift rows in the scroll region upwards by a given number of lines.
1668 *
1669 * New rows are inserted at the bottom of the scroll region to fill the
1670 * vacated rows. The new rows not filled out with the current text attributes.
1671 *
1672 * This function does not affect the scrollback rows at all. Rows shifted
1673 * off the top are lost.
1674 *
rginda87b86462011-12-14 13:48:03 -08001675 * The cursor position is not altered.
1676 *
rginda8ba33642011-12-14 12:31:31 -08001677 * @param {integer} count The number of rows to scroll.
1678 */
1679hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001680 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001681
rginda87b86462011-12-14 13:48:03 -08001682 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001683 this.deleteLines(count);
1684
rginda87b86462011-12-14 13:48:03 -08001685 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001686};
1687
1688/**
1689 * Shift rows below the cursor down by a given number of lines.
1690 *
1691 * This function respects the current scroll region.
1692 *
1693 * New rows are inserted at the top of the scroll region to fill the
1694 * vacated rows. The new rows not filled out with the current text attributes.
1695 *
1696 * This function does not affect the scrollback rows at all. Rows shifted
1697 * off the bottom are lost.
1698 *
1699 * @param {integer} count The number of rows to scroll.
1700 */
1701hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001702 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001703
rginda87b86462011-12-14 13:48:03 -08001704 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001705 this.insertLines(opt_count);
1706
rginda87b86462011-12-14 13:48:03 -08001707 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001708};
1709
rginda87b86462011-12-14 13:48:03 -08001710
rginda8ba33642011-12-14 12:31:31 -08001711/**
1712 * Set the cursor position.
1713 *
1714 * The cursor row is relative to the scroll region if the terminal has
1715 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1716 *
1717 * @param {integer} row The new zero-based cursor row.
1718 * @param {integer} row The new zero-based cursor column.
1719 */
1720hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1721 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001722 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001723 } else {
rginda87b86462011-12-14 13:48:03 -08001724 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001725 }
rginda87b86462011-12-14 13:48:03 -08001726};
rginda8ba33642011-12-14 12:31:31 -08001727
rginda87b86462011-12-14 13:48:03 -08001728hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1729 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07001730 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
1731 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001732 this.screen_.setCursorPosition(row, column);
1733};
1734
1735hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07001736 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
1737 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001738 this.screen_.setCursorPosition(row, column);
1739};
1740
1741/**
1742 * Set the cursor column.
1743 *
1744 * @param {integer} column The new zero-based cursor column.
1745 */
1746hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001747 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001748};
1749
1750/**
1751 * Return the cursor column.
1752 *
1753 * @return {integer} The zero-based cursor column.
1754 */
1755hterm.Terminal.prototype.getCursorColumn = function() {
1756 return this.screen_.cursorPosition.column;
1757};
1758
1759/**
1760 * Set the cursor row.
1761 *
1762 * The cursor row is relative to the scroll region if the terminal has
1763 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1764 *
1765 * @param {integer} row The new cursor row.
1766 */
rginda87b86462011-12-14 13:48:03 -08001767hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1768 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001769};
1770
1771/**
1772 * Return the cursor row.
1773 *
1774 * @return {integer} The zero-based cursor row.
1775 */
1776hterm.Terminal.prototype.getCursorRow = function(row) {
1777 return this.screen_.cursorPosition.row;
1778};
1779
1780/**
1781 * Request that the ScrollPort redraw itself soon.
1782 *
1783 * The redraw will happen asynchronously, soon after the call stack winds down.
1784 * Multiple calls will be coalesced into a single redraw.
1785 */
1786hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001787 if (this.timeouts_.redraw)
1788 return;
rginda8ba33642011-12-14 12:31:31 -08001789
1790 var self = this;
rginda87b86462011-12-14 13:48:03 -08001791 this.timeouts_.redraw = setTimeout(function() {
1792 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001793 self.scrollPort_.redraw_();
1794 }, 0);
1795};
1796
1797/**
1798 * Request that the ScrollPort be scrolled to the bottom.
1799 *
1800 * The scroll will happen asynchronously, soon after the call stack winds down.
1801 * Multiple calls will be coalesced into a single scroll.
1802 *
1803 * This affects the scrollbar position of the ScrollPort, and has nothing to
1804 * do with the VT scroll commands.
1805 */
1806hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1807 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001808 return;
rginda8ba33642011-12-14 12:31:31 -08001809
1810 var self = this;
1811 this.timeouts_.scrollDown = setTimeout(function() {
1812 delete self.timeouts_.scrollDown;
1813 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1814 }, 10);
1815};
1816
1817/**
1818 * Move the cursor up a specified number of rows.
1819 *
1820 * @param {integer} count The number of rows to move the cursor.
1821 */
1822hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001823 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001824};
1825
1826/**
1827 * Move the cursor down a specified number of rows.
1828 *
1829 * @param {integer} count The number of rows to move the cursor.
1830 */
1831hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001832 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001833 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1834 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1835 this.screenSize.height - 1);
1836
rgindacbbd7482012-06-13 15:06:16 -07001837 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08001838 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001839 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001840};
1841
1842/**
1843 * Move the cursor left a specified number of columns.
1844 *
1845 * @param {integer} count The number of columns to move the cursor.
1846 */
1847hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001848 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001849};
1850
1851/**
1852 * Move the cursor right a specified number of columns.
1853 *
1854 * @param {integer} count The number of columns to move the cursor.
1855 */
1856hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001857 count = count || 1;
rgindacbbd7482012-06-13 15:06:16 -07001858 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001859 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001860 this.setCursorColumn(column);
1861};
1862
1863/**
1864 * Reverse the foreground and background colors of the terminal.
1865 *
1866 * This only affects text that was drawn with no attributes.
1867 *
1868 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1869 * been drawn with attributes that happen to coincide with the default
1870 * 'no-attribute' colors. My guess is probably not.
1871 */
1872hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001873 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001874 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08001875 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
1876 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08001877 } else {
rginda9f5222b2012-03-05 11:53:28 -08001878 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
1879 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08001880 }
1881};
1882
1883/**
rginda87b86462011-12-14 13:48:03 -08001884 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07001885 *
1886 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08001887 */
1888hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08001889 this.cursorNode_.style.backgroundColor =
1890 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001891
1892 var self = this;
1893 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08001894 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08001895 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07001896
1897 if (this.bellAudio_.getAttribute('src')) {
Robert Gindaa6331372013-03-19 10:35:39 -07001898 if (this.bellSquelchTimeout_)
Robert Ginda92e18102013-03-14 13:56:37 -07001899 return;
1900
1901 this.bellAudio_.play();
1902
1903 this.bellSequelchTimeout_ = setTimeout(function() {
1904 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07001905 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07001906 } else {
1907 delete this.bellSquelchTimeout_;
1908 }
rginda87b86462011-12-14 13:48:03 -08001909};
1910
1911/**
rginda8ba33642011-12-14 12:31:31 -08001912 * Set the origin mode bit.
1913 *
1914 * If origin mode is on, certain VT cursor and scrolling commands measure their
1915 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1916 * to the top of the addressable screen.
1917 *
1918 * Defaults to off.
1919 *
1920 * @param {boolean} state True to set origin mode, false to unset.
1921 */
1922hterm.Terminal.prototype.setOriginMode = function(state) {
1923 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08001924 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08001925};
1926
1927/**
1928 * Set the insert mode bit.
1929 *
1930 * If insert mode is on, existing text beyond the cursor position will be
1931 * shifted right to make room for new text. Otherwise, new text overwrites
1932 * any existing text.
1933 *
1934 * Defaults to off.
1935 *
1936 * @param {boolean} state True to set insert mode, false to unset.
1937 */
1938hterm.Terminal.prototype.setInsertMode = function(state) {
1939 this.options_.insertMode = state;
1940};
1941
1942/**
rginda87b86462011-12-14 13:48:03 -08001943 * Set the auto carriage return bit.
1944 *
1945 * If auto carriage return is on then a formfeed character is interpreted
1946 * as a newline, otherwise it's the same as a linefeed. The difference boils
1947 * down to whether or not the cursor column is reset.
1948 */
1949hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1950 this.options_.autoCarriageReturn = state;
1951};
1952
1953/**
rginda8ba33642011-12-14 12:31:31 -08001954 * Set the wraparound mode bit.
1955 *
1956 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1957 * to the start of the following row. Otherwise, the cursor is clamped to the
1958 * end of the screen and attempts to write past it are ignored.
1959 *
1960 * Defaults to on.
1961 *
1962 * @param {boolean} state True to set wraparound mode, false to unset.
1963 */
1964hterm.Terminal.prototype.setWraparound = function(state) {
1965 this.options_.wraparound = state;
1966};
1967
1968/**
1969 * Set the reverse-wraparound mode bit.
1970 *
1971 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1972 * to the end of the previous row. Otherwise, the cursor is clamped to column
1973 * 0.
1974 *
1975 * Defaults to off.
1976 *
1977 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
1978 */
1979hterm.Terminal.prototype.setReverseWraparound = function(state) {
1980 this.options_.reverseWraparound = state;
1981};
1982
1983/**
1984 * Selects between the primary and alternate screens.
1985 *
1986 * If alternate mode is on, the alternate screen is active. Otherwise the
1987 * primary screen is active.
1988 *
1989 * Swapping screens has no effect on the scrollback buffer.
1990 *
1991 * Each screen maintains its own cursor position.
1992 *
1993 * Defaults to off.
1994 *
1995 * @param {boolean} state True to set alternate mode, false to unset.
1996 */
1997hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08001998 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001999 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2000
rginda35c456b2012-02-09 17:29:05 -08002001 if (this.screen_.rowsArray.length &&
2002 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2003 // If the screen changed sizes while we were away, our rowIndexes may
2004 // be incorrect.
2005 var offset = this.scrollbackRows_.length;
2006 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002007 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002008 ary[i].rowIndex = offset + i;
2009 }
2010 }
rginda8ba33642011-12-14 12:31:31 -08002011
rginda35c456b2012-02-09 17:29:05 -08002012 this.realizeWidth_(this.screenSize.width);
2013 this.realizeHeight_(this.screenSize.height);
2014 this.scrollPort_.syncScrollHeight();
2015 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002016
rginda6d397402012-01-17 10:58:29 -08002017 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002018 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002019};
2020
2021/**
2022 * Set the cursor-blink mode bit.
2023 *
2024 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2025 * a visible cursor does not blink.
2026 *
2027 * You should make sure to turn blinking off if you're going to dispose of a
2028 * terminal, otherwise you'll leak a timeout.
2029 *
2030 * Defaults to on.
2031 *
2032 * @param {boolean} state True to set cursor-blink mode, false to unset.
2033 */
2034hterm.Terminal.prototype.setCursorBlink = function(state) {
2035 this.options_.cursorBlink = state;
2036
2037 if (!state && this.timeouts_.cursorBlink) {
2038 clearTimeout(this.timeouts_.cursorBlink);
2039 delete this.timeouts_.cursorBlink;
2040 }
2041
2042 if (this.options_.cursorVisible)
2043 this.setCursorVisible(true);
2044};
2045
2046/**
2047 * Set the cursor-visible mode bit.
2048 *
2049 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2050 *
2051 * Defaults to on.
2052 *
2053 * @param {boolean} state True to set cursor-visible mode, false to unset.
2054 */
2055hterm.Terminal.prototype.setCursorVisible = function(state) {
2056 this.options_.cursorVisible = state;
2057
2058 if (!state) {
rginda87b86462011-12-14 13:48:03 -08002059 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002060 return;
2061 }
2062
rginda87b86462011-12-14 13:48:03 -08002063 this.syncCursorPosition_();
2064
2065 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002066
2067 if (this.options_.cursorBlink) {
2068 if (this.timeouts_.cursorBlink)
2069 return;
2070
2071 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
2072 500);
2073 } else {
2074 if (this.timeouts_.cursorBlink) {
2075 clearTimeout(this.timeouts_.cursorBlink);
2076 delete this.timeouts_.cursorBlink;
2077 }
2078 }
2079};
2080
2081/**
rginda87b86462011-12-14 13:48:03 -08002082 * Synchronizes the visible cursor and document selection with the current
2083 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002084 */
2085hterm.Terminal.prototype.syncCursorPosition_ = function() {
2086 var topRowIndex = this.scrollPort_.getTopRowIndex();
2087 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2088 var cursorRowIndex = this.scrollbackRows_.length +
2089 this.screen_.cursorPosition.row;
2090
2091 if (cursorRowIndex > bottomRowIndex) {
2092 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002093 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002094 return;
2095 }
2096
rginda35c456b2012-02-09 17:29:05 -08002097 this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px';
2098 this.cursorNode_.style.height = this.scrollPort_.characterSize.height + 'px';
2099
rginda8ba33642011-12-14 12:31:31 -08002100 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002101 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2102 'px';
2103 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2104 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002105
2106 this.cursorNode_.setAttribute('title',
2107 '(' + this.screen_.cursorPosition.row +
2108 ', ' + this.screen_.cursorPosition.column +
2109 ')');
2110
2111 // Update the caret for a11y purposes.
2112 var selection = this.document_.getSelection();
2113 if (selection && selection.isCollapsed)
2114 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002115};
2116
2117/**
2118 * Synchronizes the visible cursor with the current cursor coordinates.
2119 *
2120 * The sync will happen asynchronously, soon after the call stack winds down.
2121 * Multiple calls will be coalesced into a single sync.
2122 */
2123hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2124 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002125 return;
rginda8ba33642011-12-14 12:31:31 -08002126
2127 var self = this;
2128 this.timeouts_.syncCursor = setTimeout(function() {
2129 self.syncCursorPosition_();
2130 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002131 }, 0);
2132};
2133
rgindacc2996c2012-02-24 14:59:31 -08002134/**
rgindaf522ce02012-04-17 17:49:17 -07002135 * Show or hide the zoom warning.
2136 *
2137 * The zoom warning is a message warning the user that their browser zoom must
2138 * be set to 100% in order for hterm to function properly.
2139 *
2140 * @param {boolean} state True to show the message, false to hide it.
2141 */
2142hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2143 if (!this.zoomWarningNode_) {
2144 if (!state)
2145 return;
2146
2147 this.zoomWarningNode_ = this.document_.createElement('div');
2148 this.zoomWarningNode_.style.cssText = (
2149 'color: black;' +
2150 'background-color: #ff2222;' +
2151 'font-size: large;' +
2152 'border-radius: 8px;' +
2153 'opacity: 0.75;' +
2154 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2155 'top: 0.5em;' +
2156 'right: 1.2em;' +
2157 'position: absolute;' +
2158 '-webkit-text-size-adjust: none;' +
2159 '-webkit-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002160 }
2161
Robert Gindab4839c22013-02-28 16:52:10 -08002162 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2163 hterm.zoomWarningMessage,
2164 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2165
rgindaf522ce02012-04-17 17:49:17 -07002166 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2167
2168 if (state) {
2169 if (!this.zoomWarningNode_.parentNode)
2170 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2171 } else if (this.zoomWarningNode_.parentNode) {
2172 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2173 }
2174};
2175
2176/**
rgindacc2996c2012-02-24 14:59:31 -08002177 * Show the terminal overlay for a given amount of time.
2178 *
2179 * The terminal overlay appears in inverse video in a large font, centered
2180 * over the terminal. You should probably keep the overlay message brief,
2181 * since it's in a large font and you probably aren't going to check the size
2182 * of the terminal first.
2183 *
2184 * @param {string} msg The text (not HTML) message to display in the overlay.
2185 * @param {number} opt_timeout The amount of time to wait before fading out
2186 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2187 * stay up forever (or until the next overlay).
2188 */
2189hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002190 if (!this.overlayNode_) {
2191 if (!this.div_)
2192 return;
2193
2194 this.overlayNode_ = this.document_.createElement('div');
2195 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002196 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002197 'font-size: xx-large;' +
2198 'opacity: 0.75;' +
2199 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2200 'position: absolute;' +
2201 '-webkit-user-select: none;' +
2202 '-webkit-transition: opacity 180ms ease-in;');
2203 }
2204
rginda9f5222b2012-03-05 11:53:28 -08002205 this.overlayNode_.style.color = this.prefs_.get('background-color');
2206 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2207 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2208
rgindaf0090c92012-02-10 14:58:52 -08002209 this.overlayNode_.textContent = msg;
2210 this.overlayNode_.style.opacity = '0.75';
2211
2212 if (!this.overlayNode_.parentNode)
2213 this.div_.appendChild(this.overlayNode_);
2214
Robert Ginda97769282013-02-01 15:30:30 -08002215 var divSize = hterm.getClientSize(this.div_);
2216 var overlaySize = hterm.getClientSize(this.overlayNode_);
2217
2218 this.overlayNode_.style.top = (divSize.height - overlaySize.height) / 2;
2219 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
2220 this.scrollPort_.currentScrollbarWidthPx) / 2;
rgindaf0090c92012-02-10 14:58:52 -08002221
2222 var self = this;
2223
2224 if (this.overlayTimeout_)
2225 clearTimeout(this.overlayTimeout_);
2226
rgindacc2996c2012-02-24 14:59:31 -08002227 if (opt_timeout === null)
2228 return;
2229
rgindaf0090c92012-02-10 14:58:52 -08002230 this.overlayTimeout_ = setTimeout(function() {
2231 self.overlayNode_.style.opacity = '0';
2232 setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002233 if (self.overlayNode_.parentNode)
2234 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002235 self.overlayTimeout_ = null;
2236 self.overlayNode_.style.opacity = '0.75';
2237 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002238 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002239};
2240
rginda4bba5e12012-06-20 16:15:30 -07002241/**
2242 * Paste from the system clipboard to the terminal.
2243 */
2244hterm.Terminal.prototype.paste = function() {
2245 hterm.pasteFromClipboard(this.document_);
2246};
2247
2248/**
2249 * Copy a string to the system clipboard.
2250 *
2251 * Note: If there is a selected range in the terminal, it'll be cleared.
2252 */
2253hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda9fb38222012-09-11 14:19:12 -07002254 if (this.prefs_.get('enable-clipboard-notice'))
Robert Gindab4839c22013-02-28 16:52:10 -08002255 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
rgindaa09e7332012-08-17 12:49:51 -07002256
2257 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002258 copySource.textContent = str;
2259 copySource.style.cssText = (
2260 '-webkit-user-select: text;' +
2261 'position: absolute;' +
2262 'top: -99px');
2263
2264 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002265
rginda4bba5e12012-06-20 16:15:30 -07002266 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002267 var anchorNode = selection.anchorNode;
2268 var anchorOffset = selection.anchorOffset;
2269 var focusNode = selection.focusNode;
2270 var focusOffset = selection.focusOffset;
2271
rginda4bba5e12012-06-20 16:15:30 -07002272 selection.selectAllChildren(copySource);
2273
rgindaa09e7332012-08-17 12:49:51 -07002274 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002275
rgindafaa74742012-08-21 13:34:03 -07002276 selection.collapse(anchorNode, anchorOffset);
2277 selection.extend(focusNode, focusOffset);
2278
rginda4bba5e12012-06-20 16:15:30 -07002279 copySource.parentNode.removeChild(copySource);
2280};
2281
rgindaa09e7332012-08-17 12:49:51 -07002282hterm.Terminal.prototype.getSelectionText = function() {
2283 var selection = this.scrollPort_.selection;
2284 selection.sync();
2285
2286 if (selection.isCollapsed)
2287 return null;
2288
2289
2290 // Start offset measures from the beginning of the line.
2291 var startOffset = selection.startOffset;
2292 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002293
Robert Gindafdbb3f22012-09-06 20:23:06 -07002294 if (node.nodeName != 'X-ROW') {
2295 // If the selection doesn't start on an x-row node, then it must be
2296 // somewhere inside the x-row. Add any characters from previous siblings
2297 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002298
2299 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2300 // If node is the text node in a styled span, move up to the span node.
2301 node = node.parentNode;
2302 }
2303
Robert Gindafdbb3f22012-09-06 20:23:06 -07002304 while (node.previousSibling) {
2305 node = node.previousSibling;
2306 startOffset += node.textContent.length;
2307 }
rgindaa09e7332012-08-17 12:49:51 -07002308 }
2309
2310 // End offset measures from the end of the line.
2311 var endOffset = selection.endNode.textContent.length - selection.endOffset;
2312 var node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002313
Robert Gindafdbb3f22012-09-06 20:23:06 -07002314 if (node.nodeName != 'X-ROW') {
2315 // If the selection doesn't end on an x-row node, then it must be
2316 // somewhere inside the x-row. Add any characters from following siblings
2317 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002318
2319 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2320 // If node is the text node in a styled span, move up to the span node.
2321 node = node.parentNode;
2322 }
2323
Robert Gindafdbb3f22012-09-06 20:23:06 -07002324 while (node.nextSibling) {
2325 node = node.nextSibling;
2326 endOffset += node.textContent.length;
2327 }
rgindaa09e7332012-08-17 12:49:51 -07002328 }
2329
2330 var rv = this.getRowsText(selection.startRow.rowIndex,
2331 selection.endRow.rowIndex + 1);
2332 return rv.substring(startOffset, rv.length - endOffset);
2333};
2334
rginda4bba5e12012-06-20 16:15:30 -07002335/**
2336 * Copy the current selection to the system clipboard, then clear it after a
2337 * short delay.
2338 */
2339hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002340 var text = this.getSelectionText();
2341 if (text != null)
2342 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002343};
2344
rgindaf0090c92012-02-10 14:58:52 -08002345hterm.Terminal.prototype.overlaySize = function() {
2346 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2347};
2348
rginda87b86462011-12-14 13:48:03 -08002349/**
2350 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2351 *
2352 * @param {string} string The VT string representing the keystroke.
2353 */
2354hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002355 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002356 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2357
2358 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08002359};
2360
2361/**
rgindad5613292012-06-19 15:40:37 -07002362 * Add the terminalRow and terminalColumn properties to mouse events and
2363 * then forward on to onMouse().
2364 *
2365 * The terminalRow and terminalColumn properties contain the (row, column)
2366 * coordinates for the mouse event.
2367 */
2368hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002369 if (e.processedByTerminalHandler_) {
2370 // We register our event handlers on the document, as well as the cursor
2371 // and the scroll blocker. Mouse events that occur on the cursor or
2372 // scroll blocker will also appear on the document, but we don't want to
2373 // process them twice.
2374 //
2375 // We can't just prevent bubbling because that has other side effects, so
2376 // we decorate the event object with this property instead.
2377 return;
2378 }
2379
2380 e.processedByTerminalHandler_ = true;
2381
rginda4bba5e12012-06-20 16:15:30 -07002382 if (e.type == 'mousedown' && e.which == this.mousePasteButton) {
2383 this.paste();
2384 return;
2385 }
2386
2387 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2388 !this.document_.getSelection().isCollapsed) {
rgindafaa74742012-08-21 13:34:03 -07002389 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002390 return;
2391 }
2392
rgindad5613292012-06-19 15:40:37 -07002393 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2394 this.scrollPort_.characterSize.height) + 1;
2395 e.terminalColumn = parseInt(e.clientX /
2396 this.scrollPort_.characterSize.width) + 1;
2397
2398 if (e.type == 'mousedown') {
2399 if (e.terminalColumn > this.screenSize.width) {
2400 // Mousedown in the scrollbar area.
2401 return;
2402 }
2403
2404 if (!this.enableMouseDragScroll) {
2405 // Move the scroll-blocker into place if we want to keep the scrollport
2406 // from scrolling.
2407 this.scrollBlockerNode_.engaged = true;
2408 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2409 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2410 }
2411 } else if (this.scrollBlockerNode_.engaged &&
2412 (e.type == 'mousemove' || e.type == 'mouseup')) {
2413 // Disengage the scroll-blocker after one of these events.
2414 this.scrollBlockerNode_.engaged = false;
2415 this.scrollBlockerNode_.style.top = '-99px';
2416 }
2417
rgindafaa74742012-08-21 13:34:03 -07002418 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07002419};
2420
2421/**
2422 * Clients should override this if they care to know about mouse events.
2423 *
2424 * The event parameter will be a normal DOM mouse click event with additional
2425 * 'terminalRow' and 'terminalColumn' properties.
2426 */
2427hterm.Terminal.prototype.onMouse = function(e) { };
2428
2429/**
rginda8e92a692012-05-20 19:37:20 -07002430 * React when focus changes.
2431 */
2432hterm.Terminal.prototype.onFocusChange_ = function(state) {
2433 this.cursorNode_.setAttribute('focus', state ? 'true' : 'false');
2434};
2435
2436/**
rginda8ba33642011-12-14 12:31:31 -08002437 * React when the ScrollPort is scrolled.
2438 */
2439hterm.Terminal.prototype.onScroll_ = function() {
2440 this.scheduleSyncCursorPosition_();
2441};
2442
2443/**
rginda9846e2f2012-01-27 13:53:33 -08002444 * React when text is pasted into the scrollPort.
2445 */
2446hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaf2547f12012-10-25 20:36:21 -07002447 var text = this.vt.encodeUTF8(e.text);
2448 text = text.replace(/\n/mg, '\r');
2449 this.io.onVTKeystroke(text);
rginda9846e2f2012-01-27 13:53:33 -08002450};
2451
2452/**
rgindaa09e7332012-08-17 12:49:51 -07002453 * React when the user tries to copy from the scrollPort.
2454 */
2455hterm.Terminal.prototype.onCopy_ = function(e) {
2456 e.preventDefault();
rgindafaa74742012-08-21 13:34:03 -07002457 this.copySelectionToClipboard();
rgindaa09e7332012-08-17 12:49:51 -07002458};
2459
2460/**
rginda8ba33642011-12-14 12:31:31 -08002461 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002462 *
2463 * Note: This function should not directly contain code that alters the internal
2464 * state of the terminal. That kind of code belongs in realizeWidth or
2465 * realizeHeight, so that it can be executed synchronously in the case of a
2466 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002467 */
2468hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08002469 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002470 this.scrollPort_.characterSize.width);
2471 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
2472 this.scrollPort_.characterSize.height);
2473
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002474 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08002475 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002476 // gets removed from the document or during the initial load, and we can't
2477 // deal with that.
rginda35c456b2012-02-09 17:29:05 -08002478 return;
2479 }
2480
rgindaa8ba17d2012-08-15 14:41:10 -07002481 var isNewSize = (columnCount != this.screenSize.width ||
2482 rowCount != this.screenSize.height);
2483
2484 // We do this even if the size didn't change, just to be sure everything is
2485 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002486 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07002487 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07002488
2489 if (isNewSize)
2490 this.overlaySize();
2491
2492 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08002493};
2494
2495/**
2496 * Service the cursor blink timeout.
2497 */
2498hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08002499 if (this.cursorNode_.style.opacity == '0') {
2500 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002501 } else {
rginda87b86462011-12-14 13:48:03 -08002502 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002503 }
2504};
David Reveman8f552492012-03-28 12:18:41 -04002505
2506/**
2507 * Set the scrollbar-visible mode bit.
2508 *
2509 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2510 * Otherwise it will not.
2511 *
2512 * Defaults to on.
2513 *
2514 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2515 */
2516hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2517 this.scrollPort_.setScrollbarVisible(state);
2518};