blob: b8c413314888e971d16e3901429eb6083f4af9e3 [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/**
rginda9f5222b2012-03-05 11:53:28 -0800139 * Select a preference profile.
140 *
141 * This will load the terminal preferences for the given profile name and
142 * associate subsequent preference changes with the new preference profile.
143 *
144 * @param {string} newName The name of the preference profile. Forward slash
145 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700146 * @param {function} opt_callback Optional callback to invoke when the profile
147 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800148 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700149hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
150 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800151
Robert Ginda57f03b42012-09-13 11:02:48 -0700152 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800153
Robert Ginda57f03b42012-09-13 11:02:48 -0700154 if (this.prefs_)
155 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800156
Robert Ginda57f03b42012-09-13 11:02:48 -0700157 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
158 this.prefs_.addObservers(null, {
159 'alt-is-meta': function(v) {
160 terminal.keyboard.altIsMeta = v;
161 },
162
163 'alt-sends-what': function(v) {
164 if (!/^(escape|8-bit|browser-key)$/.test(v))
165 v = 'escape';
166
167 terminal.keyboard.altSendsWhat = v;
168 },
169
170 'audible-bell-sound': function(v) {
171 terminal.bellAudio_.setAttribute('src', v);
172 },
173
174 'background-color': function(v) {
175 terminal.setBackgroundColor(v);
176 },
177
178 'background-image': function(v) {
179 terminal.scrollPort_.setBackgroundImage(v);
180 },
181
182 'background-size': function(v) {
183 terminal.scrollPort_.setBackgroundSize(v);
184 },
185
186 'background-position': function(v) {
187 terminal.scrollPort_.setBackgroundPosition(v);
188 },
189
190 'backspace-sends-backspace': function(v) {
191 terminal.keyboard.backspaceSendsBackspace = v;
192 },
193
194 'cursor-blink': function(v) {
195 terminal.setCursorBlink(!!v);
196 },
197
198 'cursor-color': function(v) {
199 terminal.setCursorColor(v);
200 },
201
202 'color-palette-overrides': function(v) {
203 if (!(v == null || v instanceof Object || v instanceof Array)) {
204 console.warn('Preference color-palette-overrides is not an array or ' +
205 'object: ' + v);
206 return;
rginda9f5222b2012-03-05 11:53:28 -0800207 }
rginda9f5222b2012-03-05 11:53:28 -0800208
Robert Ginda57f03b42012-09-13 11:02:48 -0700209 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700210
Robert Ginda57f03b42012-09-13 11:02:48 -0700211 if (v) {
212 for (var key in v) {
213 var i = parseInt(key);
214 if (isNaN(i) || i < 0 || i > 255) {
215 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
216 continue;
217 }
218
219 if (v[i]) {
220 var rgb = lib.colors.normalizeCSS(v[i]);
221 if (rgb)
222 lib.colors.colorPalette[i] = rgb;
223 }
224 }
rginda30f20f62012-04-05 16:36:19 -0700225 }
rginda30f20f62012-04-05 16:36:19 -0700226
Robert Ginda57f03b42012-09-13 11:02:48 -0700227 terminal.primaryScreen_.textAttributes.resetColorPalette()
228 terminal.alternateScreen_.textAttributes.resetColorPalette();
229 },
rginda30f20f62012-04-05 16:36:19 -0700230
Robert Ginda57f03b42012-09-13 11:02:48 -0700231 'copy-on-select': function(v) {
232 terminal.copyOnSelect = !!v;
233 },
rginda9f5222b2012-03-05 11:53:28 -0800234
Robert Ginda57f03b42012-09-13 11:02:48 -0700235 'enable-8-bit-control': function(v) {
236 terminal.vt.enable8BitControl = !!v;
237 },
rginda30f20f62012-04-05 16:36:19 -0700238
Robert Ginda57f03b42012-09-13 11:02:48 -0700239 'enable-bold': function(v) {
240 terminal.syncBoldSafeState();
241 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400242
Robert Ginda57f03b42012-09-13 11:02:48 -0700243 'enable-clipboard-write': function(v) {
244 terminal.vt.enableClipboardWrite = !!v;
245 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400246
Robert Ginda57f03b42012-09-13 11:02:48 -0700247 'font-family': function(v) {
248 terminal.syncFontFamily();
249 },
rginda30f20f62012-04-05 16:36:19 -0700250
Robert Ginda57f03b42012-09-13 11:02:48 -0700251 'font-size': function(v) {
252 terminal.setFontSize(v);
253 },
rginda9875d902012-08-20 16:21:57 -0700254
Robert Ginda57f03b42012-09-13 11:02:48 -0700255 'font-smoothing': function(v) {
256 terminal.syncFontFamily();
257 },
rgindade84e382012-04-20 15:39:31 -0700258
Robert Ginda57f03b42012-09-13 11:02:48 -0700259 'foreground-color': function(v) {
260 terminal.setForegroundColor(v);
261 },
rginda30f20f62012-04-05 16:36:19 -0700262
Robert Ginda57f03b42012-09-13 11:02:48 -0700263 'home-keys-scroll': function(v) {
264 terminal.keyboard.homeKeysScroll = v;
265 },
rginda4bba5e12012-06-20 16:15:30 -0700266
Robert Ginda57f03b42012-09-13 11:02:48 -0700267 'max-string-sequence': function(v) {
268 terminal.vt.maxStringSequence = v;
269 },
rginda11057d52012-04-25 12:29:56 -0700270
Robert Ginda57f03b42012-09-13 11:02:48 -0700271 'meta-sends-escape': function(v) {
272 terminal.keyboard.metaSendsEscape = v;
273 },
rginda30f20f62012-04-05 16:36:19 -0700274
Robert Ginda57f03b42012-09-13 11:02:48 -0700275 'mouse-cell-motion-trick': function(v) {
276 terminal.vt.setMouseCellMotionTrick(v);
277 },
Robert Ginda9fb38222012-09-11 14:19:12 -0700278
Robert Ginda57f03b42012-09-13 11:02:48 -0700279 'mouse-paste-button': function(v) {
280 terminal.syncMousePasteButton();
281 },
rgindaa8ba17d2012-08-15 14:41:10 -0700282
Robert Ginda40932892012-12-10 17:26:40 -0800283 'pass-alt-number': function(v) {
284 if (v == null) {
285 var osx = window.navigator.userAgent.match(/Mac OS X/);
286
287 // Let Alt-1..9 pass to the browser (to control tab switching) on
288 // non-OS X systems, or if hterm is not opened in an app window.
289 v = (!osx && hterm.windowType != 'popup');
290 }
291
292 terminal.passAltNumber = v;
293 },
294
295 'pass-ctrl-number': function(v) {
296 if (v == null) {
297 var osx = window.navigator.userAgent.match(/Mac OS X/);
298
299 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
300 // non-OS X systems, or if hterm is not opened in an app window.
301 v = (!osx && hterm.windowType != 'popup');
302 }
303
304 terminal.passCtrlNumber = v;
305 },
306
307 'pass-meta-number': function(v) {
308 if (v == null) {
309 var osx = window.navigator.userAgent.match(/Mac OS X/);
310
311 // Let Meta-1..9 pass to the browser (to control tab switching) on
312 // OS X systems, or if hterm is not opened in an app window.
313 v = (osx && hterm.windowType != 'popup');
314 }
315
316 terminal.passMetaNumber = v;
317 },
318
Robert Ginda57f03b42012-09-13 11:02:48 -0700319 'scroll-on-keystroke': function(v) {
320 terminal.scrollOnKeystroke_ = v;
321 },
rginda9f5222b2012-03-05 11:53:28 -0800322
Robert Ginda57f03b42012-09-13 11:02:48 -0700323 'scroll-on-output': function(v) {
324 terminal.scrollOnOutput_ = v;
325 },
rginda30f20f62012-04-05 16:36:19 -0700326
Robert Ginda57f03b42012-09-13 11:02:48 -0700327 'scrollbar-visible': function(v) {
328 terminal.setScrollbarVisible(v);
329 },
rginda9f5222b2012-03-05 11:53:28 -0800330
Robert Ginda57f03b42012-09-13 11:02:48 -0700331 'shift-insert-paste': function(v) {
332 terminal.keyboard.shiftInsertPaste = v;
333 },
rginda9f5222b2012-03-05 11:53:28 -0800334
Robert Ginda57f03b42012-09-13 11:02:48 -0700335 'page-keys-scroll': function(v) {
336 terminal.keyboard.pageKeysScroll = v;
337 }
338 });
rginda30f20f62012-04-05 16:36:19 -0700339
Robert Ginda57f03b42012-09-13 11:02:48 -0700340 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800341 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700342
343 if (opt_callback)
344 opt_callback();
345 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800346};
347
rginda8e92a692012-05-20 19:37:20 -0700348
349/**
350 * Set the color for the cursor.
351 *
352 * If you want this setting to persist, set it through prefs_, rather than
353 * with this method.
354 */
355hterm.Terminal.prototype.setCursorColor = function(color) {
356 this.cursorNode_.style.backgroundColor = color;
357 this.cursorNode_.style.borderColor = color;
358};
359
360/**
361 * Return the current cursor color as a string.
362 */
363hterm.Terminal.prototype.getCursorColor = function() {
364 return this.cursorNode_.style.backgroundColor;
365};
366
367/**
rgindad5613292012-06-19 15:40:37 -0700368 * Enable or disable mouse based text selection in the terminal.
369 */
370hterm.Terminal.prototype.setSelectionEnabled = function(state) {
371 this.enableMouseDragScroll = state;
372 this.scrollPort_.setSelectionEnabled(state);
373};
374
375/**
rginda8e92a692012-05-20 19:37:20 -0700376 * Set the background color.
377 *
378 * If you want this setting to persist, set it through prefs_, rather than
379 * with this method.
380 */
381hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700382 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700383 this.primaryScreen_.textAttributes.setDefaults(
384 this.foregroundColor_, this.backgroundColor_);
385 this.alternateScreen_.textAttributes.setDefaults(
386 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700387 this.scrollPort_.setBackgroundColor(color);
388};
389
rginda9f5222b2012-03-05 11:53:28 -0800390/**
391 * Return the current terminal background color.
392 *
393 * Intended for use by other classes, so we don't have to expose the entire
394 * prefs_ object.
395 */
396hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700397 return this.backgroundColor_;
398};
399
400/**
401 * Set the foreground color.
402 *
403 * If you want this setting to persist, set it through prefs_, rather than
404 * with this method.
405 */
406hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700407 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700408 this.primaryScreen_.textAttributes.setDefaults(
409 this.foregroundColor_, this.backgroundColor_);
410 this.alternateScreen_.textAttributes.setDefaults(
411 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700412 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800413};
414
415/**
416 * Return the current terminal foreground color.
417 *
418 * Intended for use by other classes, so we don't have to expose the entire
419 * prefs_ object.
420 */
421hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700422 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800423};
424
425/**
rginda87b86462011-12-14 13:48:03 -0800426 * Create a new instance of a terminal command and run it with a given
427 * argument string.
428 *
429 * @param {function} commandClass The constructor for a terminal command.
430 * @param {string} argString The argument string to pass to the command.
431 */
432hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700433 var environment = this.prefs_.get('environment');
434 if (typeof environment != 'object' || environment == null)
435 environment = {};
436
rginda87b86462011-12-14 13:48:03 -0800437 var self = this;
438 this.command = new commandClass(
439 { argString: argString || '',
440 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700441 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800442 onExit: function(code) {
443 self.io.pop();
444 self.io.println(hterm.msg('COMMAND_COMPLETE',
445 [self.command.commandName, code]));
rgindafeaf3142012-01-31 15:14:20 -0800446 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700447 if (self.prefs_.get('close-on-exit'))
448 window.close();
rginda87b86462011-12-14 13:48:03 -0800449 }
450 });
451
rgindafeaf3142012-01-31 15:14:20 -0800452 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800453 this.command.run();
454};
455
456/**
rgindafeaf3142012-01-31 15:14:20 -0800457 * Returns true if the current screen is the primary screen, false otherwise.
458 */
459hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700460 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800461};
462
463/**
464 * Install the keyboard handler for this terminal.
465 *
466 * This will prevent the browser from seeing any keystrokes sent to the
467 * terminal.
468 */
469hterm.Terminal.prototype.installKeyboard = function() {
470 this.keyboard.installKeyboard(this.document_.body.firstChild);
471}
472
473/**
474 * Uninstall the keyboard handler for this terminal.
475 */
476hterm.Terminal.prototype.uninstallKeyboard = function() {
477 this.keyboard.installKeyboard(null);
478}
479
480/**
rginda35c456b2012-02-09 17:29:05 -0800481 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800482 *
483 * Call setFontSize(0) to reset to the default font size.
484 *
485 * This function does not modify the font-size preference.
486 *
487 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800488 */
489hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800490 if (px === 0)
491 px = this.prefs_.get('font-size');
492
rginda35c456b2012-02-09 17:29:05 -0800493 this.scrollPort_.setFontSize(px);
494};
495
496/**
497 * Get the current font size.
498 */
499hterm.Terminal.prototype.getFontSize = function() {
500 return this.scrollPort_.getFontSize();
501};
502
503/**
rginda8e92a692012-05-20 19:37:20 -0700504 * Get the current font family.
505 */
506hterm.Terminal.prototype.getFontFamily = function() {
507 return this.scrollPort_.getFontFamily();
508};
509
510/**
rginda35c456b2012-02-09 17:29:05 -0800511 * Set the CSS "font-family" for this terminal.
512 */
rginda9f5222b2012-03-05 11:53:28 -0800513hterm.Terminal.prototype.syncFontFamily = function() {
514 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
515 this.prefs_.get('font-smoothing'));
516 this.syncBoldSafeState();
517};
518
rginda4bba5e12012-06-20 16:15:30 -0700519/**
520 * Set this.mousePasteButton based on the mouse-paste-button pref,
521 * autodetecting if necessary.
522 */
523hterm.Terminal.prototype.syncMousePasteButton = function() {
524 var button = this.prefs_.get('mouse-paste-button');
525 if (typeof button == 'number') {
526 this.mousePasteButton = button;
527 return;
528 }
529
530 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
531 if (!ary || ary[2] == 'CrOS') {
532 this.mousePasteButton = 2;
533 } else {
534 this.mousePasteButton = 3;
535 }
536};
537
538/**
539 * Enable or disable bold based on the enable-bold pref, autodetecting if
540 * necessary.
541 */
rginda9f5222b2012-03-05 11:53:28 -0800542hterm.Terminal.prototype.syncBoldSafeState = function() {
543 var enableBold = this.prefs_.get('enable-bold');
544 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700545 this.primaryScreen_.textAttributes.enableBold = enableBold;
546 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800547 return;
548 }
549
rgindaf7521392012-02-28 17:20:34 -0800550 var normalSize = this.scrollPort_.measureCharacterSize();
551 var boldSize = this.scrollPort_.measureCharacterSize('bold');
552
553 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800554 if (!isBoldSafe) {
555 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700556 'from normal. Font family is: ' +
557 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800558 }
rginda9f5222b2012-03-05 11:53:28 -0800559
Robert Gindaed016262012-10-26 16:27:09 -0700560 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
561 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800562};
563
564/**
rginda87b86462011-12-14 13:48:03 -0800565 * Return a copy of the current cursor position.
566 *
567 * @return {hterm.RowCol} The RowCol object representing the current position.
568 */
569hterm.Terminal.prototype.saveCursor = function() {
570 return this.screen_.cursorPosition.clone();
571};
572
rgindaa19afe22012-01-25 15:40:22 -0800573hterm.Terminal.prototype.getTextAttributes = function() {
574 return this.screen_.textAttributes;
575};
576
rginda1a09aa02012-06-18 21:11:25 -0700577hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
578 this.screen_.textAttributes = textAttributes;
579};
580
rginda87b86462011-12-14 13:48:03 -0800581/**
rgindaf522ce02012-04-17 17:49:17 -0700582 * Return the current browser zoom factor applied to the terminal.
583 *
584 * @return {number} The current browser zoom factor.
585 */
586hterm.Terminal.prototype.getZoomFactor = function() {
587 return this.scrollPort_.characterSize.zoomFactor;
588};
589
590/**
rginda9846e2f2012-01-27 13:53:33 -0800591 * Change the title of this terminal's window.
592 */
593hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800594 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800595};
596
597/**
rginda87b86462011-12-14 13:48:03 -0800598 * Restore a previously saved cursor position.
599 *
600 * @param {hterm.RowCol} cursor The position to restore.
601 */
602hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700603 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
604 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800605 this.screen_.setCursorPosition(row, column);
606 if (cursor.column > column ||
607 cursor.column == column && cursor.overflow) {
608 this.screen_.cursorPosition.overflow = true;
609 }
rginda87b86462011-12-14 13:48:03 -0800610};
611
612/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400613 * Clear the cursor's overflow flag.
614 */
615hterm.Terminal.prototype.clearCursorOverflow = function() {
616 this.screen_.cursorPosition.overflow = false;
617};
618
619/**
rginda87b86462011-12-14 13:48:03 -0800620 * Set the width of the terminal, resizing the UI to match.
621 */
622hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800623 if (columnCount == null) {
624 this.div_.style.width = '100%';
625 return;
626 }
627
rginda35c456b2012-02-09 17:29:05 -0800628 this.div_.style.width = this.scrollPort_.characterSize.width *
Robert Ginda97769282013-02-01 15:30:30 -0800629 columnCount + this.scrollPort_.currentScrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400630 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800631 this.scheduleSyncCursorPosition_();
632};
rginda87b86462011-12-14 13:48:03 -0800633
rgindac9bc5502012-01-18 11:48:44 -0800634/**
rginda35c456b2012-02-09 17:29:05 -0800635 * Set the height of the terminal, resizing the UI to match.
636 */
637hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800638 if (rowCount == null) {
639 this.div_.style.height = '100%';
640 return;
641 }
642
rginda35c456b2012-02-09 17:29:05 -0800643 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700644 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800645 this.realizeSize_(this.screenSize.width, rowCount);
646 this.scheduleSyncCursorPosition_();
647};
648
649/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400650 * Deal with terminal size changes.
651 *
652 */
653hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
654 if (columnCount != this.screenSize.width)
655 this.realizeWidth_(columnCount);
656
657 if (rowCount != this.screenSize.height)
658 this.realizeHeight_(rowCount);
659
660 // Send new terminal size to plugin.
661 this.io.onTerminalResize(columnCount, rowCount);
662};
663
664/**
rgindac9bc5502012-01-18 11:48:44 -0800665 * Deal with terminal width changes.
666 *
667 * This function does what needs to be done when the terminal width changes
668 * out from under us. It happens here rather than in onResize_() because this
669 * code may need to run synchronously to handle programmatic changes of
670 * terminal width.
671 *
672 * Relying on the browser to send us an async resize event means we may not be
673 * in the correct state yet when the next escape sequence hits.
674 */
675hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700676 if (columnCount <= 0)
677 throw new Error('Attempt to realize bad width: ' + columnCount);
678
rgindac9bc5502012-01-18 11:48:44 -0800679 var deltaColumns = columnCount - this.screen_.getWidth();
680
rginda87b86462011-12-14 13:48:03 -0800681 this.screenSize.width = columnCount;
682 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800683
684 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400685 if (this.defaultTabStops)
686 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800687 } else {
688 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400689 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800690 break;
691
692 this.tabStops_.pop();
693 }
694 }
695
696 this.screen_.setColumnCount(this.screenSize.width);
697};
698
699/**
700 * Deal with terminal height changes.
701 *
702 * This function does what needs to be done when the terminal height changes
703 * out from under us. It happens here rather than in onResize_() because this
704 * code may need to run synchronously to handle programmatic changes of
705 * terminal height.
706 *
707 * Relying on the browser to send us an async resize event means we may not be
708 * in the correct state yet when the next escape sequence hits.
709 */
710hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700711 if (rowCount <= 0)
712 throw new Error('Attempt to realize bad height: ' + rowCount);
713
rgindac9bc5502012-01-18 11:48:44 -0800714 var deltaRows = rowCount - this.screen_.getHeight();
715
716 this.screenSize.height = rowCount;
717
718 var cursor = this.saveCursor();
719
720 if (deltaRows < 0) {
721 // Screen got smaller.
722 deltaRows *= -1;
723 while (deltaRows) {
724 var lastRow = this.getRowCount() - 1;
725 if (lastRow - this.scrollbackRows_.length == cursor.row)
726 break;
727
728 if (this.getRowText(lastRow))
729 break;
730
731 this.screen_.popRow();
732 deltaRows--;
733 }
734
735 var ary = this.screen_.shiftRows(deltaRows);
736 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
737
738 // We just removed rows from the top of the screen, we need to update
739 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800740 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800741 } else if (deltaRows > 0) {
742 // Screen got larger.
743
744 if (deltaRows <= this.scrollbackRows_.length) {
745 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
746 var rows = this.scrollbackRows_.splice(
747 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
748 this.screen_.unshiftRows(rows);
749 deltaRows -= scrollbackCount;
750 cursor.row += scrollbackCount;
751 }
752
753 if (deltaRows)
754 this.appendRows_(deltaRows);
755 }
756
rginda35c456b2012-02-09 17:29:05 -0800757 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800758 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800759};
760
761/**
762 * Scroll the terminal to the top of the scrollback buffer.
763 */
764hterm.Terminal.prototype.scrollHome = function() {
765 this.scrollPort_.scrollRowToTop(0);
766};
767
768/**
769 * Scroll the terminal to the end.
770 */
771hterm.Terminal.prototype.scrollEnd = function() {
772 this.scrollPort_.scrollRowToBottom(this.getRowCount());
773};
774
775/**
776 * Scroll the terminal one page up (minus one line) relative to the current
777 * position.
778 */
779hterm.Terminal.prototype.scrollPageUp = function() {
780 var i = this.scrollPort_.getTopRowIndex();
781 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
782};
783
784/**
785 * Scroll the terminal one page down (minus one line) relative to the current
786 * position.
787 */
788hterm.Terminal.prototype.scrollPageDown = function() {
789 var i = this.scrollPort_.getTopRowIndex();
790 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800791};
792
rgindac9bc5502012-01-18 11:48:44 -0800793/**
Robert Ginda40932892012-12-10 17:26:40 -0800794 * Clear primary screen, secondary screen, and the scrollback buffer.
795 */
796hterm.Terminal.prototype.wipeContents = function() {
797 this.scrollbackRows_.length = 0;
798 this.scrollPort_.resetCache();
799
800 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
801 var bottom = screen.getHeight();
802 if (bottom > 0) {
803 this.renumberRows_(0, bottom);
804 this.clearHome(screen);
805 }
806 }.bind(this));
807
808 this.syncCursorPosition_();
809};
810
811/**
rgindac9bc5502012-01-18 11:48:44 -0800812 * Full terminal reset.
813 */
rginda87b86462011-12-14 13:48:03 -0800814hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800815 this.clearAllTabStops();
816 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -0700817
818 this.clearHome(this.primaryScreen_);
819 this.primaryScreen_.textAttributes.reset();
820
821 this.clearHome(this.alternateScreen_);
822 this.alternateScreen_.textAttributes.reset();
823
rgindab8bc8932012-04-27 12:45:03 -0700824 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
825
rgindac9bc5502012-01-18 11:48:44 -0800826 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800827};
828
rgindac9bc5502012-01-18 11:48:44 -0800829/**
830 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -0700831 *
832 * Perform a soft reset to the default values listed in
833 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -0800834 */
rginda0f5c0292012-01-13 11:00:13 -0800835hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -0700836 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -0800837 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -0700838
rgindab8bc8932012-04-27 12:45:03 -0700839 // Xterm also resets the color palette on soft reset, even though it doesn't
840 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -0700841 this.primaryScreen_.textAttributes.resetColorPalette();
842 this.alternateScreen_.textAttributes.resetColorPalette();
843
rgindab8bc8932012-04-27 12:45:03 -0700844 // The xterm man page explicitly says this will happen on soft reset.
845 this.setVTScrollRegion(null, null);
846
847 // Xterm also shows the cursor on soft reset, but does not alter the blink
848 // state.
rgindaa19afe22012-01-25 15:40:22 -0800849 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -0800850};
851
rgindac9bc5502012-01-18 11:48:44 -0800852/**
853 * Move the cursor forward to the next tab stop, or to the last column
854 * if no more tab stops are set.
855 */
856hterm.Terminal.prototype.forwardTabStop = function() {
857 var column = this.screen_.cursorPosition.column;
858
859 for (var i = 0; i < this.tabStops_.length; i++) {
860 if (this.tabStops_[i] > column) {
861 this.setCursorColumn(this.tabStops_[i]);
862 return;
863 }
864 }
865
David Benjamin66e954d2012-05-05 21:08:12 -0400866 // xterm does not clear the overflow flag on HT or CHT.
867 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -0800868 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -0400869 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -0800870};
871
rgindac9bc5502012-01-18 11:48:44 -0800872/**
873 * Move the cursor backward to the previous tab stop, or to the first column
874 * if no previous tab stops are set.
875 */
876hterm.Terminal.prototype.backwardTabStop = function() {
877 var column = this.screen_.cursorPosition.column;
878
879 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
880 if (this.tabStops_[i] < column) {
881 this.setCursorColumn(this.tabStops_[i]);
882 return;
883 }
884 }
885
886 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800887};
888
rgindac9bc5502012-01-18 11:48:44 -0800889/**
890 * Set a tab stop at the given column.
891 *
892 * @param {int} column Zero based column.
893 */
894hterm.Terminal.prototype.setTabStop = function(column) {
895 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
896 if (this.tabStops_[i] == column)
897 return;
898
899 if (this.tabStops_[i] < column) {
900 this.tabStops_.splice(i + 1, 0, column);
901 return;
902 }
903 }
904
905 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800906};
907
rgindac9bc5502012-01-18 11:48:44 -0800908/**
909 * Clear the tab stop at the current cursor position.
910 *
911 * No effect if there is no tab stop at the current cursor position.
912 */
913hterm.Terminal.prototype.clearTabStopAtCursor = function() {
914 var column = this.screen_.cursorPosition.column;
915
916 var i = this.tabStops_.indexOf(column);
917 if (i == -1)
918 return;
919
920 this.tabStops_.splice(i, 1);
921};
922
923/**
924 * Clear all tab stops.
925 */
926hterm.Terminal.prototype.clearAllTabStops = function() {
927 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -0400928 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -0800929};
930
931/**
932 * Set up the default tab stops, starting from a given column.
933 *
934 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -0400935 * from the specified column, or 0 if no column is provided. It also flags
936 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -0800937 *
938 * This does not clear the existing tab stops first, use clearAllTabStops
939 * for that.
940 *
941 * @param {int} opt_start Optional starting zero based starting column, useful
942 * for filling out missing tab stops when the terminal is resized.
943 */
944hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
945 var start = opt_start || 0;
946 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -0400947 // Round start up to a default tab stop.
948 start = start - 1 - ((start - 1) % w) + w;
949 for (var i = start; i < this.screenSize.width; i += w) {
950 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -0800951 }
David Benjamin66e954d2012-05-05 21:08:12 -0400952
953 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -0800954};
955
rginda6d397402012-01-17 10:58:29 -0800956/**
rginda8ba33642011-12-14 12:31:31 -0800957 * Interpret a sequence of characters.
958 *
959 * Incomplete escape sequences are buffered until the next call.
960 *
961 * @param {string} str Sequence of characters to interpret or pass through.
962 */
963hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -0800964 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -0800965 this.scheduleSyncCursorPosition_();
966};
967
968/**
969 * Take over the given DIV for use as the terminal display.
970 *
971 * @param {HTMLDivElement} div The div to use as the terminal display.
972 */
973hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -0800974 this.div_ = div;
975
rginda8ba33642011-12-14 12:31:31 -0800976 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -0700977 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -0400978 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
979 this.scrollPort_.setBackgroundPosition(
980 this.prefs_.get('background-position'));
rginda30f20f62012-04-05 16:36:19 -0700981
rginda0918b652012-04-04 11:26:24 -0700982 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -0800983
rginda9f5222b2012-03-05 11:53:28 -0800984 this.setFontSize(this.prefs_.get('font-size'));
985 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -0800986
David Reveman8f552492012-03-28 12:18:41 -0400987 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
988
rginda8ba33642011-12-14 12:31:31 -0800989 this.document_ = this.scrollPort_.getDocument();
990
rginda4bba5e12012-06-20 16:15:30 -0700991 this.document_.body.oncontextmenu = function() { return false };
992
993 var onMouse = this.onMouse_.bind(this);
994 this.document_.body.firstChild.addEventListener('mousedown', onMouse);
995 this.document_.body.firstChild.addEventListener('mouseup', onMouse);
996 this.document_.body.firstChild.addEventListener('mousemove', onMouse);
997 this.scrollPort_.onScrollWheel = onMouse;
998
rginda8e92a692012-05-20 19:37:20 -0700999 this.document_.body.firstChild.addEventListener(
1000 'focus', this.onFocusChange_.bind(this, true));
1001 this.document_.body.firstChild.addEventListener(
1002 'blur', this.onFocusChange_.bind(this, false));
1003
1004 var style = this.document_.createElement('style');
1005 style.textContent =
1006 ('.cursor-node[focus="false"] {' +
1007 ' box-sizing: border-box;' +
1008 ' background-color: transparent !important;' +
1009 ' border-width: 2px;' +
1010 ' border-style: solid;' +
1011 '}');
1012 this.document_.head.appendChild(style);
1013
rginda8ba33642011-12-14 12:31:31 -08001014 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001015 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001016 this.cursorNode_.style.cssText =
1017 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001018 'top: -99px;' +
1019 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001020 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1021 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001022 '-webkit-transition: opacity, background-color 100ms linear;');
1023 this.setCursorColor(this.prefs_.get('cursor-color'));
rgindad5613292012-06-19 15:40:37 -07001024
rginda8ba33642011-12-14 12:31:31 -08001025 this.document_.body.appendChild(this.cursorNode_);
1026
rgindad5613292012-06-19 15:40:37 -07001027 // When 'enableMouseDragScroll' is off we reposition this element directly
1028 // under the mouse cursor after a click. This makes Chrome associate
1029 // subsequent mousemove events with the scroll-blocker. Since the
1030 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1031 // events do not cause the scrollport to scroll.
1032 //
1033 // It's a hack, but it's the cleanest way I could find.
1034 this.scrollBlockerNode_ = this.document_.createElement('div');
1035 this.scrollBlockerNode_.style.cssText =
1036 ('position: absolute;' +
1037 'top: -99px;' +
1038 'display: block;' +
1039 'width: 10px;' +
1040 'height: 10px;');
1041 this.document_.body.appendChild(this.scrollBlockerNode_);
1042
1043 var onMouse = this.onMouse_.bind(this);
1044 this.scrollPort_.onScrollWheel = onMouse;
1045 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1046 ].forEach(function(event) {
1047 this.scrollBlockerNode_.addEventListener(event, onMouse);
1048 this.cursorNode_.addEventListener(event, onMouse);
1049 this.document_.addEventListener(event, onMouse);
1050 }.bind(this));
1051
1052 this.cursorNode_.addEventListener('mousedown', function() {
1053 setTimeout(this.focus.bind(this));
1054 }.bind(this));
1055
rgindade84e382012-04-20 15:39:31 -07001056 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
rginda8ba33642011-12-14 12:31:31 -08001057 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001058
rginda87b86462011-12-14 13:48:03 -08001059 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001060 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001061};
1062
rginda0918b652012-04-04 11:26:24 -07001063/**
1064 * Return the HTML document that contains the terminal DOM nodes.
1065 */
rginda87b86462011-12-14 13:48:03 -08001066hterm.Terminal.prototype.getDocument = function() {
1067 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001068};
1069
1070/**
rginda0918b652012-04-04 11:26:24 -07001071 * Focus the terminal.
1072 */
1073hterm.Terminal.prototype.focus = function() {
1074 this.scrollPort_.focus();
1075};
1076
1077/**
rginda8ba33642011-12-14 12:31:31 -08001078 * Return the HTML Element for a given row index.
1079 *
1080 * This is a method from the RowProvider interface. The ScrollPort uses
1081 * it to fetch rows on demand as they are scrolled into view.
1082 *
1083 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1084 * pairs to conserve memory.
1085 *
1086 * @param {integer} index The zero-based row index, measured relative to the
1087 * start of the scrollback buffer. On-screen rows will always have the
1088 * largest indicies.
1089 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1090 */
1091hterm.Terminal.prototype.getRowNode = function(index) {
1092 if (index < this.scrollbackRows_.length)
1093 return this.scrollbackRows_[index];
1094
1095 var screenIndex = index - this.scrollbackRows_.length;
1096 return this.screen_.rowsArray[screenIndex];
1097};
1098
1099/**
1100 * Return the text content for a given range of rows.
1101 *
1102 * This is a method from the RowProvider interface. The ScrollPort uses
1103 * it to fetch text content on demand when the user attempts to copy their
1104 * selection to the clipboard.
1105 *
1106 * @param {integer} start The zero-based row index to start from, measured
1107 * relative to the start of the scrollback buffer. On-screen rows will
1108 * always have the largest indicies.
1109 * @param {integer} end The zero-based row index to end on, measured
1110 * relative to the start of the scrollback buffer.
1111 * @return {string} A single string containing the text value of the range of
1112 * rows. Lines will be newline delimited, with no trailing newline.
1113 */
1114hterm.Terminal.prototype.getRowsText = function(start, end) {
1115 var ary = [];
1116 for (var i = start; i < end; i++) {
1117 var node = this.getRowNode(i);
1118 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001119 if (i < end - 1 && !node.getAttribute('line-overflow'))
1120 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001121 }
1122
rgindaa09e7332012-08-17 12:49:51 -07001123 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001124};
1125
1126/**
1127 * Return the text content for a given row.
1128 *
1129 * This is a method from the RowProvider interface. The ScrollPort uses
1130 * it to fetch text content on demand when the user attempts to copy their
1131 * selection to the clipboard.
1132 *
1133 * @param {integer} index The zero-based row index to return, measured
1134 * relative to the start of the scrollback buffer. On-screen rows will
1135 * always have the largest indicies.
1136 * @return {string} A string containing the text value of the selected row.
1137 */
1138hterm.Terminal.prototype.getRowText = function(index) {
1139 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001140 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001141};
1142
1143/**
1144 * Return the total number of rows in the addressable screen and in the
1145 * scrollback buffer of this terminal.
1146 *
1147 * This is a method from the RowProvider interface. The ScrollPort uses
1148 * it to compute the size of the scrollbar.
1149 *
1150 * @return {integer} The number of rows in this terminal.
1151 */
1152hterm.Terminal.prototype.getRowCount = function() {
1153 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1154};
1155
1156/**
1157 * Create DOM nodes for new rows and append them to the end of the terminal.
1158 *
1159 * This is the only correct way to add a new DOM node for a row. Notice that
1160 * the new row is appended to the bottom of the list of rows, and does not
1161 * require renumbering (of the rowIndex property) of previous rows.
1162 *
1163 * If you think you want a new blank row somewhere in the middle of the
1164 * terminal, look into moveRows_().
1165 *
1166 * This method does not pay attention to vtScrollTop/Bottom, since you should
1167 * be using moveRows() in cases where they would matter.
1168 *
1169 * The cursor will be positioned at column 0 of the first inserted line.
1170 */
1171hterm.Terminal.prototype.appendRows_ = function(count) {
1172 var cursorRow = this.screen_.rowsArray.length;
1173 var offset = this.scrollbackRows_.length + cursorRow;
1174 for (var i = 0; i < count; i++) {
1175 var row = this.document_.createElement('x-row');
1176 row.appendChild(this.document_.createTextNode(''));
1177 row.rowIndex = offset + i;
1178 this.screen_.pushRow(row);
1179 }
1180
1181 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1182 if (extraRows > 0) {
1183 var ary = this.screen_.shiftRows(extraRows);
1184 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001185 if (this.scrollPort_.isScrolledEnd)
1186 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001187 }
1188
1189 if (cursorRow >= this.screen_.rowsArray.length)
1190 cursorRow = this.screen_.rowsArray.length - 1;
1191
rginda87b86462011-12-14 13:48:03 -08001192 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001193};
1194
1195/**
1196 * Relocate rows from one part of the addressable screen to another.
1197 *
1198 * This is used to recycle rows during VT scrolls (those which are driven
1199 * by VT commands, rather than by the user manipulating the scrollbar.)
1200 *
1201 * In this case, the blank lines scrolled into the scroll region are made of
1202 * the nodes we scrolled off. These have their rowIndex properties carefully
1203 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001204 */
1205hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1206 var ary = this.screen_.removeRows(fromIndex, count);
1207 this.screen_.insertRows(toIndex, ary);
1208
1209 var start, end;
1210 if (fromIndex < toIndex) {
1211 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001212 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001213 } else {
1214 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001215 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001216 }
1217
1218 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001219 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001220};
1221
1222/**
1223 * Renumber the rowIndex property of the given range of rows.
1224 *
1225 * The start and end indicies are relative to the screen, not the scrollback.
1226 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001227 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001228 * no need to renumber scrollback rows.
1229 */
Robert Ginda40932892012-12-10 17:26:40 -08001230hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1231 var screen = opt_screen || this.screen_;
1232
rginda8ba33642011-12-14 12:31:31 -08001233 var offset = this.scrollbackRows_.length;
1234 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001235 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001236 }
1237};
1238
1239/**
1240 * Print a string to the terminal.
1241 *
1242 * This respects the current insert and wraparound modes. It will add new lines
1243 * to the end of the terminal, scrolling off the top into the scrollback buffer
1244 * if necessary.
1245 *
1246 * The string is *not* parsed for escape codes. Use the interpret() method if
1247 * that's what you're after.
1248 *
1249 * @param{string} str The string to print.
1250 */
1251hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001252 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001253
rgindaa9abdd82012-08-06 18:05:09 -07001254 while (startOffset < str.length) {
rgindaa09e7332012-08-17 12:49:51 -07001255 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1256 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001257 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001258 }
rgindaa19afe22012-01-25 15:40:22 -08001259
rgindaa9abdd82012-08-06 18:05:09 -07001260 var count = str.length - startOffset;
1261 var didOverflow = false;
1262 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001263
rgindaa9abdd82012-08-06 18:05:09 -07001264 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1265 didOverflow = true;
1266 count = this.screenSize.width - this.screen_.cursorPosition.column;
1267 }
rgindaa19afe22012-01-25 15:40:22 -08001268
rgindaa9abdd82012-08-06 18:05:09 -07001269 if (didOverflow && !this.options_.wraparound) {
1270 // If the string overflowed the line but wraparound is off, then the
1271 // last printed character should be the last of the string.
1272 // TODO: This will add to our problems with multibyte UTF-16 characters.
1273 substr = str.substr(startOffset, count - 1) +
1274 str.substr(str.length - 1);
1275 count = str.length;
1276 } else {
1277 substr = str.substr(startOffset, count);
1278 }
rgindaa19afe22012-01-25 15:40:22 -08001279
rgindaa9abdd82012-08-06 18:05:09 -07001280 if (this.options_.insertMode) {
1281 this.screen_.insertString(substr);
1282 } else {
1283 this.screen_.overwriteString(substr);
1284 }
1285
1286 this.screen_.maybeClipCurrentRow();
1287 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001288 }
rginda8ba33642011-12-14 12:31:31 -08001289
1290 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001291
rginda9f5222b2012-03-05 11:53:28 -08001292 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001293 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001294};
1295
1296/**
rginda87b86462011-12-14 13:48:03 -08001297 * Set the VT scroll region.
1298 *
rginda87b86462011-12-14 13:48:03 -08001299 * This also resets the cursor position to the absolute (0, 0) position, since
1300 * that's what xterm appears to do.
1301 *
1302 * @param {integer} scrollTop The zero-based top of the scroll region.
1303 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1304 * inclusive.
1305 */
1306hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
1307 this.vtScrollTop_ = scrollTop;
1308 this.vtScrollBottom_ = scrollBottom;
rginda87b86462011-12-14 13:48:03 -08001309};
1310
1311/**
rginda8ba33642011-12-14 12:31:31 -08001312 * Return the top row index according to the VT.
1313 *
1314 * This will return 0 unless the terminal has been told to restrict scrolling
1315 * to some lower row. It is used for some VT cursor positioning and scrolling
1316 * commands.
1317 *
1318 * @return {integer} The topmost row in the terminal's scroll region.
1319 */
1320hterm.Terminal.prototype.getVTScrollTop = function() {
1321 if (this.vtScrollTop_ != null)
1322 return this.vtScrollTop_;
1323
1324 return 0;
rginda87b86462011-12-14 13:48:03 -08001325};
rginda8ba33642011-12-14 12:31:31 -08001326
1327/**
1328 * Return the bottom row index according to the VT.
1329 *
1330 * This will return the height of the terminal unless the it has been told to
1331 * restrict scrolling to some higher row. It is used for some VT cursor
1332 * positioning and scrolling commands.
1333 *
1334 * @return {integer} The bottommost row in the terminal's scroll region.
1335 */
1336hterm.Terminal.prototype.getVTScrollBottom = function() {
1337 if (this.vtScrollBottom_ != null)
1338 return this.vtScrollBottom_;
1339
rginda87b86462011-12-14 13:48:03 -08001340 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001341}
1342
1343/**
1344 * Process a '\n' character.
1345 *
1346 * If the cursor is on the final row of the terminal this will append a new
1347 * blank row to the screen and scroll the topmost row into the scrollback
1348 * buffer.
1349 *
1350 * Otherwise, this moves the cursor to column zero of the next row.
1351 */
1352hterm.Terminal.prototype.newLine = function() {
1353 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -08001354 // If we're at the end of the screen we need to append a new line and
1355 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -08001356 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -08001357 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
1358 // End of the scroll region does not affect the scrollback buffer.
1359 this.vtScrollUp(1);
1360 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -08001361 } else {
rginda87b86462011-12-14 13:48:03 -08001362 // Anywhere else in the screen just moves the cursor.
1363 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001364 }
1365};
1366
1367/**
1368 * Like newLine(), except maintain the cursor column.
1369 */
1370hterm.Terminal.prototype.lineFeed = function() {
1371 var column = this.screen_.cursorPosition.column;
1372 this.newLine();
1373 this.setCursorColumn(column);
1374};
1375
1376/**
rginda87b86462011-12-14 13:48:03 -08001377 * If autoCarriageReturn is set then newLine(), else lineFeed().
1378 */
1379hterm.Terminal.prototype.formFeed = function() {
1380 if (this.options_.autoCarriageReturn) {
1381 this.newLine();
1382 } else {
1383 this.lineFeed();
1384 }
1385};
1386
1387/**
1388 * Move the cursor up one row, possibly inserting a blank line.
1389 *
1390 * The cursor column is not changed.
1391 */
1392hterm.Terminal.prototype.reverseLineFeed = function() {
1393 var scrollTop = this.getVTScrollTop();
1394 var currentRow = this.screen_.cursorPosition.row;
1395
1396 if (currentRow == scrollTop) {
1397 this.insertLines(1);
1398 } else {
1399 this.setAbsoluteCursorRow(currentRow - 1);
1400 }
1401};
1402
1403/**
rginda8ba33642011-12-14 12:31:31 -08001404 * Replace all characters to the left of the current cursor with the space
1405 * character.
1406 *
1407 * TODO(rginda): This should probably *remove* the characters (not just replace
1408 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001409 * position.
rginda8ba33642011-12-14 12:31:31 -08001410 */
1411hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001412 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001413 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001414 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001415 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001416};
1417
1418/**
David Benjamin684a9b72012-05-01 17:19:58 -04001419 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001420 *
1421 * The cursor position is unchanged.
1422 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001423 * If the current background color is not the default background color this
1424 * will insert spaces rather than delete. This is unfortunate because the
1425 * trailing space will affect text selection, but it's difficult to come up
1426 * with a way to style empty space that wouldn't trip up the hterm.Screen
1427 * code.
rginda8ba33642011-12-14 12:31:31 -08001428 */
1429hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001430 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1431 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001432
1433 if (this.screen_.textAttributes.background ===
1434 this.screen_.textAttributes.DEFAULT_COLOR) {
1435 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
1436 if (cursorRow.textContent.length <=
1437 this.screen_.cursorPosition.column + count) {
1438 this.screen_.deleteChars(count);
1439 this.clearCursorOverflow();
1440 return;
1441 }
1442 }
1443
rginda87b86462011-12-14 13:48:03 -08001444 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001445 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001446 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001447 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001448};
1449
1450/**
1451 * Erase the current line.
1452 *
1453 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001454 */
1455hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001456 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001457 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001458 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001459 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001460};
1461
1462/**
David Benjamina08d78f2012-05-05 00:28:49 -04001463 * Erase all characters from the start of the screen to the current cursor
1464 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001465 *
1466 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001467 */
1468hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001469 var cursor = this.saveCursor();
1470
1471 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001472
David Benjamina08d78f2012-05-05 00:28:49 -04001473 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001474 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001475 this.screen_.clearCursorRow();
1476 }
1477
rginda87b86462011-12-14 13:48:03 -08001478 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001479 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001480};
1481
1482/**
1483 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001484 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001485 *
1486 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001487 */
1488hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001489 var cursor = this.saveCursor();
1490
1491 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001492
David Benjamina08d78f2012-05-05 00:28:49 -04001493 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001494 for (var i = cursor.row + 1; i <= bottom; i++) {
1495 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001496 this.screen_.clearCursorRow();
1497 }
1498
rginda87b86462011-12-14 13:48:03 -08001499 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001500 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001501};
1502
1503/**
1504 * Fill the terminal with a given character.
1505 *
1506 * This methods does not respect the VT scroll region.
1507 *
1508 * @param {string} ch The character to use for the fill.
1509 */
1510hterm.Terminal.prototype.fill = function(ch) {
1511 var cursor = this.saveCursor();
1512
1513 this.setAbsoluteCursorPosition(0, 0);
1514 for (var row = 0; row < this.screenSize.height; row++) {
1515 for (var col = 0; col < this.screenSize.width; col++) {
1516 this.setAbsoluteCursorPosition(row, col);
1517 this.screen_.overwriteString(ch);
1518 }
1519 }
1520
1521 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001522};
1523
1524/**
rginda9ea433c2012-03-16 11:57:00 -07001525 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001526 *
rginda9ea433c2012-03-16 11:57:00 -07001527 * This does not respect the scroll region.
1528 *
1529 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1530 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001531 */
rginda9ea433c2012-03-16 11:57:00 -07001532hterm.Terminal.prototype.clearHome = function(opt_screen) {
1533 var screen = opt_screen || this.screen_;
1534 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001535
rginda11057d52012-04-25 12:29:56 -07001536 if (bottom == 0) {
1537 // Empty screen, nothing to do.
1538 return;
1539 }
1540
rgindae4d29232012-01-19 10:47:13 -08001541 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001542 screen.setCursorPosition(i, 0);
1543 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001544 }
1545
rginda9ea433c2012-03-16 11:57:00 -07001546 screen.setCursorPosition(0, 0);
1547};
1548
1549/**
1550 * Erase the entire display without changing the cursor position.
1551 *
1552 * The cursor position is unchanged. This does not respect the scroll
1553 * region.
1554 *
1555 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1556 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001557 */
1558hterm.Terminal.prototype.clear = function(opt_screen) {
1559 var screen = opt_screen || this.screen_;
1560 var cursor = screen.cursorPosition.clone();
1561 this.clearHome(screen);
1562 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001563};
1564
1565/**
1566 * VT command to insert lines at the current cursor row.
1567 *
1568 * This respects the current scroll region. Rows pushed off the bottom are
1569 * lost (they won't show up in the scrollback buffer).
1570 *
rginda8ba33642011-12-14 12:31:31 -08001571 * @param {integer} count The number of lines to insert.
1572 */
1573hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07001574 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08001575
1576 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07001577 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08001578
Robert Ginda579186b2012-09-26 11:40:04 -07001579 // The moveCount is the number of rows we need to relocate to make room for
1580 // the new row(s). The count is the distance to move them.
1581 var moveCount = bottom - cursorRow - count + 1;
1582 if (moveCount)
1583 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08001584
Robert Ginda579186b2012-09-26 11:40:04 -07001585 for (var i = count - 1; i >= 0; i--) {
1586 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001587 this.screen_.clearCursorRow();
1588 }
rginda8ba33642011-12-14 12:31:31 -08001589};
1590
1591/**
1592 * VT command to delete lines at the current cursor row.
1593 *
1594 * New rows are added to the bottom of scroll region to take their place. New
1595 * rows are strictly there to take up space and have no content or style.
1596 */
1597hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001598 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001599
rginda87b86462011-12-14 13:48:03 -08001600 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001601 var bottom = this.getVTScrollBottom();
1602
rginda87b86462011-12-14 13:48:03 -08001603 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001604 count = Math.min(count, maxCount);
1605
rginda87b86462011-12-14 13:48:03 -08001606 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001607 if (count != maxCount)
1608 this.moveRows_(top, count, moveStart);
1609
1610 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001611 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001612 this.screen_.clearCursorRow();
1613 }
1614
rginda87b86462011-12-14 13:48:03 -08001615 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001616 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001617};
1618
1619/**
1620 * Inserts the given number of spaces at the current cursor position.
1621 *
rginda87b86462011-12-14 13:48:03 -08001622 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001623 */
1624hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001625 var cursor = this.saveCursor();
1626
rgindacbbd7482012-06-13 15:06:16 -07001627 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001628 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001629 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001630
1631 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001632 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001633};
1634
1635/**
1636 * Forward-delete the specified number of characters starting at the cursor
1637 * position.
1638 *
1639 * @param {integer} count The number of characters to delete.
1640 */
1641hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001642 var deleted = this.screen_.deleteChars(count);
1643 if (deleted && !this.screen_.textAttributes.isDefault()) {
1644 var cursor = this.saveCursor();
1645 this.setCursorColumn(this.screenSize.width - deleted);
1646 this.screen_.insertString(lib.f.getWhitespace(deleted));
1647 this.restoreCursor(cursor);
1648 }
1649
David Benjamin54e8bf62012-06-01 22:31:40 -04001650 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001651};
1652
1653/**
1654 * Shift rows in the scroll region upwards by a given number of lines.
1655 *
1656 * New rows are inserted at the bottom of the scroll region to fill the
1657 * vacated rows. The new rows not filled out with the current text attributes.
1658 *
1659 * This function does not affect the scrollback rows at all. Rows shifted
1660 * off the top are lost.
1661 *
rginda87b86462011-12-14 13:48:03 -08001662 * The cursor position is not altered.
1663 *
rginda8ba33642011-12-14 12:31:31 -08001664 * @param {integer} count The number of rows to scroll.
1665 */
1666hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001667 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001668
rginda87b86462011-12-14 13:48:03 -08001669 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001670 this.deleteLines(count);
1671
rginda87b86462011-12-14 13:48:03 -08001672 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001673};
1674
1675/**
1676 * Shift rows below the cursor down by a given number of lines.
1677 *
1678 * This function respects the current scroll region.
1679 *
1680 * New rows are inserted at the top of the scroll region to fill the
1681 * vacated rows. The new rows not filled out with the current text attributes.
1682 *
1683 * This function does not affect the scrollback rows at all. Rows shifted
1684 * off the bottom are lost.
1685 *
1686 * @param {integer} count The number of rows to scroll.
1687 */
1688hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001689 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001690
rginda87b86462011-12-14 13:48:03 -08001691 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001692 this.insertLines(opt_count);
1693
rginda87b86462011-12-14 13:48:03 -08001694 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001695};
1696
rginda87b86462011-12-14 13:48:03 -08001697
rginda8ba33642011-12-14 12:31:31 -08001698/**
1699 * Set the cursor position.
1700 *
1701 * The cursor row is relative to the scroll region if the terminal has
1702 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1703 *
1704 * @param {integer} row The new zero-based cursor row.
1705 * @param {integer} row The new zero-based cursor column.
1706 */
1707hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1708 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001709 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001710 } else {
rginda87b86462011-12-14 13:48:03 -08001711 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001712 }
rginda87b86462011-12-14 13:48:03 -08001713};
rginda8ba33642011-12-14 12:31:31 -08001714
rginda87b86462011-12-14 13:48:03 -08001715hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1716 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07001717 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
1718 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001719 this.screen_.setCursorPosition(row, column);
1720};
1721
1722hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07001723 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
1724 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001725 this.screen_.setCursorPosition(row, column);
1726};
1727
1728/**
1729 * Set the cursor column.
1730 *
1731 * @param {integer} column The new zero-based cursor column.
1732 */
1733hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001734 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001735};
1736
1737/**
1738 * Return the cursor column.
1739 *
1740 * @return {integer} The zero-based cursor column.
1741 */
1742hterm.Terminal.prototype.getCursorColumn = function() {
1743 return this.screen_.cursorPosition.column;
1744};
1745
1746/**
1747 * Set the cursor row.
1748 *
1749 * The cursor row is relative to the scroll region if the terminal has
1750 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1751 *
1752 * @param {integer} row The new cursor row.
1753 */
rginda87b86462011-12-14 13:48:03 -08001754hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1755 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001756};
1757
1758/**
1759 * Return the cursor row.
1760 *
1761 * @return {integer} The zero-based cursor row.
1762 */
1763hterm.Terminal.prototype.getCursorRow = function(row) {
1764 return this.screen_.cursorPosition.row;
1765};
1766
1767/**
1768 * Request that the ScrollPort redraw itself soon.
1769 *
1770 * The redraw will happen asynchronously, soon after the call stack winds down.
1771 * Multiple calls will be coalesced into a single redraw.
1772 */
1773hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001774 if (this.timeouts_.redraw)
1775 return;
rginda8ba33642011-12-14 12:31:31 -08001776
1777 var self = this;
rginda87b86462011-12-14 13:48:03 -08001778 this.timeouts_.redraw = setTimeout(function() {
1779 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001780 self.scrollPort_.redraw_();
1781 }, 0);
1782};
1783
1784/**
1785 * Request that the ScrollPort be scrolled to the bottom.
1786 *
1787 * The scroll will happen asynchronously, soon after the call stack winds down.
1788 * Multiple calls will be coalesced into a single scroll.
1789 *
1790 * This affects the scrollbar position of the ScrollPort, and has nothing to
1791 * do with the VT scroll commands.
1792 */
1793hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1794 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001795 return;
rginda8ba33642011-12-14 12:31:31 -08001796
1797 var self = this;
1798 this.timeouts_.scrollDown = setTimeout(function() {
1799 delete self.timeouts_.scrollDown;
1800 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1801 }, 10);
1802};
1803
1804/**
1805 * Move the cursor up a specified number of rows.
1806 *
1807 * @param {integer} count The number of rows to move the cursor.
1808 */
1809hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001810 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001811};
1812
1813/**
1814 * Move the cursor down a specified number of rows.
1815 *
1816 * @param {integer} count The number of rows to move the cursor.
1817 */
1818hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001819 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001820 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1821 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1822 this.screenSize.height - 1);
1823
rgindacbbd7482012-06-13 15:06:16 -07001824 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08001825 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001826 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001827};
1828
1829/**
1830 * Move the cursor left a specified number of columns.
1831 *
1832 * @param {integer} count The number of columns to move the cursor.
1833 */
1834hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001835 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001836};
1837
1838/**
1839 * Move the cursor right a specified number of columns.
1840 *
1841 * @param {integer} count The number of columns to move the cursor.
1842 */
1843hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001844 count = count || 1;
rgindacbbd7482012-06-13 15:06:16 -07001845 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001846 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001847 this.setCursorColumn(column);
1848};
1849
1850/**
1851 * Reverse the foreground and background colors of the terminal.
1852 *
1853 * This only affects text that was drawn with no attributes.
1854 *
1855 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1856 * been drawn with attributes that happen to coincide with the default
1857 * 'no-attribute' colors. My guess is probably not.
1858 */
1859hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001860 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001861 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08001862 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
1863 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08001864 } else {
rginda9f5222b2012-03-05 11:53:28 -08001865 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
1866 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08001867 }
1868};
1869
1870/**
rginda87b86462011-12-14 13:48:03 -08001871 * Ring the terminal bell.
rginda87b86462011-12-14 13:48:03 -08001872 */
1873hterm.Terminal.prototype.ringBell = function() {
rginda9f5222b2012-03-05 11:53:28 -08001874 if (this.bellAudio_.getAttribute('src'))
1875 this.bellAudio_.play();
rgindaf0090c92012-02-10 14:58:52 -08001876
rginda6d397402012-01-17 10:58:29 -08001877 this.cursorNode_.style.backgroundColor =
1878 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001879
1880 var self = this;
1881 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08001882 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08001883 }, 200);
rginda87b86462011-12-14 13:48:03 -08001884};
1885
1886/**
rginda8ba33642011-12-14 12:31:31 -08001887 * Set the origin mode bit.
1888 *
1889 * If origin mode is on, certain VT cursor and scrolling commands measure their
1890 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1891 * to the top of the addressable screen.
1892 *
1893 * Defaults to off.
1894 *
1895 * @param {boolean} state True to set origin mode, false to unset.
1896 */
1897hterm.Terminal.prototype.setOriginMode = function(state) {
1898 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08001899 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08001900};
1901
1902/**
1903 * Set the insert mode bit.
1904 *
1905 * If insert mode is on, existing text beyond the cursor position will be
1906 * shifted right to make room for new text. Otherwise, new text overwrites
1907 * any existing text.
1908 *
1909 * Defaults to off.
1910 *
1911 * @param {boolean} state True to set insert mode, false to unset.
1912 */
1913hterm.Terminal.prototype.setInsertMode = function(state) {
1914 this.options_.insertMode = state;
1915};
1916
1917/**
rginda87b86462011-12-14 13:48:03 -08001918 * Set the auto carriage return bit.
1919 *
1920 * If auto carriage return is on then a formfeed character is interpreted
1921 * as a newline, otherwise it's the same as a linefeed. The difference boils
1922 * down to whether or not the cursor column is reset.
1923 */
1924hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1925 this.options_.autoCarriageReturn = state;
1926};
1927
1928/**
rginda8ba33642011-12-14 12:31:31 -08001929 * Set the wraparound mode bit.
1930 *
1931 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1932 * to the start of the following row. Otherwise, the cursor is clamped to the
1933 * end of the screen and attempts to write past it are ignored.
1934 *
1935 * Defaults to on.
1936 *
1937 * @param {boolean} state True to set wraparound mode, false to unset.
1938 */
1939hterm.Terminal.prototype.setWraparound = function(state) {
1940 this.options_.wraparound = state;
1941};
1942
1943/**
1944 * Set the reverse-wraparound mode bit.
1945 *
1946 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1947 * to the end of the previous row. Otherwise, the cursor is clamped to column
1948 * 0.
1949 *
1950 * Defaults to off.
1951 *
1952 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
1953 */
1954hterm.Terminal.prototype.setReverseWraparound = function(state) {
1955 this.options_.reverseWraparound = state;
1956};
1957
1958/**
1959 * Selects between the primary and alternate screens.
1960 *
1961 * If alternate mode is on, the alternate screen is active. Otherwise the
1962 * primary screen is active.
1963 *
1964 * Swapping screens has no effect on the scrollback buffer.
1965 *
1966 * Each screen maintains its own cursor position.
1967 *
1968 * Defaults to off.
1969 *
1970 * @param {boolean} state True to set alternate mode, false to unset.
1971 */
1972hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08001973 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001974 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
1975
rginda35c456b2012-02-09 17:29:05 -08001976 if (this.screen_.rowsArray.length &&
1977 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
1978 // If the screen changed sizes while we were away, our rowIndexes may
1979 // be incorrect.
1980 var offset = this.scrollbackRows_.length;
1981 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07001982 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08001983 ary[i].rowIndex = offset + i;
1984 }
1985 }
rginda8ba33642011-12-14 12:31:31 -08001986
rginda35c456b2012-02-09 17:29:05 -08001987 this.realizeWidth_(this.screenSize.width);
1988 this.realizeHeight_(this.screenSize.height);
1989 this.scrollPort_.syncScrollHeight();
1990 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08001991
rginda6d397402012-01-17 10:58:29 -08001992 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08001993 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08001994};
1995
1996/**
1997 * Set the cursor-blink mode bit.
1998 *
1999 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2000 * a visible cursor does not blink.
2001 *
2002 * You should make sure to turn blinking off if you're going to dispose of a
2003 * terminal, otherwise you'll leak a timeout.
2004 *
2005 * Defaults to on.
2006 *
2007 * @param {boolean} state True to set cursor-blink mode, false to unset.
2008 */
2009hterm.Terminal.prototype.setCursorBlink = function(state) {
2010 this.options_.cursorBlink = state;
2011
2012 if (!state && this.timeouts_.cursorBlink) {
2013 clearTimeout(this.timeouts_.cursorBlink);
2014 delete this.timeouts_.cursorBlink;
2015 }
2016
2017 if (this.options_.cursorVisible)
2018 this.setCursorVisible(true);
2019};
2020
2021/**
2022 * Set the cursor-visible mode bit.
2023 *
2024 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2025 *
2026 * Defaults to on.
2027 *
2028 * @param {boolean} state True to set cursor-visible mode, false to unset.
2029 */
2030hterm.Terminal.prototype.setCursorVisible = function(state) {
2031 this.options_.cursorVisible = state;
2032
2033 if (!state) {
rginda87b86462011-12-14 13:48:03 -08002034 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002035 return;
2036 }
2037
rginda87b86462011-12-14 13:48:03 -08002038 this.syncCursorPosition_();
2039
2040 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002041
2042 if (this.options_.cursorBlink) {
2043 if (this.timeouts_.cursorBlink)
2044 return;
2045
2046 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
2047 500);
2048 } else {
2049 if (this.timeouts_.cursorBlink) {
2050 clearTimeout(this.timeouts_.cursorBlink);
2051 delete this.timeouts_.cursorBlink;
2052 }
2053 }
2054};
2055
2056/**
rginda87b86462011-12-14 13:48:03 -08002057 * Synchronizes the visible cursor and document selection with the current
2058 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002059 */
2060hterm.Terminal.prototype.syncCursorPosition_ = function() {
2061 var topRowIndex = this.scrollPort_.getTopRowIndex();
2062 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2063 var cursorRowIndex = this.scrollbackRows_.length +
2064 this.screen_.cursorPosition.row;
2065
2066 if (cursorRowIndex > bottomRowIndex) {
2067 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002068 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002069 return;
2070 }
2071
rginda35c456b2012-02-09 17:29:05 -08002072 this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px';
2073 this.cursorNode_.style.height = this.scrollPort_.characterSize.height + 'px';
2074
rginda8ba33642011-12-14 12:31:31 -08002075 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002076 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2077 'px';
2078 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2079 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002080
2081 this.cursorNode_.setAttribute('title',
2082 '(' + this.screen_.cursorPosition.row +
2083 ', ' + this.screen_.cursorPosition.column +
2084 ')');
2085
2086 // Update the caret for a11y purposes.
2087 var selection = this.document_.getSelection();
2088 if (selection && selection.isCollapsed)
2089 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002090};
2091
2092/**
2093 * Synchronizes the visible cursor with the current cursor coordinates.
2094 *
2095 * The sync will happen asynchronously, soon after the call stack winds down.
2096 * Multiple calls will be coalesced into a single sync.
2097 */
2098hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2099 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002100 return;
rginda8ba33642011-12-14 12:31:31 -08002101
2102 var self = this;
2103 this.timeouts_.syncCursor = setTimeout(function() {
2104 self.syncCursorPosition_();
2105 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002106 }, 0);
2107};
2108
rgindacc2996c2012-02-24 14:59:31 -08002109/**
rgindaf522ce02012-04-17 17:49:17 -07002110 * Show or hide the zoom warning.
2111 *
2112 * The zoom warning is a message warning the user that their browser zoom must
2113 * be set to 100% in order for hterm to function properly.
2114 *
2115 * @param {boolean} state True to show the message, false to hide it.
2116 */
2117hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2118 if (!this.zoomWarningNode_) {
2119 if (!state)
2120 return;
2121
2122 this.zoomWarningNode_ = this.document_.createElement('div');
2123 this.zoomWarningNode_.style.cssText = (
2124 'color: black;' +
2125 'background-color: #ff2222;' +
2126 'font-size: large;' +
2127 'border-radius: 8px;' +
2128 'opacity: 0.75;' +
2129 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2130 'top: 0.5em;' +
2131 'right: 1.2em;' +
2132 'position: absolute;' +
2133 '-webkit-text-size-adjust: none;' +
2134 '-webkit-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002135 }
2136
rgindade84e382012-04-20 15:39:31 -07002137 this.zoomWarningNode_.textContent = hterm.msg('ZOOM_WARNING') ||
2138 ('!! ' + parseInt(this.scrollPort_.characterSize.zoomFactor * 100) +
2139 '% !!');
rgindaf522ce02012-04-17 17:49:17 -07002140 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2141
2142 if (state) {
2143 if (!this.zoomWarningNode_.parentNode)
2144 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2145 } else if (this.zoomWarningNode_.parentNode) {
2146 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2147 }
2148};
2149
2150/**
rgindacc2996c2012-02-24 14:59:31 -08002151 * Show the terminal overlay for a given amount of time.
2152 *
2153 * The terminal overlay appears in inverse video in a large font, centered
2154 * over the terminal. You should probably keep the overlay message brief,
2155 * since it's in a large font and you probably aren't going to check the size
2156 * of the terminal first.
2157 *
2158 * @param {string} msg The text (not HTML) message to display in the overlay.
2159 * @param {number} opt_timeout The amount of time to wait before fading out
2160 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2161 * stay up forever (or until the next overlay).
2162 */
2163hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002164 if (!this.overlayNode_) {
2165 if (!this.div_)
2166 return;
2167
2168 this.overlayNode_ = this.document_.createElement('div');
2169 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002170 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002171 'font-size: xx-large;' +
2172 'opacity: 0.75;' +
2173 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2174 'position: absolute;' +
2175 '-webkit-user-select: none;' +
2176 '-webkit-transition: opacity 180ms ease-in;');
2177 }
2178
rginda9f5222b2012-03-05 11:53:28 -08002179 this.overlayNode_.style.color = this.prefs_.get('background-color');
2180 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2181 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2182
rgindaf0090c92012-02-10 14:58:52 -08002183 this.overlayNode_.textContent = msg;
2184 this.overlayNode_.style.opacity = '0.75';
2185
2186 if (!this.overlayNode_.parentNode)
2187 this.div_.appendChild(this.overlayNode_);
2188
Robert Ginda97769282013-02-01 15:30:30 -08002189 var divSize = hterm.getClientSize(this.div_);
2190 var overlaySize = hterm.getClientSize(this.overlayNode_);
2191
2192 this.overlayNode_.style.top = (divSize.height - overlaySize.height) / 2;
2193 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
2194 this.scrollPort_.currentScrollbarWidthPx) / 2;
rgindaf0090c92012-02-10 14:58:52 -08002195
2196 var self = this;
2197
2198 if (this.overlayTimeout_)
2199 clearTimeout(this.overlayTimeout_);
2200
rgindacc2996c2012-02-24 14:59:31 -08002201 if (opt_timeout === null)
2202 return;
2203
rgindaf0090c92012-02-10 14:58:52 -08002204 this.overlayTimeout_ = setTimeout(function() {
2205 self.overlayNode_.style.opacity = '0';
2206 setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002207 if (self.overlayNode_.parentNode)
2208 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002209 self.overlayTimeout_ = null;
2210 self.overlayNode_.style.opacity = '0.75';
2211 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002212 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002213};
2214
rginda4bba5e12012-06-20 16:15:30 -07002215/**
2216 * Paste from the system clipboard to the terminal.
2217 */
2218hterm.Terminal.prototype.paste = function() {
2219 hterm.pasteFromClipboard(this.document_);
2220};
2221
2222/**
2223 * Copy a string to the system clipboard.
2224 *
2225 * Note: If there is a selected range in the terminal, it'll be cleared.
2226 */
2227hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda9fb38222012-09-11 14:19:12 -07002228 if (this.prefs_.get('enable-clipboard-notice'))
2229 setTimeout(this.showOverlay.bind(this, hterm.msg('NOTIFY_COPY'), 500), 200);
rgindaa09e7332012-08-17 12:49:51 -07002230
2231 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002232 copySource.textContent = str;
2233 copySource.style.cssText = (
2234 '-webkit-user-select: text;' +
2235 'position: absolute;' +
2236 'top: -99px');
2237
2238 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002239
rginda4bba5e12012-06-20 16:15:30 -07002240 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002241 var anchorNode = selection.anchorNode;
2242 var anchorOffset = selection.anchorOffset;
2243 var focusNode = selection.focusNode;
2244 var focusOffset = selection.focusOffset;
2245
rginda4bba5e12012-06-20 16:15:30 -07002246 selection.selectAllChildren(copySource);
2247
rgindaa09e7332012-08-17 12:49:51 -07002248 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002249
rgindafaa74742012-08-21 13:34:03 -07002250 selection.collapse(anchorNode, anchorOffset);
2251 selection.extend(focusNode, focusOffset);
2252
rginda4bba5e12012-06-20 16:15:30 -07002253 copySource.parentNode.removeChild(copySource);
2254};
2255
rgindaa09e7332012-08-17 12:49:51 -07002256hterm.Terminal.prototype.getSelectionText = function() {
2257 var selection = this.scrollPort_.selection;
2258 selection.sync();
2259
2260 if (selection.isCollapsed)
2261 return null;
2262
2263
2264 // Start offset measures from the beginning of the line.
2265 var startOffset = selection.startOffset;
2266 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002267
Robert Gindafdbb3f22012-09-06 20:23:06 -07002268 if (node.nodeName != 'X-ROW') {
2269 // If the selection doesn't start on an x-row node, then it must be
2270 // somewhere inside the x-row. Add any characters from previous siblings
2271 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002272
2273 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2274 // If node is the text node in a styled span, move up to the span node.
2275 node = node.parentNode;
2276 }
2277
Robert Gindafdbb3f22012-09-06 20:23:06 -07002278 while (node.previousSibling) {
2279 node = node.previousSibling;
2280 startOffset += node.textContent.length;
2281 }
rgindaa09e7332012-08-17 12:49:51 -07002282 }
2283
2284 // End offset measures from the end of the line.
2285 var endOffset = selection.endNode.textContent.length - selection.endOffset;
2286 var node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002287
Robert Gindafdbb3f22012-09-06 20:23:06 -07002288 if (node.nodeName != 'X-ROW') {
2289 // If the selection doesn't end on an x-row node, then it must be
2290 // somewhere inside the x-row. Add any characters from following siblings
2291 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002292
2293 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2294 // If node is the text node in a styled span, move up to the span node.
2295 node = node.parentNode;
2296 }
2297
Robert Gindafdbb3f22012-09-06 20:23:06 -07002298 while (node.nextSibling) {
2299 node = node.nextSibling;
2300 endOffset += node.textContent.length;
2301 }
rgindaa09e7332012-08-17 12:49:51 -07002302 }
2303
2304 var rv = this.getRowsText(selection.startRow.rowIndex,
2305 selection.endRow.rowIndex + 1);
2306 return rv.substring(startOffset, rv.length - endOffset);
2307};
2308
rginda4bba5e12012-06-20 16:15:30 -07002309/**
2310 * Copy the current selection to the system clipboard, then clear it after a
2311 * short delay.
2312 */
2313hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002314 var text = this.getSelectionText();
2315 if (text != null)
2316 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002317};
2318
rgindaf0090c92012-02-10 14:58:52 -08002319hterm.Terminal.prototype.overlaySize = function() {
2320 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2321};
2322
rginda87b86462011-12-14 13:48:03 -08002323/**
2324 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2325 *
2326 * @param {string} string The VT string representing the keystroke.
2327 */
2328hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002329 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002330 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2331
2332 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08002333};
2334
2335/**
rgindad5613292012-06-19 15:40:37 -07002336 * Add the terminalRow and terminalColumn properties to mouse events and
2337 * then forward on to onMouse().
2338 *
2339 * The terminalRow and terminalColumn properties contain the (row, column)
2340 * coordinates for the mouse event.
2341 */
2342hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002343 if (e.processedByTerminalHandler_) {
2344 // We register our event handlers on the document, as well as the cursor
2345 // and the scroll blocker. Mouse events that occur on the cursor or
2346 // scroll blocker will also appear on the document, but we don't want to
2347 // process them twice.
2348 //
2349 // We can't just prevent bubbling because that has other side effects, so
2350 // we decorate the event object with this property instead.
2351 return;
2352 }
2353
2354 e.processedByTerminalHandler_ = true;
2355
rginda4bba5e12012-06-20 16:15:30 -07002356 if (e.type == 'mousedown' && e.which == this.mousePasteButton) {
2357 this.paste();
2358 return;
2359 }
2360
2361 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2362 !this.document_.getSelection().isCollapsed) {
rgindafaa74742012-08-21 13:34:03 -07002363 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002364 return;
2365 }
2366
rgindad5613292012-06-19 15:40:37 -07002367 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2368 this.scrollPort_.characterSize.height) + 1;
2369 e.terminalColumn = parseInt(e.clientX /
2370 this.scrollPort_.characterSize.width) + 1;
2371
2372 if (e.type == 'mousedown') {
2373 if (e.terminalColumn > this.screenSize.width) {
2374 // Mousedown in the scrollbar area.
2375 return;
2376 }
2377
2378 if (!this.enableMouseDragScroll) {
2379 // Move the scroll-blocker into place if we want to keep the scrollport
2380 // from scrolling.
2381 this.scrollBlockerNode_.engaged = true;
2382 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2383 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2384 }
2385 } else if (this.scrollBlockerNode_.engaged &&
2386 (e.type == 'mousemove' || e.type == 'mouseup')) {
2387 // Disengage the scroll-blocker after one of these events.
2388 this.scrollBlockerNode_.engaged = false;
2389 this.scrollBlockerNode_.style.top = '-99px';
2390 }
2391
rgindafaa74742012-08-21 13:34:03 -07002392 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07002393};
2394
2395/**
2396 * Clients should override this if they care to know about mouse events.
2397 *
2398 * The event parameter will be a normal DOM mouse click event with additional
2399 * 'terminalRow' and 'terminalColumn' properties.
2400 */
2401hterm.Terminal.prototype.onMouse = function(e) { };
2402
2403/**
rginda8e92a692012-05-20 19:37:20 -07002404 * React when focus changes.
2405 */
2406hterm.Terminal.prototype.onFocusChange_ = function(state) {
2407 this.cursorNode_.setAttribute('focus', state ? 'true' : 'false');
2408};
2409
2410/**
rginda8ba33642011-12-14 12:31:31 -08002411 * React when the ScrollPort is scrolled.
2412 */
2413hterm.Terminal.prototype.onScroll_ = function() {
2414 this.scheduleSyncCursorPosition_();
2415};
2416
2417/**
rginda9846e2f2012-01-27 13:53:33 -08002418 * React when text is pasted into the scrollPort.
2419 */
2420hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaf2547f12012-10-25 20:36:21 -07002421 var text = this.vt.encodeUTF8(e.text);
2422 text = text.replace(/\n/mg, '\r');
2423 this.io.onVTKeystroke(text);
rginda9846e2f2012-01-27 13:53:33 -08002424};
2425
2426/**
rgindaa09e7332012-08-17 12:49:51 -07002427 * React when the user tries to copy from the scrollPort.
2428 */
2429hterm.Terminal.prototype.onCopy_ = function(e) {
2430 e.preventDefault();
rgindafaa74742012-08-21 13:34:03 -07002431 this.copySelectionToClipboard();
rgindaa09e7332012-08-17 12:49:51 -07002432};
2433
2434/**
rginda8ba33642011-12-14 12:31:31 -08002435 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002436 *
2437 * Note: This function should not directly contain code that alters the internal
2438 * state of the terminal. That kind of code belongs in realizeWidth or
2439 * realizeHeight, so that it can be executed synchronously in the case of a
2440 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002441 */
2442hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08002443 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002444 this.scrollPort_.characterSize.width);
2445 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
2446 this.scrollPort_.characterSize.height);
2447
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002448 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08002449 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002450 // gets removed from the document or during the initial load, and we can't
2451 // deal with that.
rginda35c456b2012-02-09 17:29:05 -08002452 return;
2453 }
2454
rgindaa8ba17d2012-08-15 14:41:10 -07002455 var isNewSize = (columnCount != this.screenSize.width ||
2456 rowCount != this.screenSize.height);
2457
2458 // We do this even if the size didn't change, just to be sure everything is
2459 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002460 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07002461 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07002462
2463 if (isNewSize)
2464 this.overlaySize();
2465
2466 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08002467};
2468
2469/**
2470 * Service the cursor blink timeout.
2471 */
2472hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08002473 if (this.cursorNode_.style.opacity == '0') {
2474 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002475 } else {
rginda87b86462011-12-14 13:48:03 -08002476 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002477 }
2478};
David Reveman8f552492012-03-28 12:18:41 -04002479
2480/**
2481 * Set the scrollbar-visible mode bit.
2482 *
2483 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2484 * Otherwise it will not.
2485 *
2486 * Defaults to on.
2487 *
2488 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2489 */
2490hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2491 this.scrollPort_.setScrollbarVisible(state);
2492};