blob: 2f6c8490ccf5967581ab434055c5b5d92b0dfb8f [file] [log] [blame]
rginda87b86462011-12-14 13:48:03 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rginda8ba33642011-12-14 12:31:31 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rgindacbbd7482012-06-13 15:06:16 -07005'use strict';
6
7lib.rtdep('lib.colors', 'lib.PreferenceManager',
8 'hterm.msg',
Robert Ginda57f03b42012-09-13 11:02:48 -07009 'hterm.Keyboard', 'hterm.Options', 'hterm.PreferenceManager',
10 'hterm.Screen', 'hterm.ScrollPort', 'hterm.Size', 'hterm.VT');
rgindacbbd7482012-06-13 15:06:16 -070011
rginda8ba33642011-12-14 12:31:31 -080012/**
13 * Constructor for the Terminal class.
14 *
15 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
16 * classes to provide the complete terminal functionality.
17 *
18 * There are a number of lower-level Terminal methods that can be called
19 * directly to manipulate the cursor, text, scroll region, and other terminal
20 * attributes. However, the primary method is interpret(), which parses VT
21 * escape sequences and invokes the appropriate Terminal methods.
22 *
23 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
24 *
25 * TODO(rginda): Eventually we're going to need to support characters which are
26 * displayed twice as wide as standard latin characters. This is to support
27 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080028 *
Robert Ginda57f03b42012-09-13 11:02:48 -070029 * @param {string} opt_profileId Optional preference profile name. If not
rginda9f5222b2012-03-05 11:53:28 -080030 * provided, defaults to 'default'.
rginda8ba33642011-12-14 12:31:31 -080031 */
Robert Ginda57f03b42012-09-13 11:02:48 -070032hterm.Terminal = function(opt_profileId) {
33 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080034
rginda8ba33642011-12-14 12:31:31 -080035 // Two screen instances.
36 this.primaryScreen_ = new hterm.Screen();
37 this.alternateScreen_ = new hterm.Screen();
38
39 // The "current" screen.
40 this.screen_ = this.primaryScreen_;
41
rginda8ba33642011-12-14 12:31:31 -080042 // The local notion of the screen size. ScreenBuffers also have a size which
43 // indicates their present size. During size changes, the two may disagree.
44 // Also, the inactive screen's size is not altered until it is made the active
45 // screen.
46 this.screenSize = new hterm.Size(0, 0);
47
rginda8ba33642011-12-14 12:31:31 -080048 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080049 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080050 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
51 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080052 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070053 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080054
rginda87b86462011-12-14 13:48:03 -080055 // The div that contains this terminal.
56 this.div_ = null;
57
rgindac9bc5502012-01-18 11:48:44 -080058 // The document that contains the scrollPort. Defaulted to the global
59 // document here so that the terminal is functional even if it hasn't been
60 // inserted into a document yet, but re-set in decorate().
61 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080062
rginda8ba33642011-12-14 12:31:31 -080063 // The rows that have scrolled off screen and are no longer addressable.
64 this.scrollbackRows_ = [];
65
rgindac9bc5502012-01-18 11:48:44 -080066 // Saved tab stops.
67 this.tabStops_ = [];
68
David Benjamin66e954d2012-05-05 21:08:12 -040069 // Keep track of whether default tab stops have been erased; after a TBC
70 // clears all tab stops, defaults aren't restored on resize until a reset.
71 this.defaultTabStops = true;
72
rginda8ba33642011-12-14 12:31:31 -080073 // The VT's notion of the top and bottom rows. Used during some VT
74 // cursor positioning and scrolling commands.
75 this.vtScrollTop_ = null;
76 this.vtScrollBottom_ = null;
77
78 // The DIV element for the visible cursor.
79 this.cursorNode_ = null;
80
rginda9f5222b2012-03-05 11:53:28 -080081 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070082 // each output and keystroke. They are initialized by the preference manager.
83 this.scrollOnOutput_ = null;
84 this.scrollOnKeystroke_ = null;
85 this.foregroundColor_ = null;
86 this.backgroundColor_ = null;
rginda9f5222b2012-03-05 11:53:28 -080087
rgindaf0090c92012-02-10 14:58:52 -080088 // Terminal bell sound.
89 this.bellAudio_ = this.document_.createElement('audio');
rgindaf0090c92012-02-10 14:58:52 -080090 this.bellAudio_.setAttribute('preload', 'auto');
91
rginda6d397402012-01-17 10:58:29 -080092 // Cursor position and attributes saved with DECSC.
93 this.savedOptions_ = {};
94
rginda8ba33642011-12-14 12:31:31 -080095 // The current mode bits for the terminal.
96 this.options_ = new hterm.Options();
97
98 // Timeouts we might need to clear.
99 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800100
101 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800102 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800103
rgindafeaf3142012-01-31 15:14:20 -0800104 // The keyboard hander.
105 this.keyboard = new hterm.Keyboard(this);
106
rginda87b86462011-12-14 13:48:03 -0800107 // General IO interface that can be given to third parties without exposing
108 // the entire terminal object.
109 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800110
rgindad5613292012-06-19 15:40:37 -0700111 // True if mouse-click-drag should scroll the terminal.
112 this.enableMouseDragScroll = true;
113
Robert Ginda57f03b42012-09-13 11:02:48 -0700114 this.copyOnSelect = null;
rginda4bba5e12012-06-20 16:15:30 -0700115 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700116
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400117 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800118 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700119
120 this.setProfile(opt_profileId || 'default',
121 function() { this.onTerminalReady() }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800122};
123
124/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700125 * Clients should override this to be notified when the terminal is ready
126 * for use.
127 *
128 * The terminal initialization is asynchronous, and shouldn't be used before
129 * this method is called.
130 */
131hterm.Terminal.prototype.onTerminalReady = function() { };
132
133/**
rginda35c456b2012-02-09 17:29:05 -0800134 * Default tab with of 8 to match xterm.
135 */
136hterm.Terminal.prototype.tabWidth = 8;
137
138/**
rginda35c456b2012-02-09 17:29:05 -0800139 * The assumed width of a scrollbar.
140 */
141hterm.Terminal.prototype.scrollbarWidthPx = 16;
142
143/**
rginda9f5222b2012-03-05 11:53:28 -0800144 * Select a preference profile.
145 *
146 * This will load the terminal preferences for the given profile name and
147 * associate subsequent preference changes with the new preference profile.
148 *
149 * @param {string} newName The name of the preference profile. Forward slash
150 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700151 * @param {function} opt_callback Optional callback to invoke when the profile
152 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800153 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700154hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
155 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800156
Robert Ginda57f03b42012-09-13 11:02:48 -0700157 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800158
Robert Ginda57f03b42012-09-13 11:02:48 -0700159 if (this.prefs_)
160 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800161
Robert Ginda57f03b42012-09-13 11:02:48 -0700162 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
163 this.prefs_.addObservers(null, {
164 'alt-is-meta': function(v) {
165 terminal.keyboard.altIsMeta = v;
166 },
167
168 'alt-sends-what': function(v) {
169 if (!/^(escape|8-bit|browser-key)$/.test(v))
170 v = 'escape';
171
172 terminal.keyboard.altSendsWhat = v;
173 },
174
175 'audible-bell-sound': function(v) {
176 terminal.bellAudio_.setAttribute('src', v);
177 },
178
179 'background-color': function(v) {
180 terminal.setBackgroundColor(v);
181 },
182
183 'background-image': function(v) {
184 terminal.scrollPort_.setBackgroundImage(v);
185 },
186
187 'background-size': function(v) {
188 terminal.scrollPort_.setBackgroundSize(v);
189 },
190
191 'background-position': function(v) {
192 terminal.scrollPort_.setBackgroundPosition(v);
193 },
194
195 'backspace-sends-backspace': function(v) {
196 terminal.keyboard.backspaceSendsBackspace = v;
197 },
198
199 'cursor-blink': function(v) {
200 terminal.setCursorBlink(!!v);
201 },
202
203 'cursor-color': function(v) {
204 terminal.setCursorColor(v);
205 },
206
207 'color-palette-overrides': function(v) {
208 if (!(v == null || v instanceof Object || v instanceof Array)) {
209 console.warn('Preference color-palette-overrides is not an array or ' +
210 'object: ' + v);
211 return;
rginda9f5222b2012-03-05 11:53:28 -0800212 }
rginda9f5222b2012-03-05 11:53:28 -0800213
Robert Ginda57f03b42012-09-13 11:02:48 -0700214 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700215
Robert Ginda57f03b42012-09-13 11:02:48 -0700216 if (v) {
217 for (var key in v) {
218 var i = parseInt(key);
219 if (isNaN(i) || i < 0 || i > 255) {
220 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
221 continue;
222 }
223
224 if (v[i]) {
225 var rgb = lib.colors.normalizeCSS(v[i]);
226 if (rgb)
227 lib.colors.colorPalette[i] = rgb;
228 }
229 }
rginda30f20f62012-04-05 16:36:19 -0700230 }
rginda30f20f62012-04-05 16:36:19 -0700231
Robert Ginda57f03b42012-09-13 11:02:48 -0700232 terminal.primaryScreen_.textAttributes.resetColorPalette()
233 terminal.alternateScreen_.textAttributes.resetColorPalette();
234 },
rginda30f20f62012-04-05 16:36:19 -0700235
Robert Ginda57f03b42012-09-13 11:02:48 -0700236 'copy-on-select': function(v) {
237 terminal.copyOnSelect = !!v;
238 },
rginda9f5222b2012-03-05 11:53:28 -0800239
Robert Ginda57f03b42012-09-13 11:02:48 -0700240 'enable-8-bit-control': function(v) {
241 terminal.vt.enable8BitControl = !!v;
242 },
rginda30f20f62012-04-05 16:36:19 -0700243
Robert Ginda57f03b42012-09-13 11:02:48 -0700244 'enable-bold': function(v) {
245 terminal.syncBoldSafeState();
246 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400247
Robert Ginda57f03b42012-09-13 11:02:48 -0700248 'enable-clipboard-write': function(v) {
249 terminal.vt.enableClipboardWrite = !!v;
250 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400251
Robert Ginda57f03b42012-09-13 11:02:48 -0700252 'font-family': function(v) {
253 terminal.syncFontFamily();
254 },
rginda30f20f62012-04-05 16:36:19 -0700255
Robert Ginda57f03b42012-09-13 11:02:48 -0700256 'font-size': function(v) {
257 terminal.setFontSize(v);
258 },
rginda9875d902012-08-20 16:21:57 -0700259
Robert Ginda57f03b42012-09-13 11:02:48 -0700260 'font-smoothing': function(v) {
261 terminal.syncFontFamily();
262 },
rgindade84e382012-04-20 15:39:31 -0700263
Robert Ginda57f03b42012-09-13 11:02:48 -0700264 'foreground-color': function(v) {
265 terminal.setForegroundColor(v);
266 },
rginda30f20f62012-04-05 16:36:19 -0700267
Robert Ginda57f03b42012-09-13 11:02:48 -0700268 'home-keys-scroll': function(v) {
269 terminal.keyboard.homeKeysScroll = v;
270 },
rginda4bba5e12012-06-20 16:15:30 -0700271
Robert Ginda57f03b42012-09-13 11:02:48 -0700272 'max-string-sequence': function(v) {
273 terminal.vt.maxStringSequence = v;
274 },
rginda11057d52012-04-25 12:29:56 -0700275
Robert Ginda57f03b42012-09-13 11:02:48 -0700276 'meta-sends-escape': function(v) {
277 terminal.keyboard.metaSendsEscape = v;
278 },
rginda30f20f62012-04-05 16:36:19 -0700279
Robert Ginda57f03b42012-09-13 11:02:48 -0700280 'mouse-cell-motion-trick': function(v) {
281 terminal.vt.setMouseCellMotionTrick(v);
282 },
Robert Ginda9fb38222012-09-11 14:19:12 -0700283
Robert Ginda57f03b42012-09-13 11:02:48 -0700284 'mouse-paste-button': function(v) {
285 terminal.syncMousePasteButton();
286 },
rgindaa8ba17d2012-08-15 14:41:10 -0700287
Robert Ginda57f03b42012-09-13 11:02:48 -0700288 'scroll-on-keystroke': function(v) {
289 terminal.scrollOnKeystroke_ = v;
290 },
rginda9f5222b2012-03-05 11:53:28 -0800291
Robert Ginda57f03b42012-09-13 11:02:48 -0700292 'scroll-on-output': function(v) {
293 terminal.scrollOnOutput_ = v;
294 },
rginda30f20f62012-04-05 16:36:19 -0700295
Robert Ginda57f03b42012-09-13 11:02:48 -0700296 'scrollbar-visible': function(v) {
297 terminal.setScrollbarVisible(v);
298 },
rginda9f5222b2012-03-05 11:53:28 -0800299
Robert Ginda57f03b42012-09-13 11:02:48 -0700300 'shift-insert-paste': function(v) {
301 terminal.keyboard.shiftInsertPaste = v;
302 },
rginda9f5222b2012-03-05 11:53:28 -0800303
Robert Ginda57f03b42012-09-13 11:02:48 -0700304 'page-keys-scroll': function(v) {
305 terminal.keyboard.pageKeysScroll = v;
306 }
307 });
rginda30f20f62012-04-05 16:36:19 -0700308
Robert Ginda57f03b42012-09-13 11:02:48 -0700309 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800310 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700311
312 if (opt_callback)
313 opt_callback();
314 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800315};
316
rginda8e92a692012-05-20 19:37:20 -0700317
318/**
319 * Set the color for the cursor.
320 *
321 * If you want this setting to persist, set it through prefs_, rather than
322 * with this method.
323 */
324hterm.Terminal.prototype.setCursorColor = function(color) {
325 this.cursorNode_.style.backgroundColor = color;
326 this.cursorNode_.style.borderColor = color;
327};
328
329/**
330 * Return the current cursor color as a string.
331 */
332hterm.Terminal.prototype.getCursorColor = function() {
333 return this.cursorNode_.style.backgroundColor;
334};
335
336/**
rgindad5613292012-06-19 15:40:37 -0700337 * Enable or disable mouse based text selection in the terminal.
338 */
339hterm.Terminal.prototype.setSelectionEnabled = function(state) {
340 this.enableMouseDragScroll = state;
341 this.scrollPort_.setSelectionEnabled(state);
342};
343
344/**
rginda8e92a692012-05-20 19:37:20 -0700345 * Set the background color.
346 *
347 * If you want this setting to persist, set it through prefs_, rather than
348 * with this method.
349 */
350hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700351 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700352 this.primaryScreen_.textAttributes.setDefaults(
353 this.foregroundColor_, this.backgroundColor_);
354 this.alternateScreen_.textAttributes.setDefaults(
355 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700356 this.scrollPort_.setBackgroundColor(color);
357};
358
rginda9f5222b2012-03-05 11:53:28 -0800359/**
360 * Return the current terminal background color.
361 *
362 * Intended for use by other classes, so we don't have to expose the entire
363 * prefs_ object.
364 */
365hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700366 return this.backgroundColor_;
367};
368
369/**
370 * Set the foreground color.
371 *
372 * If you want this setting to persist, set it through prefs_, rather than
373 * with this method.
374 */
375hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700376 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700377 this.primaryScreen_.textAttributes.setDefaults(
378 this.foregroundColor_, this.backgroundColor_);
379 this.alternateScreen_.textAttributes.setDefaults(
380 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700381 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800382};
383
384/**
385 * Return the current terminal foreground color.
386 *
387 * Intended for use by other classes, so we don't have to expose the entire
388 * prefs_ object.
389 */
390hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700391 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800392};
393
394/**
rginda87b86462011-12-14 13:48:03 -0800395 * Create a new instance of a terminal command and run it with a given
396 * argument string.
397 *
398 * @param {function} commandClass The constructor for a terminal command.
399 * @param {string} argString The argument string to pass to the command.
400 */
401hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700402 var environment = this.prefs_.get('environment');
403 if (typeof environment != 'object' || environment == null)
404 environment = {};
405
rginda87b86462011-12-14 13:48:03 -0800406 var self = this;
407 this.command = new commandClass(
408 { argString: argString || '',
409 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700410 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800411 onExit: function(code) {
412 self.io.pop();
413 self.io.println(hterm.msg('COMMAND_COMPLETE',
414 [self.command.commandName, code]));
rgindafeaf3142012-01-31 15:14:20 -0800415 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700416 if (self.prefs_.get('close-on-exit'))
417 window.close();
rginda87b86462011-12-14 13:48:03 -0800418 }
419 });
420
rgindafeaf3142012-01-31 15:14:20 -0800421 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800422 this.command.run();
423};
424
425/**
rgindafeaf3142012-01-31 15:14:20 -0800426 * Returns true if the current screen is the primary screen, false otherwise.
427 */
428hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700429 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800430};
431
432/**
433 * Install the keyboard handler for this terminal.
434 *
435 * This will prevent the browser from seeing any keystrokes sent to the
436 * terminal.
437 */
438hterm.Terminal.prototype.installKeyboard = function() {
439 this.keyboard.installKeyboard(this.document_.body.firstChild);
440}
441
442/**
443 * Uninstall the keyboard handler for this terminal.
444 */
445hterm.Terminal.prototype.uninstallKeyboard = function() {
446 this.keyboard.installKeyboard(null);
447}
448
449/**
rginda35c456b2012-02-09 17:29:05 -0800450 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800451 *
452 * Call setFontSize(0) to reset to the default font size.
453 *
454 * This function does not modify the font-size preference.
455 *
456 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800457 */
458hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800459 if (px === 0)
460 px = this.prefs_.get('font-size');
461
rginda35c456b2012-02-09 17:29:05 -0800462 this.scrollPort_.setFontSize(px);
463};
464
465/**
466 * Get the current font size.
467 */
468hterm.Terminal.prototype.getFontSize = function() {
469 return this.scrollPort_.getFontSize();
470};
471
472/**
rginda8e92a692012-05-20 19:37:20 -0700473 * Get the current font family.
474 */
475hterm.Terminal.prototype.getFontFamily = function() {
476 return this.scrollPort_.getFontFamily();
477};
478
479/**
rginda35c456b2012-02-09 17:29:05 -0800480 * Set the CSS "font-family" for this terminal.
481 */
rginda9f5222b2012-03-05 11:53:28 -0800482hterm.Terminal.prototype.syncFontFamily = function() {
483 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
484 this.prefs_.get('font-smoothing'));
485 this.syncBoldSafeState();
486};
487
rginda4bba5e12012-06-20 16:15:30 -0700488/**
489 * Set this.mousePasteButton based on the mouse-paste-button pref,
490 * autodetecting if necessary.
491 */
492hterm.Terminal.prototype.syncMousePasteButton = function() {
493 var button = this.prefs_.get('mouse-paste-button');
494 if (typeof button == 'number') {
495 this.mousePasteButton = button;
496 return;
497 }
498
499 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
500 if (!ary || ary[2] == 'CrOS') {
501 this.mousePasteButton = 2;
502 } else {
503 this.mousePasteButton = 3;
504 }
505};
506
507/**
508 * Enable or disable bold based on the enable-bold pref, autodetecting if
509 * necessary.
510 */
rginda9f5222b2012-03-05 11:53:28 -0800511hterm.Terminal.prototype.syncBoldSafeState = function() {
512 var enableBold = this.prefs_.get('enable-bold');
513 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700514 this.primaryScreen_.textAttributes.enableBold = enableBold;
515 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800516 return;
517 }
518
rgindaf7521392012-02-28 17:20:34 -0800519 var normalSize = this.scrollPort_.measureCharacterSize();
520 var boldSize = this.scrollPort_.measureCharacterSize('bold');
521
522 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800523 if (!isBoldSafe) {
524 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700525 'from normal. Font family is: ' +
526 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800527 }
rginda9f5222b2012-03-05 11:53:28 -0800528
Robert Gindaed016262012-10-26 16:27:09 -0700529 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
530 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800531};
532
533/**
rginda87b86462011-12-14 13:48:03 -0800534 * Return a copy of the current cursor position.
535 *
536 * @return {hterm.RowCol} The RowCol object representing the current position.
537 */
538hterm.Terminal.prototype.saveCursor = function() {
539 return this.screen_.cursorPosition.clone();
540};
541
rgindaa19afe22012-01-25 15:40:22 -0800542hterm.Terminal.prototype.getTextAttributes = function() {
543 return this.screen_.textAttributes;
544};
545
rginda1a09aa02012-06-18 21:11:25 -0700546hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
547 this.screen_.textAttributes = textAttributes;
548};
549
rginda87b86462011-12-14 13:48:03 -0800550/**
rgindaf522ce02012-04-17 17:49:17 -0700551 * Return the current browser zoom factor applied to the terminal.
552 *
553 * @return {number} The current browser zoom factor.
554 */
555hterm.Terminal.prototype.getZoomFactor = function() {
556 return this.scrollPort_.characterSize.zoomFactor;
557};
558
559/**
rginda9846e2f2012-01-27 13:53:33 -0800560 * Change the title of this terminal's window.
561 */
562hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800563 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800564};
565
566/**
rginda87b86462011-12-14 13:48:03 -0800567 * Restore a previously saved cursor position.
568 *
569 * @param {hterm.RowCol} cursor The position to restore.
570 */
571hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700572 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
573 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800574 this.screen_.setCursorPosition(row, column);
575 if (cursor.column > column ||
576 cursor.column == column && cursor.overflow) {
577 this.screen_.cursorPosition.overflow = true;
578 }
rginda87b86462011-12-14 13:48:03 -0800579};
580
581/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400582 * Clear the cursor's overflow flag.
583 */
584hterm.Terminal.prototype.clearCursorOverflow = function() {
585 this.screen_.cursorPosition.overflow = false;
586};
587
588/**
rginda87b86462011-12-14 13:48:03 -0800589 * Set the width of the terminal, resizing the UI to match.
590 */
591hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800592 if (columnCount == null) {
593 this.div_.style.width = '100%';
594 return;
595 }
596
rginda35c456b2012-02-09 17:29:05 -0800597 this.div_.style.width = this.scrollPort_.characterSize.width *
598 columnCount + this.scrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400599 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800600 this.scheduleSyncCursorPosition_();
601};
rginda87b86462011-12-14 13:48:03 -0800602
rgindac9bc5502012-01-18 11:48:44 -0800603/**
rginda35c456b2012-02-09 17:29:05 -0800604 * Set the height of the terminal, resizing the UI to match.
605 */
606hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800607 if (rowCount == null) {
608 this.div_.style.height = '100%';
609 return;
610 }
611
rginda35c456b2012-02-09 17:29:05 -0800612 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700613 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800614 this.realizeSize_(this.screenSize.width, rowCount);
615 this.scheduleSyncCursorPosition_();
616};
617
618/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400619 * Deal with terminal size changes.
620 *
621 */
622hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
623 if (columnCount != this.screenSize.width)
624 this.realizeWidth_(columnCount);
625
626 if (rowCount != this.screenSize.height)
627 this.realizeHeight_(rowCount);
628
629 // Send new terminal size to plugin.
630 this.io.onTerminalResize(columnCount, rowCount);
631};
632
633/**
rgindac9bc5502012-01-18 11:48:44 -0800634 * Deal with terminal width changes.
635 *
636 * This function does what needs to be done when the terminal width changes
637 * out from under us. It happens here rather than in onResize_() because this
638 * code may need to run synchronously to handle programmatic changes of
639 * terminal width.
640 *
641 * Relying on the browser to send us an async resize event means we may not be
642 * in the correct state yet when the next escape sequence hits.
643 */
644hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700645 if (columnCount <= 0)
646 throw new Error('Attempt to realize bad width: ' + columnCount);
647
rgindac9bc5502012-01-18 11:48:44 -0800648 var deltaColumns = columnCount - this.screen_.getWidth();
649
rginda87b86462011-12-14 13:48:03 -0800650 this.screenSize.width = columnCount;
651 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800652
653 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400654 if (this.defaultTabStops)
655 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800656 } else {
657 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400658 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800659 break;
660
661 this.tabStops_.pop();
662 }
663 }
664
665 this.screen_.setColumnCount(this.screenSize.width);
666};
667
668/**
669 * Deal with terminal height changes.
670 *
671 * This function does what needs to be done when the terminal height changes
672 * out from under us. It happens here rather than in onResize_() because this
673 * code may need to run synchronously to handle programmatic changes of
674 * terminal height.
675 *
676 * Relying on the browser to send us an async resize event means we may not be
677 * in the correct state yet when the next escape sequence hits.
678 */
679hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700680 if (rowCount <= 0)
681 throw new Error('Attempt to realize bad height: ' + rowCount);
682
rgindac9bc5502012-01-18 11:48:44 -0800683 var deltaRows = rowCount - this.screen_.getHeight();
684
685 this.screenSize.height = rowCount;
686
687 var cursor = this.saveCursor();
688
689 if (deltaRows < 0) {
690 // Screen got smaller.
691 deltaRows *= -1;
692 while (deltaRows) {
693 var lastRow = this.getRowCount() - 1;
694 if (lastRow - this.scrollbackRows_.length == cursor.row)
695 break;
696
697 if (this.getRowText(lastRow))
698 break;
699
700 this.screen_.popRow();
701 deltaRows--;
702 }
703
704 var ary = this.screen_.shiftRows(deltaRows);
705 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
706
707 // We just removed rows from the top of the screen, we need to update
708 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800709 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800710 } else if (deltaRows > 0) {
711 // Screen got larger.
712
713 if (deltaRows <= this.scrollbackRows_.length) {
714 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
715 var rows = this.scrollbackRows_.splice(
716 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
717 this.screen_.unshiftRows(rows);
718 deltaRows -= scrollbackCount;
719 cursor.row += scrollbackCount;
720 }
721
722 if (deltaRows)
723 this.appendRows_(deltaRows);
724 }
725
rginda35c456b2012-02-09 17:29:05 -0800726 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800727 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800728};
729
730/**
731 * Scroll the terminal to the top of the scrollback buffer.
732 */
733hterm.Terminal.prototype.scrollHome = function() {
734 this.scrollPort_.scrollRowToTop(0);
735};
736
737/**
738 * Scroll the terminal to the end.
739 */
740hterm.Terminal.prototype.scrollEnd = function() {
741 this.scrollPort_.scrollRowToBottom(this.getRowCount());
742};
743
744/**
745 * Scroll the terminal one page up (minus one line) relative to the current
746 * position.
747 */
748hterm.Terminal.prototype.scrollPageUp = function() {
749 var i = this.scrollPort_.getTopRowIndex();
750 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
751};
752
753/**
754 * Scroll the terminal one page down (minus one line) relative to the current
755 * position.
756 */
757hterm.Terminal.prototype.scrollPageDown = function() {
758 var i = this.scrollPort_.getTopRowIndex();
759 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800760};
761
rgindac9bc5502012-01-18 11:48:44 -0800762/**
763 * Full terminal reset.
764 */
rginda87b86462011-12-14 13:48:03 -0800765hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800766 this.clearAllTabStops();
767 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -0700768
769 this.clearHome(this.primaryScreen_);
770 this.primaryScreen_.textAttributes.reset();
771
772 this.clearHome(this.alternateScreen_);
773 this.alternateScreen_.textAttributes.reset();
774
rgindab8bc8932012-04-27 12:45:03 -0700775 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
776
rgindac9bc5502012-01-18 11:48:44 -0800777 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800778};
779
rgindac9bc5502012-01-18 11:48:44 -0800780/**
781 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -0700782 *
783 * Perform a soft reset to the default values listed in
784 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -0800785 */
rginda0f5c0292012-01-13 11:00:13 -0800786hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -0700787 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -0800788 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -0700789
rgindab8bc8932012-04-27 12:45:03 -0700790 // Xterm also resets the color palette on soft reset, even though it doesn't
791 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -0700792 this.primaryScreen_.textAttributes.resetColorPalette();
793 this.alternateScreen_.textAttributes.resetColorPalette();
794
rgindab8bc8932012-04-27 12:45:03 -0700795 // The xterm man page explicitly says this will happen on soft reset.
796 this.setVTScrollRegion(null, null);
797
798 // Xterm also shows the cursor on soft reset, but does not alter the blink
799 // state.
rgindaa19afe22012-01-25 15:40:22 -0800800 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -0800801};
802
rgindac9bc5502012-01-18 11:48:44 -0800803/**
804 * Move the cursor forward to the next tab stop, or to the last column
805 * if no more tab stops are set.
806 */
807hterm.Terminal.prototype.forwardTabStop = function() {
808 var column = this.screen_.cursorPosition.column;
809
810 for (var i = 0; i < this.tabStops_.length; i++) {
811 if (this.tabStops_[i] > column) {
812 this.setCursorColumn(this.tabStops_[i]);
813 return;
814 }
815 }
816
David Benjamin66e954d2012-05-05 21:08:12 -0400817 // xterm does not clear the overflow flag on HT or CHT.
818 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -0800819 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -0400820 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -0800821};
822
rgindac9bc5502012-01-18 11:48:44 -0800823/**
824 * Move the cursor backward to the previous tab stop, or to the first column
825 * if no previous tab stops are set.
826 */
827hterm.Terminal.prototype.backwardTabStop = function() {
828 var column = this.screen_.cursorPosition.column;
829
830 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
831 if (this.tabStops_[i] < column) {
832 this.setCursorColumn(this.tabStops_[i]);
833 return;
834 }
835 }
836
837 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800838};
839
rgindac9bc5502012-01-18 11:48:44 -0800840/**
841 * Set a tab stop at the given column.
842 *
843 * @param {int} column Zero based column.
844 */
845hterm.Terminal.prototype.setTabStop = function(column) {
846 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
847 if (this.tabStops_[i] == column)
848 return;
849
850 if (this.tabStops_[i] < column) {
851 this.tabStops_.splice(i + 1, 0, column);
852 return;
853 }
854 }
855
856 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800857};
858
rgindac9bc5502012-01-18 11:48:44 -0800859/**
860 * Clear the tab stop at the current cursor position.
861 *
862 * No effect if there is no tab stop at the current cursor position.
863 */
864hterm.Terminal.prototype.clearTabStopAtCursor = function() {
865 var column = this.screen_.cursorPosition.column;
866
867 var i = this.tabStops_.indexOf(column);
868 if (i == -1)
869 return;
870
871 this.tabStops_.splice(i, 1);
872};
873
874/**
875 * Clear all tab stops.
876 */
877hterm.Terminal.prototype.clearAllTabStops = function() {
878 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -0400879 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -0800880};
881
882/**
883 * Set up the default tab stops, starting from a given column.
884 *
885 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -0400886 * from the specified column, or 0 if no column is provided. It also flags
887 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -0800888 *
889 * This does not clear the existing tab stops first, use clearAllTabStops
890 * for that.
891 *
892 * @param {int} opt_start Optional starting zero based starting column, useful
893 * for filling out missing tab stops when the terminal is resized.
894 */
895hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
896 var start = opt_start || 0;
897 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -0400898 // Round start up to a default tab stop.
899 start = start - 1 - ((start - 1) % w) + w;
900 for (var i = start; i < this.screenSize.width; i += w) {
901 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -0800902 }
David Benjamin66e954d2012-05-05 21:08:12 -0400903
904 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -0800905};
906
rginda6d397402012-01-17 10:58:29 -0800907/**
rginda8ba33642011-12-14 12:31:31 -0800908 * Interpret a sequence of characters.
909 *
910 * Incomplete escape sequences are buffered until the next call.
911 *
912 * @param {string} str Sequence of characters to interpret or pass through.
913 */
914hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -0800915 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -0800916 this.scheduleSyncCursorPosition_();
917};
918
919/**
920 * Take over the given DIV for use as the terminal display.
921 *
922 * @param {HTMLDivElement} div The div to use as the terminal display.
923 */
924hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -0800925 this.div_ = div;
926
rginda8ba33642011-12-14 12:31:31 -0800927 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -0700928 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -0400929 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
930 this.scrollPort_.setBackgroundPosition(
931 this.prefs_.get('background-position'));
rginda30f20f62012-04-05 16:36:19 -0700932
rginda0918b652012-04-04 11:26:24 -0700933 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -0800934
rginda9f5222b2012-03-05 11:53:28 -0800935 this.setFontSize(this.prefs_.get('font-size'));
936 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -0800937
David Reveman8f552492012-03-28 12:18:41 -0400938 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
939
rginda8ba33642011-12-14 12:31:31 -0800940 this.document_ = this.scrollPort_.getDocument();
941
rginda4bba5e12012-06-20 16:15:30 -0700942 this.document_.body.oncontextmenu = function() { return false };
943
944 var onMouse = this.onMouse_.bind(this);
945 this.document_.body.firstChild.addEventListener('mousedown', onMouse);
946 this.document_.body.firstChild.addEventListener('mouseup', onMouse);
947 this.document_.body.firstChild.addEventListener('mousemove', onMouse);
948 this.scrollPort_.onScrollWheel = onMouse;
949
rginda8e92a692012-05-20 19:37:20 -0700950 this.document_.body.firstChild.addEventListener(
951 'focus', this.onFocusChange_.bind(this, true));
952 this.document_.body.firstChild.addEventListener(
953 'blur', this.onFocusChange_.bind(this, false));
954
955 var style = this.document_.createElement('style');
956 style.textContent =
957 ('.cursor-node[focus="false"] {' +
958 ' box-sizing: border-box;' +
959 ' background-color: transparent !important;' +
960 ' border-width: 2px;' +
961 ' border-style: solid;' +
962 '}');
963 this.document_.head.appendChild(style);
964
rginda8ba33642011-12-14 12:31:31 -0800965 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -0700966 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -0800967 this.cursorNode_.style.cssText =
968 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -0800969 'top: -99px;' +
970 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -0800971 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
972 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda8e92a692012-05-20 19:37:20 -0700973 '-webkit-transition: opacity, background-color 100ms linear;');
974 this.setCursorColor(this.prefs_.get('cursor-color'));
rgindad5613292012-06-19 15:40:37 -0700975
rginda8ba33642011-12-14 12:31:31 -0800976 this.document_.body.appendChild(this.cursorNode_);
977
rgindad5613292012-06-19 15:40:37 -0700978 // When 'enableMouseDragScroll' is off we reposition this element directly
979 // under the mouse cursor after a click. This makes Chrome associate
980 // subsequent mousemove events with the scroll-blocker. Since the
981 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
982 // events do not cause the scrollport to scroll.
983 //
984 // It's a hack, but it's the cleanest way I could find.
985 this.scrollBlockerNode_ = this.document_.createElement('div');
986 this.scrollBlockerNode_.style.cssText =
987 ('position: absolute;' +
988 'top: -99px;' +
989 'display: block;' +
990 'width: 10px;' +
991 'height: 10px;');
992 this.document_.body.appendChild(this.scrollBlockerNode_);
993
994 var onMouse = this.onMouse_.bind(this);
995 this.scrollPort_.onScrollWheel = onMouse;
996 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
997 ].forEach(function(event) {
998 this.scrollBlockerNode_.addEventListener(event, onMouse);
999 this.cursorNode_.addEventListener(event, onMouse);
1000 this.document_.addEventListener(event, onMouse);
1001 }.bind(this));
1002
1003 this.cursorNode_.addEventListener('mousedown', function() {
1004 setTimeout(this.focus.bind(this));
1005 }.bind(this));
1006
rgindade84e382012-04-20 15:39:31 -07001007 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
rginda8ba33642011-12-14 12:31:31 -08001008 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001009
rginda87b86462011-12-14 13:48:03 -08001010 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001011 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001012};
1013
rginda0918b652012-04-04 11:26:24 -07001014/**
1015 * Return the HTML document that contains the terminal DOM nodes.
1016 */
rginda87b86462011-12-14 13:48:03 -08001017hterm.Terminal.prototype.getDocument = function() {
1018 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001019};
1020
1021/**
rginda0918b652012-04-04 11:26:24 -07001022 * Focus the terminal.
1023 */
1024hterm.Terminal.prototype.focus = function() {
1025 this.scrollPort_.focus();
1026};
1027
1028/**
rginda8ba33642011-12-14 12:31:31 -08001029 * Return the HTML Element for a given row index.
1030 *
1031 * This is a method from the RowProvider interface. The ScrollPort uses
1032 * it to fetch rows on demand as they are scrolled into view.
1033 *
1034 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1035 * pairs to conserve memory.
1036 *
1037 * @param {integer} index The zero-based row index, measured relative to the
1038 * start of the scrollback buffer. On-screen rows will always have the
1039 * largest indicies.
1040 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1041 */
1042hterm.Terminal.prototype.getRowNode = function(index) {
1043 if (index < this.scrollbackRows_.length)
1044 return this.scrollbackRows_[index];
1045
1046 var screenIndex = index - this.scrollbackRows_.length;
1047 return this.screen_.rowsArray[screenIndex];
1048};
1049
1050/**
1051 * Return the text content for a given range of rows.
1052 *
1053 * This is a method from the RowProvider interface. The ScrollPort uses
1054 * it to fetch text content on demand when the user attempts to copy their
1055 * selection to the clipboard.
1056 *
1057 * @param {integer} start The zero-based row index to start from, measured
1058 * relative to the start of the scrollback buffer. On-screen rows will
1059 * always have the largest indicies.
1060 * @param {integer} end The zero-based row index to end on, measured
1061 * relative to the start of the scrollback buffer.
1062 * @return {string} A single string containing the text value of the range of
1063 * rows. Lines will be newline delimited, with no trailing newline.
1064 */
1065hterm.Terminal.prototype.getRowsText = function(start, end) {
1066 var ary = [];
1067 for (var i = start; i < end; i++) {
1068 var node = this.getRowNode(i);
1069 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001070 if (i < end - 1 && !node.getAttribute('line-overflow'))
1071 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001072 }
1073
rgindaa09e7332012-08-17 12:49:51 -07001074 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001075};
1076
1077/**
1078 * Return the text content for a given row.
1079 *
1080 * This is a method from the RowProvider interface. The ScrollPort uses
1081 * it to fetch text content on demand when the user attempts to copy their
1082 * selection to the clipboard.
1083 *
1084 * @param {integer} index The zero-based row index to return, measured
1085 * relative to the start of the scrollback buffer. On-screen rows will
1086 * always have the largest indicies.
1087 * @return {string} A string containing the text value of the selected row.
1088 */
1089hterm.Terminal.prototype.getRowText = function(index) {
1090 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001091 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001092};
1093
1094/**
1095 * Return the total number of rows in the addressable screen and in the
1096 * scrollback buffer of this terminal.
1097 *
1098 * This is a method from the RowProvider interface. The ScrollPort uses
1099 * it to compute the size of the scrollbar.
1100 *
1101 * @return {integer} The number of rows in this terminal.
1102 */
1103hterm.Terminal.prototype.getRowCount = function() {
1104 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1105};
1106
1107/**
1108 * Create DOM nodes for new rows and append them to the end of the terminal.
1109 *
1110 * This is the only correct way to add a new DOM node for a row. Notice that
1111 * the new row is appended to the bottom of the list of rows, and does not
1112 * require renumbering (of the rowIndex property) of previous rows.
1113 *
1114 * If you think you want a new blank row somewhere in the middle of the
1115 * terminal, look into moveRows_().
1116 *
1117 * This method does not pay attention to vtScrollTop/Bottom, since you should
1118 * be using moveRows() in cases where they would matter.
1119 *
1120 * The cursor will be positioned at column 0 of the first inserted line.
1121 */
1122hterm.Terminal.prototype.appendRows_ = function(count) {
1123 var cursorRow = this.screen_.rowsArray.length;
1124 var offset = this.scrollbackRows_.length + cursorRow;
1125 for (var i = 0; i < count; i++) {
1126 var row = this.document_.createElement('x-row');
1127 row.appendChild(this.document_.createTextNode(''));
1128 row.rowIndex = offset + i;
1129 this.screen_.pushRow(row);
1130 }
1131
1132 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1133 if (extraRows > 0) {
1134 var ary = this.screen_.shiftRows(extraRows);
1135 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001136 if (this.scrollPort_.isScrolledEnd)
1137 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001138 }
1139
1140 if (cursorRow >= this.screen_.rowsArray.length)
1141 cursorRow = this.screen_.rowsArray.length - 1;
1142
rginda87b86462011-12-14 13:48:03 -08001143 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001144};
1145
1146/**
1147 * Relocate rows from one part of the addressable screen to another.
1148 *
1149 * This is used to recycle rows during VT scrolls (those which are driven
1150 * by VT commands, rather than by the user manipulating the scrollbar.)
1151 *
1152 * In this case, the blank lines scrolled into the scroll region are made of
1153 * the nodes we scrolled off. These have their rowIndex properties carefully
1154 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001155 */
1156hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1157 var ary = this.screen_.removeRows(fromIndex, count);
1158 this.screen_.insertRows(toIndex, ary);
1159
1160 var start, end;
1161 if (fromIndex < toIndex) {
1162 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001163 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001164 } else {
1165 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001166 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001167 }
1168
1169 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001170 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001171};
1172
1173/**
1174 * Renumber the rowIndex property of the given range of rows.
1175 *
1176 * The start and end indicies are relative to the screen, not the scrollback.
1177 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001178 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001179 * no need to renumber scrollback rows.
1180 */
1181hterm.Terminal.prototype.renumberRows_ = function(start, end) {
1182 var offset = this.scrollbackRows_.length;
1183 for (var i = start; i < end; i++) {
1184 this.screen_.rowsArray[i].rowIndex = offset + i;
1185 }
1186};
1187
1188/**
1189 * Print a string to the terminal.
1190 *
1191 * This respects the current insert and wraparound modes. It will add new lines
1192 * to the end of the terminal, scrolling off the top into the scrollback buffer
1193 * if necessary.
1194 *
1195 * The string is *not* parsed for escape codes. Use the interpret() method if
1196 * that's what you're after.
1197 *
1198 * @param{string} str The string to print.
1199 */
1200hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001201 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001202
rgindaa9abdd82012-08-06 18:05:09 -07001203 while (startOffset < str.length) {
rgindaa09e7332012-08-17 12:49:51 -07001204 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1205 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001206 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001207 }
rgindaa19afe22012-01-25 15:40:22 -08001208
rgindaa9abdd82012-08-06 18:05:09 -07001209 var count = str.length - startOffset;
1210 var didOverflow = false;
1211 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001212
rgindaa9abdd82012-08-06 18:05:09 -07001213 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1214 didOverflow = true;
1215 count = this.screenSize.width - this.screen_.cursorPosition.column;
1216 }
rgindaa19afe22012-01-25 15:40:22 -08001217
rgindaa9abdd82012-08-06 18:05:09 -07001218 if (didOverflow && !this.options_.wraparound) {
1219 // If the string overflowed the line but wraparound is off, then the
1220 // last printed character should be the last of the string.
1221 // TODO: This will add to our problems with multibyte UTF-16 characters.
1222 substr = str.substr(startOffset, count - 1) +
1223 str.substr(str.length - 1);
1224 count = str.length;
1225 } else {
1226 substr = str.substr(startOffset, count);
1227 }
rgindaa19afe22012-01-25 15:40:22 -08001228
rgindaa9abdd82012-08-06 18:05:09 -07001229 if (this.options_.insertMode) {
1230 this.screen_.insertString(substr);
1231 } else {
1232 this.screen_.overwriteString(substr);
1233 }
1234
1235 this.screen_.maybeClipCurrentRow();
1236 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001237 }
rginda8ba33642011-12-14 12:31:31 -08001238
1239 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001240
rginda9f5222b2012-03-05 11:53:28 -08001241 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001242 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001243};
1244
1245/**
rginda87b86462011-12-14 13:48:03 -08001246 * Set the VT scroll region.
1247 *
rginda87b86462011-12-14 13:48:03 -08001248 * This also resets the cursor position to the absolute (0, 0) position, since
1249 * that's what xterm appears to do.
1250 *
1251 * @param {integer} scrollTop The zero-based top of the scroll region.
1252 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1253 * inclusive.
1254 */
1255hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
1256 this.vtScrollTop_ = scrollTop;
1257 this.vtScrollBottom_ = scrollBottom;
rginda87b86462011-12-14 13:48:03 -08001258};
1259
1260/**
rginda8ba33642011-12-14 12:31:31 -08001261 * Return the top row index according to the VT.
1262 *
1263 * This will return 0 unless the terminal has been told to restrict scrolling
1264 * to some lower row. It is used for some VT cursor positioning and scrolling
1265 * commands.
1266 *
1267 * @return {integer} The topmost row in the terminal's scroll region.
1268 */
1269hterm.Terminal.prototype.getVTScrollTop = function() {
1270 if (this.vtScrollTop_ != null)
1271 return this.vtScrollTop_;
1272
1273 return 0;
rginda87b86462011-12-14 13:48:03 -08001274};
rginda8ba33642011-12-14 12:31:31 -08001275
1276/**
1277 * Return the bottom row index according to the VT.
1278 *
1279 * This will return the height of the terminal unless the it has been told to
1280 * restrict scrolling to some higher row. It is used for some VT cursor
1281 * positioning and scrolling commands.
1282 *
1283 * @return {integer} The bottommost row in the terminal's scroll region.
1284 */
1285hterm.Terminal.prototype.getVTScrollBottom = function() {
1286 if (this.vtScrollBottom_ != null)
1287 return this.vtScrollBottom_;
1288
rginda87b86462011-12-14 13:48:03 -08001289 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001290}
1291
1292/**
1293 * Process a '\n' character.
1294 *
1295 * If the cursor is on the final row of the terminal this will append a new
1296 * blank row to the screen and scroll the topmost row into the scrollback
1297 * buffer.
1298 *
1299 * Otherwise, this moves the cursor to column zero of the next row.
1300 */
1301hterm.Terminal.prototype.newLine = function() {
1302 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -08001303 // If we're at the end of the screen we need to append a new line and
1304 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -08001305 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -08001306 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
1307 // End of the scroll region does not affect the scrollback buffer.
1308 this.vtScrollUp(1);
1309 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -08001310 } else {
rginda87b86462011-12-14 13:48:03 -08001311 // Anywhere else in the screen just moves the cursor.
1312 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001313 }
1314};
1315
1316/**
1317 * Like newLine(), except maintain the cursor column.
1318 */
1319hterm.Terminal.prototype.lineFeed = function() {
1320 var column = this.screen_.cursorPosition.column;
1321 this.newLine();
1322 this.setCursorColumn(column);
1323};
1324
1325/**
rginda87b86462011-12-14 13:48:03 -08001326 * If autoCarriageReturn is set then newLine(), else lineFeed().
1327 */
1328hterm.Terminal.prototype.formFeed = function() {
1329 if (this.options_.autoCarriageReturn) {
1330 this.newLine();
1331 } else {
1332 this.lineFeed();
1333 }
1334};
1335
1336/**
1337 * Move the cursor up one row, possibly inserting a blank line.
1338 *
1339 * The cursor column is not changed.
1340 */
1341hterm.Terminal.prototype.reverseLineFeed = function() {
1342 var scrollTop = this.getVTScrollTop();
1343 var currentRow = this.screen_.cursorPosition.row;
1344
1345 if (currentRow == scrollTop) {
1346 this.insertLines(1);
1347 } else {
1348 this.setAbsoluteCursorRow(currentRow - 1);
1349 }
1350};
1351
1352/**
rginda8ba33642011-12-14 12:31:31 -08001353 * Replace all characters to the left of the current cursor with the space
1354 * character.
1355 *
1356 * TODO(rginda): This should probably *remove* the characters (not just replace
1357 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001358 * position.
rginda8ba33642011-12-14 12:31:31 -08001359 */
1360hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001361 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001362 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001363 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001364 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001365};
1366
1367/**
David Benjamin684a9b72012-05-01 17:19:58 -04001368 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001369 *
1370 * The cursor position is unchanged.
1371 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001372 * If the current background color is not the default background color this
1373 * will insert spaces rather than delete. This is unfortunate because the
1374 * trailing space will affect text selection, but it's difficult to come up
1375 * with a way to style empty space that wouldn't trip up the hterm.Screen
1376 * code.
rginda8ba33642011-12-14 12:31:31 -08001377 */
1378hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001379 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1380 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001381
1382 if (this.screen_.textAttributes.background ===
1383 this.screen_.textAttributes.DEFAULT_COLOR) {
1384 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
1385 if (cursorRow.textContent.length <=
1386 this.screen_.cursorPosition.column + count) {
1387 this.screen_.deleteChars(count);
1388 this.clearCursorOverflow();
1389 return;
1390 }
1391 }
1392
rginda87b86462011-12-14 13:48:03 -08001393 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001394 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001395 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001396 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001397};
1398
1399/**
1400 * Erase the current line.
1401 *
1402 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001403 */
1404hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001405 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001406 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001407 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001408 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001409};
1410
1411/**
David Benjamina08d78f2012-05-05 00:28:49 -04001412 * Erase all characters from the start of the screen to the current cursor
1413 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001414 *
1415 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001416 */
1417hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001418 var cursor = this.saveCursor();
1419
1420 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001421
David Benjamina08d78f2012-05-05 00:28:49 -04001422 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001423 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001424 this.screen_.clearCursorRow();
1425 }
1426
rginda87b86462011-12-14 13:48:03 -08001427 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001428 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001429};
1430
1431/**
1432 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001433 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001434 *
1435 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001436 */
1437hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001438 var cursor = this.saveCursor();
1439
1440 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001441
David Benjamina08d78f2012-05-05 00:28:49 -04001442 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001443 for (var i = cursor.row + 1; i <= bottom; i++) {
1444 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001445 this.screen_.clearCursorRow();
1446 }
1447
rginda87b86462011-12-14 13:48:03 -08001448 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001449 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001450};
1451
1452/**
1453 * Fill the terminal with a given character.
1454 *
1455 * This methods does not respect the VT scroll region.
1456 *
1457 * @param {string} ch The character to use for the fill.
1458 */
1459hterm.Terminal.prototype.fill = function(ch) {
1460 var cursor = this.saveCursor();
1461
1462 this.setAbsoluteCursorPosition(0, 0);
1463 for (var row = 0; row < this.screenSize.height; row++) {
1464 for (var col = 0; col < this.screenSize.width; col++) {
1465 this.setAbsoluteCursorPosition(row, col);
1466 this.screen_.overwriteString(ch);
1467 }
1468 }
1469
1470 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001471};
1472
1473/**
rginda9ea433c2012-03-16 11:57:00 -07001474 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001475 *
rginda9ea433c2012-03-16 11:57:00 -07001476 * This does not respect the scroll region.
1477 *
1478 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1479 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001480 */
rginda9ea433c2012-03-16 11:57:00 -07001481hterm.Terminal.prototype.clearHome = function(opt_screen) {
1482 var screen = opt_screen || this.screen_;
1483 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001484
rginda11057d52012-04-25 12:29:56 -07001485 if (bottom == 0) {
1486 // Empty screen, nothing to do.
1487 return;
1488 }
1489
rgindae4d29232012-01-19 10:47:13 -08001490 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001491 screen.setCursorPosition(i, 0);
1492 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001493 }
1494
rginda9ea433c2012-03-16 11:57:00 -07001495 screen.setCursorPosition(0, 0);
1496};
1497
1498/**
1499 * Erase the entire display without changing the cursor position.
1500 *
1501 * The cursor position is unchanged. This does not respect the scroll
1502 * region.
1503 *
1504 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1505 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001506 */
1507hterm.Terminal.prototype.clear = function(opt_screen) {
1508 var screen = opt_screen || this.screen_;
1509 var cursor = screen.cursorPosition.clone();
1510 this.clearHome(screen);
1511 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001512};
1513
1514/**
1515 * VT command to insert lines at the current cursor row.
1516 *
1517 * This respects the current scroll region. Rows pushed off the bottom are
1518 * lost (they won't show up in the scrollback buffer).
1519 *
rginda8ba33642011-12-14 12:31:31 -08001520 * @param {integer} count The number of lines to insert.
1521 */
1522hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07001523 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08001524
1525 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07001526 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08001527
Robert Ginda579186b2012-09-26 11:40:04 -07001528 // The moveCount is the number of rows we need to relocate to make room for
1529 // the new row(s). The count is the distance to move them.
1530 var moveCount = bottom - cursorRow - count + 1;
1531 if (moveCount)
1532 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08001533
Robert Ginda579186b2012-09-26 11:40:04 -07001534 for (var i = count - 1; i >= 0; i--) {
1535 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001536 this.screen_.clearCursorRow();
1537 }
rginda8ba33642011-12-14 12:31:31 -08001538};
1539
1540/**
1541 * VT command to delete lines at the current cursor row.
1542 *
1543 * New rows are added to the bottom of scroll region to take their place. New
1544 * rows are strictly there to take up space and have no content or style.
1545 */
1546hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001547 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001548
rginda87b86462011-12-14 13:48:03 -08001549 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001550 var bottom = this.getVTScrollBottom();
1551
rginda87b86462011-12-14 13:48:03 -08001552 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001553 count = Math.min(count, maxCount);
1554
rginda87b86462011-12-14 13:48:03 -08001555 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001556 if (count != maxCount)
1557 this.moveRows_(top, count, moveStart);
1558
1559 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001560 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001561 this.screen_.clearCursorRow();
1562 }
1563
rginda87b86462011-12-14 13:48:03 -08001564 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001565 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001566};
1567
1568/**
1569 * Inserts the given number of spaces at the current cursor position.
1570 *
rginda87b86462011-12-14 13:48:03 -08001571 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001572 */
1573hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001574 var cursor = this.saveCursor();
1575
rgindacbbd7482012-06-13 15:06:16 -07001576 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001577 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001578 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001579
1580 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001581 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001582};
1583
1584/**
1585 * Forward-delete the specified number of characters starting at the cursor
1586 * position.
1587 *
1588 * @param {integer} count The number of characters to delete.
1589 */
1590hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001591 var deleted = this.screen_.deleteChars(count);
1592 if (deleted && !this.screen_.textAttributes.isDefault()) {
1593 var cursor = this.saveCursor();
1594 this.setCursorColumn(this.screenSize.width - deleted);
1595 this.screen_.insertString(lib.f.getWhitespace(deleted));
1596 this.restoreCursor(cursor);
1597 }
1598
David Benjamin54e8bf62012-06-01 22:31:40 -04001599 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001600};
1601
1602/**
1603 * Shift rows in the scroll region upwards by a given number of lines.
1604 *
1605 * New rows are inserted at the bottom of the scroll region to fill the
1606 * vacated rows. The new rows not filled out with the current text attributes.
1607 *
1608 * This function does not affect the scrollback rows at all. Rows shifted
1609 * off the top are lost.
1610 *
rginda87b86462011-12-14 13:48:03 -08001611 * The cursor position is not altered.
1612 *
rginda8ba33642011-12-14 12:31:31 -08001613 * @param {integer} count The number of rows to scroll.
1614 */
1615hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001616 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001617
rginda87b86462011-12-14 13:48:03 -08001618 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001619 this.deleteLines(count);
1620
rginda87b86462011-12-14 13:48:03 -08001621 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001622};
1623
1624/**
1625 * Shift rows below the cursor down by a given number of lines.
1626 *
1627 * This function respects the current scroll region.
1628 *
1629 * New rows are inserted at the top of the scroll region to fill the
1630 * vacated rows. The new rows not filled out with the current text attributes.
1631 *
1632 * This function does not affect the scrollback rows at all. Rows shifted
1633 * off the bottom are lost.
1634 *
1635 * @param {integer} count The number of rows to scroll.
1636 */
1637hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001638 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001639
rginda87b86462011-12-14 13:48:03 -08001640 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001641 this.insertLines(opt_count);
1642
rginda87b86462011-12-14 13:48:03 -08001643 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001644};
1645
rginda87b86462011-12-14 13:48:03 -08001646
rginda8ba33642011-12-14 12:31:31 -08001647/**
1648 * Set the cursor position.
1649 *
1650 * The cursor row is relative to the scroll region if the terminal has
1651 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1652 *
1653 * @param {integer} row The new zero-based cursor row.
1654 * @param {integer} row The new zero-based cursor column.
1655 */
1656hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1657 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001658 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001659 } else {
rginda87b86462011-12-14 13:48:03 -08001660 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001661 }
rginda87b86462011-12-14 13:48:03 -08001662};
rginda8ba33642011-12-14 12:31:31 -08001663
rginda87b86462011-12-14 13:48:03 -08001664hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1665 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07001666 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
1667 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001668 this.screen_.setCursorPosition(row, column);
1669};
1670
1671hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07001672 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
1673 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001674 this.screen_.setCursorPosition(row, column);
1675};
1676
1677/**
1678 * Set the cursor column.
1679 *
1680 * @param {integer} column The new zero-based cursor column.
1681 */
1682hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001683 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001684};
1685
1686/**
1687 * Return the cursor column.
1688 *
1689 * @return {integer} The zero-based cursor column.
1690 */
1691hterm.Terminal.prototype.getCursorColumn = function() {
1692 return this.screen_.cursorPosition.column;
1693};
1694
1695/**
1696 * Set the cursor row.
1697 *
1698 * The cursor row is relative to the scroll region if the terminal has
1699 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1700 *
1701 * @param {integer} row The new cursor row.
1702 */
rginda87b86462011-12-14 13:48:03 -08001703hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1704 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001705};
1706
1707/**
1708 * Return the cursor row.
1709 *
1710 * @return {integer} The zero-based cursor row.
1711 */
1712hterm.Terminal.prototype.getCursorRow = function(row) {
1713 return this.screen_.cursorPosition.row;
1714};
1715
1716/**
1717 * Request that the ScrollPort redraw itself soon.
1718 *
1719 * The redraw will happen asynchronously, soon after the call stack winds down.
1720 * Multiple calls will be coalesced into a single redraw.
1721 */
1722hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001723 if (this.timeouts_.redraw)
1724 return;
rginda8ba33642011-12-14 12:31:31 -08001725
1726 var self = this;
rginda87b86462011-12-14 13:48:03 -08001727 this.timeouts_.redraw = setTimeout(function() {
1728 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001729 self.scrollPort_.redraw_();
1730 }, 0);
1731};
1732
1733/**
1734 * Request that the ScrollPort be scrolled to the bottom.
1735 *
1736 * The scroll will happen asynchronously, soon after the call stack winds down.
1737 * Multiple calls will be coalesced into a single scroll.
1738 *
1739 * This affects the scrollbar position of the ScrollPort, and has nothing to
1740 * do with the VT scroll commands.
1741 */
1742hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1743 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001744 return;
rginda8ba33642011-12-14 12:31:31 -08001745
1746 var self = this;
1747 this.timeouts_.scrollDown = setTimeout(function() {
1748 delete self.timeouts_.scrollDown;
1749 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1750 }, 10);
1751};
1752
1753/**
1754 * Move the cursor up a specified number of rows.
1755 *
1756 * @param {integer} count The number of rows to move the cursor.
1757 */
1758hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001759 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001760};
1761
1762/**
1763 * Move the cursor down a specified number of rows.
1764 *
1765 * @param {integer} count The number of rows to move the cursor.
1766 */
1767hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001768 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001769 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1770 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1771 this.screenSize.height - 1);
1772
rgindacbbd7482012-06-13 15:06:16 -07001773 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08001774 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001775 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001776};
1777
1778/**
1779 * Move the cursor left a specified number of columns.
1780 *
1781 * @param {integer} count The number of columns to move the cursor.
1782 */
1783hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001784 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001785};
1786
1787/**
1788 * Move the cursor right a specified number of columns.
1789 *
1790 * @param {integer} count The number of columns to move the cursor.
1791 */
1792hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001793 count = count || 1;
rgindacbbd7482012-06-13 15:06:16 -07001794 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001795 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001796 this.setCursorColumn(column);
1797};
1798
1799/**
1800 * Reverse the foreground and background colors of the terminal.
1801 *
1802 * This only affects text that was drawn with no attributes.
1803 *
1804 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1805 * been drawn with attributes that happen to coincide with the default
1806 * 'no-attribute' colors. My guess is probably not.
1807 */
1808hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001809 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001810 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08001811 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
1812 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08001813 } else {
rginda9f5222b2012-03-05 11:53:28 -08001814 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
1815 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08001816 }
1817};
1818
1819/**
rginda87b86462011-12-14 13:48:03 -08001820 * Ring the terminal bell.
rginda87b86462011-12-14 13:48:03 -08001821 */
1822hterm.Terminal.prototype.ringBell = function() {
rginda9f5222b2012-03-05 11:53:28 -08001823 if (this.bellAudio_.getAttribute('src'))
1824 this.bellAudio_.play();
rgindaf0090c92012-02-10 14:58:52 -08001825
rginda6d397402012-01-17 10:58:29 -08001826 this.cursorNode_.style.backgroundColor =
1827 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001828
1829 var self = this;
1830 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08001831 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08001832 }, 200);
rginda87b86462011-12-14 13:48:03 -08001833};
1834
1835/**
rginda8ba33642011-12-14 12:31:31 -08001836 * Set the origin mode bit.
1837 *
1838 * If origin mode is on, certain VT cursor and scrolling commands measure their
1839 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1840 * to the top of the addressable screen.
1841 *
1842 * Defaults to off.
1843 *
1844 * @param {boolean} state True to set origin mode, false to unset.
1845 */
1846hterm.Terminal.prototype.setOriginMode = function(state) {
1847 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08001848 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08001849};
1850
1851/**
1852 * Set the insert mode bit.
1853 *
1854 * If insert mode is on, existing text beyond the cursor position will be
1855 * shifted right to make room for new text. Otherwise, new text overwrites
1856 * any existing text.
1857 *
1858 * Defaults to off.
1859 *
1860 * @param {boolean} state True to set insert mode, false to unset.
1861 */
1862hterm.Terminal.prototype.setInsertMode = function(state) {
1863 this.options_.insertMode = state;
1864};
1865
1866/**
rginda87b86462011-12-14 13:48:03 -08001867 * Set the auto carriage return bit.
1868 *
1869 * If auto carriage return is on then a formfeed character is interpreted
1870 * as a newline, otherwise it's the same as a linefeed. The difference boils
1871 * down to whether or not the cursor column is reset.
1872 */
1873hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1874 this.options_.autoCarriageReturn = state;
1875};
1876
1877/**
rginda8ba33642011-12-14 12:31:31 -08001878 * Set the wraparound mode bit.
1879 *
1880 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1881 * to the start of the following row. Otherwise, the cursor is clamped to the
1882 * end of the screen and attempts to write past it are ignored.
1883 *
1884 * Defaults to on.
1885 *
1886 * @param {boolean} state True to set wraparound mode, false to unset.
1887 */
1888hterm.Terminal.prototype.setWraparound = function(state) {
1889 this.options_.wraparound = state;
1890};
1891
1892/**
1893 * Set the reverse-wraparound mode bit.
1894 *
1895 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1896 * to the end of the previous row. Otherwise, the cursor is clamped to column
1897 * 0.
1898 *
1899 * Defaults to off.
1900 *
1901 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
1902 */
1903hterm.Terminal.prototype.setReverseWraparound = function(state) {
1904 this.options_.reverseWraparound = state;
1905};
1906
1907/**
1908 * Selects between the primary and alternate screens.
1909 *
1910 * If alternate mode is on, the alternate screen is active. Otherwise the
1911 * primary screen is active.
1912 *
1913 * Swapping screens has no effect on the scrollback buffer.
1914 *
1915 * Each screen maintains its own cursor position.
1916 *
1917 * Defaults to off.
1918 *
1919 * @param {boolean} state True to set alternate mode, false to unset.
1920 */
1921hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08001922 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001923 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
1924
rginda35c456b2012-02-09 17:29:05 -08001925 if (this.screen_.rowsArray.length &&
1926 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
1927 // If the screen changed sizes while we were away, our rowIndexes may
1928 // be incorrect.
1929 var offset = this.scrollbackRows_.length;
1930 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07001931 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08001932 ary[i].rowIndex = offset + i;
1933 }
1934 }
rginda8ba33642011-12-14 12:31:31 -08001935
rginda35c456b2012-02-09 17:29:05 -08001936 this.realizeWidth_(this.screenSize.width);
1937 this.realizeHeight_(this.screenSize.height);
1938 this.scrollPort_.syncScrollHeight();
1939 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08001940
rginda6d397402012-01-17 10:58:29 -08001941 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08001942 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08001943};
1944
1945/**
1946 * Set the cursor-blink mode bit.
1947 *
1948 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
1949 * a visible cursor does not blink.
1950 *
1951 * You should make sure to turn blinking off if you're going to dispose of a
1952 * terminal, otherwise you'll leak a timeout.
1953 *
1954 * Defaults to on.
1955 *
1956 * @param {boolean} state True to set cursor-blink mode, false to unset.
1957 */
1958hterm.Terminal.prototype.setCursorBlink = function(state) {
1959 this.options_.cursorBlink = state;
1960
1961 if (!state && this.timeouts_.cursorBlink) {
1962 clearTimeout(this.timeouts_.cursorBlink);
1963 delete this.timeouts_.cursorBlink;
1964 }
1965
1966 if (this.options_.cursorVisible)
1967 this.setCursorVisible(true);
1968};
1969
1970/**
1971 * Set the cursor-visible mode bit.
1972 *
1973 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
1974 *
1975 * Defaults to on.
1976 *
1977 * @param {boolean} state True to set cursor-visible mode, false to unset.
1978 */
1979hterm.Terminal.prototype.setCursorVisible = function(state) {
1980 this.options_.cursorVisible = state;
1981
1982 if (!state) {
rginda87b86462011-12-14 13:48:03 -08001983 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001984 return;
1985 }
1986
rginda87b86462011-12-14 13:48:03 -08001987 this.syncCursorPosition_();
1988
1989 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001990
1991 if (this.options_.cursorBlink) {
1992 if (this.timeouts_.cursorBlink)
1993 return;
1994
1995 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
1996 500);
1997 } else {
1998 if (this.timeouts_.cursorBlink) {
1999 clearTimeout(this.timeouts_.cursorBlink);
2000 delete this.timeouts_.cursorBlink;
2001 }
2002 }
2003};
2004
2005/**
rginda87b86462011-12-14 13:48:03 -08002006 * Synchronizes the visible cursor and document selection with the current
2007 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002008 */
2009hterm.Terminal.prototype.syncCursorPosition_ = function() {
2010 var topRowIndex = this.scrollPort_.getTopRowIndex();
2011 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2012 var cursorRowIndex = this.scrollbackRows_.length +
2013 this.screen_.cursorPosition.row;
2014
2015 if (cursorRowIndex > bottomRowIndex) {
2016 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002017 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002018 return;
2019 }
2020
rginda35c456b2012-02-09 17:29:05 -08002021 this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px';
2022 this.cursorNode_.style.height = this.scrollPort_.characterSize.height + 'px';
2023
rginda8ba33642011-12-14 12:31:31 -08002024 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002025 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2026 'px';
2027 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2028 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002029
2030 this.cursorNode_.setAttribute('title',
2031 '(' + this.screen_.cursorPosition.row +
2032 ', ' + this.screen_.cursorPosition.column +
2033 ')');
2034
2035 // Update the caret for a11y purposes.
2036 var selection = this.document_.getSelection();
2037 if (selection && selection.isCollapsed)
2038 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002039};
2040
2041/**
2042 * Synchronizes the visible cursor with the current cursor coordinates.
2043 *
2044 * The sync will happen asynchronously, soon after the call stack winds down.
2045 * Multiple calls will be coalesced into a single sync.
2046 */
2047hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2048 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002049 return;
rginda8ba33642011-12-14 12:31:31 -08002050
2051 var self = this;
2052 this.timeouts_.syncCursor = setTimeout(function() {
2053 self.syncCursorPosition_();
2054 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002055 }, 0);
2056};
2057
rgindacc2996c2012-02-24 14:59:31 -08002058/**
rgindaf522ce02012-04-17 17:49:17 -07002059 * Show or hide the zoom warning.
2060 *
2061 * The zoom warning is a message warning the user that their browser zoom must
2062 * be set to 100% in order for hterm to function properly.
2063 *
2064 * @param {boolean} state True to show the message, false to hide it.
2065 */
2066hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2067 if (!this.zoomWarningNode_) {
2068 if (!state)
2069 return;
2070
2071 this.zoomWarningNode_ = this.document_.createElement('div');
2072 this.zoomWarningNode_.style.cssText = (
2073 'color: black;' +
2074 'background-color: #ff2222;' +
2075 'font-size: large;' +
2076 'border-radius: 8px;' +
2077 'opacity: 0.75;' +
2078 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2079 'top: 0.5em;' +
2080 'right: 1.2em;' +
2081 'position: absolute;' +
2082 '-webkit-text-size-adjust: none;' +
2083 '-webkit-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002084 }
2085
rgindade84e382012-04-20 15:39:31 -07002086 this.zoomWarningNode_.textContent = hterm.msg('ZOOM_WARNING') ||
2087 ('!! ' + parseInt(this.scrollPort_.characterSize.zoomFactor * 100) +
2088 '% !!');
rgindaf522ce02012-04-17 17:49:17 -07002089 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2090
2091 if (state) {
2092 if (!this.zoomWarningNode_.parentNode)
2093 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2094 } else if (this.zoomWarningNode_.parentNode) {
2095 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2096 }
2097};
2098
2099/**
rgindacc2996c2012-02-24 14:59:31 -08002100 * Show the terminal overlay for a given amount of time.
2101 *
2102 * The terminal overlay appears in inverse video in a large font, centered
2103 * over the terminal. You should probably keep the overlay message brief,
2104 * since it's in a large font and you probably aren't going to check the size
2105 * of the terminal first.
2106 *
2107 * @param {string} msg The text (not HTML) message to display in the overlay.
2108 * @param {number} opt_timeout The amount of time to wait before fading out
2109 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2110 * stay up forever (or until the next overlay).
2111 */
2112hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002113 if (!this.overlayNode_) {
2114 if (!this.div_)
2115 return;
2116
2117 this.overlayNode_ = this.document_.createElement('div');
2118 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002119 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002120 'font-size: xx-large;' +
2121 'opacity: 0.75;' +
2122 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2123 'position: absolute;' +
2124 '-webkit-user-select: none;' +
2125 '-webkit-transition: opacity 180ms ease-in;');
2126 }
2127
rginda9f5222b2012-03-05 11:53:28 -08002128 this.overlayNode_.style.color = this.prefs_.get('background-color');
2129 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2130 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2131
rgindaf0090c92012-02-10 14:58:52 -08002132 this.overlayNode_.textContent = msg;
2133 this.overlayNode_.style.opacity = '0.75';
2134
2135 if (!this.overlayNode_.parentNode)
2136 this.div_.appendChild(this.overlayNode_);
2137
2138 this.overlayNode_.style.top = (
2139 this.div_.clientHeight - this.overlayNode_.clientHeight) / 2;
2140 this.overlayNode_.style.left = (
2141 this.div_.clientWidth - this.overlayNode_.clientWidth -
2142 this.scrollbarWidthPx) / 2;
2143
2144 var self = this;
2145
2146 if (this.overlayTimeout_)
2147 clearTimeout(this.overlayTimeout_);
2148
rgindacc2996c2012-02-24 14:59:31 -08002149 if (opt_timeout === null)
2150 return;
2151
rgindaf0090c92012-02-10 14:58:52 -08002152 this.overlayTimeout_ = setTimeout(function() {
2153 self.overlayNode_.style.opacity = '0';
2154 setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002155 if (self.overlayNode_.parentNode)
2156 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002157 self.overlayTimeout_ = null;
2158 self.overlayNode_.style.opacity = '0.75';
2159 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002160 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002161};
2162
rginda4bba5e12012-06-20 16:15:30 -07002163/**
2164 * Paste from the system clipboard to the terminal.
2165 */
2166hterm.Terminal.prototype.paste = function() {
2167 hterm.pasteFromClipboard(this.document_);
2168};
2169
2170/**
2171 * Copy a string to the system clipboard.
2172 *
2173 * Note: If there is a selected range in the terminal, it'll be cleared.
2174 */
2175hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda9fb38222012-09-11 14:19:12 -07002176 if (this.prefs_.get('enable-clipboard-notice'))
2177 setTimeout(this.showOverlay.bind(this, hterm.msg('NOTIFY_COPY'), 500), 200);
rgindaa09e7332012-08-17 12:49:51 -07002178
2179 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002180 copySource.textContent = str;
2181 copySource.style.cssText = (
2182 '-webkit-user-select: text;' +
2183 'position: absolute;' +
2184 'top: -99px');
2185
2186 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002187
rginda4bba5e12012-06-20 16:15:30 -07002188 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002189 var anchorNode = selection.anchorNode;
2190 var anchorOffset = selection.anchorOffset;
2191 var focusNode = selection.focusNode;
2192 var focusOffset = selection.focusOffset;
2193
rginda4bba5e12012-06-20 16:15:30 -07002194 selection.selectAllChildren(copySource);
2195
rgindaa09e7332012-08-17 12:49:51 -07002196 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002197
rgindafaa74742012-08-21 13:34:03 -07002198 selection.collapse(anchorNode, anchorOffset);
2199 selection.extend(focusNode, focusOffset);
2200
rginda4bba5e12012-06-20 16:15:30 -07002201 copySource.parentNode.removeChild(copySource);
2202};
2203
rgindaa09e7332012-08-17 12:49:51 -07002204hterm.Terminal.prototype.getSelectionText = function() {
2205 var selection = this.scrollPort_.selection;
2206 selection.sync();
2207
2208 if (selection.isCollapsed)
2209 return null;
2210
2211
2212 // Start offset measures from the beginning of the line.
2213 var startOffset = selection.startOffset;
2214 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002215
Robert Gindafdbb3f22012-09-06 20:23:06 -07002216 if (node.nodeName != 'X-ROW') {
2217 // If the selection doesn't start on an x-row node, then it must be
2218 // somewhere inside the x-row. Add any characters from previous siblings
2219 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002220
2221 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2222 // If node is the text node in a styled span, move up to the span node.
2223 node = node.parentNode;
2224 }
2225
Robert Gindafdbb3f22012-09-06 20:23:06 -07002226 while (node.previousSibling) {
2227 node = node.previousSibling;
2228 startOffset += node.textContent.length;
2229 }
rgindaa09e7332012-08-17 12:49:51 -07002230 }
2231
2232 // End offset measures from the end of the line.
2233 var endOffset = selection.endNode.textContent.length - selection.endOffset;
2234 var node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002235
Robert Gindafdbb3f22012-09-06 20:23:06 -07002236 if (node.nodeName != 'X-ROW') {
2237 // If the selection doesn't end on an x-row node, then it must be
2238 // somewhere inside the x-row. Add any characters from following siblings
2239 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002240
2241 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2242 // If node is the text node in a styled span, move up to the span node.
2243 node = node.parentNode;
2244 }
2245
Robert Gindafdbb3f22012-09-06 20:23:06 -07002246 while (node.nextSibling) {
2247 node = node.nextSibling;
2248 endOffset += node.textContent.length;
2249 }
rgindaa09e7332012-08-17 12:49:51 -07002250 }
2251
2252 var rv = this.getRowsText(selection.startRow.rowIndex,
2253 selection.endRow.rowIndex + 1);
2254 return rv.substring(startOffset, rv.length - endOffset);
2255};
2256
rginda4bba5e12012-06-20 16:15:30 -07002257/**
2258 * Copy the current selection to the system clipboard, then clear it after a
2259 * short delay.
2260 */
2261hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002262 var text = this.getSelectionText();
2263 if (text != null)
2264 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002265};
2266
rgindaf0090c92012-02-10 14:58:52 -08002267hterm.Terminal.prototype.overlaySize = function() {
2268 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2269};
2270
rginda87b86462011-12-14 13:48:03 -08002271/**
2272 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2273 *
2274 * @param {string} string The VT string representing the keystroke.
2275 */
2276hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002277 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002278 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2279
2280 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08002281};
2282
2283/**
rgindad5613292012-06-19 15:40:37 -07002284 * Add the terminalRow and terminalColumn properties to mouse events and
2285 * then forward on to onMouse().
2286 *
2287 * The terminalRow and terminalColumn properties contain the (row, column)
2288 * coordinates for the mouse event.
2289 */
2290hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002291 if (e.processedByTerminalHandler_) {
2292 // We register our event handlers on the document, as well as the cursor
2293 // and the scroll blocker. Mouse events that occur on the cursor or
2294 // scroll blocker will also appear on the document, but we don't want to
2295 // process them twice.
2296 //
2297 // We can't just prevent bubbling because that has other side effects, so
2298 // we decorate the event object with this property instead.
2299 return;
2300 }
2301
2302 e.processedByTerminalHandler_ = true;
2303
rginda4bba5e12012-06-20 16:15:30 -07002304 if (e.type == 'mousedown' && e.which == this.mousePasteButton) {
2305 this.paste();
2306 return;
2307 }
2308
2309 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2310 !this.document_.getSelection().isCollapsed) {
rgindafaa74742012-08-21 13:34:03 -07002311 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002312 return;
2313 }
2314
rgindad5613292012-06-19 15:40:37 -07002315 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2316 this.scrollPort_.characterSize.height) + 1;
2317 e.terminalColumn = parseInt(e.clientX /
2318 this.scrollPort_.characterSize.width) + 1;
2319
2320 if (e.type == 'mousedown') {
2321 if (e.terminalColumn > this.screenSize.width) {
2322 // Mousedown in the scrollbar area.
2323 return;
2324 }
2325
2326 if (!this.enableMouseDragScroll) {
2327 // Move the scroll-blocker into place if we want to keep the scrollport
2328 // from scrolling.
2329 this.scrollBlockerNode_.engaged = true;
2330 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2331 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2332 }
2333 } else if (this.scrollBlockerNode_.engaged &&
2334 (e.type == 'mousemove' || e.type == 'mouseup')) {
2335 // Disengage the scroll-blocker after one of these events.
2336 this.scrollBlockerNode_.engaged = false;
2337 this.scrollBlockerNode_.style.top = '-99px';
2338 }
2339
rgindafaa74742012-08-21 13:34:03 -07002340 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07002341};
2342
2343/**
2344 * Clients should override this if they care to know about mouse events.
2345 *
2346 * The event parameter will be a normal DOM mouse click event with additional
2347 * 'terminalRow' and 'terminalColumn' properties.
2348 */
2349hterm.Terminal.prototype.onMouse = function(e) { };
2350
2351/**
rginda8e92a692012-05-20 19:37:20 -07002352 * React when focus changes.
2353 */
2354hterm.Terminal.prototype.onFocusChange_ = function(state) {
2355 this.cursorNode_.setAttribute('focus', state ? 'true' : 'false');
2356};
2357
2358/**
rginda8ba33642011-12-14 12:31:31 -08002359 * React when the ScrollPort is scrolled.
2360 */
2361hterm.Terminal.prototype.onScroll_ = function() {
2362 this.scheduleSyncCursorPosition_();
2363};
2364
2365/**
rginda9846e2f2012-01-27 13:53:33 -08002366 * React when text is pasted into the scrollPort.
2367 */
2368hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaf2547f12012-10-25 20:36:21 -07002369 var text = this.vt.encodeUTF8(e.text);
2370 text = text.replace(/\n/mg, '\r');
2371 this.io.onVTKeystroke(text);
rginda9846e2f2012-01-27 13:53:33 -08002372};
2373
2374/**
rgindaa09e7332012-08-17 12:49:51 -07002375 * React when the user tries to copy from the scrollPort.
2376 */
2377hterm.Terminal.prototype.onCopy_ = function(e) {
2378 e.preventDefault();
rgindafaa74742012-08-21 13:34:03 -07002379 this.copySelectionToClipboard();
rgindaa09e7332012-08-17 12:49:51 -07002380};
2381
2382/**
rginda8ba33642011-12-14 12:31:31 -08002383 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002384 *
2385 * Note: This function should not directly contain code that alters the internal
2386 * state of the terminal. That kind of code belongs in realizeWidth or
2387 * realizeHeight, so that it can be executed synchronously in the case of a
2388 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002389 */
2390hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08002391 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002392 this.scrollPort_.characterSize.width);
2393 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
2394 this.scrollPort_.characterSize.height);
2395
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002396 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08002397 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002398 // gets removed from the document or during the initial load, and we can't
2399 // deal with that.
rginda35c456b2012-02-09 17:29:05 -08002400 return;
2401 }
2402
rgindaa8ba17d2012-08-15 14:41:10 -07002403 var isNewSize = (columnCount != this.screenSize.width ||
2404 rowCount != this.screenSize.height);
2405
2406 // We do this even if the size didn't change, just to be sure everything is
2407 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002408 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07002409 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07002410
2411 if (isNewSize)
2412 this.overlaySize();
2413
2414 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08002415};
2416
2417/**
2418 * Service the cursor blink timeout.
2419 */
2420hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08002421 if (this.cursorNode_.style.opacity == '0') {
2422 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002423 } else {
rginda87b86462011-12-14 13:48:03 -08002424 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002425 }
2426};
David Reveman8f552492012-03-28 12:18:41 -04002427
2428/**
2429 * Set the scrollbar-visible mode bit.
2430 *
2431 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2432 * Otherwise it will not.
2433 *
2434 * Defaults to on.
2435 *
2436 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2437 */
2438hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2439 this.scrollPort_.setScrollbarVisible(state);
2440};