blob: e8d68d396986a2d30accb261a4f2caa8211aa53f [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
7lib.rtdep('lib.colors', 'lib.PreferenceManager',
8 'hterm.msg',
Robert Ginda57f03b42012-09-13 11:02:48 -07009 'hterm.Keyboard', 'hterm.Options', 'hterm.PreferenceManager',
10 'hterm.Screen', 'hterm.ScrollPort', 'hterm.Size', 'hterm.VT');
rgindacbbd7482012-06-13 15:06:16 -070011
rginda8ba33642011-12-14 12:31:31 -080012/**
13 * Constructor for the Terminal class.
14 *
15 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
16 * classes to provide the complete terminal functionality.
17 *
18 * There are a number of lower-level Terminal methods that can be called
19 * directly to manipulate the cursor, text, scroll region, and other terminal
20 * attributes. However, the primary method is interpret(), which parses VT
21 * escape sequences and invokes the appropriate Terminal methods.
22 *
23 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
24 *
25 * TODO(rginda): Eventually we're going to need to support characters which are
26 * displayed twice as wide as standard latin characters. This is to support
27 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080028 *
Robert Ginda57f03b42012-09-13 11:02:48 -070029 * @param {string} opt_profileId Optional preference profile name. If not
rginda9f5222b2012-03-05 11:53:28 -080030 * provided, defaults to 'default'.
rginda8ba33642011-12-14 12:31:31 -080031 */
Robert Ginda57f03b42012-09-13 11:02:48 -070032hterm.Terminal = function(opt_profileId) {
33 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080034
rginda8ba33642011-12-14 12:31:31 -080035 // Two screen instances.
36 this.primaryScreen_ = new hterm.Screen();
37 this.alternateScreen_ = new hterm.Screen();
38
39 // The "current" screen.
40 this.screen_ = this.primaryScreen_;
41
rginda8ba33642011-12-14 12:31:31 -080042 // The local notion of the screen size. ScreenBuffers also have a size which
43 // indicates their present size. During size changes, the two may disagree.
44 // Also, the inactive screen's size is not altered until it is made the active
45 // screen.
46 this.screenSize = new hterm.Size(0, 0);
47
rginda8ba33642011-12-14 12:31:31 -080048 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080049 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080050 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
51 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080052 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070053 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080054
rginda87b86462011-12-14 13:48:03 -080055 // The div that contains this terminal.
56 this.div_ = null;
57
rgindac9bc5502012-01-18 11:48:44 -080058 // The document that contains the scrollPort. Defaulted to the global
59 // document here so that the terminal is functional even if it hasn't been
60 // inserted into a document yet, but re-set in decorate().
61 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080062
rginda8ba33642011-12-14 12:31:31 -080063 // The rows that have scrolled off screen and are no longer addressable.
64 this.scrollbackRows_ = [];
65
rgindac9bc5502012-01-18 11:48:44 -080066 // Saved tab stops.
67 this.tabStops_ = [];
68
David Benjamin66e954d2012-05-05 21:08:12 -040069 // Keep track of whether default tab stops have been erased; after a TBC
70 // clears all tab stops, defaults aren't restored on resize until a reset.
71 this.defaultTabStops = true;
72
rginda8ba33642011-12-14 12:31:31 -080073 // The VT's notion of the top and bottom rows. Used during some VT
74 // cursor positioning and scrolling commands.
75 this.vtScrollTop_ = null;
76 this.vtScrollBottom_ = null;
77
78 // The DIV element for the visible cursor.
79 this.cursorNode_ = null;
80
rginda9f5222b2012-03-05 11:53:28 -080081 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070082 // each output and keystroke. They are initialized by the preference manager.
83 this.scrollOnOutput_ = null;
84 this.scrollOnKeystroke_ = null;
85 this.foregroundColor_ = null;
86 this.backgroundColor_ = null;
rginda9f5222b2012-03-05 11:53:28 -080087
rgindaf0090c92012-02-10 14:58:52 -080088 // Terminal bell sound.
89 this.bellAudio_ = this.document_.createElement('audio');
rgindaf0090c92012-02-10 14:58:52 -080090 this.bellAudio_.setAttribute('preload', 'auto');
91
rginda6d397402012-01-17 10:58:29 -080092 // Cursor position and attributes saved with DECSC.
93 this.savedOptions_ = {};
94
rginda8ba33642011-12-14 12:31:31 -080095 // The current mode bits for the terminal.
96 this.options_ = new hterm.Options();
97
98 // Timeouts we might need to clear.
99 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800100
101 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800102 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800103
rgindafeaf3142012-01-31 15:14:20 -0800104 // The keyboard hander.
105 this.keyboard = new hterm.Keyboard(this);
106
rginda87b86462011-12-14 13:48:03 -0800107 // General IO interface that can be given to third parties without exposing
108 // the entire terminal object.
109 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800110
rgindad5613292012-06-19 15:40:37 -0700111 // True if mouse-click-drag should scroll the terminal.
112 this.enableMouseDragScroll = true;
113
Robert Ginda57f03b42012-09-13 11:02:48 -0700114 this.copyOnSelect = null;
rginda4bba5e12012-06-20 16:15:30 -0700115 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700116
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400117 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800118 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700119
120 this.setProfile(opt_profileId || 'default',
121 function() { this.onTerminalReady() }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800122};
123
124/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700125 * Clients should override this to be notified when the terminal is ready
126 * for use.
127 *
128 * The terminal initialization is asynchronous, and shouldn't be used before
129 * this method is called.
130 */
131hterm.Terminal.prototype.onTerminalReady = function() { };
132
133/**
rginda35c456b2012-02-09 17:29:05 -0800134 * Default tab with of 8 to match xterm.
135 */
136hterm.Terminal.prototype.tabWidth = 8;
137
138/**
rginda35c456b2012-02-09 17:29:05 -0800139 * The assumed width of a scrollbar.
140 */
141hterm.Terminal.prototype.scrollbarWidthPx = 16;
142
143/**
rginda9f5222b2012-03-05 11:53:28 -0800144 * Select a preference profile.
145 *
146 * This will load the terminal preferences for the given profile name and
147 * associate subsequent preference changes with the new preference profile.
148 *
149 * @param {string} newName The name of the preference profile. Forward slash
150 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700151 * @param {function} opt_callback Optional callback to invoke when the profile
152 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800153 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700154hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
155 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800156
Robert Ginda57f03b42012-09-13 11:02:48 -0700157 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800158
Robert Ginda57f03b42012-09-13 11:02:48 -0700159 if (this.prefs_)
160 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800161
Robert Ginda57f03b42012-09-13 11:02:48 -0700162 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
163 this.prefs_.addObservers(null, {
164 'alt-is-meta': function(v) {
165 terminal.keyboard.altIsMeta = v;
166 },
167
168 'alt-sends-what': function(v) {
169 if (!/^(escape|8-bit|browser-key)$/.test(v))
170 v = 'escape';
171
172 terminal.keyboard.altSendsWhat = v;
173 },
174
175 'audible-bell-sound': function(v) {
176 terminal.bellAudio_.setAttribute('src', v);
177 },
178
179 'background-color': function(v) {
180 terminal.setBackgroundColor(v);
181 },
182
183 'background-image': function(v) {
184 terminal.scrollPort_.setBackgroundImage(v);
185 },
186
187 'background-size': function(v) {
188 terminal.scrollPort_.setBackgroundSize(v);
189 },
190
191 'background-position': function(v) {
192 terminal.scrollPort_.setBackgroundPosition(v);
193 },
194
195 'backspace-sends-backspace': function(v) {
196 terminal.keyboard.backspaceSendsBackspace = v;
197 },
198
199 'cursor-blink': function(v) {
200 terminal.setCursorBlink(!!v);
201 },
202
203 'cursor-color': function(v) {
204 terminal.setCursorColor(v);
205 },
206
207 'color-palette-overrides': function(v) {
208 if (!(v == null || v instanceof Object || v instanceof Array)) {
209 console.warn('Preference color-palette-overrides is not an array or ' +
210 'object: ' + v);
211 return;
rginda9f5222b2012-03-05 11:53:28 -0800212 }
rginda9f5222b2012-03-05 11:53:28 -0800213
Robert Ginda57f03b42012-09-13 11:02:48 -0700214 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700215
Robert Ginda57f03b42012-09-13 11:02:48 -0700216 if (v) {
217 for (var key in v) {
218 var i = parseInt(key);
219 if (isNaN(i) || i < 0 || i > 255) {
220 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
221 continue;
222 }
223
224 if (v[i]) {
225 var rgb = lib.colors.normalizeCSS(v[i]);
226 if (rgb)
227 lib.colors.colorPalette[i] = rgb;
228 }
229 }
rginda30f20f62012-04-05 16:36:19 -0700230 }
rginda30f20f62012-04-05 16:36:19 -0700231
Robert Ginda57f03b42012-09-13 11:02:48 -0700232 terminal.primaryScreen_.textAttributes.resetColorPalette()
233 terminal.alternateScreen_.textAttributes.resetColorPalette();
234 },
rginda30f20f62012-04-05 16:36:19 -0700235
Robert Ginda57f03b42012-09-13 11:02:48 -0700236 'copy-on-select': function(v) {
237 terminal.copyOnSelect = !!v;
238 },
rginda9f5222b2012-03-05 11:53:28 -0800239
Robert Ginda57f03b42012-09-13 11:02:48 -0700240 'enable-8-bit-control': function(v) {
241 terminal.vt.enable8BitControl = !!v;
242 },
rginda30f20f62012-04-05 16:36:19 -0700243
Robert Ginda57f03b42012-09-13 11:02:48 -0700244 'enable-bold': function(v) {
245 terminal.syncBoldSafeState();
246 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400247
Robert Ginda57f03b42012-09-13 11:02:48 -0700248 'enable-clipboard-write': function(v) {
249 terminal.vt.enableClipboardWrite = !!v;
250 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400251
Robert Ginda57f03b42012-09-13 11:02:48 -0700252 'font-family': function(v) {
253 terminal.syncFontFamily();
254 },
rginda30f20f62012-04-05 16:36:19 -0700255
Robert Ginda57f03b42012-09-13 11:02:48 -0700256 'font-size': function(v) {
257 terminal.setFontSize(v);
258 },
rginda9875d902012-08-20 16:21:57 -0700259
Robert Ginda57f03b42012-09-13 11:02:48 -0700260 'font-smoothing': function(v) {
261 terminal.syncFontFamily();
262 },
rgindade84e382012-04-20 15:39:31 -0700263
Robert Ginda57f03b42012-09-13 11:02:48 -0700264 'foreground-color': function(v) {
265 terminal.setForegroundColor(v);
266 },
rginda30f20f62012-04-05 16:36:19 -0700267
Robert Ginda57f03b42012-09-13 11:02:48 -0700268 'home-keys-scroll': function(v) {
269 terminal.keyboard.homeKeysScroll = v;
270 },
rginda4bba5e12012-06-20 16:15:30 -0700271
Robert Ginda57f03b42012-09-13 11:02:48 -0700272 'max-string-sequence': function(v) {
273 terminal.vt.maxStringSequence = v;
274 },
rginda11057d52012-04-25 12:29:56 -0700275
Robert Ginda57f03b42012-09-13 11:02:48 -0700276 'meta-sends-escape': function(v) {
277 terminal.keyboard.metaSendsEscape = v;
278 },
rginda30f20f62012-04-05 16:36:19 -0700279
Robert Ginda57f03b42012-09-13 11:02:48 -0700280 'mouse-cell-motion-trick': function(v) {
281 terminal.vt.setMouseCellMotionTrick(v);
282 },
Robert Ginda9fb38222012-09-11 14:19:12 -0700283
Robert Ginda57f03b42012-09-13 11:02:48 -0700284 'mouse-paste-button': function(v) {
285 terminal.syncMousePasteButton();
286 },
rgindaa8ba17d2012-08-15 14:41:10 -0700287
Robert Ginda40932892012-12-10 17:26:40 -0800288 'pass-alt-number': function(v) {
289 if (v == null) {
290 var osx = window.navigator.userAgent.match(/Mac OS X/);
291
292 // Let Alt-1..9 pass to the browser (to control tab switching) on
293 // non-OS X systems, or if hterm is not opened in an app window.
294 v = (!osx && hterm.windowType != 'popup');
295 }
296
297 terminal.passAltNumber = v;
298 },
299
300 'pass-ctrl-number': function(v) {
301 if (v == null) {
302 var osx = window.navigator.userAgent.match(/Mac OS X/);
303
304 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
305 // non-OS X systems, or if hterm is not opened in an app window.
306 v = (!osx && hterm.windowType != 'popup');
307 }
308
309 terminal.passCtrlNumber = v;
310 },
311
312 'pass-meta-number': function(v) {
313 if (v == null) {
314 var osx = window.navigator.userAgent.match(/Mac OS X/);
315
316 // Let Meta-1..9 pass to the browser (to control tab switching) on
317 // OS X systems, or if hterm is not opened in an app window.
318 v = (osx && hterm.windowType != 'popup');
319 }
320
321 terminal.passMetaNumber = v;
322 },
323
Robert Ginda57f03b42012-09-13 11:02:48 -0700324 'scroll-on-keystroke': function(v) {
325 terminal.scrollOnKeystroke_ = v;
326 },
rginda9f5222b2012-03-05 11:53:28 -0800327
Robert Ginda57f03b42012-09-13 11:02:48 -0700328 'scroll-on-output': function(v) {
329 terminal.scrollOnOutput_ = v;
330 },
rginda30f20f62012-04-05 16:36:19 -0700331
Robert Ginda57f03b42012-09-13 11:02:48 -0700332 'scrollbar-visible': function(v) {
333 terminal.setScrollbarVisible(v);
334 },
rginda9f5222b2012-03-05 11:53:28 -0800335
Robert Ginda57f03b42012-09-13 11:02:48 -0700336 'shift-insert-paste': function(v) {
337 terminal.keyboard.shiftInsertPaste = v;
338 },
rginda9f5222b2012-03-05 11:53:28 -0800339
Robert Ginda57f03b42012-09-13 11:02:48 -0700340 'page-keys-scroll': function(v) {
341 terminal.keyboard.pageKeysScroll = v;
342 }
343 });
rginda30f20f62012-04-05 16:36:19 -0700344
Robert Ginda57f03b42012-09-13 11:02:48 -0700345 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800346 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700347
348 if (opt_callback)
349 opt_callback();
350 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800351};
352
rginda8e92a692012-05-20 19:37:20 -0700353
354/**
355 * Set the color for the cursor.
356 *
357 * If you want this setting to persist, set it through prefs_, rather than
358 * with this method.
359 */
360hterm.Terminal.prototype.setCursorColor = function(color) {
361 this.cursorNode_.style.backgroundColor = color;
362 this.cursorNode_.style.borderColor = color;
363};
364
365/**
366 * Return the current cursor color as a string.
367 */
368hterm.Terminal.prototype.getCursorColor = function() {
369 return this.cursorNode_.style.backgroundColor;
370};
371
372/**
rgindad5613292012-06-19 15:40:37 -0700373 * Enable or disable mouse based text selection in the terminal.
374 */
375hterm.Terminal.prototype.setSelectionEnabled = function(state) {
376 this.enableMouseDragScroll = state;
377 this.scrollPort_.setSelectionEnabled(state);
378};
379
380/**
rginda8e92a692012-05-20 19:37:20 -0700381 * Set the background color.
382 *
383 * If you want this setting to persist, set it through prefs_, rather than
384 * with this method.
385 */
386hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700387 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700388 this.primaryScreen_.textAttributes.setDefaults(
389 this.foregroundColor_, this.backgroundColor_);
390 this.alternateScreen_.textAttributes.setDefaults(
391 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700392 this.scrollPort_.setBackgroundColor(color);
393};
394
rginda9f5222b2012-03-05 11:53:28 -0800395/**
396 * Return the current terminal background color.
397 *
398 * Intended for use by other classes, so we don't have to expose the entire
399 * prefs_ object.
400 */
401hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700402 return this.backgroundColor_;
403};
404
405/**
406 * Set the foreground color.
407 *
408 * If you want this setting to persist, set it through prefs_, rather than
409 * with this method.
410 */
411hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700412 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700413 this.primaryScreen_.textAttributes.setDefaults(
414 this.foregroundColor_, this.backgroundColor_);
415 this.alternateScreen_.textAttributes.setDefaults(
416 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700417 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800418};
419
420/**
421 * Return the current terminal foreground color.
422 *
423 * Intended for use by other classes, so we don't have to expose the entire
424 * prefs_ object.
425 */
426hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700427 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800428};
429
430/**
rginda87b86462011-12-14 13:48:03 -0800431 * Create a new instance of a terminal command and run it with a given
432 * argument string.
433 *
434 * @param {function} commandClass The constructor for a terminal command.
435 * @param {string} argString The argument string to pass to the command.
436 */
437hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700438 var environment = this.prefs_.get('environment');
439 if (typeof environment != 'object' || environment == null)
440 environment = {};
441
rginda87b86462011-12-14 13:48:03 -0800442 var self = this;
443 this.command = new commandClass(
444 { argString: argString || '',
445 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700446 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800447 onExit: function(code) {
448 self.io.pop();
449 self.io.println(hterm.msg('COMMAND_COMPLETE',
450 [self.command.commandName, code]));
rgindafeaf3142012-01-31 15:14:20 -0800451 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700452 if (self.prefs_.get('close-on-exit'))
453 window.close();
rginda87b86462011-12-14 13:48:03 -0800454 }
455 });
456
rgindafeaf3142012-01-31 15:14:20 -0800457 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800458 this.command.run();
459};
460
461/**
rgindafeaf3142012-01-31 15:14:20 -0800462 * Returns true if the current screen is the primary screen, false otherwise.
463 */
464hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700465 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800466};
467
468/**
469 * Install the keyboard handler for this terminal.
470 *
471 * This will prevent the browser from seeing any keystrokes sent to the
472 * terminal.
473 */
474hterm.Terminal.prototype.installKeyboard = function() {
475 this.keyboard.installKeyboard(this.document_.body.firstChild);
476}
477
478/**
479 * Uninstall the keyboard handler for this terminal.
480 */
481hterm.Terminal.prototype.uninstallKeyboard = function() {
482 this.keyboard.installKeyboard(null);
483}
484
485/**
rginda35c456b2012-02-09 17:29:05 -0800486 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800487 *
488 * Call setFontSize(0) to reset to the default font size.
489 *
490 * This function does not modify the font-size preference.
491 *
492 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800493 */
494hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800495 if (px === 0)
496 px = this.prefs_.get('font-size');
497
rginda35c456b2012-02-09 17:29:05 -0800498 this.scrollPort_.setFontSize(px);
499};
500
501/**
502 * Get the current font size.
503 */
504hterm.Terminal.prototype.getFontSize = function() {
505 return this.scrollPort_.getFontSize();
506};
507
508/**
rginda8e92a692012-05-20 19:37:20 -0700509 * Get the current font family.
510 */
511hterm.Terminal.prototype.getFontFamily = function() {
512 return this.scrollPort_.getFontFamily();
513};
514
515/**
rginda35c456b2012-02-09 17:29:05 -0800516 * Set the CSS "font-family" for this terminal.
517 */
rginda9f5222b2012-03-05 11:53:28 -0800518hterm.Terminal.prototype.syncFontFamily = function() {
519 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
520 this.prefs_.get('font-smoothing'));
521 this.syncBoldSafeState();
522};
523
rginda4bba5e12012-06-20 16:15:30 -0700524/**
525 * Set this.mousePasteButton based on the mouse-paste-button pref,
526 * autodetecting if necessary.
527 */
528hterm.Terminal.prototype.syncMousePasteButton = function() {
529 var button = this.prefs_.get('mouse-paste-button');
530 if (typeof button == 'number') {
531 this.mousePasteButton = button;
532 return;
533 }
534
535 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
536 if (!ary || ary[2] == 'CrOS') {
537 this.mousePasteButton = 2;
538 } else {
539 this.mousePasteButton = 3;
540 }
541};
542
543/**
544 * Enable or disable bold based on the enable-bold pref, autodetecting if
545 * necessary.
546 */
rginda9f5222b2012-03-05 11:53:28 -0800547hterm.Terminal.prototype.syncBoldSafeState = function() {
548 var enableBold = this.prefs_.get('enable-bold');
549 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700550 this.primaryScreen_.textAttributes.enableBold = enableBold;
551 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800552 return;
553 }
554
rgindaf7521392012-02-28 17:20:34 -0800555 var normalSize = this.scrollPort_.measureCharacterSize();
556 var boldSize = this.scrollPort_.measureCharacterSize('bold');
557
558 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800559 if (!isBoldSafe) {
560 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700561 'from normal. Font family is: ' +
562 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800563 }
rginda9f5222b2012-03-05 11:53:28 -0800564
Robert Gindaed016262012-10-26 16:27:09 -0700565 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
566 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800567};
568
569/**
rginda87b86462011-12-14 13:48:03 -0800570 * Return a copy of the current cursor position.
571 *
572 * @return {hterm.RowCol} The RowCol object representing the current position.
573 */
574hterm.Terminal.prototype.saveCursor = function() {
575 return this.screen_.cursorPosition.clone();
576};
577
rgindaa19afe22012-01-25 15:40:22 -0800578hterm.Terminal.prototype.getTextAttributes = function() {
579 return this.screen_.textAttributes;
580};
581
rginda1a09aa02012-06-18 21:11:25 -0700582hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
583 this.screen_.textAttributes = textAttributes;
584};
585
rginda87b86462011-12-14 13:48:03 -0800586/**
rgindaf522ce02012-04-17 17:49:17 -0700587 * Return the current browser zoom factor applied to the terminal.
588 *
589 * @return {number} The current browser zoom factor.
590 */
591hterm.Terminal.prototype.getZoomFactor = function() {
592 return this.scrollPort_.characterSize.zoomFactor;
593};
594
595/**
rginda9846e2f2012-01-27 13:53:33 -0800596 * Change the title of this terminal's window.
597 */
598hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800599 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800600};
601
602/**
rginda87b86462011-12-14 13:48:03 -0800603 * Restore a previously saved cursor position.
604 *
605 * @param {hterm.RowCol} cursor The position to restore.
606 */
607hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700608 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
609 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800610 this.screen_.setCursorPosition(row, column);
611 if (cursor.column > column ||
612 cursor.column == column && cursor.overflow) {
613 this.screen_.cursorPosition.overflow = true;
614 }
rginda87b86462011-12-14 13:48:03 -0800615};
616
617/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400618 * Clear the cursor's overflow flag.
619 */
620hterm.Terminal.prototype.clearCursorOverflow = function() {
621 this.screen_.cursorPosition.overflow = false;
622};
623
624/**
rginda87b86462011-12-14 13:48:03 -0800625 * Set the width of the terminal, resizing the UI to match.
626 */
627hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800628 if (columnCount == null) {
629 this.div_.style.width = '100%';
630 return;
631 }
632
rginda35c456b2012-02-09 17:29:05 -0800633 this.div_.style.width = this.scrollPort_.characterSize.width *
634 columnCount + this.scrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400635 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800636 this.scheduleSyncCursorPosition_();
637};
rginda87b86462011-12-14 13:48:03 -0800638
rgindac9bc5502012-01-18 11:48:44 -0800639/**
rginda35c456b2012-02-09 17:29:05 -0800640 * Set the height of the terminal, resizing the UI to match.
641 */
642hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800643 if (rowCount == null) {
644 this.div_.style.height = '100%';
645 return;
646 }
647
rginda35c456b2012-02-09 17:29:05 -0800648 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700649 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800650 this.realizeSize_(this.screenSize.width, rowCount);
651 this.scheduleSyncCursorPosition_();
652};
653
654/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400655 * Deal with terminal size changes.
656 *
657 */
658hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
659 if (columnCount != this.screenSize.width)
660 this.realizeWidth_(columnCount);
661
662 if (rowCount != this.screenSize.height)
663 this.realizeHeight_(rowCount);
664
665 // Send new terminal size to plugin.
666 this.io.onTerminalResize(columnCount, rowCount);
667};
668
669/**
rgindac9bc5502012-01-18 11:48:44 -0800670 * Deal with terminal width changes.
671 *
672 * This function does what needs to be done when the terminal width changes
673 * out from under us. It happens here rather than in onResize_() because this
674 * code may need to run synchronously to handle programmatic changes of
675 * terminal width.
676 *
677 * Relying on the browser to send us an async resize event means we may not be
678 * in the correct state yet when the next escape sequence hits.
679 */
680hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700681 if (columnCount <= 0)
682 throw new Error('Attempt to realize bad width: ' + columnCount);
683
rgindac9bc5502012-01-18 11:48:44 -0800684 var deltaColumns = columnCount - this.screen_.getWidth();
685
rginda87b86462011-12-14 13:48:03 -0800686 this.screenSize.width = columnCount;
687 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800688
689 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400690 if (this.defaultTabStops)
691 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800692 } else {
693 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400694 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800695 break;
696
697 this.tabStops_.pop();
698 }
699 }
700
701 this.screen_.setColumnCount(this.screenSize.width);
702};
703
704/**
705 * Deal with terminal height changes.
706 *
707 * This function does what needs to be done when the terminal height changes
708 * out from under us. It happens here rather than in onResize_() because this
709 * code may need to run synchronously to handle programmatic changes of
710 * terminal height.
711 *
712 * Relying on the browser to send us an async resize event means we may not be
713 * in the correct state yet when the next escape sequence hits.
714 */
715hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700716 if (rowCount <= 0)
717 throw new Error('Attempt to realize bad height: ' + rowCount);
718
rgindac9bc5502012-01-18 11:48:44 -0800719 var deltaRows = rowCount - this.screen_.getHeight();
720
721 this.screenSize.height = rowCount;
722
723 var cursor = this.saveCursor();
724
725 if (deltaRows < 0) {
726 // Screen got smaller.
727 deltaRows *= -1;
728 while (deltaRows) {
729 var lastRow = this.getRowCount() - 1;
730 if (lastRow - this.scrollbackRows_.length == cursor.row)
731 break;
732
733 if (this.getRowText(lastRow))
734 break;
735
736 this.screen_.popRow();
737 deltaRows--;
738 }
739
740 var ary = this.screen_.shiftRows(deltaRows);
741 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
742
743 // We just removed rows from the top of the screen, we need to update
744 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800745 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800746 } else if (deltaRows > 0) {
747 // Screen got larger.
748
749 if (deltaRows <= this.scrollbackRows_.length) {
750 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
751 var rows = this.scrollbackRows_.splice(
752 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
753 this.screen_.unshiftRows(rows);
754 deltaRows -= scrollbackCount;
755 cursor.row += scrollbackCount;
756 }
757
758 if (deltaRows)
759 this.appendRows_(deltaRows);
760 }
761
rginda35c456b2012-02-09 17:29:05 -0800762 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800763 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800764};
765
766/**
767 * Scroll the terminal to the top of the scrollback buffer.
768 */
769hterm.Terminal.prototype.scrollHome = function() {
770 this.scrollPort_.scrollRowToTop(0);
771};
772
773/**
774 * Scroll the terminal to the end.
775 */
776hterm.Terminal.prototype.scrollEnd = function() {
777 this.scrollPort_.scrollRowToBottom(this.getRowCount());
778};
779
780/**
781 * Scroll the terminal one page up (minus one line) relative to the current
782 * position.
783 */
784hterm.Terminal.prototype.scrollPageUp = function() {
785 var i = this.scrollPort_.getTopRowIndex();
786 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
787};
788
789/**
790 * Scroll the terminal one page down (minus one line) relative to the current
791 * position.
792 */
793hterm.Terminal.prototype.scrollPageDown = function() {
794 var i = this.scrollPort_.getTopRowIndex();
795 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800796};
797
rgindac9bc5502012-01-18 11:48:44 -0800798/**
Robert Ginda40932892012-12-10 17:26:40 -0800799 * Clear primary screen, secondary screen, and the scrollback buffer.
800 */
801hterm.Terminal.prototype.wipeContents = function() {
802 this.scrollbackRows_.length = 0;
803 this.scrollPort_.resetCache();
804
805 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
806 var bottom = screen.getHeight();
807 if (bottom > 0) {
808 this.renumberRows_(0, bottom);
809 this.clearHome(screen);
810 }
811 }.bind(this));
812
813 this.syncCursorPosition_();
814};
815
816/**
rgindac9bc5502012-01-18 11:48:44 -0800817 * Full terminal reset.
818 */
rginda87b86462011-12-14 13:48:03 -0800819hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800820 this.clearAllTabStops();
821 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -0700822
823 this.clearHome(this.primaryScreen_);
824 this.primaryScreen_.textAttributes.reset();
825
826 this.clearHome(this.alternateScreen_);
827 this.alternateScreen_.textAttributes.reset();
828
rgindab8bc8932012-04-27 12:45:03 -0700829 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
830
rgindac9bc5502012-01-18 11:48:44 -0800831 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800832};
833
rgindac9bc5502012-01-18 11:48:44 -0800834/**
835 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -0700836 *
837 * Perform a soft reset to the default values listed in
838 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -0800839 */
rginda0f5c0292012-01-13 11:00:13 -0800840hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -0700841 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -0800842 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -0700843
rgindab8bc8932012-04-27 12:45:03 -0700844 // Xterm also resets the color palette on soft reset, even though it doesn't
845 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -0700846 this.primaryScreen_.textAttributes.resetColorPalette();
847 this.alternateScreen_.textAttributes.resetColorPalette();
848
rgindab8bc8932012-04-27 12:45:03 -0700849 // The xterm man page explicitly says this will happen on soft reset.
850 this.setVTScrollRegion(null, null);
851
852 // Xterm also shows the cursor on soft reset, but does not alter the blink
853 // state.
rgindaa19afe22012-01-25 15:40:22 -0800854 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -0800855};
856
rgindac9bc5502012-01-18 11:48:44 -0800857/**
858 * Move the cursor forward to the next tab stop, or to the last column
859 * if no more tab stops are set.
860 */
861hterm.Terminal.prototype.forwardTabStop = function() {
862 var column = this.screen_.cursorPosition.column;
863
864 for (var i = 0; i < this.tabStops_.length; i++) {
865 if (this.tabStops_[i] > column) {
866 this.setCursorColumn(this.tabStops_[i]);
867 return;
868 }
869 }
870
David Benjamin66e954d2012-05-05 21:08:12 -0400871 // xterm does not clear the overflow flag on HT or CHT.
872 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -0800873 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -0400874 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -0800875};
876
rgindac9bc5502012-01-18 11:48:44 -0800877/**
878 * Move the cursor backward to the previous tab stop, or to the first column
879 * if no previous tab stops are set.
880 */
881hterm.Terminal.prototype.backwardTabStop = function() {
882 var column = this.screen_.cursorPosition.column;
883
884 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
885 if (this.tabStops_[i] < column) {
886 this.setCursorColumn(this.tabStops_[i]);
887 return;
888 }
889 }
890
891 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800892};
893
rgindac9bc5502012-01-18 11:48:44 -0800894/**
895 * Set a tab stop at the given column.
896 *
897 * @param {int} column Zero based column.
898 */
899hterm.Terminal.prototype.setTabStop = function(column) {
900 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
901 if (this.tabStops_[i] == column)
902 return;
903
904 if (this.tabStops_[i] < column) {
905 this.tabStops_.splice(i + 1, 0, column);
906 return;
907 }
908 }
909
910 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800911};
912
rgindac9bc5502012-01-18 11:48:44 -0800913/**
914 * Clear the tab stop at the current cursor position.
915 *
916 * No effect if there is no tab stop at the current cursor position.
917 */
918hterm.Terminal.prototype.clearTabStopAtCursor = function() {
919 var column = this.screen_.cursorPosition.column;
920
921 var i = this.tabStops_.indexOf(column);
922 if (i == -1)
923 return;
924
925 this.tabStops_.splice(i, 1);
926};
927
928/**
929 * Clear all tab stops.
930 */
931hterm.Terminal.prototype.clearAllTabStops = function() {
932 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -0400933 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -0800934};
935
936/**
937 * Set up the default tab stops, starting from a given column.
938 *
939 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -0400940 * from the specified column, or 0 if no column is provided. It also flags
941 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -0800942 *
943 * This does not clear the existing tab stops first, use clearAllTabStops
944 * for that.
945 *
946 * @param {int} opt_start Optional starting zero based starting column, useful
947 * for filling out missing tab stops when the terminal is resized.
948 */
949hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
950 var start = opt_start || 0;
951 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -0400952 // Round start up to a default tab stop.
953 start = start - 1 - ((start - 1) % w) + w;
954 for (var i = start; i < this.screenSize.width; i += w) {
955 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -0800956 }
David Benjamin66e954d2012-05-05 21:08:12 -0400957
958 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -0800959};
960
rginda6d397402012-01-17 10:58:29 -0800961/**
rginda8ba33642011-12-14 12:31:31 -0800962 * Interpret a sequence of characters.
963 *
964 * Incomplete escape sequences are buffered until the next call.
965 *
966 * @param {string} str Sequence of characters to interpret or pass through.
967 */
968hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -0800969 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -0800970 this.scheduleSyncCursorPosition_();
971};
972
973/**
974 * Take over the given DIV for use as the terminal display.
975 *
976 * @param {HTMLDivElement} div The div to use as the terminal display.
977 */
978hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -0800979 this.div_ = div;
980
rginda8ba33642011-12-14 12:31:31 -0800981 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -0700982 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -0400983 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
984 this.scrollPort_.setBackgroundPosition(
985 this.prefs_.get('background-position'));
rginda30f20f62012-04-05 16:36:19 -0700986
rginda0918b652012-04-04 11:26:24 -0700987 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -0800988
rginda9f5222b2012-03-05 11:53:28 -0800989 this.setFontSize(this.prefs_.get('font-size'));
990 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -0800991
David Reveman8f552492012-03-28 12:18:41 -0400992 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
993
rginda8ba33642011-12-14 12:31:31 -0800994 this.document_ = this.scrollPort_.getDocument();
995
rginda4bba5e12012-06-20 16:15:30 -0700996 this.document_.body.oncontextmenu = function() { return false };
997
998 var onMouse = this.onMouse_.bind(this);
999 this.document_.body.firstChild.addEventListener('mousedown', onMouse);
1000 this.document_.body.firstChild.addEventListener('mouseup', onMouse);
1001 this.document_.body.firstChild.addEventListener('mousemove', onMouse);
1002 this.scrollPort_.onScrollWheel = onMouse;
1003
rginda8e92a692012-05-20 19:37:20 -07001004 this.document_.body.firstChild.addEventListener(
1005 'focus', this.onFocusChange_.bind(this, true));
1006 this.document_.body.firstChild.addEventListener(
1007 'blur', this.onFocusChange_.bind(this, false));
1008
1009 var style = this.document_.createElement('style');
1010 style.textContent =
1011 ('.cursor-node[focus="false"] {' +
1012 ' box-sizing: border-box;' +
1013 ' background-color: transparent !important;' +
1014 ' border-width: 2px;' +
1015 ' border-style: solid;' +
1016 '}');
1017 this.document_.head.appendChild(style);
1018
rginda8ba33642011-12-14 12:31:31 -08001019 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001020 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001021 this.cursorNode_.style.cssText =
1022 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001023 'top: -99px;' +
1024 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001025 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1026 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001027 '-webkit-transition: opacity, background-color 100ms linear;');
1028 this.setCursorColor(this.prefs_.get('cursor-color'));
rgindad5613292012-06-19 15:40:37 -07001029
rginda8ba33642011-12-14 12:31:31 -08001030 this.document_.body.appendChild(this.cursorNode_);
1031
rgindad5613292012-06-19 15:40:37 -07001032 // When 'enableMouseDragScroll' is off we reposition this element directly
1033 // under the mouse cursor after a click. This makes Chrome associate
1034 // subsequent mousemove events with the scroll-blocker. Since the
1035 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1036 // events do not cause the scrollport to scroll.
1037 //
1038 // It's a hack, but it's the cleanest way I could find.
1039 this.scrollBlockerNode_ = this.document_.createElement('div');
1040 this.scrollBlockerNode_.style.cssText =
1041 ('position: absolute;' +
1042 'top: -99px;' +
1043 'display: block;' +
1044 'width: 10px;' +
1045 'height: 10px;');
1046 this.document_.body.appendChild(this.scrollBlockerNode_);
1047
1048 var onMouse = this.onMouse_.bind(this);
1049 this.scrollPort_.onScrollWheel = onMouse;
1050 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1051 ].forEach(function(event) {
1052 this.scrollBlockerNode_.addEventListener(event, onMouse);
1053 this.cursorNode_.addEventListener(event, onMouse);
1054 this.document_.addEventListener(event, onMouse);
1055 }.bind(this));
1056
1057 this.cursorNode_.addEventListener('mousedown', function() {
1058 setTimeout(this.focus.bind(this));
1059 }.bind(this));
1060
rgindade84e382012-04-20 15:39:31 -07001061 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
rginda8ba33642011-12-14 12:31:31 -08001062 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001063
rginda87b86462011-12-14 13:48:03 -08001064 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001065 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001066};
1067
rginda0918b652012-04-04 11:26:24 -07001068/**
1069 * Return the HTML document that contains the terminal DOM nodes.
1070 */
rginda87b86462011-12-14 13:48:03 -08001071hterm.Terminal.prototype.getDocument = function() {
1072 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001073};
1074
1075/**
rginda0918b652012-04-04 11:26:24 -07001076 * Focus the terminal.
1077 */
1078hterm.Terminal.prototype.focus = function() {
1079 this.scrollPort_.focus();
1080};
1081
1082/**
rginda8ba33642011-12-14 12:31:31 -08001083 * Return the HTML Element for a given row index.
1084 *
1085 * This is a method from the RowProvider interface. The ScrollPort uses
1086 * it to fetch rows on demand as they are scrolled into view.
1087 *
1088 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1089 * pairs to conserve memory.
1090 *
1091 * @param {integer} index The zero-based row index, measured relative to the
1092 * start of the scrollback buffer. On-screen rows will always have the
1093 * largest indicies.
1094 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1095 */
1096hterm.Terminal.prototype.getRowNode = function(index) {
1097 if (index < this.scrollbackRows_.length)
1098 return this.scrollbackRows_[index];
1099
1100 var screenIndex = index - this.scrollbackRows_.length;
1101 return this.screen_.rowsArray[screenIndex];
1102};
1103
1104/**
1105 * Return the text content for a given range of rows.
1106 *
1107 * This is a method from the RowProvider interface. The ScrollPort uses
1108 * it to fetch text content on demand when the user attempts to copy their
1109 * selection to the clipboard.
1110 *
1111 * @param {integer} start The zero-based row index to start from, measured
1112 * relative to the start of the scrollback buffer. On-screen rows will
1113 * always have the largest indicies.
1114 * @param {integer} end The zero-based row index to end on, measured
1115 * relative to the start of the scrollback buffer.
1116 * @return {string} A single string containing the text value of the range of
1117 * rows. Lines will be newline delimited, with no trailing newline.
1118 */
1119hterm.Terminal.prototype.getRowsText = function(start, end) {
1120 var ary = [];
1121 for (var i = start; i < end; i++) {
1122 var node = this.getRowNode(i);
1123 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001124 if (i < end - 1 && !node.getAttribute('line-overflow'))
1125 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001126 }
1127
rgindaa09e7332012-08-17 12:49:51 -07001128 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001129};
1130
1131/**
1132 * Return the text content for a given row.
1133 *
1134 * This is a method from the RowProvider interface. The ScrollPort uses
1135 * it to fetch text content on demand when the user attempts to copy their
1136 * selection to the clipboard.
1137 *
1138 * @param {integer} index The zero-based row index to return, measured
1139 * relative to the start of the scrollback buffer. On-screen rows will
1140 * always have the largest indicies.
1141 * @return {string} A string containing the text value of the selected row.
1142 */
1143hterm.Terminal.prototype.getRowText = function(index) {
1144 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001145 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001146};
1147
1148/**
1149 * Return the total number of rows in the addressable screen and in the
1150 * scrollback buffer of this terminal.
1151 *
1152 * This is a method from the RowProvider interface. The ScrollPort uses
1153 * it to compute the size of the scrollbar.
1154 *
1155 * @return {integer} The number of rows in this terminal.
1156 */
1157hterm.Terminal.prototype.getRowCount = function() {
1158 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1159};
1160
1161/**
1162 * Create DOM nodes for new rows and append them to the end of the terminal.
1163 *
1164 * This is the only correct way to add a new DOM node for a row. Notice that
1165 * the new row is appended to the bottom of the list of rows, and does not
1166 * require renumbering (of the rowIndex property) of previous rows.
1167 *
1168 * If you think you want a new blank row somewhere in the middle of the
1169 * terminal, look into moveRows_().
1170 *
1171 * This method does not pay attention to vtScrollTop/Bottom, since you should
1172 * be using moveRows() in cases where they would matter.
1173 *
1174 * The cursor will be positioned at column 0 of the first inserted line.
1175 */
1176hterm.Terminal.prototype.appendRows_ = function(count) {
1177 var cursorRow = this.screen_.rowsArray.length;
1178 var offset = this.scrollbackRows_.length + cursorRow;
1179 for (var i = 0; i < count; i++) {
1180 var row = this.document_.createElement('x-row');
1181 row.appendChild(this.document_.createTextNode(''));
1182 row.rowIndex = offset + i;
1183 this.screen_.pushRow(row);
1184 }
1185
1186 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1187 if (extraRows > 0) {
1188 var ary = this.screen_.shiftRows(extraRows);
1189 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001190 if (this.scrollPort_.isScrolledEnd)
1191 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001192 }
1193
1194 if (cursorRow >= this.screen_.rowsArray.length)
1195 cursorRow = this.screen_.rowsArray.length - 1;
1196
rginda87b86462011-12-14 13:48:03 -08001197 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001198};
1199
1200/**
1201 * Relocate rows from one part of the addressable screen to another.
1202 *
1203 * This is used to recycle rows during VT scrolls (those which are driven
1204 * by VT commands, rather than by the user manipulating the scrollbar.)
1205 *
1206 * In this case, the blank lines scrolled into the scroll region are made of
1207 * the nodes we scrolled off. These have their rowIndex properties carefully
1208 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001209 */
1210hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1211 var ary = this.screen_.removeRows(fromIndex, count);
1212 this.screen_.insertRows(toIndex, ary);
1213
1214 var start, end;
1215 if (fromIndex < toIndex) {
1216 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001217 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001218 } else {
1219 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001220 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001221 }
1222
1223 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001224 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001225};
1226
1227/**
1228 * Renumber the rowIndex property of the given range of rows.
1229 *
1230 * The start and end indicies are relative to the screen, not the scrollback.
1231 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001232 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001233 * no need to renumber scrollback rows.
1234 */
Robert Ginda40932892012-12-10 17:26:40 -08001235hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1236 var screen = opt_screen || this.screen_;
1237
rginda8ba33642011-12-14 12:31:31 -08001238 var offset = this.scrollbackRows_.length;
1239 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001240 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001241 }
1242};
1243
1244/**
1245 * Print a string to the terminal.
1246 *
1247 * This respects the current insert and wraparound modes. It will add new lines
1248 * to the end of the terminal, scrolling off the top into the scrollback buffer
1249 * if necessary.
1250 *
1251 * The string is *not* parsed for escape codes. Use the interpret() method if
1252 * that's what you're after.
1253 *
1254 * @param{string} str The string to print.
1255 */
1256hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001257 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001258
rgindaa9abdd82012-08-06 18:05:09 -07001259 while (startOffset < str.length) {
rgindaa09e7332012-08-17 12:49:51 -07001260 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1261 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001262 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001263 }
rgindaa19afe22012-01-25 15:40:22 -08001264
rgindaa9abdd82012-08-06 18:05:09 -07001265 var count = str.length - startOffset;
1266 var didOverflow = false;
1267 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001268
rgindaa9abdd82012-08-06 18:05:09 -07001269 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1270 didOverflow = true;
1271 count = this.screenSize.width - this.screen_.cursorPosition.column;
1272 }
rgindaa19afe22012-01-25 15:40:22 -08001273
rgindaa9abdd82012-08-06 18:05:09 -07001274 if (didOverflow && !this.options_.wraparound) {
1275 // If the string overflowed the line but wraparound is off, then the
1276 // last printed character should be the last of the string.
1277 // TODO: This will add to our problems with multibyte UTF-16 characters.
1278 substr = str.substr(startOffset, count - 1) +
1279 str.substr(str.length - 1);
1280 count = str.length;
1281 } else {
1282 substr = str.substr(startOffset, count);
1283 }
rgindaa19afe22012-01-25 15:40:22 -08001284
rgindaa9abdd82012-08-06 18:05:09 -07001285 if (this.options_.insertMode) {
1286 this.screen_.insertString(substr);
1287 } else {
1288 this.screen_.overwriteString(substr);
1289 }
1290
1291 this.screen_.maybeClipCurrentRow();
1292 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001293 }
rginda8ba33642011-12-14 12:31:31 -08001294
1295 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001296
rginda9f5222b2012-03-05 11:53:28 -08001297 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001298 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001299};
1300
1301/**
rginda87b86462011-12-14 13:48:03 -08001302 * Set the VT scroll region.
1303 *
rginda87b86462011-12-14 13:48:03 -08001304 * This also resets the cursor position to the absolute (0, 0) position, since
1305 * that's what xterm appears to do.
1306 *
1307 * @param {integer} scrollTop The zero-based top of the scroll region.
1308 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1309 * inclusive.
1310 */
1311hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
1312 this.vtScrollTop_ = scrollTop;
1313 this.vtScrollBottom_ = scrollBottom;
rginda87b86462011-12-14 13:48:03 -08001314};
1315
1316/**
rginda8ba33642011-12-14 12:31:31 -08001317 * Return the top row index according to the VT.
1318 *
1319 * This will return 0 unless the terminal has been told to restrict scrolling
1320 * to some lower row. It is used for some VT cursor positioning and scrolling
1321 * commands.
1322 *
1323 * @return {integer} The topmost row in the terminal's scroll region.
1324 */
1325hterm.Terminal.prototype.getVTScrollTop = function() {
1326 if (this.vtScrollTop_ != null)
1327 return this.vtScrollTop_;
1328
1329 return 0;
rginda87b86462011-12-14 13:48:03 -08001330};
rginda8ba33642011-12-14 12:31:31 -08001331
1332/**
1333 * Return the bottom row index according to the VT.
1334 *
1335 * This will return the height of the terminal unless the it has been told to
1336 * restrict scrolling to some higher row. It is used for some VT cursor
1337 * positioning and scrolling commands.
1338 *
1339 * @return {integer} The bottommost row in the terminal's scroll region.
1340 */
1341hterm.Terminal.prototype.getVTScrollBottom = function() {
1342 if (this.vtScrollBottom_ != null)
1343 return this.vtScrollBottom_;
1344
rginda87b86462011-12-14 13:48:03 -08001345 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001346}
1347
1348/**
1349 * Process a '\n' character.
1350 *
1351 * If the cursor is on the final row of the terminal this will append a new
1352 * blank row to the screen and scroll the topmost row into the scrollback
1353 * buffer.
1354 *
1355 * Otherwise, this moves the cursor to column zero of the next row.
1356 */
1357hterm.Terminal.prototype.newLine = function() {
1358 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -08001359 // If we're at the end of the screen we need to append a new line and
1360 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -08001361 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -08001362 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
1363 // End of the scroll region does not affect the scrollback buffer.
1364 this.vtScrollUp(1);
1365 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -08001366 } else {
rginda87b86462011-12-14 13:48:03 -08001367 // Anywhere else in the screen just moves the cursor.
1368 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001369 }
1370};
1371
1372/**
1373 * Like newLine(), except maintain the cursor column.
1374 */
1375hterm.Terminal.prototype.lineFeed = function() {
1376 var column = this.screen_.cursorPosition.column;
1377 this.newLine();
1378 this.setCursorColumn(column);
1379};
1380
1381/**
rginda87b86462011-12-14 13:48:03 -08001382 * If autoCarriageReturn is set then newLine(), else lineFeed().
1383 */
1384hterm.Terminal.prototype.formFeed = function() {
1385 if (this.options_.autoCarriageReturn) {
1386 this.newLine();
1387 } else {
1388 this.lineFeed();
1389 }
1390};
1391
1392/**
1393 * Move the cursor up one row, possibly inserting a blank line.
1394 *
1395 * The cursor column is not changed.
1396 */
1397hterm.Terminal.prototype.reverseLineFeed = function() {
1398 var scrollTop = this.getVTScrollTop();
1399 var currentRow = this.screen_.cursorPosition.row;
1400
1401 if (currentRow == scrollTop) {
1402 this.insertLines(1);
1403 } else {
1404 this.setAbsoluteCursorRow(currentRow - 1);
1405 }
1406};
1407
1408/**
rginda8ba33642011-12-14 12:31:31 -08001409 * Replace all characters to the left of the current cursor with the space
1410 * character.
1411 *
1412 * TODO(rginda): This should probably *remove* the characters (not just replace
1413 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001414 * position.
rginda8ba33642011-12-14 12:31:31 -08001415 */
1416hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001417 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001418 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001419 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001420 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001421};
1422
1423/**
David Benjamin684a9b72012-05-01 17:19:58 -04001424 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001425 *
1426 * The cursor position is unchanged.
1427 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001428 * If the current background color is not the default background color this
1429 * will insert spaces rather than delete. This is unfortunate because the
1430 * trailing space will affect text selection, but it's difficult to come up
1431 * with a way to style empty space that wouldn't trip up the hterm.Screen
1432 * code.
rginda8ba33642011-12-14 12:31:31 -08001433 */
1434hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001435 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1436 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001437
1438 if (this.screen_.textAttributes.background ===
1439 this.screen_.textAttributes.DEFAULT_COLOR) {
1440 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
1441 if (cursorRow.textContent.length <=
1442 this.screen_.cursorPosition.column + count) {
1443 this.screen_.deleteChars(count);
1444 this.clearCursorOverflow();
1445 return;
1446 }
1447 }
1448
rginda87b86462011-12-14 13:48:03 -08001449 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001450 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001451 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001452 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001453};
1454
1455/**
1456 * Erase the current line.
1457 *
1458 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001459 */
1460hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001461 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001462 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001463 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001464 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001465};
1466
1467/**
David Benjamina08d78f2012-05-05 00:28:49 -04001468 * Erase all characters from the start of the screen to the current cursor
1469 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001470 *
1471 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001472 */
1473hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001474 var cursor = this.saveCursor();
1475
1476 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001477
David Benjamina08d78f2012-05-05 00:28:49 -04001478 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001479 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001480 this.screen_.clearCursorRow();
1481 }
1482
rginda87b86462011-12-14 13:48:03 -08001483 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001484 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001485};
1486
1487/**
1488 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001489 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001490 *
1491 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001492 */
1493hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001494 var cursor = this.saveCursor();
1495
1496 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001497
David Benjamina08d78f2012-05-05 00:28:49 -04001498 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001499 for (var i = cursor.row + 1; i <= bottom; i++) {
1500 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001501 this.screen_.clearCursorRow();
1502 }
1503
rginda87b86462011-12-14 13:48:03 -08001504 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001505 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001506};
1507
1508/**
1509 * Fill the terminal with a given character.
1510 *
1511 * This methods does not respect the VT scroll region.
1512 *
1513 * @param {string} ch The character to use for the fill.
1514 */
1515hterm.Terminal.prototype.fill = function(ch) {
1516 var cursor = this.saveCursor();
1517
1518 this.setAbsoluteCursorPosition(0, 0);
1519 for (var row = 0; row < this.screenSize.height; row++) {
1520 for (var col = 0; col < this.screenSize.width; col++) {
1521 this.setAbsoluteCursorPosition(row, col);
1522 this.screen_.overwriteString(ch);
1523 }
1524 }
1525
1526 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001527};
1528
1529/**
rginda9ea433c2012-03-16 11:57:00 -07001530 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001531 *
rginda9ea433c2012-03-16 11:57:00 -07001532 * This does not respect the scroll region.
1533 *
1534 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1535 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001536 */
rginda9ea433c2012-03-16 11:57:00 -07001537hterm.Terminal.prototype.clearHome = function(opt_screen) {
1538 var screen = opt_screen || this.screen_;
1539 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001540
rginda11057d52012-04-25 12:29:56 -07001541 if (bottom == 0) {
1542 // Empty screen, nothing to do.
1543 return;
1544 }
1545
rgindae4d29232012-01-19 10:47:13 -08001546 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001547 screen.setCursorPosition(i, 0);
1548 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001549 }
1550
rginda9ea433c2012-03-16 11:57:00 -07001551 screen.setCursorPosition(0, 0);
1552};
1553
1554/**
1555 * Erase the entire display without changing the cursor position.
1556 *
1557 * The cursor position is unchanged. This does not respect the scroll
1558 * region.
1559 *
1560 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1561 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001562 */
1563hterm.Terminal.prototype.clear = function(opt_screen) {
1564 var screen = opt_screen || this.screen_;
1565 var cursor = screen.cursorPosition.clone();
1566 this.clearHome(screen);
1567 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001568};
1569
1570/**
1571 * VT command to insert lines at the current cursor row.
1572 *
1573 * This respects the current scroll region. Rows pushed off the bottom are
1574 * lost (they won't show up in the scrollback buffer).
1575 *
rginda8ba33642011-12-14 12:31:31 -08001576 * @param {integer} count The number of lines to insert.
1577 */
1578hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07001579 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08001580
1581 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07001582 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08001583
Robert Ginda579186b2012-09-26 11:40:04 -07001584 // The moveCount is the number of rows we need to relocate to make room for
1585 // the new row(s). The count is the distance to move them.
1586 var moveCount = bottom - cursorRow - count + 1;
1587 if (moveCount)
1588 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08001589
Robert Ginda579186b2012-09-26 11:40:04 -07001590 for (var i = count - 1; i >= 0; i--) {
1591 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001592 this.screen_.clearCursorRow();
1593 }
rginda8ba33642011-12-14 12:31:31 -08001594};
1595
1596/**
1597 * VT command to delete lines at the current cursor row.
1598 *
1599 * New rows are added to the bottom of scroll region to take their place. New
1600 * rows are strictly there to take up space and have no content or style.
1601 */
1602hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001603 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001604
rginda87b86462011-12-14 13:48:03 -08001605 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001606 var bottom = this.getVTScrollBottom();
1607
rginda87b86462011-12-14 13:48:03 -08001608 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001609 count = Math.min(count, maxCount);
1610
rginda87b86462011-12-14 13:48:03 -08001611 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001612 if (count != maxCount)
1613 this.moveRows_(top, count, moveStart);
1614
1615 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001616 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001617 this.screen_.clearCursorRow();
1618 }
1619
rginda87b86462011-12-14 13:48:03 -08001620 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001621 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001622};
1623
1624/**
1625 * Inserts the given number of spaces at the current cursor position.
1626 *
rginda87b86462011-12-14 13:48:03 -08001627 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001628 */
1629hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001630 var cursor = this.saveCursor();
1631
rgindacbbd7482012-06-13 15:06:16 -07001632 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001633 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001634 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001635
1636 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001637 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001638};
1639
1640/**
1641 * Forward-delete the specified number of characters starting at the cursor
1642 * position.
1643 *
1644 * @param {integer} count The number of characters to delete.
1645 */
1646hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001647 var deleted = this.screen_.deleteChars(count);
1648 if (deleted && !this.screen_.textAttributes.isDefault()) {
1649 var cursor = this.saveCursor();
1650 this.setCursorColumn(this.screenSize.width - deleted);
1651 this.screen_.insertString(lib.f.getWhitespace(deleted));
1652 this.restoreCursor(cursor);
1653 }
1654
David Benjamin54e8bf62012-06-01 22:31:40 -04001655 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001656};
1657
1658/**
1659 * Shift rows in the scroll region upwards by a given number of lines.
1660 *
1661 * New rows are inserted at the bottom of the scroll region to fill the
1662 * vacated rows. The new rows not filled out with the current text attributes.
1663 *
1664 * This function does not affect the scrollback rows at all. Rows shifted
1665 * off the top are lost.
1666 *
rginda87b86462011-12-14 13:48:03 -08001667 * The cursor position is not altered.
1668 *
rginda8ba33642011-12-14 12:31:31 -08001669 * @param {integer} count The number of rows to scroll.
1670 */
1671hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001672 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001673
rginda87b86462011-12-14 13:48:03 -08001674 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001675 this.deleteLines(count);
1676
rginda87b86462011-12-14 13:48:03 -08001677 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001678};
1679
1680/**
1681 * Shift rows below the cursor down by a given number of lines.
1682 *
1683 * This function respects the current scroll region.
1684 *
1685 * New rows are inserted at the top of the scroll region to fill the
1686 * vacated rows. The new rows not filled out with the current text attributes.
1687 *
1688 * This function does not affect the scrollback rows at all. Rows shifted
1689 * off the bottom are lost.
1690 *
1691 * @param {integer} count The number of rows to scroll.
1692 */
1693hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001694 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001695
rginda87b86462011-12-14 13:48:03 -08001696 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001697 this.insertLines(opt_count);
1698
rginda87b86462011-12-14 13:48:03 -08001699 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001700};
1701
rginda87b86462011-12-14 13:48:03 -08001702
rginda8ba33642011-12-14 12:31:31 -08001703/**
1704 * Set the cursor position.
1705 *
1706 * The cursor row is relative to the scroll region if the terminal has
1707 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1708 *
1709 * @param {integer} row The new zero-based cursor row.
1710 * @param {integer} row The new zero-based cursor column.
1711 */
1712hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1713 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001714 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001715 } else {
rginda87b86462011-12-14 13:48:03 -08001716 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001717 }
rginda87b86462011-12-14 13:48:03 -08001718};
rginda8ba33642011-12-14 12:31:31 -08001719
rginda87b86462011-12-14 13:48:03 -08001720hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1721 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07001722 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
1723 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001724 this.screen_.setCursorPosition(row, column);
1725};
1726
1727hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07001728 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
1729 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001730 this.screen_.setCursorPosition(row, column);
1731};
1732
1733/**
1734 * Set the cursor column.
1735 *
1736 * @param {integer} column The new zero-based cursor column.
1737 */
1738hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001739 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001740};
1741
1742/**
1743 * Return the cursor column.
1744 *
1745 * @return {integer} The zero-based cursor column.
1746 */
1747hterm.Terminal.prototype.getCursorColumn = function() {
1748 return this.screen_.cursorPosition.column;
1749};
1750
1751/**
1752 * Set the cursor row.
1753 *
1754 * The cursor row is relative to the scroll region if the terminal has
1755 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1756 *
1757 * @param {integer} row The new cursor row.
1758 */
rginda87b86462011-12-14 13:48:03 -08001759hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1760 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001761};
1762
1763/**
1764 * Return the cursor row.
1765 *
1766 * @return {integer} The zero-based cursor row.
1767 */
1768hterm.Terminal.prototype.getCursorRow = function(row) {
1769 return this.screen_.cursorPosition.row;
1770};
1771
1772/**
1773 * Request that the ScrollPort redraw itself soon.
1774 *
1775 * The redraw will happen asynchronously, soon after the call stack winds down.
1776 * Multiple calls will be coalesced into a single redraw.
1777 */
1778hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001779 if (this.timeouts_.redraw)
1780 return;
rginda8ba33642011-12-14 12:31:31 -08001781
1782 var self = this;
rginda87b86462011-12-14 13:48:03 -08001783 this.timeouts_.redraw = setTimeout(function() {
1784 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001785 self.scrollPort_.redraw_();
1786 }, 0);
1787};
1788
1789/**
1790 * Request that the ScrollPort be scrolled to the bottom.
1791 *
1792 * The scroll will happen asynchronously, soon after the call stack winds down.
1793 * Multiple calls will be coalesced into a single scroll.
1794 *
1795 * This affects the scrollbar position of the ScrollPort, and has nothing to
1796 * do with the VT scroll commands.
1797 */
1798hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1799 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001800 return;
rginda8ba33642011-12-14 12:31:31 -08001801
1802 var self = this;
1803 this.timeouts_.scrollDown = setTimeout(function() {
1804 delete self.timeouts_.scrollDown;
1805 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1806 }, 10);
1807};
1808
1809/**
1810 * Move the cursor up a specified number of rows.
1811 *
1812 * @param {integer} count The number of rows to move the cursor.
1813 */
1814hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001815 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001816};
1817
1818/**
1819 * Move the cursor down a specified number of rows.
1820 *
1821 * @param {integer} count The number of rows to move the cursor.
1822 */
1823hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001824 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001825 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1826 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1827 this.screenSize.height - 1);
1828
rgindacbbd7482012-06-13 15:06:16 -07001829 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08001830 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001831 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001832};
1833
1834/**
1835 * Move the cursor left a specified number of columns.
1836 *
1837 * @param {integer} count The number of columns to move the cursor.
1838 */
1839hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001840 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001841};
1842
1843/**
1844 * Move the cursor right a specified number of columns.
1845 *
1846 * @param {integer} count The number of columns to move the cursor.
1847 */
1848hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001849 count = count || 1;
rgindacbbd7482012-06-13 15:06:16 -07001850 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001851 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001852 this.setCursorColumn(column);
1853};
1854
1855/**
1856 * Reverse the foreground and background colors of the terminal.
1857 *
1858 * This only affects text that was drawn with no attributes.
1859 *
1860 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1861 * been drawn with attributes that happen to coincide with the default
1862 * 'no-attribute' colors. My guess is probably not.
1863 */
1864hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001865 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001866 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08001867 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
1868 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08001869 } else {
rginda9f5222b2012-03-05 11:53:28 -08001870 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
1871 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08001872 }
1873};
1874
1875/**
rginda87b86462011-12-14 13:48:03 -08001876 * Ring the terminal bell.
rginda87b86462011-12-14 13:48:03 -08001877 */
1878hterm.Terminal.prototype.ringBell = function() {
rginda9f5222b2012-03-05 11:53:28 -08001879 if (this.bellAudio_.getAttribute('src'))
1880 this.bellAudio_.play();
rgindaf0090c92012-02-10 14:58:52 -08001881
rginda6d397402012-01-17 10:58:29 -08001882 this.cursorNode_.style.backgroundColor =
1883 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001884
1885 var self = this;
1886 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08001887 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08001888 }, 200);
rginda87b86462011-12-14 13:48:03 -08001889};
1890
1891/**
rginda8ba33642011-12-14 12:31:31 -08001892 * Set the origin mode bit.
1893 *
1894 * If origin mode is on, certain VT cursor and scrolling commands measure their
1895 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1896 * to the top of the addressable screen.
1897 *
1898 * Defaults to off.
1899 *
1900 * @param {boolean} state True to set origin mode, false to unset.
1901 */
1902hterm.Terminal.prototype.setOriginMode = function(state) {
1903 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08001904 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08001905};
1906
1907/**
1908 * Set the insert mode bit.
1909 *
1910 * If insert mode is on, existing text beyond the cursor position will be
1911 * shifted right to make room for new text. Otherwise, new text overwrites
1912 * any existing text.
1913 *
1914 * Defaults to off.
1915 *
1916 * @param {boolean} state True to set insert mode, false to unset.
1917 */
1918hterm.Terminal.prototype.setInsertMode = function(state) {
1919 this.options_.insertMode = state;
1920};
1921
1922/**
rginda87b86462011-12-14 13:48:03 -08001923 * Set the auto carriage return bit.
1924 *
1925 * If auto carriage return is on then a formfeed character is interpreted
1926 * as a newline, otherwise it's the same as a linefeed. The difference boils
1927 * down to whether or not the cursor column is reset.
1928 */
1929hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1930 this.options_.autoCarriageReturn = state;
1931};
1932
1933/**
rginda8ba33642011-12-14 12:31:31 -08001934 * Set the wraparound mode bit.
1935 *
1936 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1937 * to the start of the following row. Otherwise, the cursor is clamped to the
1938 * end of the screen and attempts to write past it are ignored.
1939 *
1940 * Defaults to on.
1941 *
1942 * @param {boolean} state True to set wraparound mode, false to unset.
1943 */
1944hterm.Terminal.prototype.setWraparound = function(state) {
1945 this.options_.wraparound = state;
1946};
1947
1948/**
1949 * Set the reverse-wraparound mode bit.
1950 *
1951 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1952 * to the end of the previous row. Otherwise, the cursor is clamped to column
1953 * 0.
1954 *
1955 * Defaults to off.
1956 *
1957 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
1958 */
1959hterm.Terminal.prototype.setReverseWraparound = function(state) {
1960 this.options_.reverseWraparound = state;
1961};
1962
1963/**
1964 * Selects between the primary and alternate screens.
1965 *
1966 * If alternate mode is on, the alternate screen is active. Otherwise the
1967 * primary screen is active.
1968 *
1969 * Swapping screens has no effect on the scrollback buffer.
1970 *
1971 * Each screen maintains its own cursor position.
1972 *
1973 * Defaults to off.
1974 *
1975 * @param {boolean} state True to set alternate mode, false to unset.
1976 */
1977hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08001978 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001979 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
1980
rginda35c456b2012-02-09 17:29:05 -08001981 if (this.screen_.rowsArray.length &&
1982 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
1983 // If the screen changed sizes while we were away, our rowIndexes may
1984 // be incorrect.
1985 var offset = this.scrollbackRows_.length;
1986 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07001987 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08001988 ary[i].rowIndex = offset + i;
1989 }
1990 }
rginda8ba33642011-12-14 12:31:31 -08001991
rginda35c456b2012-02-09 17:29:05 -08001992 this.realizeWidth_(this.screenSize.width);
1993 this.realizeHeight_(this.screenSize.height);
1994 this.scrollPort_.syncScrollHeight();
1995 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08001996
rginda6d397402012-01-17 10:58:29 -08001997 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08001998 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08001999};
2000
2001/**
2002 * Set the cursor-blink mode bit.
2003 *
2004 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2005 * a visible cursor does not blink.
2006 *
2007 * You should make sure to turn blinking off if you're going to dispose of a
2008 * terminal, otherwise you'll leak a timeout.
2009 *
2010 * Defaults to on.
2011 *
2012 * @param {boolean} state True to set cursor-blink mode, false to unset.
2013 */
2014hterm.Terminal.prototype.setCursorBlink = function(state) {
2015 this.options_.cursorBlink = state;
2016
2017 if (!state && this.timeouts_.cursorBlink) {
2018 clearTimeout(this.timeouts_.cursorBlink);
2019 delete this.timeouts_.cursorBlink;
2020 }
2021
2022 if (this.options_.cursorVisible)
2023 this.setCursorVisible(true);
2024};
2025
2026/**
2027 * Set the cursor-visible mode bit.
2028 *
2029 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2030 *
2031 * Defaults to on.
2032 *
2033 * @param {boolean} state True to set cursor-visible mode, false to unset.
2034 */
2035hterm.Terminal.prototype.setCursorVisible = function(state) {
2036 this.options_.cursorVisible = state;
2037
2038 if (!state) {
rginda87b86462011-12-14 13:48:03 -08002039 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002040 return;
2041 }
2042
rginda87b86462011-12-14 13:48:03 -08002043 this.syncCursorPosition_();
2044
2045 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002046
2047 if (this.options_.cursorBlink) {
2048 if (this.timeouts_.cursorBlink)
2049 return;
2050
2051 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
2052 500);
2053 } else {
2054 if (this.timeouts_.cursorBlink) {
2055 clearTimeout(this.timeouts_.cursorBlink);
2056 delete this.timeouts_.cursorBlink;
2057 }
2058 }
2059};
2060
2061/**
rginda87b86462011-12-14 13:48:03 -08002062 * Synchronizes the visible cursor and document selection with the current
2063 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002064 */
2065hterm.Terminal.prototype.syncCursorPosition_ = function() {
2066 var topRowIndex = this.scrollPort_.getTopRowIndex();
2067 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2068 var cursorRowIndex = this.scrollbackRows_.length +
2069 this.screen_.cursorPosition.row;
2070
2071 if (cursorRowIndex > bottomRowIndex) {
2072 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002073 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002074 return;
2075 }
2076
rginda35c456b2012-02-09 17:29:05 -08002077 this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px';
2078 this.cursorNode_.style.height = this.scrollPort_.characterSize.height + 'px';
2079
rginda8ba33642011-12-14 12:31:31 -08002080 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002081 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2082 'px';
2083 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2084 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002085
2086 this.cursorNode_.setAttribute('title',
2087 '(' + this.screen_.cursorPosition.row +
2088 ', ' + this.screen_.cursorPosition.column +
2089 ')');
2090
2091 // Update the caret for a11y purposes.
2092 var selection = this.document_.getSelection();
2093 if (selection && selection.isCollapsed)
2094 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002095};
2096
2097/**
2098 * Synchronizes the visible cursor with the current cursor coordinates.
2099 *
2100 * The sync will happen asynchronously, soon after the call stack winds down.
2101 * Multiple calls will be coalesced into a single sync.
2102 */
2103hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2104 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002105 return;
rginda8ba33642011-12-14 12:31:31 -08002106
2107 var self = this;
2108 this.timeouts_.syncCursor = setTimeout(function() {
2109 self.syncCursorPosition_();
2110 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002111 }, 0);
2112};
2113
rgindacc2996c2012-02-24 14:59:31 -08002114/**
rgindaf522ce02012-04-17 17:49:17 -07002115 * Show or hide the zoom warning.
2116 *
2117 * The zoom warning is a message warning the user that their browser zoom must
2118 * be set to 100% in order for hterm to function properly.
2119 *
2120 * @param {boolean} state True to show the message, false to hide it.
2121 */
2122hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2123 if (!this.zoomWarningNode_) {
2124 if (!state)
2125 return;
2126
2127 this.zoomWarningNode_ = this.document_.createElement('div');
2128 this.zoomWarningNode_.style.cssText = (
2129 'color: black;' +
2130 'background-color: #ff2222;' +
2131 'font-size: large;' +
2132 'border-radius: 8px;' +
2133 'opacity: 0.75;' +
2134 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2135 'top: 0.5em;' +
2136 'right: 1.2em;' +
2137 'position: absolute;' +
2138 '-webkit-text-size-adjust: none;' +
2139 '-webkit-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002140 }
2141
rgindade84e382012-04-20 15:39:31 -07002142 this.zoomWarningNode_.textContent = hterm.msg('ZOOM_WARNING') ||
2143 ('!! ' + parseInt(this.scrollPort_.characterSize.zoomFactor * 100) +
2144 '% !!');
rgindaf522ce02012-04-17 17:49:17 -07002145 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2146
2147 if (state) {
2148 if (!this.zoomWarningNode_.parentNode)
2149 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2150 } else if (this.zoomWarningNode_.parentNode) {
2151 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2152 }
2153};
2154
2155/**
rgindacc2996c2012-02-24 14:59:31 -08002156 * Show the terminal overlay for a given amount of time.
2157 *
2158 * The terminal overlay appears in inverse video in a large font, centered
2159 * over the terminal. You should probably keep the overlay message brief,
2160 * since it's in a large font and you probably aren't going to check the size
2161 * of the terminal first.
2162 *
2163 * @param {string} msg The text (not HTML) message to display in the overlay.
2164 * @param {number} opt_timeout The amount of time to wait before fading out
2165 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2166 * stay up forever (or until the next overlay).
2167 */
2168hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002169 if (!this.overlayNode_) {
2170 if (!this.div_)
2171 return;
2172
2173 this.overlayNode_ = this.document_.createElement('div');
2174 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002175 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002176 'font-size: xx-large;' +
2177 'opacity: 0.75;' +
2178 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2179 'position: absolute;' +
2180 '-webkit-user-select: none;' +
2181 '-webkit-transition: opacity 180ms ease-in;');
2182 }
2183
rginda9f5222b2012-03-05 11:53:28 -08002184 this.overlayNode_.style.color = this.prefs_.get('background-color');
2185 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2186 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2187
rgindaf0090c92012-02-10 14:58:52 -08002188 this.overlayNode_.textContent = msg;
2189 this.overlayNode_.style.opacity = '0.75';
2190
2191 if (!this.overlayNode_.parentNode)
2192 this.div_.appendChild(this.overlayNode_);
2193
2194 this.overlayNode_.style.top = (
2195 this.div_.clientHeight - this.overlayNode_.clientHeight) / 2;
2196 this.overlayNode_.style.left = (
2197 this.div_.clientWidth - this.overlayNode_.clientWidth -
2198 this.scrollbarWidthPx) / 2;
2199
2200 var self = this;
2201
2202 if (this.overlayTimeout_)
2203 clearTimeout(this.overlayTimeout_);
2204
rgindacc2996c2012-02-24 14:59:31 -08002205 if (opt_timeout === null)
2206 return;
2207
rgindaf0090c92012-02-10 14:58:52 -08002208 this.overlayTimeout_ = setTimeout(function() {
2209 self.overlayNode_.style.opacity = '0';
2210 setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002211 if (self.overlayNode_.parentNode)
2212 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002213 self.overlayTimeout_ = null;
2214 self.overlayNode_.style.opacity = '0.75';
2215 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002216 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002217};
2218
rginda4bba5e12012-06-20 16:15:30 -07002219/**
2220 * Paste from the system clipboard to the terminal.
2221 */
2222hterm.Terminal.prototype.paste = function() {
2223 hterm.pasteFromClipboard(this.document_);
2224};
2225
2226/**
2227 * Copy a string to the system clipboard.
2228 *
2229 * Note: If there is a selected range in the terminal, it'll be cleared.
2230 */
2231hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda9fb38222012-09-11 14:19:12 -07002232 if (this.prefs_.get('enable-clipboard-notice'))
2233 setTimeout(this.showOverlay.bind(this, hterm.msg('NOTIFY_COPY'), 500), 200);
rgindaa09e7332012-08-17 12:49:51 -07002234
2235 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002236 copySource.textContent = str;
2237 copySource.style.cssText = (
2238 '-webkit-user-select: text;' +
2239 'position: absolute;' +
2240 'top: -99px');
2241
2242 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002243
rginda4bba5e12012-06-20 16:15:30 -07002244 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002245 var anchorNode = selection.anchorNode;
2246 var anchorOffset = selection.anchorOffset;
2247 var focusNode = selection.focusNode;
2248 var focusOffset = selection.focusOffset;
2249
rginda4bba5e12012-06-20 16:15:30 -07002250 selection.selectAllChildren(copySource);
2251
rgindaa09e7332012-08-17 12:49:51 -07002252 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002253
rgindafaa74742012-08-21 13:34:03 -07002254 selection.collapse(anchorNode, anchorOffset);
2255 selection.extend(focusNode, focusOffset);
2256
rginda4bba5e12012-06-20 16:15:30 -07002257 copySource.parentNode.removeChild(copySource);
2258};
2259
rgindaa09e7332012-08-17 12:49:51 -07002260hterm.Terminal.prototype.getSelectionText = function() {
2261 var selection = this.scrollPort_.selection;
2262 selection.sync();
2263
2264 if (selection.isCollapsed)
2265 return null;
2266
2267
2268 // Start offset measures from the beginning of the line.
2269 var startOffset = selection.startOffset;
2270 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002271
Robert Gindafdbb3f22012-09-06 20:23:06 -07002272 if (node.nodeName != 'X-ROW') {
2273 // If the selection doesn't start on an x-row node, then it must be
2274 // somewhere inside the x-row. Add any characters from previous siblings
2275 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002276
2277 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2278 // If node is the text node in a styled span, move up to the span node.
2279 node = node.parentNode;
2280 }
2281
Robert Gindafdbb3f22012-09-06 20:23:06 -07002282 while (node.previousSibling) {
2283 node = node.previousSibling;
2284 startOffset += node.textContent.length;
2285 }
rgindaa09e7332012-08-17 12:49:51 -07002286 }
2287
2288 // End offset measures from the end of the line.
2289 var endOffset = selection.endNode.textContent.length - selection.endOffset;
2290 var node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002291
Robert Gindafdbb3f22012-09-06 20:23:06 -07002292 if (node.nodeName != 'X-ROW') {
2293 // If the selection doesn't end on an x-row node, then it must be
2294 // somewhere inside the x-row. Add any characters from following siblings
2295 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002296
2297 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2298 // If node is the text node in a styled span, move up to the span node.
2299 node = node.parentNode;
2300 }
2301
Robert Gindafdbb3f22012-09-06 20:23:06 -07002302 while (node.nextSibling) {
2303 node = node.nextSibling;
2304 endOffset += node.textContent.length;
2305 }
rgindaa09e7332012-08-17 12:49:51 -07002306 }
2307
2308 var rv = this.getRowsText(selection.startRow.rowIndex,
2309 selection.endRow.rowIndex + 1);
2310 return rv.substring(startOffset, rv.length - endOffset);
2311};
2312
rginda4bba5e12012-06-20 16:15:30 -07002313/**
2314 * Copy the current selection to the system clipboard, then clear it after a
2315 * short delay.
2316 */
2317hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002318 var text = this.getSelectionText();
2319 if (text != null)
2320 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002321};
2322
rgindaf0090c92012-02-10 14:58:52 -08002323hterm.Terminal.prototype.overlaySize = function() {
2324 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2325};
2326
rginda87b86462011-12-14 13:48:03 -08002327/**
2328 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2329 *
2330 * @param {string} string The VT string representing the keystroke.
2331 */
2332hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002333 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002334 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2335
2336 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08002337};
2338
2339/**
rgindad5613292012-06-19 15:40:37 -07002340 * Add the terminalRow and terminalColumn properties to mouse events and
2341 * then forward on to onMouse().
2342 *
2343 * The terminalRow and terminalColumn properties contain the (row, column)
2344 * coordinates for the mouse event.
2345 */
2346hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002347 if (e.processedByTerminalHandler_) {
2348 // We register our event handlers on the document, as well as the cursor
2349 // and the scroll blocker. Mouse events that occur on the cursor or
2350 // scroll blocker will also appear on the document, but we don't want to
2351 // process them twice.
2352 //
2353 // We can't just prevent bubbling because that has other side effects, so
2354 // we decorate the event object with this property instead.
2355 return;
2356 }
2357
2358 e.processedByTerminalHandler_ = true;
2359
rginda4bba5e12012-06-20 16:15:30 -07002360 if (e.type == 'mousedown' && e.which == this.mousePasteButton) {
2361 this.paste();
2362 return;
2363 }
2364
2365 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2366 !this.document_.getSelection().isCollapsed) {
rgindafaa74742012-08-21 13:34:03 -07002367 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002368 return;
2369 }
2370
rgindad5613292012-06-19 15:40:37 -07002371 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2372 this.scrollPort_.characterSize.height) + 1;
2373 e.terminalColumn = parseInt(e.clientX /
2374 this.scrollPort_.characterSize.width) + 1;
2375
2376 if (e.type == 'mousedown') {
2377 if (e.terminalColumn > this.screenSize.width) {
2378 // Mousedown in the scrollbar area.
2379 return;
2380 }
2381
2382 if (!this.enableMouseDragScroll) {
2383 // Move the scroll-blocker into place if we want to keep the scrollport
2384 // from scrolling.
2385 this.scrollBlockerNode_.engaged = true;
2386 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2387 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2388 }
2389 } else if (this.scrollBlockerNode_.engaged &&
2390 (e.type == 'mousemove' || e.type == 'mouseup')) {
2391 // Disengage the scroll-blocker after one of these events.
2392 this.scrollBlockerNode_.engaged = false;
2393 this.scrollBlockerNode_.style.top = '-99px';
2394 }
2395
rgindafaa74742012-08-21 13:34:03 -07002396 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07002397};
2398
2399/**
2400 * Clients should override this if they care to know about mouse events.
2401 *
2402 * The event parameter will be a normal DOM mouse click event with additional
2403 * 'terminalRow' and 'terminalColumn' properties.
2404 */
2405hterm.Terminal.prototype.onMouse = function(e) { };
2406
2407/**
rginda8e92a692012-05-20 19:37:20 -07002408 * React when focus changes.
2409 */
2410hterm.Terminal.prototype.onFocusChange_ = function(state) {
2411 this.cursorNode_.setAttribute('focus', state ? 'true' : 'false');
2412};
2413
2414/**
rginda8ba33642011-12-14 12:31:31 -08002415 * React when the ScrollPort is scrolled.
2416 */
2417hterm.Terminal.prototype.onScroll_ = function() {
2418 this.scheduleSyncCursorPosition_();
2419};
2420
2421/**
rginda9846e2f2012-01-27 13:53:33 -08002422 * React when text is pasted into the scrollPort.
2423 */
2424hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaf2547f12012-10-25 20:36:21 -07002425 var text = this.vt.encodeUTF8(e.text);
2426 text = text.replace(/\n/mg, '\r');
2427 this.io.onVTKeystroke(text);
rginda9846e2f2012-01-27 13:53:33 -08002428};
2429
2430/**
rgindaa09e7332012-08-17 12:49:51 -07002431 * React when the user tries to copy from the scrollPort.
2432 */
2433hterm.Terminal.prototype.onCopy_ = function(e) {
2434 e.preventDefault();
rgindafaa74742012-08-21 13:34:03 -07002435 this.copySelectionToClipboard();
rgindaa09e7332012-08-17 12:49:51 -07002436};
2437
2438/**
rginda8ba33642011-12-14 12:31:31 -08002439 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002440 *
2441 * Note: This function should not directly contain code that alters the internal
2442 * state of the terminal. That kind of code belongs in realizeWidth or
2443 * realizeHeight, so that it can be executed synchronously in the case of a
2444 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002445 */
2446hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08002447 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002448 this.scrollPort_.characterSize.width);
2449 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
2450 this.scrollPort_.characterSize.height);
2451
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002452 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08002453 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002454 // gets removed from the document or during the initial load, and we can't
2455 // deal with that.
rginda35c456b2012-02-09 17:29:05 -08002456 return;
2457 }
2458
rgindaa8ba17d2012-08-15 14:41:10 -07002459 var isNewSize = (columnCount != this.screenSize.width ||
2460 rowCount != this.screenSize.height);
2461
2462 // We do this even if the size didn't change, just to be sure everything is
2463 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002464 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07002465 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07002466
2467 if (isNewSize)
2468 this.overlaySize();
2469
2470 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08002471};
2472
2473/**
2474 * Service the cursor blink timeout.
2475 */
2476hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08002477 if (this.cursorNode_.style.opacity == '0') {
2478 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002479 } else {
rginda87b86462011-12-14 13:48:03 -08002480 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002481 }
2482};
David Reveman8f552492012-03-28 12:18:41 -04002483
2484/**
2485 * Set the scrollbar-visible mode bit.
2486 *
2487 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2488 * Otherwise it will not.
2489 *
2490 * Defaults to on.
2491 *
2492 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2493 */
2494hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2495 this.scrollPort_.setScrollbarVisible(state);
2496};