blob: f535b514640e20d7657eb591f21c4e165c7ff4d3 [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.
Robert Gindae81427f2013-05-24 10:34:46 -0700672 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400673};
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_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -0700820 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -0800821};
822
823/**
rgindac9bc5502012-01-18 11:48:44 -0800824 * Full terminal reset.
825 */
rginda87b86462011-12-14 13:48:03 -0800826hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800827 this.clearAllTabStops();
828 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -0700829
830 this.clearHome(this.primaryScreen_);
831 this.primaryScreen_.textAttributes.reset();
832
833 this.clearHome(this.alternateScreen_);
834 this.alternateScreen_.textAttributes.reset();
835
rgindab8bc8932012-04-27 12:45:03 -0700836 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
837
Robert Ginda92e18102013-03-14 13:56:37 -0700838 this.vt.reset();
839
rgindac9bc5502012-01-18 11:48:44 -0800840 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800841};
842
rgindac9bc5502012-01-18 11:48:44 -0800843/**
844 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -0700845 *
846 * Perform a soft reset to the default values listed in
847 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -0800848 */
rginda0f5c0292012-01-13 11:00:13 -0800849hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -0700850 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -0800851 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -0700852
rgindab8bc8932012-04-27 12:45:03 -0700853 // Xterm also resets the color palette on soft reset, even though it doesn't
854 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -0700855 this.primaryScreen_.textAttributes.resetColorPalette();
856 this.alternateScreen_.textAttributes.resetColorPalette();
857
rgindab8bc8932012-04-27 12:45:03 -0700858 // The xterm man page explicitly says this will happen on soft reset.
859 this.setVTScrollRegion(null, null);
860
861 // Xterm also shows the cursor on soft reset, but does not alter the blink
862 // state.
rgindaa19afe22012-01-25 15:40:22 -0800863 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -0800864};
865
rgindac9bc5502012-01-18 11:48:44 -0800866/**
867 * Move the cursor forward to the next tab stop, or to the last column
868 * if no more tab stops are set.
869 */
870hterm.Terminal.prototype.forwardTabStop = function() {
871 var column = this.screen_.cursorPosition.column;
872
873 for (var i = 0; i < this.tabStops_.length; i++) {
874 if (this.tabStops_[i] > column) {
875 this.setCursorColumn(this.tabStops_[i]);
876 return;
877 }
878 }
879
David Benjamin66e954d2012-05-05 21:08:12 -0400880 // xterm does not clear the overflow flag on HT or CHT.
881 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -0800882 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -0400883 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -0800884};
885
rgindac9bc5502012-01-18 11:48:44 -0800886/**
887 * Move the cursor backward to the previous tab stop, or to the first column
888 * if no previous tab stops are set.
889 */
890hterm.Terminal.prototype.backwardTabStop = function() {
891 var column = this.screen_.cursorPosition.column;
892
893 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
894 if (this.tabStops_[i] < column) {
895 this.setCursorColumn(this.tabStops_[i]);
896 return;
897 }
898 }
899
900 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800901};
902
rgindac9bc5502012-01-18 11:48:44 -0800903/**
904 * Set a tab stop at the given column.
905 *
906 * @param {int} column Zero based column.
907 */
908hterm.Terminal.prototype.setTabStop = function(column) {
909 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
910 if (this.tabStops_[i] == column)
911 return;
912
913 if (this.tabStops_[i] < column) {
914 this.tabStops_.splice(i + 1, 0, column);
915 return;
916 }
917 }
918
919 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800920};
921
rgindac9bc5502012-01-18 11:48:44 -0800922/**
923 * Clear the tab stop at the current cursor position.
924 *
925 * No effect if there is no tab stop at the current cursor position.
926 */
927hterm.Terminal.prototype.clearTabStopAtCursor = function() {
928 var column = this.screen_.cursorPosition.column;
929
930 var i = this.tabStops_.indexOf(column);
931 if (i == -1)
932 return;
933
934 this.tabStops_.splice(i, 1);
935};
936
937/**
938 * Clear all tab stops.
939 */
940hterm.Terminal.prototype.clearAllTabStops = function() {
941 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -0400942 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -0800943};
944
945/**
946 * Set up the default tab stops, starting from a given column.
947 *
948 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -0400949 * from the specified column, or 0 if no column is provided. It also flags
950 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -0800951 *
952 * This does not clear the existing tab stops first, use clearAllTabStops
953 * for that.
954 *
955 * @param {int} opt_start Optional starting zero based starting column, useful
956 * for filling out missing tab stops when the terminal is resized.
957 */
958hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
959 var start = opt_start || 0;
960 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -0400961 // Round start up to a default tab stop.
962 start = start - 1 - ((start - 1) % w) + w;
963 for (var i = start; i < this.screenSize.width; i += w) {
964 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -0800965 }
David Benjamin66e954d2012-05-05 21:08:12 -0400966
967 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -0800968};
969
rginda6d397402012-01-17 10:58:29 -0800970/**
rginda8ba33642011-12-14 12:31:31 -0800971 * Interpret a sequence of characters.
972 *
973 * Incomplete escape sequences are buffered until the next call.
974 *
975 * @param {string} str Sequence of characters to interpret or pass through.
976 */
977hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -0800978 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -0800979 this.scheduleSyncCursorPosition_();
980};
981
982/**
983 * Take over the given DIV for use as the terminal display.
984 *
985 * @param {HTMLDivElement} div The div to use as the terminal display.
986 */
987hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -0800988 this.div_ = div;
989
rginda8ba33642011-12-14 12:31:31 -0800990 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -0700991 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -0400992 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
993 this.scrollPort_.setBackgroundPosition(
994 this.prefs_.get('background-position'));
rginda30f20f62012-04-05 16:36:19 -0700995
rginda0918b652012-04-04 11:26:24 -0700996 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -0800997
rginda9f5222b2012-03-05 11:53:28 -0800998 this.setFontSize(this.prefs_.get('font-size'));
999 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001000
David Reveman8f552492012-03-28 12:18:41 -04001001 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
1002
rginda8ba33642011-12-14 12:31:31 -08001003 this.document_ = this.scrollPort_.getDocument();
1004
rginda4bba5e12012-06-20 16:15:30 -07001005 this.document_.body.oncontextmenu = function() { return false };
1006
1007 var onMouse = this.onMouse_.bind(this);
1008 this.document_.body.firstChild.addEventListener('mousedown', onMouse);
1009 this.document_.body.firstChild.addEventListener('mouseup', onMouse);
1010 this.document_.body.firstChild.addEventListener('mousemove', onMouse);
1011 this.scrollPort_.onScrollWheel = onMouse;
1012
rginda8e92a692012-05-20 19:37:20 -07001013 this.document_.body.firstChild.addEventListener(
1014 'focus', this.onFocusChange_.bind(this, true));
1015 this.document_.body.firstChild.addEventListener(
1016 'blur', this.onFocusChange_.bind(this, false));
1017
1018 var style = this.document_.createElement('style');
1019 style.textContent =
1020 ('.cursor-node[focus="false"] {' +
1021 ' box-sizing: border-box;' +
1022 ' background-color: transparent !important;' +
1023 ' border-width: 2px;' +
1024 ' border-style: solid;' +
1025 '}');
1026 this.document_.head.appendChild(style);
1027
rginda8ba33642011-12-14 12:31:31 -08001028 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001029 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001030 this.cursorNode_.style.cssText =
1031 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001032 'top: -99px;' +
1033 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001034 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1035 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001036 '-webkit-transition: opacity, background-color 100ms linear;');
1037 this.setCursorColor(this.prefs_.get('cursor-color'));
rgindad5613292012-06-19 15:40:37 -07001038
rginda8ba33642011-12-14 12:31:31 -08001039 this.document_.body.appendChild(this.cursorNode_);
1040
rgindad5613292012-06-19 15:40:37 -07001041 // When 'enableMouseDragScroll' is off we reposition this element directly
1042 // under the mouse cursor after a click. This makes Chrome associate
1043 // subsequent mousemove events with the scroll-blocker. Since the
1044 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1045 // events do not cause the scrollport to scroll.
1046 //
1047 // It's a hack, but it's the cleanest way I could find.
1048 this.scrollBlockerNode_ = this.document_.createElement('div');
1049 this.scrollBlockerNode_.style.cssText =
1050 ('position: absolute;' +
1051 'top: -99px;' +
1052 'display: block;' +
1053 'width: 10px;' +
1054 'height: 10px;');
1055 this.document_.body.appendChild(this.scrollBlockerNode_);
1056
1057 var onMouse = this.onMouse_.bind(this);
1058 this.scrollPort_.onScrollWheel = onMouse;
1059 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1060 ].forEach(function(event) {
1061 this.scrollBlockerNode_.addEventListener(event, onMouse);
1062 this.cursorNode_.addEventListener(event, onMouse);
1063 this.document_.addEventListener(event, onMouse);
1064 }.bind(this));
1065
1066 this.cursorNode_.addEventListener('mousedown', function() {
1067 setTimeout(this.focus.bind(this));
1068 }.bind(this));
1069
rgindade84e382012-04-20 15:39:31 -07001070 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
rginda8ba33642011-12-14 12:31:31 -08001071 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001072
rginda87b86462011-12-14 13:48:03 -08001073 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001074 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001075};
1076
rginda0918b652012-04-04 11:26:24 -07001077/**
1078 * Return the HTML document that contains the terminal DOM nodes.
1079 */
rginda87b86462011-12-14 13:48:03 -08001080hterm.Terminal.prototype.getDocument = function() {
1081 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001082};
1083
1084/**
rginda0918b652012-04-04 11:26:24 -07001085 * Focus the terminal.
1086 */
1087hterm.Terminal.prototype.focus = function() {
1088 this.scrollPort_.focus();
1089};
1090
1091/**
rginda8ba33642011-12-14 12:31:31 -08001092 * Return the HTML Element for a given row index.
1093 *
1094 * This is a method from the RowProvider interface. The ScrollPort uses
1095 * it to fetch rows on demand as they are scrolled into view.
1096 *
1097 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1098 * pairs to conserve memory.
1099 *
1100 * @param {integer} index The zero-based row index, measured relative to the
1101 * start of the scrollback buffer. On-screen rows will always have the
1102 * largest indicies.
1103 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1104 */
1105hterm.Terminal.prototype.getRowNode = function(index) {
1106 if (index < this.scrollbackRows_.length)
1107 return this.scrollbackRows_[index];
1108
1109 var screenIndex = index - this.scrollbackRows_.length;
1110 return this.screen_.rowsArray[screenIndex];
1111};
1112
1113/**
1114 * Return the text content for a given range of rows.
1115 *
1116 * This is a method from the RowProvider interface. The ScrollPort uses
1117 * it to fetch text content on demand when the user attempts to copy their
1118 * selection to the clipboard.
1119 *
1120 * @param {integer} start The zero-based row index to start from, measured
1121 * relative to the start of the scrollback buffer. On-screen rows will
1122 * always have the largest indicies.
1123 * @param {integer} end The zero-based row index to end on, measured
1124 * relative to the start of the scrollback buffer.
1125 * @return {string} A single string containing the text value of the range of
1126 * rows. Lines will be newline delimited, with no trailing newline.
1127 */
1128hterm.Terminal.prototype.getRowsText = function(start, end) {
1129 var ary = [];
1130 for (var i = start; i < end; i++) {
1131 var node = this.getRowNode(i);
1132 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001133 if (i < end - 1 && !node.getAttribute('line-overflow'))
1134 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001135 }
1136
rgindaa09e7332012-08-17 12:49:51 -07001137 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001138};
1139
1140/**
1141 * Return the text content for a given row.
1142 *
1143 * This is a method from the RowProvider interface. The ScrollPort uses
1144 * it to fetch text content on demand when the user attempts to copy their
1145 * selection to the clipboard.
1146 *
1147 * @param {integer} index The zero-based row index to return, measured
1148 * relative to the start of the scrollback buffer. On-screen rows will
1149 * always have the largest indicies.
1150 * @return {string} A string containing the text value of the selected row.
1151 */
1152hterm.Terminal.prototype.getRowText = function(index) {
1153 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001154 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001155};
1156
1157/**
1158 * Return the total number of rows in the addressable screen and in the
1159 * scrollback buffer of this terminal.
1160 *
1161 * This is a method from the RowProvider interface. The ScrollPort uses
1162 * it to compute the size of the scrollbar.
1163 *
1164 * @return {integer} The number of rows in this terminal.
1165 */
1166hterm.Terminal.prototype.getRowCount = function() {
1167 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1168};
1169
1170/**
1171 * Create DOM nodes for new rows and append them to the end of the terminal.
1172 *
1173 * This is the only correct way to add a new DOM node for a row. Notice that
1174 * the new row is appended to the bottom of the list of rows, and does not
1175 * require renumbering (of the rowIndex property) of previous rows.
1176 *
1177 * If you think you want a new blank row somewhere in the middle of the
1178 * terminal, look into moveRows_().
1179 *
1180 * This method does not pay attention to vtScrollTop/Bottom, since you should
1181 * be using moveRows() in cases where they would matter.
1182 *
1183 * The cursor will be positioned at column 0 of the first inserted line.
1184 */
1185hterm.Terminal.prototype.appendRows_ = function(count) {
1186 var cursorRow = this.screen_.rowsArray.length;
1187 var offset = this.scrollbackRows_.length + cursorRow;
1188 for (var i = 0; i < count; i++) {
1189 var row = this.document_.createElement('x-row');
1190 row.appendChild(this.document_.createTextNode(''));
1191 row.rowIndex = offset + i;
1192 this.screen_.pushRow(row);
1193 }
1194
1195 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1196 if (extraRows > 0) {
1197 var ary = this.screen_.shiftRows(extraRows);
1198 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001199 if (this.scrollPort_.isScrolledEnd)
1200 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001201 }
1202
1203 if (cursorRow >= this.screen_.rowsArray.length)
1204 cursorRow = this.screen_.rowsArray.length - 1;
1205
rginda87b86462011-12-14 13:48:03 -08001206 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001207};
1208
1209/**
1210 * Relocate rows from one part of the addressable screen to another.
1211 *
1212 * This is used to recycle rows during VT scrolls (those which are driven
1213 * by VT commands, rather than by the user manipulating the scrollbar.)
1214 *
1215 * In this case, the blank lines scrolled into the scroll region are made of
1216 * the nodes we scrolled off. These have their rowIndex properties carefully
1217 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001218 */
1219hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1220 var ary = this.screen_.removeRows(fromIndex, count);
1221 this.screen_.insertRows(toIndex, ary);
1222
1223 var start, end;
1224 if (fromIndex < toIndex) {
1225 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001226 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001227 } else {
1228 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001229 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001230 }
1231
1232 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001233 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001234};
1235
1236/**
1237 * Renumber the rowIndex property of the given range of rows.
1238 *
1239 * The start and end indicies are relative to the screen, not the scrollback.
1240 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001241 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001242 * no need to renumber scrollback rows.
1243 */
Robert Ginda40932892012-12-10 17:26:40 -08001244hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1245 var screen = opt_screen || this.screen_;
1246
rginda8ba33642011-12-14 12:31:31 -08001247 var offset = this.scrollbackRows_.length;
1248 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001249 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001250 }
1251};
1252
1253/**
1254 * Print a string to the terminal.
1255 *
1256 * This respects the current insert and wraparound modes. It will add new lines
1257 * to the end of the terminal, scrolling off the top into the scrollback buffer
1258 * if necessary.
1259 *
1260 * The string is *not* parsed for escape codes. Use the interpret() method if
1261 * that's what you're after.
1262 *
1263 * @param{string} str The string to print.
1264 */
1265hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001266 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001267
rgindaa9abdd82012-08-06 18:05:09 -07001268 while (startOffset < str.length) {
rgindaa09e7332012-08-17 12:49:51 -07001269 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1270 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001271 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001272 }
rgindaa19afe22012-01-25 15:40:22 -08001273
rgindaa9abdd82012-08-06 18:05:09 -07001274 var count = str.length - startOffset;
1275 var didOverflow = false;
1276 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001277
rgindaa9abdd82012-08-06 18:05:09 -07001278 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1279 didOverflow = true;
1280 count = this.screenSize.width - this.screen_.cursorPosition.column;
1281 }
rgindaa19afe22012-01-25 15:40:22 -08001282
rgindaa9abdd82012-08-06 18:05:09 -07001283 if (didOverflow && !this.options_.wraparound) {
1284 // If the string overflowed the line but wraparound is off, then the
1285 // last printed character should be the last of the string.
1286 // TODO: This will add to our problems with multibyte UTF-16 characters.
1287 substr = str.substr(startOffset, count - 1) +
1288 str.substr(str.length - 1);
1289 count = str.length;
1290 } else {
1291 substr = str.substr(startOffset, count);
1292 }
rgindaa19afe22012-01-25 15:40:22 -08001293
rgindaa9abdd82012-08-06 18:05:09 -07001294 if (this.options_.insertMode) {
1295 this.screen_.insertString(substr);
1296 } else {
1297 this.screen_.overwriteString(substr);
1298 }
1299
1300 this.screen_.maybeClipCurrentRow();
1301 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001302 }
rginda8ba33642011-12-14 12:31:31 -08001303
1304 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001305
rginda9f5222b2012-03-05 11:53:28 -08001306 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001307 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001308};
1309
1310/**
rginda87b86462011-12-14 13:48:03 -08001311 * Set the VT scroll region.
1312 *
rginda87b86462011-12-14 13:48:03 -08001313 * This also resets the cursor position to the absolute (0, 0) position, since
1314 * that's what xterm appears to do.
1315 *
1316 * @param {integer} scrollTop The zero-based top of the scroll region.
1317 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1318 * inclusive.
1319 */
1320hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
1321 this.vtScrollTop_ = scrollTop;
1322 this.vtScrollBottom_ = scrollBottom;
rginda87b86462011-12-14 13:48:03 -08001323};
1324
1325/**
rginda8ba33642011-12-14 12:31:31 -08001326 * Return the top row index according to the VT.
1327 *
1328 * This will return 0 unless the terminal has been told to restrict scrolling
1329 * to some lower row. It is used for some VT cursor positioning and scrolling
1330 * commands.
1331 *
1332 * @return {integer} The topmost row in the terminal's scroll region.
1333 */
1334hterm.Terminal.prototype.getVTScrollTop = function() {
1335 if (this.vtScrollTop_ != null)
1336 return this.vtScrollTop_;
1337
1338 return 0;
rginda87b86462011-12-14 13:48:03 -08001339};
rginda8ba33642011-12-14 12:31:31 -08001340
1341/**
1342 * Return the bottom row index according to the VT.
1343 *
1344 * This will return the height of the terminal unless the it has been told to
1345 * restrict scrolling to some higher row. It is used for some VT cursor
1346 * positioning and scrolling commands.
1347 *
1348 * @return {integer} The bottommost row in the terminal's scroll region.
1349 */
1350hterm.Terminal.prototype.getVTScrollBottom = function() {
1351 if (this.vtScrollBottom_ != null)
1352 return this.vtScrollBottom_;
1353
rginda87b86462011-12-14 13:48:03 -08001354 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001355}
1356
1357/**
1358 * Process a '\n' character.
1359 *
1360 * If the cursor is on the final row of the terminal this will append a new
1361 * blank row to the screen and scroll the topmost row into the scrollback
1362 * buffer.
1363 *
1364 * Otherwise, this moves the cursor to column zero of the next row.
1365 */
1366hterm.Terminal.prototype.newLine = function() {
1367 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -08001368 // If we're at the end of the screen we need to append a new line and
1369 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -08001370 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -08001371 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
1372 // End of the scroll region does not affect the scrollback buffer.
1373 this.vtScrollUp(1);
1374 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -08001375 } else {
rginda87b86462011-12-14 13:48:03 -08001376 // Anywhere else in the screen just moves the cursor.
1377 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001378 }
1379};
1380
1381/**
1382 * Like newLine(), except maintain the cursor column.
1383 */
1384hterm.Terminal.prototype.lineFeed = function() {
1385 var column = this.screen_.cursorPosition.column;
1386 this.newLine();
1387 this.setCursorColumn(column);
1388};
1389
1390/**
rginda87b86462011-12-14 13:48:03 -08001391 * If autoCarriageReturn is set then newLine(), else lineFeed().
1392 */
1393hterm.Terminal.prototype.formFeed = function() {
1394 if (this.options_.autoCarriageReturn) {
1395 this.newLine();
1396 } else {
1397 this.lineFeed();
1398 }
1399};
1400
1401/**
1402 * Move the cursor up one row, possibly inserting a blank line.
1403 *
1404 * The cursor column is not changed.
1405 */
1406hterm.Terminal.prototype.reverseLineFeed = function() {
1407 var scrollTop = this.getVTScrollTop();
1408 var currentRow = this.screen_.cursorPosition.row;
1409
1410 if (currentRow == scrollTop) {
1411 this.insertLines(1);
1412 } else {
1413 this.setAbsoluteCursorRow(currentRow - 1);
1414 }
1415};
1416
1417/**
rginda8ba33642011-12-14 12:31:31 -08001418 * Replace all characters to the left of the current cursor with the space
1419 * character.
1420 *
1421 * TODO(rginda): This should probably *remove* the characters (not just replace
1422 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001423 * position.
rginda8ba33642011-12-14 12:31:31 -08001424 */
1425hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001426 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001427 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001428 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001429 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001430};
1431
1432/**
David Benjamin684a9b72012-05-01 17:19:58 -04001433 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001434 *
1435 * The cursor position is unchanged.
1436 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001437 * If the current background color is not the default background color this
1438 * will insert spaces rather than delete. This is unfortunate because the
1439 * trailing space will affect text selection, but it's difficult to come up
1440 * with a way to style empty space that wouldn't trip up the hterm.Screen
1441 * code.
rginda8ba33642011-12-14 12:31:31 -08001442 */
1443hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001444 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1445 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001446
1447 if (this.screen_.textAttributes.background ===
1448 this.screen_.textAttributes.DEFAULT_COLOR) {
1449 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
1450 if (cursorRow.textContent.length <=
1451 this.screen_.cursorPosition.column + count) {
1452 this.screen_.deleteChars(count);
1453 this.clearCursorOverflow();
1454 return;
1455 }
1456 }
1457
rginda87b86462011-12-14 13:48:03 -08001458 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001459 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001460 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001461 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001462};
1463
1464/**
1465 * Erase the current line.
1466 *
1467 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001468 */
1469hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001470 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001471 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001472 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001473 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001474};
1475
1476/**
David Benjamina08d78f2012-05-05 00:28:49 -04001477 * Erase all characters from the start of the screen to the current cursor
1478 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001479 *
1480 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001481 */
1482hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001483 var cursor = this.saveCursor();
1484
1485 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001486
David Benjamina08d78f2012-05-05 00:28:49 -04001487 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001488 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001489 this.screen_.clearCursorRow();
1490 }
1491
rginda87b86462011-12-14 13:48:03 -08001492 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001493 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001494};
1495
1496/**
1497 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001498 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001499 *
1500 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001501 */
1502hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001503 var cursor = this.saveCursor();
1504
1505 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001506
David Benjamina08d78f2012-05-05 00:28:49 -04001507 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001508 for (var i = cursor.row + 1; i <= bottom; i++) {
1509 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001510 this.screen_.clearCursorRow();
1511 }
1512
rginda87b86462011-12-14 13:48:03 -08001513 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001514 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001515};
1516
1517/**
1518 * Fill the terminal with a given character.
1519 *
1520 * This methods does not respect the VT scroll region.
1521 *
1522 * @param {string} ch The character to use for the fill.
1523 */
1524hterm.Terminal.prototype.fill = function(ch) {
1525 var cursor = this.saveCursor();
1526
1527 this.setAbsoluteCursorPosition(0, 0);
1528 for (var row = 0; row < this.screenSize.height; row++) {
1529 for (var col = 0; col < this.screenSize.width; col++) {
1530 this.setAbsoluteCursorPosition(row, col);
1531 this.screen_.overwriteString(ch);
1532 }
1533 }
1534
1535 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001536};
1537
1538/**
rginda9ea433c2012-03-16 11:57:00 -07001539 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001540 *
rginda9ea433c2012-03-16 11:57:00 -07001541 * This does not respect the scroll region.
1542 *
1543 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1544 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001545 */
rginda9ea433c2012-03-16 11:57:00 -07001546hterm.Terminal.prototype.clearHome = function(opt_screen) {
1547 var screen = opt_screen || this.screen_;
1548 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001549
rginda11057d52012-04-25 12:29:56 -07001550 if (bottom == 0) {
1551 // Empty screen, nothing to do.
1552 return;
1553 }
1554
rgindae4d29232012-01-19 10:47:13 -08001555 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001556 screen.setCursorPosition(i, 0);
1557 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001558 }
1559
rginda9ea433c2012-03-16 11:57:00 -07001560 screen.setCursorPosition(0, 0);
1561};
1562
1563/**
1564 * Erase the entire display without changing the cursor position.
1565 *
1566 * The cursor position is unchanged. This does not respect the scroll
1567 * region.
1568 *
1569 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1570 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001571 */
1572hterm.Terminal.prototype.clear = function(opt_screen) {
1573 var screen = opt_screen || this.screen_;
1574 var cursor = screen.cursorPosition.clone();
1575 this.clearHome(screen);
1576 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001577};
1578
1579/**
1580 * VT command to insert lines at the current cursor row.
1581 *
1582 * This respects the current scroll region. Rows pushed off the bottom are
1583 * lost (they won't show up in the scrollback buffer).
1584 *
rginda8ba33642011-12-14 12:31:31 -08001585 * @param {integer} count The number of lines to insert.
1586 */
1587hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07001588 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08001589
1590 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07001591 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08001592
Robert Ginda579186b2012-09-26 11:40:04 -07001593 // The moveCount is the number of rows we need to relocate to make room for
1594 // the new row(s). The count is the distance to move them.
1595 var moveCount = bottom - cursorRow - count + 1;
1596 if (moveCount)
1597 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08001598
Robert Ginda579186b2012-09-26 11:40:04 -07001599 for (var i = count - 1; i >= 0; i--) {
1600 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001601 this.screen_.clearCursorRow();
1602 }
rginda8ba33642011-12-14 12:31:31 -08001603};
1604
1605/**
1606 * VT command to delete lines at the current cursor row.
1607 *
1608 * New rows are added to the bottom of scroll region to take their place. New
1609 * rows are strictly there to take up space and have no content or style.
1610 */
1611hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001612 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001613
rginda87b86462011-12-14 13:48:03 -08001614 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001615 var bottom = this.getVTScrollBottom();
1616
rginda87b86462011-12-14 13:48:03 -08001617 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001618 count = Math.min(count, maxCount);
1619
rginda87b86462011-12-14 13:48:03 -08001620 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001621 if (count != maxCount)
1622 this.moveRows_(top, count, moveStart);
1623
1624 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001625 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001626 this.screen_.clearCursorRow();
1627 }
1628
rginda87b86462011-12-14 13:48:03 -08001629 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001630 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001631};
1632
1633/**
1634 * Inserts the given number of spaces at the current cursor position.
1635 *
rginda87b86462011-12-14 13:48:03 -08001636 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001637 */
1638hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001639 var cursor = this.saveCursor();
1640
rgindacbbd7482012-06-13 15:06:16 -07001641 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001642 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001643 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001644
1645 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001646 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001647};
1648
1649/**
1650 * Forward-delete the specified number of characters starting at the cursor
1651 * position.
1652 *
1653 * @param {integer} count The number of characters to delete.
1654 */
1655hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001656 var deleted = this.screen_.deleteChars(count);
1657 if (deleted && !this.screen_.textAttributes.isDefault()) {
1658 var cursor = this.saveCursor();
1659 this.setCursorColumn(this.screenSize.width - deleted);
1660 this.screen_.insertString(lib.f.getWhitespace(deleted));
1661 this.restoreCursor(cursor);
1662 }
1663
David Benjamin54e8bf62012-06-01 22:31:40 -04001664 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001665};
1666
1667/**
1668 * Shift rows in the scroll region upwards by a given number of lines.
1669 *
1670 * New rows are inserted at the bottom of the scroll region to fill the
1671 * vacated rows. The new rows not filled out with the current text attributes.
1672 *
1673 * This function does not affect the scrollback rows at all. Rows shifted
1674 * off the top are lost.
1675 *
rginda87b86462011-12-14 13:48:03 -08001676 * The cursor position is not altered.
1677 *
rginda8ba33642011-12-14 12:31:31 -08001678 * @param {integer} count The number of rows to scroll.
1679 */
1680hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001681 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001682
rginda87b86462011-12-14 13:48:03 -08001683 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001684 this.deleteLines(count);
1685
rginda87b86462011-12-14 13:48:03 -08001686 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001687};
1688
1689/**
1690 * Shift rows below the cursor down by a given number of lines.
1691 *
1692 * This function respects the current scroll region.
1693 *
1694 * New rows are inserted at the top of the scroll region to fill the
1695 * vacated rows. The new rows not filled out with the current text attributes.
1696 *
1697 * This function does not affect the scrollback rows at all. Rows shifted
1698 * off the bottom are lost.
1699 *
1700 * @param {integer} count The number of rows to scroll.
1701 */
1702hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001703 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001704
rginda87b86462011-12-14 13:48:03 -08001705 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001706 this.insertLines(opt_count);
1707
rginda87b86462011-12-14 13:48:03 -08001708 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001709};
1710
rginda87b86462011-12-14 13:48:03 -08001711
rginda8ba33642011-12-14 12:31:31 -08001712/**
1713 * Set the cursor position.
1714 *
1715 * The cursor row is relative to the scroll region if the terminal has
1716 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1717 *
1718 * @param {integer} row The new zero-based cursor row.
1719 * @param {integer} row The new zero-based cursor column.
1720 */
1721hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1722 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001723 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001724 } else {
rginda87b86462011-12-14 13:48:03 -08001725 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001726 }
rginda87b86462011-12-14 13:48:03 -08001727};
rginda8ba33642011-12-14 12:31:31 -08001728
rginda87b86462011-12-14 13:48:03 -08001729hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1730 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07001731 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
1732 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001733 this.screen_.setCursorPosition(row, column);
1734};
1735
1736hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07001737 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
1738 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001739 this.screen_.setCursorPosition(row, column);
1740};
1741
1742/**
1743 * Set the cursor column.
1744 *
1745 * @param {integer} column The new zero-based cursor column.
1746 */
1747hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001748 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001749};
1750
1751/**
1752 * Return the cursor column.
1753 *
1754 * @return {integer} The zero-based cursor column.
1755 */
1756hterm.Terminal.prototype.getCursorColumn = function() {
1757 return this.screen_.cursorPosition.column;
1758};
1759
1760/**
1761 * Set the cursor row.
1762 *
1763 * The cursor row is relative to the scroll region if the terminal has
1764 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1765 *
1766 * @param {integer} row The new cursor row.
1767 */
rginda87b86462011-12-14 13:48:03 -08001768hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1769 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001770};
1771
1772/**
1773 * Return the cursor row.
1774 *
1775 * @return {integer} The zero-based cursor row.
1776 */
1777hterm.Terminal.prototype.getCursorRow = function(row) {
1778 return this.screen_.cursorPosition.row;
1779};
1780
1781/**
1782 * Request that the ScrollPort redraw itself soon.
1783 *
1784 * The redraw will happen asynchronously, soon after the call stack winds down.
1785 * Multiple calls will be coalesced into a single redraw.
1786 */
1787hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001788 if (this.timeouts_.redraw)
1789 return;
rginda8ba33642011-12-14 12:31:31 -08001790
1791 var self = this;
rginda87b86462011-12-14 13:48:03 -08001792 this.timeouts_.redraw = setTimeout(function() {
1793 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001794 self.scrollPort_.redraw_();
1795 }, 0);
1796};
1797
1798/**
1799 * Request that the ScrollPort be scrolled to the bottom.
1800 *
1801 * The scroll will happen asynchronously, soon after the call stack winds down.
1802 * Multiple calls will be coalesced into a single scroll.
1803 *
1804 * This affects the scrollbar position of the ScrollPort, and has nothing to
1805 * do with the VT scroll commands.
1806 */
1807hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1808 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001809 return;
rginda8ba33642011-12-14 12:31:31 -08001810
1811 var self = this;
1812 this.timeouts_.scrollDown = setTimeout(function() {
1813 delete self.timeouts_.scrollDown;
1814 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1815 }, 10);
1816};
1817
1818/**
1819 * Move the cursor up a specified number of rows.
1820 *
1821 * @param {integer} count The number of rows to move the cursor.
1822 */
1823hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001824 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001825};
1826
1827/**
1828 * Move the cursor down a specified number of rows.
1829 *
1830 * @param {integer} count The number of rows to move the cursor.
1831 */
1832hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001833 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001834 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1835 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1836 this.screenSize.height - 1);
1837
rgindacbbd7482012-06-13 15:06:16 -07001838 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08001839 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001840 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001841};
1842
1843/**
1844 * Move the cursor left a specified number of columns.
1845 *
1846 * @param {integer} count The number of columns to move the cursor.
1847 */
1848hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001849 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001850};
1851
1852/**
1853 * Move the cursor right a specified number of columns.
1854 *
1855 * @param {integer} count The number of columns to move the cursor.
1856 */
1857hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001858 count = count || 1;
rgindacbbd7482012-06-13 15:06:16 -07001859 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001860 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001861 this.setCursorColumn(column);
1862};
1863
1864/**
1865 * Reverse the foreground and background colors of the terminal.
1866 *
1867 * This only affects text that was drawn with no attributes.
1868 *
1869 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1870 * been drawn with attributes that happen to coincide with the default
1871 * 'no-attribute' colors. My guess is probably not.
1872 */
1873hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001874 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001875 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08001876 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
1877 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08001878 } else {
rginda9f5222b2012-03-05 11:53:28 -08001879 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
1880 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08001881 }
1882};
1883
1884/**
rginda87b86462011-12-14 13:48:03 -08001885 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07001886 *
1887 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08001888 */
1889hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08001890 this.cursorNode_.style.backgroundColor =
1891 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001892
1893 var self = this;
1894 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08001895 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08001896 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07001897
1898 if (this.bellAudio_.getAttribute('src')) {
Robert Gindaa6331372013-03-19 10:35:39 -07001899 if (this.bellSquelchTimeout_)
Robert Ginda92e18102013-03-14 13:56:37 -07001900 return;
1901
1902 this.bellAudio_.play();
1903
1904 this.bellSequelchTimeout_ = setTimeout(function() {
1905 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07001906 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07001907 } else {
1908 delete this.bellSquelchTimeout_;
1909 }
rginda87b86462011-12-14 13:48:03 -08001910};
1911
1912/**
rginda8ba33642011-12-14 12:31:31 -08001913 * Set the origin mode bit.
1914 *
1915 * If origin mode is on, certain VT cursor and scrolling commands measure their
1916 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1917 * to the top of the addressable screen.
1918 *
1919 * Defaults to off.
1920 *
1921 * @param {boolean} state True to set origin mode, false to unset.
1922 */
1923hterm.Terminal.prototype.setOriginMode = function(state) {
1924 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08001925 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08001926};
1927
1928/**
1929 * Set the insert mode bit.
1930 *
1931 * If insert mode is on, existing text beyond the cursor position will be
1932 * shifted right to make room for new text. Otherwise, new text overwrites
1933 * any existing text.
1934 *
1935 * Defaults to off.
1936 *
1937 * @param {boolean} state True to set insert mode, false to unset.
1938 */
1939hterm.Terminal.prototype.setInsertMode = function(state) {
1940 this.options_.insertMode = state;
1941};
1942
1943/**
rginda87b86462011-12-14 13:48:03 -08001944 * Set the auto carriage return bit.
1945 *
1946 * If auto carriage return is on then a formfeed character is interpreted
1947 * as a newline, otherwise it's the same as a linefeed. The difference boils
1948 * down to whether or not the cursor column is reset.
1949 */
1950hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1951 this.options_.autoCarriageReturn = state;
1952};
1953
1954/**
rginda8ba33642011-12-14 12:31:31 -08001955 * Set the wraparound mode bit.
1956 *
1957 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1958 * to the start of the following row. Otherwise, the cursor is clamped to the
1959 * end of the screen and attempts to write past it are ignored.
1960 *
1961 * Defaults to on.
1962 *
1963 * @param {boolean} state True to set wraparound mode, false to unset.
1964 */
1965hterm.Terminal.prototype.setWraparound = function(state) {
1966 this.options_.wraparound = state;
1967};
1968
1969/**
1970 * Set the reverse-wraparound mode bit.
1971 *
1972 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1973 * to the end of the previous row. Otherwise, the cursor is clamped to column
1974 * 0.
1975 *
1976 * Defaults to off.
1977 *
1978 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
1979 */
1980hterm.Terminal.prototype.setReverseWraparound = function(state) {
1981 this.options_.reverseWraparound = state;
1982};
1983
1984/**
1985 * Selects between the primary and alternate screens.
1986 *
1987 * If alternate mode is on, the alternate screen is active. Otherwise the
1988 * primary screen is active.
1989 *
1990 * Swapping screens has no effect on the scrollback buffer.
1991 *
1992 * Each screen maintains its own cursor position.
1993 *
1994 * Defaults to off.
1995 *
1996 * @param {boolean} state True to set alternate mode, false to unset.
1997 */
1998hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08001999 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002000 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2001
rginda35c456b2012-02-09 17:29:05 -08002002 if (this.screen_.rowsArray.length &&
2003 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2004 // If the screen changed sizes while we were away, our rowIndexes may
2005 // be incorrect.
2006 var offset = this.scrollbackRows_.length;
2007 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002008 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002009 ary[i].rowIndex = offset + i;
2010 }
2011 }
rginda8ba33642011-12-14 12:31:31 -08002012
rginda35c456b2012-02-09 17:29:05 -08002013 this.realizeWidth_(this.screenSize.width);
2014 this.realizeHeight_(this.screenSize.height);
2015 this.scrollPort_.syncScrollHeight();
2016 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002017
rginda6d397402012-01-17 10:58:29 -08002018 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002019 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002020};
2021
2022/**
2023 * Set the cursor-blink mode bit.
2024 *
2025 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2026 * a visible cursor does not blink.
2027 *
2028 * You should make sure to turn blinking off if you're going to dispose of a
2029 * terminal, otherwise you'll leak a timeout.
2030 *
2031 * Defaults to on.
2032 *
2033 * @param {boolean} state True to set cursor-blink mode, false to unset.
2034 */
2035hterm.Terminal.prototype.setCursorBlink = function(state) {
2036 this.options_.cursorBlink = state;
2037
2038 if (!state && this.timeouts_.cursorBlink) {
2039 clearTimeout(this.timeouts_.cursorBlink);
2040 delete this.timeouts_.cursorBlink;
2041 }
2042
2043 if (this.options_.cursorVisible)
2044 this.setCursorVisible(true);
2045};
2046
2047/**
2048 * Set the cursor-visible mode bit.
2049 *
2050 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2051 *
2052 * Defaults to on.
2053 *
2054 * @param {boolean} state True to set cursor-visible mode, false to unset.
2055 */
2056hterm.Terminal.prototype.setCursorVisible = function(state) {
2057 this.options_.cursorVisible = state;
2058
2059 if (!state) {
rginda87b86462011-12-14 13:48:03 -08002060 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002061 return;
2062 }
2063
rginda87b86462011-12-14 13:48:03 -08002064 this.syncCursorPosition_();
2065
2066 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002067
2068 if (this.options_.cursorBlink) {
2069 if (this.timeouts_.cursorBlink)
2070 return;
2071
2072 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
2073 500);
2074 } else {
2075 if (this.timeouts_.cursorBlink) {
2076 clearTimeout(this.timeouts_.cursorBlink);
2077 delete this.timeouts_.cursorBlink;
2078 }
2079 }
2080};
2081
2082/**
rginda87b86462011-12-14 13:48:03 -08002083 * Synchronizes the visible cursor and document selection with the current
2084 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002085 */
2086hterm.Terminal.prototype.syncCursorPosition_ = function() {
2087 var topRowIndex = this.scrollPort_.getTopRowIndex();
2088 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2089 var cursorRowIndex = this.scrollbackRows_.length +
2090 this.screen_.cursorPosition.row;
2091
2092 if (cursorRowIndex > bottomRowIndex) {
2093 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002094 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002095 return;
2096 }
2097
rginda35c456b2012-02-09 17:29:05 -08002098 this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px';
2099 this.cursorNode_.style.height = this.scrollPort_.characterSize.height + 'px';
2100
rginda8ba33642011-12-14 12:31:31 -08002101 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002102 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2103 'px';
2104 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2105 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002106
2107 this.cursorNode_.setAttribute('title',
2108 '(' + this.screen_.cursorPosition.row +
2109 ', ' + this.screen_.cursorPosition.column +
2110 ')');
2111
2112 // Update the caret for a11y purposes.
2113 var selection = this.document_.getSelection();
2114 if (selection && selection.isCollapsed)
2115 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002116};
2117
2118/**
2119 * Synchronizes the visible cursor with the current cursor coordinates.
2120 *
2121 * The sync will happen asynchronously, soon after the call stack winds down.
2122 * Multiple calls will be coalesced into a single sync.
2123 */
2124hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2125 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002126 return;
rginda8ba33642011-12-14 12:31:31 -08002127
2128 var self = this;
2129 this.timeouts_.syncCursor = setTimeout(function() {
2130 self.syncCursorPosition_();
2131 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002132 }, 0);
2133};
2134
rgindacc2996c2012-02-24 14:59:31 -08002135/**
rgindaf522ce02012-04-17 17:49:17 -07002136 * Show or hide the zoom warning.
2137 *
2138 * The zoom warning is a message warning the user that their browser zoom must
2139 * be set to 100% in order for hterm to function properly.
2140 *
2141 * @param {boolean} state True to show the message, false to hide it.
2142 */
2143hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2144 if (!this.zoomWarningNode_) {
2145 if (!state)
2146 return;
2147
2148 this.zoomWarningNode_ = this.document_.createElement('div');
2149 this.zoomWarningNode_.style.cssText = (
2150 'color: black;' +
2151 'background-color: #ff2222;' +
2152 'font-size: large;' +
2153 'border-radius: 8px;' +
2154 'opacity: 0.75;' +
2155 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2156 'top: 0.5em;' +
2157 'right: 1.2em;' +
2158 'position: absolute;' +
2159 '-webkit-text-size-adjust: none;' +
2160 '-webkit-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002161 }
2162
Robert Gindab4839c22013-02-28 16:52:10 -08002163 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2164 hterm.zoomWarningMessage,
2165 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2166
rgindaf522ce02012-04-17 17:49:17 -07002167 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2168
2169 if (state) {
2170 if (!this.zoomWarningNode_.parentNode)
2171 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2172 } else if (this.zoomWarningNode_.parentNode) {
2173 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2174 }
2175};
2176
2177/**
rgindacc2996c2012-02-24 14:59:31 -08002178 * Show the terminal overlay for a given amount of time.
2179 *
2180 * The terminal overlay appears in inverse video in a large font, centered
2181 * over the terminal. You should probably keep the overlay message brief,
2182 * since it's in a large font and you probably aren't going to check the size
2183 * of the terminal first.
2184 *
2185 * @param {string} msg The text (not HTML) message to display in the overlay.
2186 * @param {number} opt_timeout The amount of time to wait before fading out
2187 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2188 * stay up forever (or until the next overlay).
2189 */
2190hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002191 if (!this.overlayNode_) {
2192 if (!this.div_)
2193 return;
2194
2195 this.overlayNode_ = this.document_.createElement('div');
2196 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002197 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002198 'font-size: xx-large;' +
2199 'opacity: 0.75;' +
2200 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2201 'position: absolute;' +
2202 '-webkit-user-select: none;' +
2203 '-webkit-transition: opacity 180ms ease-in;');
2204 }
2205
rginda9f5222b2012-03-05 11:53:28 -08002206 this.overlayNode_.style.color = this.prefs_.get('background-color');
2207 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2208 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2209
rgindaf0090c92012-02-10 14:58:52 -08002210 this.overlayNode_.textContent = msg;
2211 this.overlayNode_.style.opacity = '0.75';
2212
2213 if (!this.overlayNode_.parentNode)
2214 this.div_.appendChild(this.overlayNode_);
2215
Robert Ginda97769282013-02-01 15:30:30 -08002216 var divSize = hterm.getClientSize(this.div_);
2217 var overlaySize = hterm.getClientSize(this.overlayNode_);
2218
2219 this.overlayNode_.style.top = (divSize.height - overlaySize.height) / 2;
2220 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
2221 this.scrollPort_.currentScrollbarWidthPx) / 2;
rgindaf0090c92012-02-10 14:58:52 -08002222
2223 var self = this;
2224
2225 if (this.overlayTimeout_)
2226 clearTimeout(this.overlayTimeout_);
2227
rgindacc2996c2012-02-24 14:59:31 -08002228 if (opt_timeout === null)
2229 return;
2230
rgindaf0090c92012-02-10 14:58:52 -08002231 this.overlayTimeout_ = setTimeout(function() {
2232 self.overlayNode_.style.opacity = '0';
2233 setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002234 if (self.overlayNode_.parentNode)
2235 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002236 self.overlayTimeout_ = null;
2237 self.overlayNode_.style.opacity = '0.75';
2238 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002239 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002240};
2241
rginda4bba5e12012-06-20 16:15:30 -07002242/**
2243 * Paste from the system clipboard to the terminal.
2244 */
2245hterm.Terminal.prototype.paste = function() {
2246 hterm.pasteFromClipboard(this.document_);
2247};
2248
2249/**
2250 * Copy a string to the system clipboard.
2251 *
2252 * Note: If there is a selected range in the terminal, it'll be cleared.
2253 */
2254hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda9fb38222012-09-11 14:19:12 -07002255 if (this.prefs_.get('enable-clipboard-notice'))
Robert Gindab4839c22013-02-28 16:52:10 -08002256 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
rgindaa09e7332012-08-17 12:49:51 -07002257
2258 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002259 copySource.textContent = str;
2260 copySource.style.cssText = (
2261 '-webkit-user-select: text;' +
2262 'position: absolute;' +
2263 'top: -99px');
2264
2265 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002266
rginda4bba5e12012-06-20 16:15:30 -07002267 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002268 var anchorNode = selection.anchorNode;
2269 var anchorOffset = selection.anchorOffset;
2270 var focusNode = selection.focusNode;
2271 var focusOffset = selection.focusOffset;
2272
rginda4bba5e12012-06-20 16:15:30 -07002273 selection.selectAllChildren(copySource);
2274
rgindaa09e7332012-08-17 12:49:51 -07002275 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002276
rgindafaa74742012-08-21 13:34:03 -07002277 selection.collapse(anchorNode, anchorOffset);
2278 selection.extend(focusNode, focusOffset);
2279
rginda4bba5e12012-06-20 16:15:30 -07002280 copySource.parentNode.removeChild(copySource);
2281};
2282
rgindaa09e7332012-08-17 12:49:51 -07002283hterm.Terminal.prototype.getSelectionText = function() {
2284 var selection = this.scrollPort_.selection;
2285 selection.sync();
2286
2287 if (selection.isCollapsed)
2288 return null;
2289
2290
2291 // Start offset measures from the beginning of the line.
2292 var startOffset = selection.startOffset;
2293 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002294
Robert Gindafdbb3f22012-09-06 20:23:06 -07002295 if (node.nodeName != 'X-ROW') {
2296 // If the selection doesn't start on an x-row node, then it must be
2297 // somewhere inside the x-row. Add any characters from previous siblings
2298 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002299
2300 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2301 // If node is the text node in a styled span, move up to the span node.
2302 node = node.parentNode;
2303 }
2304
Robert Gindafdbb3f22012-09-06 20:23:06 -07002305 while (node.previousSibling) {
2306 node = node.previousSibling;
2307 startOffset += node.textContent.length;
2308 }
rgindaa09e7332012-08-17 12:49:51 -07002309 }
2310
2311 // End offset measures from the end of the line.
2312 var endOffset = selection.endNode.textContent.length - selection.endOffset;
2313 var node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002314
Robert Gindafdbb3f22012-09-06 20:23:06 -07002315 if (node.nodeName != 'X-ROW') {
2316 // If the selection doesn't end on an x-row node, then it must be
2317 // somewhere inside the x-row. Add any characters from following siblings
2318 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002319
2320 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2321 // If node is the text node in a styled span, move up to the span node.
2322 node = node.parentNode;
2323 }
2324
Robert Gindafdbb3f22012-09-06 20:23:06 -07002325 while (node.nextSibling) {
2326 node = node.nextSibling;
2327 endOffset += node.textContent.length;
2328 }
rgindaa09e7332012-08-17 12:49:51 -07002329 }
2330
2331 var rv = this.getRowsText(selection.startRow.rowIndex,
2332 selection.endRow.rowIndex + 1);
2333 return rv.substring(startOffset, rv.length - endOffset);
2334};
2335
rginda4bba5e12012-06-20 16:15:30 -07002336/**
2337 * Copy the current selection to the system clipboard, then clear it after a
2338 * short delay.
2339 */
2340hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002341 var text = this.getSelectionText();
2342 if (text != null)
2343 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002344};
2345
rgindaf0090c92012-02-10 14:58:52 -08002346hterm.Terminal.prototype.overlaySize = function() {
2347 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2348};
2349
rginda87b86462011-12-14 13:48:03 -08002350/**
2351 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2352 *
2353 * @param {string} string The VT string representing the keystroke.
2354 */
2355hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002356 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002357 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2358
2359 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08002360};
2361
2362/**
rgindad5613292012-06-19 15:40:37 -07002363 * Add the terminalRow and terminalColumn properties to mouse events and
2364 * then forward on to onMouse().
2365 *
2366 * The terminalRow and terminalColumn properties contain the (row, column)
2367 * coordinates for the mouse event.
2368 */
2369hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002370 if (e.processedByTerminalHandler_) {
2371 // We register our event handlers on the document, as well as the cursor
2372 // and the scroll blocker. Mouse events that occur on the cursor or
2373 // scroll blocker will also appear on the document, but we don't want to
2374 // process them twice.
2375 //
2376 // We can't just prevent bubbling because that has other side effects, so
2377 // we decorate the event object with this property instead.
2378 return;
2379 }
2380
2381 e.processedByTerminalHandler_ = true;
2382
rginda4bba5e12012-06-20 16:15:30 -07002383 if (e.type == 'mousedown' && e.which == this.mousePasteButton) {
2384 this.paste();
2385 return;
2386 }
2387
2388 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2389 !this.document_.getSelection().isCollapsed) {
rgindafaa74742012-08-21 13:34:03 -07002390 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002391 return;
2392 }
2393
rgindad5613292012-06-19 15:40:37 -07002394 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2395 this.scrollPort_.characterSize.height) + 1;
2396 e.terminalColumn = parseInt(e.clientX /
2397 this.scrollPort_.characterSize.width) + 1;
2398
2399 if (e.type == 'mousedown') {
2400 if (e.terminalColumn > this.screenSize.width) {
2401 // Mousedown in the scrollbar area.
2402 return;
2403 }
2404
2405 if (!this.enableMouseDragScroll) {
2406 // Move the scroll-blocker into place if we want to keep the scrollport
2407 // from scrolling.
2408 this.scrollBlockerNode_.engaged = true;
2409 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2410 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2411 }
2412 } else if (this.scrollBlockerNode_.engaged &&
2413 (e.type == 'mousemove' || e.type == 'mouseup')) {
2414 // Disengage the scroll-blocker after one of these events.
2415 this.scrollBlockerNode_.engaged = false;
2416 this.scrollBlockerNode_.style.top = '-99px';
2417 }
2418
rgindafaa74742012-08-21 13:34:03 -07002419 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07002420};
2421
2422/**
2423 * Clients should override this if they care to know about mouse events.
2424 *
2425 * The event parameter will be a normal DOM mouse click event with additional
2426 * 'terminalRow' and 'terminalColumn' properties.
2427 */
2428hterm.Terminal.prototype.onMouse = function(e) { };
2429
2430/**
rginda8e92a692012-05-20 19:37:20 -07002431 * React when focus changes.
2432 */
2433hterm.Terminal.prototype.onFocusChange_ = function(state) {
2434 this.cursorNode_.setAttribute('focus', state ? 'true' : 'false');
2435};
2436
2437/**
rginda8ba33642011-12-14 12:31:31 -08002438 * React when the ScrollPort is scrolled.
2439 */
2440hterm.Terminal.prototype.onScroll_ = function() {
2441 this.scheduleSyncCursorPosition_();
2442};
2443
2444/**
rginda9846e2f2012-01-27 13:53:33 -08002445 * React when text is pasted into the scrollPort.
2446 */
2447hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaf2547f12012-10-25 20:36:21 -07002448 var text = this.vt.encodeUTF8(e.text);
2449 text = text.replace(/\n/mg, '\r');
2450 this.io.onVTKeystroke(text);
rginda9846e2f2012-01-27 13:53:33 -08002451};
2452
2453/**
rgindaa09e7332012-08-17 12:49:51 -07002454 * React when the user tries to copy from the scrollPort.
2455 */
2456hterm.Terminal.prototype.onCopy_ = function(e) {
2457 e.preventDefault();
rgindafaa74742012-08-21 13:34:03 -07002458 this.copySelectionToClipboard();
rgindaa09e7332012-08-17 12:49:51 -07002459};
2460
2461/**
rginda8ba33642011-12-14 12:31:31 -08002462 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002463 *
2464 * Note: This function should not directly contain code that alters the internal
2465 * state of the terminal. That kind of code belongs in realizeWidth or
2466 * realizeHeight, so that it can be executed synchronously in the case of a
2467 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002468 */
2469hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08002470 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002471 this.scrollPort_.characterSize.width);
2472 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
2473 this.scrollPort_.characterSize.height);
2474
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002475 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08002476 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002477 // gets removed from the document or during the initial load, and we can't
2478 // deal with that.
rginda35c456b2012-02-09 17:29:05 -08002479 return;
2480 }
2481
rgindaa8ba17d2012-08-15 14:41:10 -07002482 var isNewSize = (columnCount != this.screenSize.width ||
2483 rowCount != this.screenSize.height);
2484
2485 // We do this even if the size didn't change, just to be sure everything is
2486 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002487 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07002488 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07002489
2490 if (isNewSize)
2491 this.overlaySize();
2492
2493 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08002494};
2495
2496/**
2497 * Service the cursor blink timeout.
2498 */
2499hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08002500 if (this.cursorNode_.style.opacity == '0') {
2501 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002502 } else {
rginda87b86462011-12-14 13:48:03 -08002503 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002504 }
2505};
David Reveman8f552492012-03-28 12:18:41 -04002506
2507/**
2508 * Set the scrollbar-visible mode bit.
2509 *
2510 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2511 * Otherwise it will not.
2512 *
2513 * Defaults to on.
2514 *
2515 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2516 */
2517hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2518 this.scrollPort_.setScrollbarVisible(state);
2519};