blob: daf9e75507b9a06b3b1a4ba533dfcdf7ee48c638 [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) {
514 this.screen_.textAttributes.enableBold = enableBold;
515 return;
516 }
517
rgindaf7521392012-02-28 17:20:34 -0800518 var normalSize = this.scrollPort_.measureCharacterSize();
519 var boldSize = this.scrollPort_.measureCharacterSize('bold');
520
521 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800522 if (!isBoldSafe) {
523 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700524 'from normal. Font family is: ' +
525 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800526 }
rginda9f5222b2012-03-05 11:53:28 -0800527
528 this.screen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800529};
530
531/**
rginda87b86462011-12-14 13:48:03 -0800532 * Return a copy of the current cursor position.
533 *
534 * @return {hterm.RowCol} The RowCol object representing the current position.
535 */
536hterm.Terminal.prototype.saveCursor = function() {
537 return this.screen_.cursorPosition.clone();
538};
539
rgindaa19afe22012-01-25 15:40:22 -0800540hterm.Terminal.prototype.getTextAttributes = function() {
541 return this.screen_.textAttributes;
542};
543
rginda1a09aa02012-06-18 21:11:25 -0700544hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
545 this.screen_.textAttributes = textAttributes;
546};
547
rginda87b86462011-12-14 13:48:03 -0800548/**
rgindaf522ce02012-04-17 17:49:17 -0700549 * Return the current browser zoom factor applied to the terminal.
550 *
551 * @return {number} The current browser zoom factor.
552 */
553hterm.Terminal.prototype.getZoomFactor = function() {
554 return this.scrollPort_.characterSize.zoomFactor;
555};
556
557/**
rginda9846e2f2012-01-27 13:53:33 -0800558 * Change the title of this terminal's window.
559 */
560hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800561 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800562};
563
564/**
rginda87b86462011-12-14 13:48:03 -0800565 * Restore a previously saved cursor position.
566 *
567 * @param {hterm.RowCol} cursor The position to restore.
568 */
569hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700570 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
571 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800572 this.screen_.setCursorPosition(row, column);
573 if (cursor.column > column ||
574 cursor.column == column && cursor.overflow) {
575 this.screen_.cursorPosition.overflow = true;
576 }
rginda87b86462011-12-14 13:48:03 -0800577};
578
579/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400580 * Clear the cursor's overflow flag.
581 */
582hterm.Terminal.prototype.clearCursorOverflow = function() {
583 this.screen_.cursorPosition.overflow = false;
584};
585
586/**
rginda87b86462011-12-14 13:48:03 -0800587 * Set the width of the terminal, resizing the UI to match.
588 */
589hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800590 if (columnCount == null) {
591 this.div_.style.width = '100%';
592 return;
593 }
594
rginda35c456b2012-02-09 17:29:05 -0800595 this.div_.style.width = this.scrollPort_.characterSize.width *
596 columnCount + this.scrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400597 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800598 this.scheduleSyncCursorPosition_();
599};
rginda87b86462011-12-14 13:48:03 -0800600
rgindac9bc5502012-01-18 11:48:44 -0800601/**
rginda35c456b2012-02-09 17:29:05 -0800602 * Set the height of the terminal, resizing the UI to match.
603 */
604hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800605 if (rowCount == null) {
606 this.div_.style.height = '100%';
607 return;
608 }
609
rginda35c456b2012-02-09 17:29:05 -0800610 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700611 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800612 this.realizeSize_(this.screenSize.width, rowCount);
613 this.scheduleSyncCursorPosition_();
614};
615
616/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400617 * Deal with terminal size changes.
618 *
619 */
620hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
621 if (columnCount != this.screenSize.width)
622 this.realizeWidth_(columnCount);
623
624 if (rowCount != this.screenSize.height)
625 this.realizeHeight_(rowCount);
626
627 // Send new terminal size to plugin.
628 this.io.onTerminalResize(columnCount, rowCount);
629};
630
631/**
rgindac9bc5502012-01-18 11:48:44 -0800632 * Deal with terminal width changes.
633 *
634 * This function does what needs to be done when the terminal width changes
635 * out from under us. It happens here rather than in onResize_() because this
636 * code may need to run synchronously to handle programmatic changes of
637 * terminal width.
638 *
639 * Relying on the browser to send us an async resize event means we may not be
640 * in the correct state yet when the next escape sequence hits.
641 */
642hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700643 if (columnCount <= 0)
644 throw new Error('Attempt to realize bad width: ' + columnCount);
645
rgindac9bc5502012-01-18 11:48:44 -0800646 var deltaColumns = columnCount - this.screen_.getWidth();
647
rginda87b86462011-12-14 13:48:03 -0800648 this.screenSize.width = columnCount;
649 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800650
651 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400652 if (this.defaultTabStops)
653 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800654 } else {
655 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400656 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800657 break;
658
659 this.tabStops_.pop();
660 }
661 }
662
663 this.screen_.setColumnCount(this.screenSize.width);
664};
665
666/**
667 * Deal with terminal height changes.
668 *
669 * This function does what needs to be done when the terminal height changes
670 * out from under us. It happens here rather than in onResize_() because this
671 * code may need to run synchronously to handle programmatic changes of
672 * terminal height.
673 *
674 * Relying on the browser to send us an async resize event means we may not be
675 * in the correct state yet when the next escape sequence hits.
676 */
677hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700678 if (rowCount <= 0)
679 throw new Error('Attempt to realize bad height: ' + rowCount);
680
rgindac9bc5502012-01-18 11:48:44 -0800681 var deltaRows = rowCount - this.screen_.getHeight();
682
683 this.screenSize.height = rowCount;
684
685 var cursor = this.saveCursor();
686
687 if (deltaRows < 0) {
688 // Screen got smaller.
689 deltaRows *= -1;
690 while (deltaRows) {
691 var lastRow = this.getRowCount() - 1;
692 if (lastRow - this.scrollbackRows_.length == cursor.row)
693 break;
694
695 if (this.getRowText(lastRow))
696 break;
697
698 this.screen_.popRow();
699 deltaRows--;
700 }
701
702 var ary = this.screen_.shiftRows(deltaRows);
703 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
704
705 // We just removed rows from the top of the screen, we need to update
706 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800707 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800708 } else if (deltaRows > 0) {
709 // Screen got larger.
710
711 if (deltaRows <= this.scrollbackRows_.length) {
712 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
713 var rows = this.scrollbackRows_.splice(
714 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
715 this.screen_.unshiftRows(rows);
716 deltaRows -= scrollbackCount;
717 cursor.row += scrollbackCount;
718 }
719
720 if (deltaRows)
721 this.appendRows_(deltaRows);
722 }
723
rginda35c456b2012-02-09 17:29:05 -0800724 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800725 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800726};
727
728/**
729 * Scroll the terminal to the top of the scrollback buffer.
730 */
731hterm.Terminal.prototype.scrollHome = function() {
732 this.scrollPort_.scrollRowToTop(0);
733};
734
735/**
736 * Scroll the terminal to the end.
737 */
738hterm.Terminal.prototype.scrollEnd = function() {
739 this.scrollPort_.scrollRowToBottom(this.getRowCount());
740};
741
742/**
743 * Scroll the terminal one page up (minus one line) relative to the current
744 * position.
745 */
746hterm.Terminal.prototype.scrollPageUp = function() {
747 var i = this.scrollPort_.getTopRowIndex();
748 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
749};
750
751/**
752 * Scroll the terminal one page down (minus one line) relative to the current
753 * position.
754 */
755hterm.Terminal.prototype.scrollPageDown = function() {
756 var i = this.scrollPort_.getTopRowIndex();
757 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800758};
759
rgindac9bc5502012-01-18 11:48:44 -0800760/**
761 * Full terminal reset.
762 */
rginda87b86462011-12-14 13:48:03 -0800763hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800764 this.clearAllTabStops();
765 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -0700766
767 this.clearHome(this.primaryScreen_);
768 this.primaryScreen_.textAttributes.reset();
769
770 this.clearHome(this.alternateScreen_);
771 this.alternateScreen_.textAttributes.reset();
772
rgindab8bc8932012-04-27 12:45:03 -0700773 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
774
rgindac9bc5502012-01-18 11:48:44 -0800775 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800776};
777
rgindac9bc5502012-01-18 11:48:44 -0800778/**
779 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -0700780 *
781 * Perform a soft reset to the default values listed in
782 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -0800783 */
rginda0f5c0292012-01-13 11:00:13 -0800784hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -0700785 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -0800786 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -0700787
rgindab8bc8932012-04-27 12:45:03 -0700788 // Xterm also resets the color palette on soft reset, even though it doesn't
789 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -0700790 this.primaryScreen_.textAttributes.resetColorPalette();
791 this.alternateScreen_.textAttributes.resetColorPalette();
792
rgindab8bc8932012-04-27 12:45:03 -0700793 // The xterm man page explicitly says this will happen on soft reset.
794 this.setVTScrollRegion(null, null);
795
796 // Xterm also shows the cursor on soft reset, but does not alter the blink
797 // state.
rgindaa19afe22012-01-25 15:40:22 -0800798 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -0800799};
800
rgindac9bc5502012-01-18 11:48:44 -0800801/**
802 * Move the cursor forward to the next tab stop, or to the last column
803 * if no more tab stops are set.
804 */
805hterm.Terminal.prototype.forwardTabStop = function() {
806 var column = this.screen_.cursorPosition.column;
807
808 for (var i = 0; i < this.tabStops_.length; i++) {
809 if (this.tabStops_[i] > column) {
810 this.setCursorColumn(this.tabStops_[i]);
811 return;
812 }
813 }
814
David Benjamin66e954d2012-05-05 21:08:12 -0400815 // xterm does not clear the overflow flag on HT or CHT.
816 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -0800817 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -0400818 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -0800819};
820
rgindac9bc5502012-01-18 11:48:44 -0800821/**
822 * Move the cursor backward to the previous tab stop, or to the first column
823 * if no previous tab stops are set.
824 */
825hterm.Terminal.prototype.backwardTabStop = function() {
826 var column = this.screen_.cursorPosition.column;
827
828 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
829 if (this.tabStops_[i] < column) {
830 this.setCursorColumn(this.tabStops_[i]);
831 return;
832 }
833 }
834
835 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800836};
837
rgindac9bc5502012-01-18 11:48:44 -0800838/**
839 * Set a tab stop at the given column.
840 *
841 * @param {int} column Zero based column.
842 */
843hterm.Terminal.prototype.setTabStop = function(column) {
844 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
845 if (this.tabStops_[i] == column)
846 return;
847
848 if (this.tabStops_[i] < column) {
849 this.tabStops_.splice(i + 1, 0, column);
850 return;
851 }
852 }
853
854 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800855};
856
rgindac9bc5502012-01-18 11:48:44 -0800857/**
858 * Clear the tab stop at the current cursor position.
859 *
860 * No effect if there is no tab stop at the current cursor position.
861 */
862hterm.Terminal.prototype.clearTabStopAtCursor = function() {
863 var column = this.screen_.cursorPosition.column;
864
865 var i = this.tabStops_.indexOf(column);
866 if (i == -1)
867 return;
868
869 this.tabStops_.splice(i, 1);
870};
871
872/**
873 * Clear all tab stops.
874 */
875hterm.Terminal.prototype.clearAllTabStops = function() {
876 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -0400877 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -0800878};
879
880/**
881 * Set up the default tab stops, starting from a given column.
882 *
883 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -0400884 * from the specified column, or 0 if no column is provided. It also flags
885 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -0800886 *
887 * This does not clear the existing tab stops first, use clearAllTabStops
888 * for that.
889 *
890 * @param {int} opt_start Optional starting zero based starting column, useful
891 * for filling out missing tab stops when the terminal is resized.
892 */
893hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
894 var start = opt_start || 0;
895 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -0400896 // Round start up to a default tab stop.
897 start = start - 1 - ((start - 1) % w) + w;
898 for (var i = start; i < this.screenSize.width; i += w) {
899 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -0800900 }
David Benjamin66e954d2012-05-05 21:08:12 -0400901
902 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -0800903};
904
rginda6d397402012-01-17 10:58:29 -0800905/**
rginda8ba33642011-12-14 12:31:31 -0800906 * Interpret a sequence of characters.
907 *
908 * Incomplete escape sequences are buffered until the next call.
909 *
910 * @param {string} str Sequence of characters to interpret or pass through.
911 */
912hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -0800913 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -0800914 this.scheduleSyncCursorPosition_();
915};
916
917/**
918 * Take over the given DIV for use as the terminal display.
919 *
920 * @param {HTMLDivElement} div The div to use as the terminal display.
921 */
922hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -0800923 this.div_ = div;
924
rginda8ba33642011-12-14 12:31:31 -0800925 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -0700926 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -0400927 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
928 this.scrollPort_.setBackgroundPosition(
929 this.prefs_.get('background-position'));
rginda30f20f62012-04-05 16:36:19 -0700930
rginda0918b652012-04-04 11:26:24 -0700931 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -0800932
rginda9f5222b2012-03-05 11:53:28 -0800933 this.setFontSize(this.prefs_.get('font-size'));
934 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -0800935
David Reveman8f552492012-03-28 12:18:41 -0400936 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
937
rginda8ba33642011-12-14 12:31:31 -0800938 this.document_ = this.scrollPort_.getDocument();
939
rginda4bba5e12012-06-20 16:15:30 -0700940 this.document_.body.oncontextmenu = function() { return false };
941
942 var onMouse = this.onMouse_.bind(this);
943 this.document_.body.firstChild.addEventListener('mousedown', onMouse);
944 this.document_.body.firstChild.addEventListener('mouseup', onMouse);
945 this.document_.body.firstChild.addEventListener('mousemove', onMouse);
946 this.scrollPort_.onScrollWheel = onMouse;
947
rginda8e92a692012-05-20 19:37:20 -0700948 this.document_.body.firstChild.addEventListener(
949 'focus', this.onFocusChange_.bind(this, true));
950 this.document_.body.firstChild.addEventListener(
951 'blur', this.onFocusChange_.bind(this, false));
952
953 var style = this.document_.createElement('style');
954 style.textContent =
955 ('.cursor-node[focus="false"] {' +
956 ' box-sizing: border-box;' +
957 ' background-color: transparent !important;' +
958 ' border-width: 2px;' +
959 ' border-style: solid;' +
960 '}');
961 this.document_.head.appendChild(style);
962
rginda8ba33642011-12-14 12:31:31 -0800963 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -0700964 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -0800965 this.cursorNode_.style.cssText =
966 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -0800967 'top: -99px;' +
968 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -0800969 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
970 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda8e92a692012-05-20 19:37:20 -0700971 '-webkit-transition: opacity, background-color 100ms linear;');
972 this.setCursorColor(this.prefs_.get('cursor-color'));
rgindad5613292012-06-19 15:40:37 -0700973
rginda8ba33642011-12-14 12:31:31 -0800974 this.document_.body.appendChild(this.cursorNode_);
975
rgindad5613292012-06-19 15:40:37 -0700976 // When 'enableMouseDragScroll' is off we reposition this element directly
977 // under the mouse cursor after a click. This makes Chrome associate
978 // subsequent mousemove events with the scroll-blocker. Since the
979 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
980 // events do not cause the scrollport to scroll.
981 //
982 // It's a hack, but it's the cleanest way I could find.
983 this.scrollBlockerNode_ = this.document_.createElement('div');
984 this.scrollBlockerNode_.style.cssText =
985 ('position: absolute;' +
986 'top: -99px;' +
987 'display: block;' +
988 'width: 10px;' +
989 'height: 10px;');
990 this.document_.body.appendChild(this.scrollBlockerNode_);
991
992 var onMouse = this.onMouse_.bind(this);
993 this.scrollPort_.onScrollWheel = onMouse;
994 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
995 ].forEach(function(event) {
996 this.scrollBlockerNode_.addEventListener(event, onMouse);
997 this.cursorNode_.addEventListener(event, onMouse);
998 this.document_.addEventListener(event, onMouse);
999 }.bind(this));
1000
1001 this.cursorNode_.addEventListener('mousedown', function() {
1002 setTimeout(this.focus.bind(this));
1003 }.bind(this));
1004
rgindade84e382012-04-20 15:39:31 -07001005 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
rginda8ba33642011-12-14 12:31:31 -08001006 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001007
rginda87b86462011-12-14 13:48:03 -08001008 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001009 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001010};
1011
rginda0918b652012-04-04 11:26:24 -07001012/**
1013 * Return the HTML document that contains the terminal DOM nodes.
1014 */
rginda87b86462011-12-14 13:48:03 -08001015hterm.Terminal.prototype.getDocument = function() {
1016 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001017};
1018
1019/**
rginda0918b652012-04-04 11:26:24 -07001020 * Focus the terminal.
1021 */
1022hterm.Terminal.prototype.focus = function() {
1023 this.scrollPort_.focus();
1024};
1025
1026/**
rginda8ba33642011-12-14 12:31:31 -08001027 * Return the HTML Element for a given row index.
1028 *
1029 * This is a method from the RowProvider interface. The ScrollPort uses
1030 * it to fetch rows on demand as they are scrolled into view.
1031 *
1032 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1033 * pairs to conserve memory.
1034 *
1035 * @param {integer} index The zero-based row index, measured relative to the
1036 * start of the scrollback buffer. On-screen rows will always have the
1037 * largest indicies.
1038 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1039 */
1040hterm.Terminal.prototype.getRowNode = function(index) {
1041 if (index < this.scrollbackRows_.length)
1042 return this.scrollbackRows_[index];
1043
1044 var screenIndex = index - this.scrollbackRows_.length;
1045 return this.screen_.rowsArray[screenIndex];
1046};
1047
1048/**
1049 * Return the text content for a given range of rows.
1050 *
1051 * This is a method from the RowProvider interface. The ScrollPort uses
1052 * it to fetch text content on demand when the user attempts to copy their
1053 * selection to the clipboard.
1054 *
1055 * @param {integer} start The zero-based row index to start from, measured
1056 * relative to the start of the scrollback buffer. On-screen rows will
1057 * always have the largest indicies.
1058 * @param {integer} end The zero-based row index to end on, measured
1059 * relative to the start of the scrollback buffer.
1060 * @return {string} A single string containing the text value of the range of
1061 * rows. Lines will be newline delimited, with no trailing newline.
1062 */
1063hterm.Terminal.prototype.getRowsText = function(start, end) {
1064 var ary = [];
1065 for (var i = start; i < end; i++) {
1066 var node = this.getRowNode(i);
1067 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001068 if (i < end - 1 && !node.getAttribute('line-overflow'))
1069 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001070 }
1071
rgindaa09e7332012-08-17 12:49:51 -07001072 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001073};
1074
1075/**
1076 * Return the text content for a given row.
1077 *
1078 * This is a method from the RowProvider interface. The ScrollPort uses
1079 * it to fetch text content on demand when the user attempts to copy their
1080 * selection to the clipboard.
1081 *
1082 * @param {integer} index The zero-based row index to return, measured
1083 * relative to the start of the scrollback buffer. On-screen rows will
1084 * always have the largest indicies.
1085 * @return {string} A string containing the text value of the selected row.
1086 */
1087hterm.Terminal.prototype.getRowText = function(index) {
1088 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001089 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001090};
1091
1092/**
1093 * Return the total number of rows in the addressable screen and in the
1094 * scrollback buffer of this terminal.
1095 *
1096 * This is a method from the RowProvider interface. The ScrollPort uses
1097 * it to compute the size of the scrollbar.
1098 *
1099 * @return {integer} The number of rows in this terminal.
1100 */
1101hterm.Terminal.prototype.getRowCount = function() {
1102 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1103};
1104
1105/**
1106 * Create DOM nodes for new rows and append them to the end of the terminal.
1107 *
1108 * This is the only correct way to add a new DOM node for a row. Notice that
1109 * the new row is appended to the bottom of the list of rows, and does not
1110 * require renumbering (of the rowIndex property) of previous rows.
1111 *
1112 * If you think you want a new blank row somewhere in the middle of the
1113 * terminal, look into moveRows_().
1114 *
1115 * This method does not pay attention to vtScrollTop/Bottom, since you should
1116 * be using moveRows() in cases where they would matter.
1117 *
1118 * The cursor will be positioned at column 0 of the first inserted line.
1119 */
1120hterm.Terminal.prototype.appendRows_ = function(count) {
1121 var cursorRow = this.screen_.rowsArray.length;
1122 var offset = this.scrollbackRows_.length + cursorRow;
1123 for (var i = 0; i < count; i++) {
1124 var row = this.document_.createElement('x-row');
1125 row.appendChild(this.document_.createTextNode(''));
1126 row.rowIndex = offset + i;
1127 this.screen_.pushRow(row);
1128 }
1129
1130 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1131 if (extraRows > 0) {
1132 var ary = this.screen_.shiftRows(extraRows);
1133 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001134 if (this.scrollPort_.isScrolledEnd)
1135 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001136 }
1137
1138 if (cursorRow >= this.screen_.rowsArray.length)
1139 cursorRow = this.screen_.rowsArray.length - 1;
1140
rginda87b86462011-12-14 13:48:03 -08001141 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001142};
1143
1144/**
1145 * Relocate rows from one part of the addressable screen to another.
1146 *
1147 * This is used to recycle rows during VT scrolls (those which are driven
1148 * by VT commands, rather than by the user manipulating the scrollbar.)
1149 *
1150 * In this case, the blank lines scrolled into the scroll region are made of
1151 * the nodes we scrolled off. These have their rowIndex properties carefully
1152 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001153 */
1154hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1155 var ary = this.screen_.removeRows(fromIndex, count);
1156 this.screen_.insertRows(toIndex, ary);
1157
1158 var start, end;
1159 if (fromIndex < toIndex) {
1160 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001161 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001162 } else {
1163 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001164 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001165 }
1166
1167 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001168 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001169};
1170
1171/**
1172 * Renumber the rowIndex property of the given range of rows.
1173 *
1174 * The start and end indicies are relative to the screen, not the scrollback.
1175 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001176 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001177 * no need to renumber scrollback rows.
1178 */
1179hterm.Terminal.prototype.renumberRows_ = function(start, end) {
1180 var offset = this.scrollbackRows_.length;
1181 for (var i = start; i < end; i++) {
1182 this.screen_.rowsArray[i].rowIndex = offset + i;
1183 }
1184};
1185
1186/**
1187 * Print a string to the terminal.
1188 *
1189 * This respects the current insert and wraparound modes. It will add new lines
1190 * to the end of the terminal, scrolling off the top into the scrollback buffer
1191 * if necessary.
1192 *
1193 * The string is *not* parsed for escape codes. Use the interpret() method if
1194 * that's what you're after.
1195 *
1196 * @param{string} str The string to print.
1197 */
1198hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001199 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001200
rgindaa9abdd82012-08-06 18:05:09 -07001201 while (startOffset < str.length) {
rgindaa09e7332012-08-17 12:49:51 -07001202 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1203 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001204 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001205 }
rgindaa19afe22012-01-25 15:40:22 -08001206
rgindaa9abdd82012-08-06 18:05:09 -07001207 var count = str.length - startOffset;
1208 var didOverflow = false;
1209 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001210
rgindaa9abdd82012-08-06 18:05:09 -07001211 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1212 didOverflow = true;
1213 count = this.screenSize.width - this.screen_.cursorPosition.column;
1214 }
rgindaa19afe22012-01-25 15:40:22 -08001215
rgindaa9abdd82012-08-06 18:05:09 -07001216 if (didOverflow && !this.options_.wraparound) {
1217 // If the string overflowed the line but wraparound is off, then the
1218 // last printed character should be the last of the string.
1219 // TODO: This will add to our problems with multibyte UTF-16 characters.
1220 substr = str.substr(startOffset, count - 1) +
1221 str.substr(str.length - 1);
1222 count = str.length;
1223 } else {
1224 substr = str.substr(startOffset, count);
1225 }
rgindaa19afe22012-01-25 15:40:22 -08001226
rgindaa9abdd82012-08-06 18:05:09 -07001227 if (this.options_.insertMode) {
1228 this.screen_.insertString(substr);
1229 } else {
1230 this.screen_.overwriteString(substr);
1231 }
1232
1233 this.screen_.maybeClipCurrentRow();
1234 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001235 }
rginda8ba33642011-12-14 12:31:31 -08001236
1237 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001238
rginda9f5222b2012-03-05 11:53:28 -08001239 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001240 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001241};
1242
1243/**
rginda87b86462011-12-14 13:48:03 -08001244 * Set the VT scroll region.
1245 *
rginda87b86462011-12-14 13:48:03 -08001246 * This also resets the cursor position to the absolute (0, 0) position, since
1247 * that's what xterm appears to do.
1248 *
1249 * @param {integer} scrollTop The zero-based top of the scroll region.
1250 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1251 * inclusive.
1252 */
1253hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
1254 this.vtScrollTop_ = scrollTop;
1255 this.vtScrollBottom_ = scrollBottom;
rginda87b86462011-12-14 13:48:03 -08001256};
1257
1258/**
rginda8ba33642011-12-14 12:31:31 -08001259 * Return the top row index according to the VT.
1260 *
1261 * This will return 0 unless the terminal has been told to restrict scrolling
1262 * to some lower row. It is used for some VT cursor positioning and scrolling
1263 * commands.
1264 *
1265 * @return {integer} The topmost row in the terminal's scroll region.
1266 */
1267hterm.Terminal.prototype.getVTScrollTop = function() {
1268 if (this.vtScrollTop_ != null)
1269 return this.vtScrollTop_;
1270
1271 return 0;
rginda87b86462011-12-14 13:48:03 -08001272};
rginda8ba33642011-12-14 12:31:31 -08001273
1274/**
1275 * Return the bottom row index according to the VT.
1276 *
1277 * This will return the height of the terminal unless the it has been told to
1278 * restrict scrolling to some higher row. It is used for some VT cursor
1279 * positioning and scrolling commands.
1280 *
1281 * @return {integer} The bottommost row in the terminal's scroll region.
1282 */
1283hterm.Terminal.prototype.getVTScrollBottom = function() {
1284 if (this.vtScrollBottom_ != null)
1285 return this.vtScrollBottom_;
1286
rginda87b86462011-12-14 13:48:03 -08001287 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001288}
1289
1290/**
1291 * Process a '\n' character.
1292 *
1293 * If the cursor is on the final row of the terminal this will append a new
1294 * blank row to the screen and scroll the topmost row into the scrollback
1295 * buffer.
1296 *
1297 * Otherwise, this moves the cursor to column zero of the next row.
1298 */
1299hterm.Terminal.prototype.newLine = function() {
1300 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -08001301 // If we're at the end of the screen we need to append a new line and
1302 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -08001303 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -08001304 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
1305 // End of the scroll region does not affect the scrollback buffer.
1306 this.vtScrollUp(1);
1307 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -08001308 } else {
rginda87b86462011-12-14 13:48:03 -08001309 // Anywhere else in the screen just moves the cursor.
1310 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001311 }
1312};
1313
1314/**
1315 * Like newLine(), except maintain the cursor column.
1316 */
1317hterm.Terminal.prototype.lineFeed = function() {
1318 var column = this.screen_.cursorPosition.column;
1319 this.newLine();
1320 this.setCursorColumn(column);
1321};
1322
1323/**
rginda87b86462011-12-14 13:48:03 -08001324 * If autoCarriageReturn is set then newLine(), else lineFeed().
1325 */
1326hterm.Terminal.prototype.formFeed = function() {
1327 if (this.options_.autoCarriageReturn) {
1328 this.newLine();
1329 } else {
1330 this.lineFeed();
1331 }
1332};
1333
1334/**
1335 * Move the cursor up one row, possibly inserting a blank line.
1336 *
1337 * The cursor column is not changed.
1338 */
1339hterm.Terminal.prototype.reverseLineFeed = function() {
1340 var scrollTop = this.getVTScrollTop();
1341 var currentRow = this.screen_.cursorPosition.row;
1342
1343 if (currentRow == scrollTop) {
1344 this.insertLines(1);
1345 } else {
1346 this.setAbsoluteCursorRow(currentRow - 1);
1347 }
1348};
1349
1350/**
rginda8ba33642011-12-14 12:31:31 -08001351 * Replace all characters to the left of the current cursor with the space
1352 * character.
1353 *
1354 * TODO(rginda): This should probably *remove* the characters (not just replace
1355 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001356 * position.
rginda8ba33642011-12-14 12:31:31 -08001357 */
1358hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001359 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001360 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001361 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001362 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001363};
1364
1365/**
David Benjamin684a9b72012-05-01 17:19:58 -04001366 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001367 *
1368 * The cursor position is unchanged.
1369 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001370 * If the current background color is not the default background color this
1371 * will insert spaces rather than delete. This is unfortunate because the
1372 * trailing space will affect text selection, but it's difficult to come up
1373 * with a way to style empty space that wouldn't trip up the hterm.Screen
1374 * code.
rginda8ba33642011-12-14 12:31:31 -08001375 */
1376hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001377 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1378 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001379
1380 if (this.screen_.textAttributes.background ===
1381 this.screen_.textAttributes.DEFAULT_COLOR) {
1382 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
1383 if (cursorRow.textContent.length <=
1384 this.screen_.cursorPosition.column + count) {
1385 this.screen_.deleteChars(count);
1386 this.clearCursorOverflow();
1387 return;
1388 }
1389 }
1390
rginda87b86462011-12-14 13:48:03 -08001391 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001392 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001393 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001394 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001395};
1396
1397/**
1398 * Erase the current line.
1399 *
1400 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001401 */
1402hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001403 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001404 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001405 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001406 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001407};
1408
1409/**
David Benjamina08d78f2012-05-05 00:28:49 -04001410 * Erase all characters from the start of the screen to the current cursor
1411 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001412 *
1413 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001414 */
1415hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001416 var cursor = this.saveCursor();
1417
1418 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001419
David Benjamina08d78f2012-05-05 00:28:49 -04001420 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001421 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001422 this.screen_.clearCursorRow();
1423 }
1424
rginda87b86462011-12-14 13:48:03 -08001425 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001426 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001427};
1428
1429/**
1430 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001431 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001432 *
1433 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001434 */
1435hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001436 var cursor = this.saveCursor();
1437
1438 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001439
David Benjamina08d78f2012-05-05 00:28:49 -04001440 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001441 for (var i = cursor.row + 1; i <= bottom; i++) {
1442 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001443 this.screen_.clearCursorRow();
1444 }
1445
rginda87b86462011-12-14 13:48:03 -08001446 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001447 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001448};
1449
1450/**
1451 * Fill the terminal with a given character.
1452 *
1453 * This methods does not respect the VT scroll region.
1454 *
1455 * @param {string} ch The character to use for the fill.
1456 */
1457hterm.Terminal.prototype.fill = function(ch) {
1458 var cursor = this.saveCursor();
1459
1460 this.setAbsoluteCursorPosition(0, 0);
1461 for (var row = 0; row < this.screenSize.height; row++) {
1462 for (var col = 0; col < this.screenSize.width; col++) {
1463 this.setAbsoluteCursorPosition(row, col);
1464 this.screen_.overwriteString(ch);
1465 }
1466 }
1467
1468 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001469};
1470
1471/**
rginda9ea433c2012-03-16 11:57:00 -07001472 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001473 *
rginda9ea433c2012-03-16 11:57:00 -07001474 * This does not respect the scroll region.
1475 *
1476 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1477 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001478 */
rginda9ea433c2012-03-16 11:57:00 -07001479hterm.Terminal.prototype.clearHome = function(opt_screen) {
1480 var screen = opt_screen || this.screen_;
1481 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001482
rginda11057d52012-04-25 12:29:56 -07001483 if (bottom == 0) {
1484 // Empty screen, nothing to do.
1485 return;
1486 }
1487
rgindae4d29232012-01-19 10:47:13 -08001488 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001489 screen.setCursorPosition(i, 0);
1490 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001491 }
1492
rginda9ea433c2012-03-16 11:57:00 -07001493 screen.setCursorPosition(0, 0);
1494};
1495
1496/**
1497 * Erase the entire display without changing the cursor position.
1498 *
1499 * The cursor position is unchanged. This does not respect the scroll
1500 * region.
1501 *
1502 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1503 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001504 */
1505hterm.Terminal.prototype.clear = function(opt_screen) {
1506 var screen = opt_screen || this.screen_;
1507 var cursor = screen.cursorPosition.clone();
1508 this.clearHome(screen);
1509 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001510};
1511
1512/**
1513 * VT command to insert lines at the current cursor row.
1514 *
1515 * This respects the current scroll region. Rows pushed off the bottom are
1516 * lost (they won't show up in the scrollback buffer).
1517 *
rginda8ba33642011-12-14 12:31:31 -08001518 * @param {integer} count The number of lines to insert.
1519 */
1520hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07001521 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08001522
1523 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07001524 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08001525
Robert Ginda579186b2012-09-26 11:40:04 -07001526 // The moveCount is the number of rows we need to relocate to make room for
1527 // the new row(s). The count is the distance to move them.
1528 var moveCount = bottom - cursorRow - count + 1;
1529 if (moveCount)
1530 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08001531
Robert Ginda579186b2012-09-26 11:40:04 -07001532 for (var i = count - 1; i >= 0; i--) {
1533 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001534 this.screen_.clearCursorRow();
1535 }
rginda8ba33642011-12-14 12:31:31 -08001536};
1537
1538/**
1539 * VT command to delete lines at the current cursor row.
1540 *
1541 * New rows are added to the bottom of scroll region to take their place. New
1542 * rows are strictly there to take up space and have no content or style.
1543 */
1544hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001545 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001546
rginda87b86462011-12-14 13:48:03 -08001547 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001548 var bottom = this.getVTScrollBottom();
1549
rginda87b86462011-12-14 13:48:03 -08001550 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001551 count = Math.min(count, maxCount);
1552
rginda87b86462011-12-14 13:48:03 -08001553 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001554 if (count != maxCount)
1555 this.moveRows_(top, count, moveStart);
1556
1557 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001558 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001559 this.screen_.clearCursorRow();
1560 }
1561
rginda87b86462011-12-14 13:48:03 -08001562 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001563 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001564};
1565
1566/**
1567 * Inserts the given number of spaces at the current cursor position.
1568 *
rginda87b86462011-12-14 13:48:03 -08001569 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001570 */
1571hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001572 var cursor = this.saveCursor();
1573
rgindacbbd7482012-06-13 15:06:16 -07001574 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001575 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001576 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001577
1578 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001579 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001580};
1581
1582/**
1583 * Forward-delete the specified number of characters starting at the cursor
1584 * position.
1585 *
1586 * @param {integer} count The number of characters to delete.
1587 */
1588hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001589 var deleted = this.screen_.deleteChars(count);
1590 if (deleted && !this.screen_.textAttributes.isDefault()) {
1591 var cursor = this.saveCursor();
1592 this.setCursorColumn(this.screenSize.width - deleted);
1593 this.screen_.insertString(lib.f.getWhitespace(deleted));
1594 this.restoreCursor(cursor);
1595 }
1596
David Benjamin54e8bf62012-06-01 22:31:40 -04001597 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001598};
1599
1600/**
1601 * Shift rows in the scroll region upwards by a given number of lines.
1602 *
1603 * New rows are inserted at the bottom of the scroll region to fill the
1604 * vacated rows. The new rows not filled out with the current text attributes.
1605 *
1606 * This function does not affect the scrollback rows at all. Rows shifted
1607 * off the top are lost.
1608 *
rginda87b86462011-12-14 13:48:03 -08001609 * The cursor position is not altered.
1610 *
rginda8ba33642011-12-14 12:31:31 -08001611 * @param {integer} count The number of rows to scroll.
1612 */
1613hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001614 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001615
rginda87b86462011-12-14 13:48:03 -08001616 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001617 this.deleteLines(count);
1618
rginda87b86462011-12-14 13:48:03 -08001619 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001620};
1621
1622/**
1623 * Shift rows below the cursor down by a given number of lines.
1624 *
1625 * This function respects the current scroll region.
1626 *
1627 * New rows are inserted at the top of the scroll region to fill the
1628 * vacated rows. The new rows not filled out with the current text attributes.
1629 *
1630 * This function does not affect the scrollback rows at all. Rows shifted
1631 * off the bottom are lost.
1632 *
1633 * @param {integer} count The number of rows to scroll.
1634 */
1635hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001636 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001637
rginda87b86462011-12-14 13:48:03 -08001638 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001639 this.insertLines(opt_count);
1640
rginda87b86462011-12-14 13:48:03 -08001641 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001642};
1643
rginda87b86462011-12-14 13:48:03 -08001644
rginda8ba33642011-12-14 12:31:31 -08001645/**
1646 * Set the cursor position.
1647 *
1648 * The cursor row is relative to the scroll region if the terminal has
1649 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1650 *
1651 * @param {integer} row The new zero-based cursor row.
1652 * @param {integer} row The new zero-based cursor column.
1653 */
1654hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1655 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001656 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001657 } else {
rginda87b86462011-12-14 13:48:03 -08001658 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001659 }
rginda87b86462011-12-14 13:48:03 -08001660};
rginda8ba33642011-12-14 12:31:31 -08001661
rginda87b86462011-12-14 13:48:03 -08001662hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1663 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07001664 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
1665 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001666 this.screen_.setCursorPosition(row, column);
1667};
1668
1669hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07001670 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
1671 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001672 this.screen_.setCursorPosition(row, column);
1673};
1674
1675/**
1676 * Set the cursor column.
1677 *
1678 * @param {integer} column The new zero-based cursor column.
1679 */
1680hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001681 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001682};
1683
1684/**
1685 * Return the cursor column.
1686 *
1687 * @return {integer} The zero-based cursor column.
1688 */
1689hterm.Terminal.prototype.getCursorColumn = function() {
1690 return this.screen_.cursorPosition.column;
1691};
1692
1693/**
1694 * Set the cursor row.
1695 *
1696 * The cursor row is relative to the scroll region if the terminal has
1697 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1698 *
1699 * @param {integer} row The new cursor row.
1700 */
rginda87b86462011-12-14 13:48:03 -08001701hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1702 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001703};
1704
1705/**
1706 * Return the cursor row.
1707 *
1708 * @return {integer} The zero-based cursor row.
1709 */
1710hterm.Terminal.prototype.getCursorRow = function(row) {
1711 return this.screen_.cursorPosition.row;
1712};
1713
1714/**
1715 * Request that the ScrollPort redraw itself soon.
1716 *
1717 * The redraw will happen asynchronously, soon after the call stack winds down.
1718 * Multiple calls will be coalesced into a single redraw.
1719 */
1720hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001721 if (this.timeouts_.redraw)
1722 return;
rginda8ba33642011-12-14 12:31:31 -08001723
1724 var self = this;
rginda87b86462011-12-14 13:48:03 -08001725 this.timeouts_.redraw = setTimeout(function() {
1726 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001727 self.scrollPort_.redraw_();
1728 }, 0);
1729};
1730
1731/**
1732 * Request that the ScrollPort be scrolled to the bottom.
1733 *
1734 * The scroll will happen asynchronously, soon after the call stack winds down.
1735 * Multiple calls will be coalesced into a single scroll.
1736 *
1737 * This affects the scrollbar position of the ScrollPort, and has nothing to
1738 * do with the VT scroll commands.
1739 */
1740hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1741 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001742 return;
rginda8ba33642011-12-14 12:31:31 -08001743
1744 var self = this;
1745 this.timeouts_.scrollDown = setTimeout(function() {
1746 delete self.timeouts_.scrollDown;
1747 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1748 }, 10);
1749};
1750
1751/**
1752 * Move the cursor up a specified number of rows.
1753 *
1754 * @param {integer} count The number of rows to move the cursor.
1755 */
1756hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001757 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001758};
1759
1760/**
1761 * Move the cursor down a specified number of rows.
1762 *
1763 * @param {integer} count The number of rows to move the cursor.
1764 */
1765hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001766 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001767 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1768 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1769 this.screenSize.height - 1);
1770
rgindacbbd7482012-06-13 15:06:16 -07001771 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08001772 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001773 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001774};
1775
1776/**
1777 * Move the cursor left a specified number of columns.
1778 *
1779 * @param {integer} count The number of columns to move the cursor.
1780 */
1781hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001782 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001783};
1784
1785/**
1786 * Move the cursor right a specified number of columns.
1787 *
1788 * @param {integer} count The number of columns to move the cursor.
1789 */
1790hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001791 count = count || 1;
rgindacbbd7482012-06-13 15:06:16 -07001792 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001793 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001794 this.setCursorColumn(column);
1795};
1796
1797/**
1798 * Reverse the foreground and background colors of the terminal.
1799 *
1800 * This only affects text that was drawn with no attributes.
1801 *
1802 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1803 * been drawn with attributes that happen to coincide with the default
1804 * 'no-attribute' colors. My guess is probably not.
1805 */
1806hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001807 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001808 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08001809 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
1810 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08001811 } else {
rginda9f5222b2012-03-05 11:53:28 -08001812 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
1813 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08001814 }
1815};
1816
1817/**
rginda87b86462011-12-14 13:48:03 -08001818 * Ring the terminal bell.
rginda87b86462011-12-14 13:48:03 -08001819 */
1820hterm.Terminal.prototype.ringBell = function() {
rginda9f5222b2012-03-05 11:53:28 -08001821 if (this.bellAudio_.getAttribute('src'))
1822 this.bellAudio_.play();
rgindaf0090c92012-02-10 14:58:52 -08001823
rginda6d397402012-01-17 10:58:29 -08001824 this.cursorNode_.style.backgroundColor =
1825 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001826
1827 var self = this;
1828 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08001829 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08001830 }, 200);
rginda87b86462011-12-14 13:48:03 -08001831};
1832
1833/**
rginda8ba33642011-12-14 12:31:31 -08001834 * Set the origin mode bit.
1835 *
1836 * If origin mode is on, certain VT cursor and scrolling commands measure their
1837 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1838 * to the top of the addressable screen.
1839 *
1840 * Defaults to off.
1841 *
1842 * @param {boolean} state True to set origin mode, false to unset.
1843 */
1844hterm.Terminal.prototype.setOriginMode = function(state) {
1845 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08001846 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08001847};
1848
1849/**
1850 * Set the insert mode bit.
1851 *
1852 * If insert mode is on, existing text beyond the cursor position will be
1853 * shifted right to make room for new text. Otherwise, new text overwrites
1854 * any existing text.
1855 *
1856 * Defaults to off.
1857 *
1858 * @param {boolean} state True to set insert mode, false to unset.
1859 */
1860hterm.Terminal.prototype.setInsertMode = function(state) {
1861 this.options_.insertMode = state;
1862};
1863
1864/**
rginda87b86462011-12-14 13:48:03 -08001865 * Set the auto carriage return bit.
1866 *
1867 * If auto carriage return is on then a formfeed character is interpreted
1868 * as a newline, otherwise it's the same as a linefeed. The difference boils
1869 * down to whether or not the cursor column is reset.
1870 */
1871hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1872 this.options_.autoCarriageReturn = state;
1873};
1874
1875/**
rginda8ba33642011-12-14 12:31:31 -08001876 * Set the wraparound mode bit.
1877 *
1878 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1879 * to the start of the following row. Otherwise, the cursor is clamped to the
1880 * end of the screen and attempts to write past it are ignored.
1881 *
1882 * Defaults to on.
1883 *
1884 * @param {boolean} state True to set wraparound mode, false to unset.
1885 */
1886hterm.Terminal.prototype.setWraparound = function(state) {
1887 this.options_.wraparound = state;
1888};
1889
1890/**
1891 * Set the reverse-wraparound mode bit.
1892 *
1893 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1894 * to the end of the previous row. Otherwise, the cursor is clamped to column
1895 * 0.
1896 *
1897 * Defaults to off.
1898 *
1899 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
1900 */
1901hterm.Terminal.prototype.setReverseWraparound = function(state) {
1902 this.options_.reverseWraparound = state;
1903};
1904
1905/**
1906 * Selects between the primary and alternate screens.
1907 *
1908 * If alternate mode is on, the alternate screen is active. Otherwise the
1909 * primary screen is active.
1910 *
1911 * Swapping screens has no effect on the scrollback buffer.
1912 *
1913 * Each screen maintains its own cursor position.
1914 *
1915 * Defaults to off.
1916 *
1917 * @param {boolean} state True to set alternate mode, false to unset.
1918 */
1919hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08001920 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001921 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
1922
rginda35c456b2012-02-09 17:29:05 -08001923 if (this.screen_.rowsArray.length &&
1924 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
1925 // If the screen changed sizes while we were away, our rowIndexes may
1926 // be incorrect.
1927 var offset = this.scrollbackRows_.length;
1928 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07001929 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08001930 ary[i].rowIndex = offset + i;
1931 }
1932 }
rginda8ba33642011-12-14 12:31:31 -08001933
rginda35c456b2012-02-09 17:29:05 -08001934 this.realizeWidth_(this.screenSize.width);
1935 this.realizeHeight_(this.screenSize.height);
1936 this.scrollPort_.syncScrollHeight();
1937 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08001938
rginda6d397402012-01-17 10:58:29 -08001939 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08001940 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08001941};
1942
1943/**
1944 * Set the cursor-blink mode bit.
1945 *
1946 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
1947 * a visible cursor does not blink.
1948 *
1949 * You should make sure to turn blinking off if you're going to dispose of a
1950 * terminal, otherwise you'll leak a timeout.
1951 *
1952 * Defaults to on.
1953 *
1954 * @param {boolean} state True to set cursor-blink mode, false to unset.
1955 */
1956hterm.Terminal.prototype.setCursorBlink = function(state) {
1957 this.options_.cursorBlink = state;
1958
1959 if (!state && this.timeouts_.cursorBlink) {
1960 clearTimeout(this.timeouts_.cursorBlink);
1961 delete this.timeouts_.cursorBlink;
1962 }
1963
1964 if (this.options_.cursorVisible)
1965 this.setCursorVisible(true);
1966};
1967
1968/**
1969 * Set the cursor-visible mode bit.
1970 *
1971 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
1972 *
1973 * Defaults to on.
1974 *
1975 * @param {boolean} state True to set cursor-visible mode, false to unset.
1976 */
1977hterm.Terminal.prototype.setCursorVisible = function(state) {
1978 this.options_.cursorVisible = state;
1979
1980 if (!state) {
rginda87b86462011-12-14 13:48:03 -08001981 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08001982 return;
1983 }
1984
rginda87b86462011-12-14 13:48:03 -08001985 this.syncCursorPosition_();
1986
1987 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08001988
1989 if (this.options_.cursorBlink) {
1990 if (this.timeouts_.cursorBlink)
1991 return;
1992
1993 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
1994 500);
1995 } else {
1996 if (this.timeouts_.cursorBlink) {
1997 clearTimeout(this.timeouts_.cursorBlink);
1998 delete this.timeouts_.cursorBlink;
1999 }
2000 }
2001};
2002
2003/**
rginda87b86462011-12-14 13:48:03 -08002004 * Synchronizes the visible cursor and document selection with the current
2005 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002006 */
2007hterm.Terminal.prototype.syncCursorPosition_ = function() {
2008 var topRowIndex = this.scrollPort_.getTopRowIndex();
2009 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2010 var cursorRowIndex = this.scrollbackRows_.length +
2011 this.screen_.cursorPosition.row;
2012
2013 if (cursorRowIndex > bottomRowIndex) {
2014 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002015 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002016 return;
2017 }
2018
rginda35c456b2012-02-09 17:29:05 -08002019 this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px';
2020 this.cursorNode_.style.height = this.scrollPort_.characterSize.height + 'px';
2021
rginda8ba33642011-12-14 12:31:31 -08002022 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002023 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2024 'px';
2025 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2026 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002027
2028 this.cursorNode_.setAttribute('title',
2029 '(' + this.screen_.cursorPosition.row +
2030 ', ' + this.screen_.cursorPosition.column +
2031 ')');
2032
2033 // Update the caret for a11y purposes.
2034 var selection = this.document_.getSelection();
2035 if (selection && selection.isCollapsed)
2036 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002037};
2038
2039/**
2040 * Synchronizes the visible cursor with the current cursor coordinates.
2041 *
2042 * The sync will happen asynchronously, soon after the call stack winds down.
2043 * Multiple calls will be coalesced into a single sync.
2044 */
2045hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2046 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002047 return;
rginda8ba33642011-12-14 12:31:31 -08002048
2049 var self = this;
2050 this.timeouts_.syncCursor = setTimeout(function() {
2051 self.syncCursorPosition_();
2052 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002053 }, 0);
2054};
2055
rgindacc2996c2012-02-24 14:59:31 -08002056/**
rgindaf522ce02012-04-17 17:49:17 -07002057 * Show or hide the zoom warning.
2058 *
2059 * The zoom warning is a message warning the user that their browser zoom must
2060 * be set to 100% in order for hterm to function properly.
2061 *
2062 * @param {boolean} state True to show the message, false to hide it.
2063 */
2064hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2065 if (!this.zoomWarningNode_) {
2066 if (!state)
2067 return;
2068
2069 this.zoomWarningNode_ = this.document_.createElement('div');
2070 this.zoomWarningNode_.style.cssText = (
2071 'color: black;' +
2072 'background-color: #ff2222;' +
2073 'font-size: large;' +
2074 'border-radius: 8px;' +
2075 'opacity: 0.75;' +
2076 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2077 'top: 0.5em;' +
2078 'right: 1.2em;' +
2079 'position: absolute;' +
2080 '-webkit-text-size-adjust: none;' +
2081 '-webkit-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002082 }
2083
rgindade84e382012-04-20 15:39:31 -07002084 this.zoomWarningNode_.textContent = hterm.msg('ZOOM_WARNING') ||
2085 ('!! ' + parseInt(this.scrollPort_.characterSize.zoomFactor * 100) +
2086 '% !!');
rgindaf522ce02012-04-17 17:49:17 -07002087 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2088
2089 if (state) {
2090 if (!this.zoomWarningNode_.parentNode)
2091 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2092 } else if (this.zoomWarningNode_.parentNode) {
2093 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2094 }
2095};
2096
2097/**
rgindacc2996c2012-02-24 14:59:31 -08002098 * Show the terminal overlay for a given amount of time.
2099 *
2100 * The terminal overlay appears in inverse video in a large font, centered
2101 * over the terminal. You should probably keep the overlay message brief,
2102 * since it's in a large font and you probably aren't going to check the size
2103 * of the terminal first.
2104 *
2105 * @param {string} msg The text (not HTML) message to display in the overlay.
2106 * @param {number} opt_timeout The amount of time to wait before fading out
2107 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2108 * stay up forever (or until the next overlay).
2109 */
2110hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002111 if (!this.overlayNode_) {
2112 if (!this.div_)
2113 return;
2114
2115 this.overlayNode_ = this.document_.createElement('div');
2116 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002117 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002118 'font-size: xx-large;' +
2119 'opacity: 0.75;' +
2120 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2121 'position: absolute;' +
2122 '-webkit-user-select: none;' +
2123 '-webkit-transition: opacity 180ms ease-in;');
2124 }
2125
rginda9f5222b2012-03-05 11:53:28 -08002126 this.overlayNode_.style.color = this.prefs_.get('background-color');
2127 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2128 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2129
rgindaf0090c92012-02-10 14:58:52 -08002130 this.overlayNode_.textContent = msg;
2131 this.overlayNode_.style.opacity = '0.75';
2132
2133 if (!this.overlayNode_.parentNode)
2134 this.div_.appendChild(this.overlayNode_);
2135
2136 this.overlayNode_.style.top = (
2137 this.div_.clientHeight - this.overlayNode_.clientHeight) / 2;
2138 this.overlayNode_.style.left = (
2139 this.div_.clientWidth - this.overlayNode_.clientWidth -
2140 this.scrollbarWidthPx) / 2;
2141
2142 var self = this;
2143
2144 if (this.overlayTimeout_)
2145 clearTimeout(this.overlayTimeout_);
2146
rgindacc2996c2012-02-24 14:59:31 -08002147 if (opt_timeout === null)
2148 return;
2149
rgindaf0090c92012-02-10 14:58:52 -08002150 this.overlayTimeout_ = setTimeout(function() {
2151 self.overlayNode_.style.opacity = '0';
2152 setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002153 if (self.overlayNode_.parentNode)
2154 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002155 self.overlayTimeout_ = null;
2156 self.overlayNode_.style.opacity = '0.75';
2157 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002158 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002159};
2160
rginda4bba5e12012-06-20 16:15:30 -07002161/**
2162 * Paste from the system clipboard to the terminal.
2163 */
2164hterm.Terminal.prototype.paste = function() {
2165 hterm.pasteFromClipboard(this.document_);
2166};
2167
2168/**
2169 * Copy a string to the system clipboard.
2170 *
2171 * Note: If there is a selected range in the terminal, it'll be cleared.
2172 */
2173hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda9fb38222012-09-11 14:19:12 -07002174 if (this.prefs_.get('enable-clipboard-notice'))
2175 setTimeout(this.showOverlay.bind(this, hterm.msg('NOTIFY_COPY'), 500), 200);
rgindaa09e7332012-08-17 12:49:51 -07002176
2177 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002178 copySource.textContent = str;
2179 copySource.style.cssText = (
2180 '-webkit-user-select: text;' +
2181 'position: absolute;' +
2182 'top: -99px');
2183
2184 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002185
rginda4bba5e12012-06-20 16:15:30 -07002186 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002187 var anchorNode = selection.anchorNode;
2188 var anchorOffset = selection.anchorOffset;
2189 var focusNode = selection.focusNode;
2190 var focusOffset = selection.focusOffset;
2191
rginda4bba5e12012-06-20 16:15:30 -07002192 selection.selectAllChildren(copySource);
2193
rgindaa09e7332012-08-17 12:49:51 -07002194 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002195
rgindafaa74742012-08-21 13:34:03 -07002196 selection.collapse(anchorNode, anchorOffset);
2197 selection.extend(focusNode, focusOffset);
2198
rginda4bba5e12012-06-20 16:15:30 -07002199 copySource.parentNode.removeChild(copySource);
2200};
2201
rgindaa09e7332012-08-17 12:49:51 -07002202hterm.Terminal.prototype.getSelectionText = function() {
2203 var selection = this.scrollPort_.selection;
2204 selection.sync();
2205
2206 if (selection.isCollapsed)
2207 return null;
2208
2209
2210 // Start offset measures from the beginning of the line.
2211 var startOffset = selection.startOffset;
2212 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002213
Robert Gindafdbb3f22012-09-06 20:23:06 -07002214 if (node.nodeName != 'X-ROW') {
2215 // If the selection doesn't start on an x-row node, then it must be
2216 // somewhere inside the x-row. Add any characters from previous siblings
2217 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002218
2219 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2220 // If node is the text node in a styled span, move up to the span node.
2221 node = node.parentNode;
2222 }
2223
Robert Gindafdbb3f22012-09-06 20:23:06 -07002224 while (node.previousSibling) {
2225 node = node.previousSibling;
2226 startOffset += node.textContent.length;
2227 }
rgindaa09e7332012-08-17 12:49:51 -07002228 }
2229
2230 // End offset measures from the end of the line.
2231 var endOffset = selection.endNode.textContent.length - selection.endOffset;
2232 var node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002233
Robert Gindafdbb3f22012-09-06 20:23:06 -07002234 if (node.nodeName != 'X-ROW') {
2235 // If the selection doesn't end on an x-row node, then it must be
2236 // somewhere inside the x-row. Add any characters from following siblings
2237 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002238
2239 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2240 // If node is the text node in a styled span, move up to the span node.
2241 node = node.parentNode;
2242 }
2243
Robert Gindafdbb3f22012-09-06 20:23:06 -07002244 while (node.nextSibling) {
2245 node = node.nextSibling;
2246 endOffset += node.textContent.length;
2247 }
rgindaa09e7332012-08-17 12:49:51 -07002248 }
2249
2250 var rv = this.getRowsText(selection.startRow.rowIndex,
2251 selection.endRow.rowIndex + 1);
2252 return rv.substring(startOffset, rv.length - endOffset);
2253};
2254
rginda4bba5e12012-06-20 16:15:30 -07002255/**
2256 * Copy the current selection to the system clipboard, then clear it after a
2257 * short delay.
2258 */
2259hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002260 var text = this.getSelectionText();
2261 if (text != null)
2262 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002263};
2264
rgindaf0090c92012-02-10 14:58:52 -08002265hterm.Terminal.prototype.overlaySize = function() {
2266 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2267};
2268
rginda87b86462011-12-14 13:48:03 -08002269/**
2270 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2271 *
2272 * @param {string} string The VT string representing the keystroke.
2273 */
2274hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002275 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002276 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2277
2278 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08002279};
2280
2281/**
rgindad5613292012-06-19 15:40:37 -07002282 * Add the terminalRow and terminalColumn properties to mouse events and
2283 * then forward on to onMouse().
2284 *
2285 * The terminalRow and terminalColumn properties contain the (row, column)
2286 * coordinates for the mouse event.
2287 */
2288hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002289 if (e.processedByTerminalHandler_) {
2290 // We register our event handlers on the document, as well as the cursor
2291 // and the scroll blocker. Mouse events that occur on the cursor or
2292 // scroll blocker will also appear on the document, but we don't want to
2293 // process them twice.
2294 //
2295 // We can't just prevent bubbling because that has other side effects, so
2296 // we decorate the event object with this property instead.
2297 return;
2298 }
2299
2300 e.processedByTerminalHandler_ = true;
2301
rginda4bba5e12012-06-20 16:15:30 -07002302 if (e.type == 'mousedown' && e.which == this.mousePasteButton) {
2303 this.paste();
2304 return;
2305 }
2306
2307 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2308 !this.document_.getSelection().isCollapsed) {
rgindafaa74742012-08-21 13:34:03 -07002309 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002310 return;
2311 }
2312
rgindad5613292012-06-19 15:40:37 -07002313 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2314 this.scrollPort_.characterSize.height) + 1;
2315 e.terminalColumn = parseInt(e.clientX /
2316 this.scrollPort_.characterSize.width) + 1;
2317
2318 if (e.type == 'mousedown') {
2319 if (e.terminalColumn > this.screenSize.width) {
2320 // Mousedown in the scrollbar area.
2321 return;
2322 }
2323
2324 if (!this.enableMouseDragScroll) {
2325 // Move the scroll-blocker into place if we want to keep the scrollport
2326 // from scrolling.
2327 this.scrollBlockerNode_.engaged = true;
2328 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2329 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2330 }
2331 } else if (this.scrollBlockerNode_.engaged &&
2332 (e.type == 'mousemove' || e.type == 'mouseup')) {
2333 // Disengage the scroll-blocker after one of these events.
2334 this.scrollBlockerNode_.engaged = false;
2335 this.scrollBlockerNode_.style.top = '-99px';
2336 }
2337
rgindafaa74742012-08-21 13:34:03 -07002338 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07002339};
2340
2341/**
2342 * Clients should override this if they care to know about mouse events.
2343 *
2344 * The event parameter will be a normal DOM mouse click event with additional
2345 * 'terminalRow' and 'terminalColumn' properties.
2346 */
2347hterm.Terminal.prototype.onMouse = function(e) { };
2348
2349/**
rginda8e92a692012-05-20 19:37:20 -07002350 * React when focus changes.
2351 */
2352hterm.Terminal.prototype.onFocusChange_ = function(state) {
2353 this.cursorNode_.setAttribute('focus', state ? 'true' : 'false');
2354};
2355
2356/**
rginda8ba33642011-12-14 12:31:31 -08002357 * React when the ScrollPort is scrolled.
2358 */
2359hterm.Terminal.prototype.onScroll_ = function() {
2360 this.scheduleSyncCursorPosition_();
2361};
2362
2363/**
rginda9846e2f2012-01-27 13:53:33 -08002364 * React when text is pasted into the scrollPort.
2365 */
2366hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaf2547f12012-10-25 20:36:21 -07002367 var text = this.vt.encodeUTF8(e.text);
2368 text = text.replace(/\n/mg, '\r');
2369 this.io.onVTKeystroke(text);
rginda9846e2f2012-01-27 13:53:33 -08002370};
2371
2372/**
rgindaa09e7332012-08-17 12:49:51 -07002373 * React when the user tries to copy from the scrollPort.
2374 */
2375hterm.Terminal.prototype.onCopy_ = function(e) {
2376 e.preventDefault();
rgindafaa74742012-08-21 13:34:03 -07002377 this.copySelectionToClipboard();
rgindaa09e7332012-08-17 12:49:51 -07002378};
2379
2380/**
rginda8ba33642011-12-14 12:31:31 -08002381 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002382 *
2383 * Note: This function should not directly contain code that alters the internal
2384 * state of the terminal. That kind of code belongs in realizeWidth or
2385 * realizeHeight, so that it can be executed synchronously in the case of a
2386 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002387 */
2388hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08002389 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002390 this.scrollPort_.characterSize.width);
2391 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
2392 this.scrollPort_.characterSize.height);
2393
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002394 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08002395 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002396 // gets removed from the document or during the initial load, and we can't
2397 // deal with that.
rginda35c456b2012-02-09 17:29:05 -08002398 return;
2399 }
2400
rgindaa8ba17d2012-08-15 14:41:10 -07002401 var isNewSize = (columnCount != this.screenSize.width ||
2402 rowCount != this.screenSize.height);
2403
2404 // We do this even if the size didn't change, just to be sure everything is
2405 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002406 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07002407 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07002408
2409 if (isNewSize)
2410 this.overlaySize();
2411
2412 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08002413};
2414
2415/**
2416 * Service the cursor blink timeout.
2417 */
2418hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08002419 if (this.cursorNode_.style.opacity == '0') {
2420 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002421 } else {
rginda87b86462011-12-14 13:48:03 -08002422 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002423 }
2424};
David Reveman8f552492012-03-28 12:18:41 -04002425
2426/**
2427 * Set the scrollbar-visible mode bit.
2428 *
2429 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2430 * Otherwise it will not.
2431 *
2432 * Defaults to on.
2433 *
2434 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2435 */
2436hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2437 this.scrollPort_.setScrollbarVisible(state);
2438};