blob: 0db6e05e71d66f60b6e597f397996a3eff046fce [file] [log] [blame]
rginda87b86462011-12-14 13:48:03 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
rginda8ba33642011-12-14 12:31:31 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rgindacbbd7482012-06-13 15:06:16 -07005'use strict';
6
Robert Gindab4839c22013-02-28 16:52:10 -08007lib.rtdep('lib.colors', 'lib.PreferenceManager', 'lib.resource',
Robert Ginda57f03b42012-09-13 11:02:48 -07008 'hterm.Keyboard', 'hterm.Options', 'hterm.PreferenceManager',
9 'hterm.Screen', 'hterm.ScrollPort', 'hterm.Size', 'hterm.VT');
rgindacbbd7482012-06-13 15:06:16 -070010
rginda8ba33642011-12-14 12:31:31 -080011/**
12 * Constructor for the Terminal class.
13 *
14 * A Terminal pulls together the hterm.ScrollPort, hterm.Screen and hterm.VT100
15 * classes to provide the complete terminal functionality.
16 *
17 * There are a number of lower-level Terminal methods that can be called
18 * directly to manipulate the cursor, text, scroll region, and other terminal
19 * attributes. However, the primary method is interpret(), which parses VT
20 * escape sequences and invokes the appropriate Terminal methods.
21 *
22 * This class was heavily influenced by Cory Maccarrone's Framebuffer class.
23 *
24 * TODO(rginda): Eventually we're going to need to support characters which are
25 * displayed twice as wide as standard latin characters. This is to support
26 * CJK (and possibly other character sets).
rginda9f5222b2012-03-05 11:53:28 -080027 *
Robert Ginda57f03b42012-09-13 11:02:48 -070028 * @param {string} opt_profileId Optional preference profile name. If not
rginda9f5222b2012-03-05 11:53:28 -080029 * provided, defaults to 'default'.
rginda8ba33642011-12-14 12:31:31 -080030 */
Robert Ginda57f03b42012-09-13 11:02:48 -070031hterm.Terminal = function(opt_profileId) {
32 this.profileId_ = null;
rginda9f5222b2012-03-05 11:53:28 -080033
rginda8ba33642011-12-14 12:31:31 -080034 // Two screen instances.
35 this.primaryScreen_ = new hterm.Screen();
36 this.alternateScreen_ = new hterm.Screen();
37
38 // The "current" screen.
39 this.screen_ = this.primaryScreen_;
40
rginda8ba33642011-12-14 12:31:31 -080041 // The local notion of the screen size. ScreenBuffers also have a size which
42 // indicates their present size. During size changes, the two may disagree.
43 // Also, the inactive screen's size is not altered until it is made the active
44 // screen.
45 this.screenSize = new hterm.Size(0, 0);
46
rginda8ba33642011-12-14 12:31:31 -080047 // The scroll port we'll be using to display the visible rows.
rginda35c456b2012-02-09 17:29:05 -080048 this.scrollPort_ = new hterm.ScrollPort(this);
rginda8ba33642011-12-14 12:31:31 -080049 this.scrollPort_.subscribe('resize', this.onResize_.bind(this));
50 this.scrollPort_.subscribe('scroll', this.onScroll_.bind(this));
rginda9846e2f2012-01-27 13:53:33 -080051 this.scrollPort_.subscribe('paste', this.onPaste_.bind(this));
rgindaa09e7332012-08-17 12:49:51 -070052 this.scrollPort_.onCopy = this.onCopy_.bind(this);
rginda8ba33642011-12-14 12:31:31 -080053
rginda87b86462011-12-14 13:48:03 -080054 // The div that contains this terminal.
55 this.div_ = null;
56
rgindac9bc5502012-01-18 11:48:44 -080057 // The document that contains the scrollPort. Defaulted to the global
58 // document here so that the terminal is functional even if it hasn't been
59 // inserted into a document yet, but re-set in decorate().
60 this.document_ = window.document;
rginda87b86462011-12-14 13:48:03 -080061
rginda8ba33642011-12-14 12:31:31 -080062 // The rows that have scrolled off screen and are no longer addressable.
63 this.scrollbackRows_ = [];
64
rgindac9bc5502012-01-18 11:48:44 -080065 // Saved tab stops.
66 this.tabStops_ = [];
67
David Benjamin66e954d2012-05-05 21:08:12 -040068 // Keep track of whether default tab stops have been erased; after a TBC
69 // clears all tab stops, defaults aren't restored on resize until a reset.
70 this.defaultTabStops = true;
71
rginda8ba33642011-12-14 12:31:31 -080072 // The VT's notion of the top and bottom rows. Used during some VT
73 // cursor positioning and scrolling commands.
74 this.vtScrollTop_ = null;
75 this.vtScrollBottom_ = null;
76
77 // The DIV element for the visible cursor.
78 this.cursorNode_ = null;
79
rginda9f5222b2012-03-05 11:53:28 -080080 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070081 // each output and keystroke. They are initialized by the preference manager.
82 this.scrollOnOutput_ = null;
83 this.scrollOnKeystroke_ = null;
84 this.foregroundColor_ = null;
85 this.backgroundColor_ = null;
rginda9f5222b2012-03-05 11:53:28 -080086
rgindaf0090c92012-02-10 14:58:52 -080087 // Terminal bell sound.
88 this.bellAudio_ = this.document_.createElement('audio');
rgindaf0090c92012-02-10 14:58:52 -080089 this.bellAudio_.setAttribute('preload', 'auto');
90
rginda6d397402012-01-17 10:58:29 -080091 // Cursor position and attributes saved with DECSC.
92 this.savedOptions_ = {};
93
rginda8ba33642011-12-14 12:31:31 -080094 // The current mode bits for the terminal.
95 this.options_ = new hterm.Options();
96
97 // Timeouts we might need to clear.
98 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -080099
100 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800101 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800102
rgindafeaf3142012-01-31 15:14:20 -0800103 // The keyboard hander.
104 this.keyboard = new hterm.Keyboard(this);
105
rginda87b86462011-12-14 13:48:03 -0800106 // General IO interface that can be given to third parties without exposing
107 // the entire terminal object.
108 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800109
rgindad5613292012-06-19 15:40:37 -0700110 // True if mouse-click-drag should scroll the terminal.
111 this.enableMouseDragScroll = true;
112
Robert Ginda57f03b42012-09-13 11:02:48 -0700113 this.copyOnSelect = null;
rginda4bba5e12012-06-20 16:15:30 -0700114 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700115
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400116 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800117 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700118
119 this.setProfile(opt_profileId || 'default',
120 function() { this.onTerminalReady() }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800121};
122
123/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700124 * Clients should override this to be notified when the terminal is ready
125 * for use.
126 *
127 * The terminal initialization is asynchronous, and shouldn't be used before
128 * this method is called.
129 */
130hterm.Terminal.prototype.onTerminalReady = function() { };
131
132/**
rginda35c456b2012-02-09 17:29:05 -0800133 * Default tab with of 8 to match xterm.
134 */
135hterm.Terminal.prototype.tabWidth = 8;
136
137/**
rginda9f5222b2012-03-05 11:53:28 -0800138 * Select a preference profile.
139 *
140 * This will load the terminal preferences for the given profile name and
141 * associate subsequent preference changes with the new preference profile.
142 *
143 * @param {string} newName The name of the preference profile. Forward slash
144 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700145 * @param {function} opt_callback Optional callback to invoke when the profile
146 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800147 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700148hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
149 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800150
Robert Ginda57f03b42012-09-13 11:02:48 -0700151 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800152
Robert Ginda57f03b42012-09-13 11:02:48 -0700153 if (this.prefs_)
154 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800155
Robert Ginda57f03b42012-09-13 11:02:48 -0700156 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
157 this.prefs_.addObservers(null, {
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700158 'alt-backspace-is-meta-backspace': function(v) {
159 terminal.keyboard.altBackspaceIsMetaBackspace = v;
160 },
161
Robert Ginda57f03b42012-09-13 11:02:48 -0700162 'alt-is-meta': function(v) {
163 terminal.keyboard.altIsMeta = v;
164 },
165
166 'alt-sends-what': function(v) {
167 if (!/^(escape|8-bit|browser-key)$/.test(v))
168 v = 'escape';
169
170 terminal.keyboard.altSendsWhat = v;
171 },
172
173 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800174 var ary = v.match(/^lib-resource:(\S+)/);
175 if (ary) {
176 terminal.bellAudio_.setAttribute('src',
177 lib.resource.getDataUrl(ary[1]));
178 } else {
179 terminal.bellAudio_.setAttribute('src', v);
180 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700181 },
182
183 'background-color': function(v) {
184 terminal.setBackgroundColor(v);
185 },
186
187 'background-image': function(v) {
188 terminal.scrollPort_.setBackgroundImage(v);
189 },
190
191 'background-size': function(v) {
192 terminal.scrollPort_.setBackgroundSize(v);
193 },
194
195 'background-position': function(v) {
196 terminal.scrollPort_.setBackgroundPosition(v);
197 },
198
199 'backspace-sends-backspace': function(v) {
200 terminal.keyboard.backspaceSendsBackspace = v;
201 },
202
203 'cursor-blink': function(v) {
204 terminal.setCursorBlink(!!v);
205 },
206
207 'cursor-color': function(v) {
208 terminal.setCursorColor(v);
209 },
210
211 'color-palette-overrides': function(v) {
212 if (!(v == null || v instanceof Object || v instanceof Array)) {
213 console.warn('Preference color-palette-overrides is not an array or ' +
214 'object: ' + v);
215 return;
rginda9f5222b2012-03-05 11:53:28 -0800216 }
rginda9f5222b2012-03-05 11:53:28 -0800217
Robert Ginda57f03b42012-09-13 11:02:48 -0700218 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700219
Robert Ginda57f03b42012-09-13 11:02:48 -0700220 if (v) {
221 for (var key in v) {
222 var i = parseInt(key);
223 if (isNaN(i) || i < 0 || i > 255) {
224 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
225 continue;
226 }
227
228 if (v[i]) {
229 var rgb = lib.colors.normalizeCSS(v[i]);
230 if (rgb)
231 lib.colors.colorPalette[i] = rgb;
232 }
233 }
rginda30f20f62012-04-05 16:36:19 -0700234 }
rginda30f20f62012-04-05 16:36:19 -0700235
Robert Ginda57f03b42012-09-13 11:02:48 -0700236 terminal.primaryScreen_.textAttributes.resetColorPalette()
237 terminal.alternateScreen_.textAttributes.resetColorPalette();
238 },
rginda30f20f62012-04-05 16:36:19 -0700239
Robert Ginda57f03b42012-09-13 11:02:48 -0700240 'copy-on-select': function(v) {
241 terminal.copyOnSelect = !!v;
242 },
rginda9f5222b2012-03-05 11:53:28 -0800243
Robert Ginda57f03b42012-09-13 11:02:48 -0700244 'enable-8-bit-control': function(v) {
245 terminal.vt.enable8BitControl = !!v;
246 },
rginda30f20f62012-04-05 16:36:19 -0700247
Robert Ginda57f03b42012-09-13 11:02:48 -0700248 'enable-bold': function(v) {
249 terminal.syncBoldSafeState();
250 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400251
Robert Ginda57f03b42012-09-13 11:02:48 -0700252 'enable-clipboard-write': function(v) {
253 terminal.vt.enableClipboardWrite = !!v;
254 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400255
Robert Ginda3755e752013-05-31 13:34:09 -0700256 'enable-dec12': function(v) {
257 terminal.vt.enableDec12 = !!v;
258 },
259
Robert Ginda57f03b42012-09-13 11:02:48 -0700260 'font-family': function(v) {
261 terminal.syncFontFamily();
262 },
rginda30f20f62012-04-05 16:36:19 -0700263
Robert Ginda57f03b42012-09-13 11:02:48 -0700264 'font-size': function(v) {
265 terminal.setFontSize(v);
266 },
rginda9875d902012-08-20 16:21:57 -0700267
Robert Ginda57f03b42012-09-13 11:02:48 -0700268 'font-smoothing': function(v) {
269 terminal.syncFontFamily();
270 },
rgindade84e382012-04-20 15:39:31 -0700271
Robert Ginda57f03b42012-09-13 11:02:48 -0700272 'foreground-color': function(v) {
273 terminal.setForegroundColor(v);
274 },
rginda30f20f62012-04-05 16:36:19 -0700275
Robert Ginda57f03b42012-09-13 11:02:48 -0700276 'home-keys-scroll': function(v) {
277 terminal.keyboard.homeKeysScroll = v;
278 },
rginda4bba5e12012-06-20 16:15:30 -0700279
Robert Ginda57f03b42012-09-13 11:02:48 -0700280 'max-string-sequence': function(v) {
281 terminal.vt.maxStringSequence = v;
282 },
rginda11057d52012-04-25 12:29:56 -0700283
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700284 'media-keys-are-fkeys': function(v) {
285 terminal.keyboard.mediaKeysAreFKeys = v;
286 },
287
Robert Ginda57f03b42012-09-13 11:02:48 -0700288 'meta-sends-escape': function(v) {
289 terminal.keyboard.metaSendsEscape = v;
290 },
rginda30f20f62012-04-05 16:36:19 -0700291
Robert Ginda57f03b42012-09-13 11:02:48 -0700292 'mouse-cell-motion-trick': function(v) {
293 terminal.vt.setMouseCellMotionTrick(v);
294 },
Robert Ginda9fb38222012-09-11 14:19:12 -0700295
Robert Ginda57f03b42012-09-13 11:02:48 -0700296 'mouse-paste-button': function(v) {
297 terminal.syncMousePasteButton();
298 },
rgindaa8ba17d2012-08-15 14:41:10 -0700299
Robert Ginda40932892012-12-10 17:26:40 -0800300 'pass-alt-number': function(v) {
301 if (v == null) {
302 var osx = window.navigator.userAgent.match(/Mac OS X/);
303
304 // Let Alt-1..9 pass to the browser (to control tab switching) on
305 // non-OS X systems, or if hterm is not opened in an app window.
306 v = (!osx && hterm.windowType != 'popup');
307 }
308
309 terminal.passAltNumber = v;
310 },
311
312 'pass-ctrl-number': function(v) {
313 if (v == null) {
314 var osx = window.navigator.userAgent.match(/Mac OS X/);
315
316 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
317 // non-OS X systems, or if hterm is not opened in an app window.
318 v = (!osx && hterm.windowType != 'popup');
319 }
320
321 terminal.passCtrlNumber = v;
322 },
323
324 'pass-meta-number': function(v) {
325 if (v == null) {
326 var osx = window.navigator.userAgent.match(/Mac OS X/);
327
328 // Let Meta-1..9 pass to the browser (to control tab switching) on
329 // OS X systems, or if hterm is not opened in an app window.
330 v = (osx && hterm.windowType != 'popup');
331 }
332
333 terminal.passMetaNumber = v;
334 },
335
Robert Ginda57f03b42012-09-13 11:02:48 -0700336 'scroll-on-keystroke': function(v) {
337 terminal.scrollOnKeystroke_ = v;
338 },
rginda9f5222b2012-03-05 11:53:28 -0800339
Robert Ginda57f03b42012-09-13 11:02:48 -0700340 'scroll-on-output': function(v) {
341 terminal.scrollOnOutput_ = v;
342 },
rginda30f20f62012-04-05 16:36:19 -0700343
Robert Ginda57f03b42012-09-13 11:02:48 -0700344 'scrollbar-visible': function(v) {
345 terminal.setScrollbarVisible(v);
346 },
rginda9f5222b2012-03-05 11:53:28 -0800347
Robert Ginda57f03b42012-09-13 11:02:48 -0700348 'shift-insert-paste': function(v) {
349 terminal.keyboard.shiftInsertPaste = v;
350 },
rginda9f5222b2012-03-05 11:53:28 -0800351
Robert Ginda57f03b42012-09-13 11:02:48 -0700352 'page-keys-scroll': function(v) {
353 terminal.keyboard.pageKeysScroll = v;
354 }
355 });
rginda30f20f62012-04-05 16:36:19 -0700356
Robert Ginda57f03b42012-09-13 11:02:48 -0700357 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800358 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700359
360 if (opt_callback)
361 opt_callback();
362 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800363};
364
rginda8e92a692012-05-20 19:37:20 -0700365
366/**
367 * Set the color for the cursor.
368 *
369 * If you want this setting to persist, set it through prefs_, rather than
370 * with this method.
371 */
372hterm.Terminal.prototype.setCursorColor = function(color) {
373 this.cursorNode_.style.backgroundColor = color;
374 this.cursorNode_.style.borderColor = color;
375};
376
377/**
378 * Return the current cursor color as a string.
379 */
380hterm.Terminal.prototype.getCursorColor = function() {
381 return this.cursorNode_.style.backgroundColor;
382};
383
384/**
rgindad5613292012-06-19 15:40:37 -0700385 * Enable or disable mouse based text selection in the terminal.
386 */
387hterm.Terminal.prototype.setSelectionEnabled = function(state) {
388 this.enableMouseDragScroll = state;
389 this.scrollPort_.setSelectionEnabled(state);
390};
391
392/**
rginda8e92a692012-05-20 19:37:20 -0700393 * Set the background color.
394 *
395 * If you want this setting to persist, set it through prefs_, rather than
396 * with this method.
397 */
398hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700399 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700400 this.primaryScreen_.textAttributes.setDefaults(
401 this.foregroundColor_, this.backgroundColor_);
402 this.alternateScreen_.textAttributes.setDefaults(
403 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700404 this.scrollPort_.setBackgroundColor(color);
405};
406
rginda9f5222b2012-03-05 11:53:28 -0800407/**
408 * Return the current terminal background color.
409 *
410 * Intended for use by other classes, so we don't have to expose the entire
411 * prefs_ object.
412 */
413hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700414 return this.backgroundColor_;
415};
416
417/**
418 * Set the foreground color.
419 *
420 * If you want this setting to persist, set it through prefs_, rather than
421 * with this method.
422 */
423hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700424 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700425 this.primaryScreen_.textAttributes.setDefaults(
426 this.foregroundColor_, this.backgroundColor_);
427 this.alternateScreen_.textAttributes.setDefaults(
428 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700429 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800430};
431
432/**
433 * Return the current terminal foreground color.
434 *
435 * Intended for use by other classes, so we don't have to expose the entire
436 * prefs_ object.
437 */
438hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700439 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800440};
441
442/**
rginda87b86462011-12-14 13:48:03 -0800443 * Create a new instance of a terminal command and run it with a given
444 * argument string.
445 *
446 * @param {function} commandClass The constructor for a terminal command.
447 * @param {string} argString The argument string to pass to the command.
448 */
449hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700450 var environment = this.prefs_.get('environment');
451 if (typeof environment != 'object' || environment == null)
452 environment = {};
453
rginda87b86462011-12-14 13:48:03 -0800454 var self = this;
455 this.command = new commandClass(
456 { argString: argString || '',
457 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700458 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800459 onExit: function(code) {
460 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800461 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700462 if (self.prefs_.get('close-on-exit'))
463 window.close();
rginda87b86462011-12-14 13:48:03 -0800464 }
465 });
466
rgindafeaf3142012-01-31 15:14:20 -0800467 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800468 this.command.run();
469};
470
471/**
rgindafeaf3142012-01-31 15:14:20 -0800472 * Returns true if the current screen is the primary screen, false otherwise.
473 */
474hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700475 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800476};
477
478/**
479 * Install the keyboard handler for this terminal.
480 *
481 * This will prevent the browser from seeing any keystrokes sent to the
482 * terminal.
483 */
484hterm.Terminal.prototype.installKeyboard = function() {
485 this.keyboard.installKeyboard(this.document_.body.firstChild);
486}
487
488/**
489 * Uninstall the keyboard handler for this terminal.
490 */
491hterm.Terminal.prototype.uninstallKeyboard = function() {
492 this.keyboard.installKeyboard(null);
493}
494
495/**
rginda35c456b2012-02-09 17:29:05 -0800496 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800497 *
498 * Call setFontSize(0) to reset to the default font size.
499 *
500 * This function does not modify the font-size preference.
501 *
502 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800503 */
504hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800505 if (px === 0)
506 px = this.prefs_.get('font-size');
507
rginda35c456b2012-02-09 17:29:05 -0800508 this.scrollPort_.setFontSize(px);
509};
510
511/**
512 * Get the current font size.
513 */
514hterm.Terminal.prototype.getFontSize = function() {
515 return this.scrollPort_.getFontSize();
516};
517
518/**
rginda8e92a692012-05-20 19:37:20 -0700519 * Get the current font family.
520 */
521hterm.Terminal.prototype.getFontFamily = function() {
522 return this.scrollPort_.getFontFamily();
523};
524
525/**
rginda35c456b2012-02-09 17:29:05 -0800526 * Set the CSS "font-family" for this terminal.
527 */
rginda9f5222b2012-03-05 11:53:28 -0800528hterm.Terminal.prototype.syncFontFamily = function() {
529 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
530 this.prefs_.get('font-smoothing'));
531 this.syncBoldSafeState();
532};
533
rginda4bba5e12012-06-20 16:15:30 -0700534/**
535 * Set this.mousePasteButton based on the mouse-paste-button pref,
536 * autodetecting if necessary.
537 */
538hterm.Terminal.prototype.syncMousePasteButton = function() {
539 var button = this.prefs_.get('mouse-paste-button');
540 if (typeof button == 'number') {
541 this.mousePasteButton = button;
542 return;
543 }
544
545 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
546 if (!ary || ary[2] == 'CrOS') {
547 this.mousePasteButton = 2;
548 } else {
549 this.mousePasteButton = 3;
550 }
551};
552
553/**
554 * Enable or disable bold based on the enable-bold pref, autodetecting if
555 * necessary.
556 */
rginda9f5222b2012-03-05 11:53:28 -0800557hterm.Terminal.prototype.syncBoldSafeState = function() {
558 var enableBold = this.prefs_.get('enable-bold');
559 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700560 this.primaryScreen_.textAttributes.enableBold = enableBold;
561 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800562 return;
563 }
564
rgindaf7521392012-02-28 17:20:34 -0800565 var normalSize = this.scrollPort_.measureCharacterSize();
566 var boldSize = this.scrollPort_.measureCharacterSize('bold');
567
568 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800569 if (!isBoldSafe) {
570 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700571 'from normal. Font family is: ' +
572 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800573 }
rginda9f5222b2012-03-05 11:53:28 -0800574
Robert Gindaed016262012-10-26 16:27:09 -0700575 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
576 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800577};
578
579/**
rginda87b86462011-12-14 13:48:03 -0800580 * Return a copy of the current cursor position.
581 *
582 * @return {hterm.RowCol} The RowCol object representing the current position.
583 */
584hterm.Terminal.prototype.saveCursor = function() {
585 return this.screen_.cursorPosition.clone();
586};
587
rgindaa19afe22012-01-25 15:40:22 -0800588hterm.Terminal.prototype.getTextAttributes = function() {
589 return this.screen_.textAttributes;
590};
591
rginda1a09aa02012-06-18 21:11:25 -0700592hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
593 this.screen_.textAttributes = textAttributes;
594};
595
rginda87b86462011-12-14 13:48:03 -0800596/**
rgindaf522ce02012-04-17 17:49:17 -0700597 * Return the current browser zoom factor applied to the terminal.
598 *
599 * @return {number} The current browser zoom factor.
600 */
601hterm.Terminal.prototype.getZoomFactor = function() {
602 return this.scrollPort_.characterSize.zoomFactor;
603};
604
605/**
rginda9846e2f2012-01-27 13:53:33 -0800606 * Change the title of this terminal's window.
607 */
608hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800609 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800610};
611
612/**
rginda87b86462011-12-14 13:48:03 -0800613 * Restore a previously saved cursor position.
614 *
615 * @param {hterm.RowCol} cursor The position to restore.
616 */
617hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700618 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
619 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800620 this.screen_.setCursorPosition(row, column);
621 if (cursor.column > column ||
622 cursor.column == column && cursor.overflow) {
623 this.screen_.cursorPosition.overflow = true;
624 }
rginda87b86462011-12-14 13:48:03 -0800625};
626
627/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400628 * Clear the cursor's overflow flag.
629 */
630hterm.Terminal.prototype.clearCursorOverflow = function() {
631 this.screen_.cursorPosition.overflow = false;
632};
633
634/**
rginda87b86462011-12-14 13:48:03 -0800635 * Set the width of the terminal, resizing the UI to match.
636 */
637hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800638 if (columnCount == null) {
639 this.div_.style.width = '100%';
640 return;
641 }
642
rginda35c456b2012-02-09 17:29:05 -0800643 this.div_.style.width = this.scrollPort_.characterSize.width *
Robert Ginda97769282013-02-01 15:30:30 -0800644 columnCount + this.scrollPort_.currentScrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400645 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800646 this.scheduleSyncCursorPosition_();
647};
rginda87b86462011-12-14 13:48:03 -0800648
rgindac9bc5502012-01-18 11:48:44 -0800649/**
rginda35c456b2012-02-09 17:29:05 -0800650 * Set the height of the terminal, resizing the UI to match.
651 */
652hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800653 if (rowCount == null) {
654 this.div_.style.height = '100%';
655 return;
656 }
657
rginda35c456b2012-02-09 17:29:05 -0800658 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700659 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800660 this.realizeSize_(this.screenSize.width, rowCount);
661 this.scheduleSyncCursorPosition_();
662};
663
664/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400665 * Deal with terminal size changes.
666 *
667 */
668hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
669 if (columnCount != this.screenSize.width)
670 this.realizeWidth_(columnCount);
671
672 if (rowCount != this.screenSize.height)
673 this.realizeHeight_(rowCount);
674
675 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -0700676 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400677};
678
679/**
rgindac9bc5502012-01-18 11:48:44 -0800680 * Deal with terminal width changes.
681 *
682 * This function does what needs to be done when the terminal width changes
683 * out from under us. It happens here rather than in onResize_() because this
684 * code may need to run synchronously to handle programmatic changes of
685 * terminal width.
686 *
687 * Relying on the browser to send us an async resize event means we may not be
688 * in the correct state yet when the next escape sequence hits.
689 */
690hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700691 if (columnCount <= 0)
692 throw new Error('Attempt to realize bad width: ' + columnCount);
693
rgindac9bc5502012-01-18 11:48:44 -0800694 var deltaColumns = columnCount - this.screen_.getWidth();
695
rginda87b86462011-12-14 13:48:03 -0800696 this.screenSize.width = columnCount;
697 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800698
699 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400700 if (this.defaultTabStops)
701 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800702 } else {
703 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400704 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800705 break;
706
707 this.tabStops_.pop();
708 }
709 }
710
711 this.screen_.setColumnCount(this.screenSize.width);
712};
713
714/**
715 * Deal with terminal height changes.
716 *
717 * This function does what needs to be done when the terminal height changes
718 * out from under us. It happens here rather than in onResize_() because this
719 * code may need to run synchronously to handle programmatic changes of
720 * terminal height.
721 *
722 * Relying on the browser to send us an async resize event means we may not be
723 * in the correct state yet when the next escape sequence hits.
724 */
725hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700726 if (rowCount <= 0)
727 throw new Error('Attempt to realize bad height: ' + rowCount);
728
rgindac9bc5502012-01-18 11:48:44 -0800729 var deltaRows = rowCount - this.screen_.getHeight();
730
731 this.screenSize.height = rowCount;
732
733 var cursor = this.saveCursor();
734
735 if (deltaRows < 0) {
736 // Screen got smaller.
737 deltaRows *= -1;
738 while (deltaRows) {
739 var lastRow = this.getRowCount() - 1;
740 if (lastRow - this.scrollbackRows_.length == cursor.row)
741 break;
742
743 if (this.getRowText(lastRow))
744 break;
745
746 this.screen_.popRow();
747 deltaRows--;
748 }
749
750 var ary = this.screen_.shiftRows(deltaRows);
751 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
752
753 // We just removed rows from the top of the screen, we need to update
754 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800755 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800756 } else if (deltaRows > 0) {
757 // Screen got larger.
758
759 if (deltaRows <= this.scrollbackRows_.length) {
760 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
761 var rows = this.scrollbackRows_.splice(
762 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
763 this.screen_.unshiftRows(rows);
764 deltaRows -= scrollbackCount;
765 cursor.row += scrollbackCount;
766 }
767
768 if (deltaRows)
769 this.appendRows_(deltaRows);
770 }
771
rginda35c456b2012-02-09 17:29:05 -0800772 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800773 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800774};
775
776/**
777 * Scroll the terminal to the top of the scrollback buffer.
778 */
779hterm.Terminal.prototype.scrollHome = function() {
780 this.scrollPort_.scrollRowToTop(0);
781};
782
783/**
784 * Scroll the terminal to the end.
785 */
786hterm.Terminal.prototype.scrollEnd = function() {
787 this.scrollPort_.scrollRowToBottom(this.getRowCount());
788};
789
790/**
791 * Scroll the terminal one page up (minus one line) relative to the current
792 * position.
793 */
794hterm.Terminal.prototype.scrollPageUp = function() {
795 var i = this.scrollPort_.getTopRowIndex();
796 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
797};
798
799/**
800 * Scroll the terminal one page down (minus one line) relative to the current
801 * position.
802 */
803hterm.Terminal.prototype.scrollPageDown = function() {
804 var i = this.scrollPort_.getTopRowIndex();
805 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800806};
807
rgindac9bc5502012-01-18 11:48:44 -0800808/**
Robert Ginda40932892012-12-10 17:26:40 -0800809 * Clear primary screen, secondary screen, and the scrollback buffer.
810 */
811hterm.Terminal.prototype.wipeContents = function() {
812 this.scrollbackRows_.length = 0;
813 this.scrollPort_.resetCache();
814
815 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
816 var bottom = screen.getHeight();
817 if (bottom > 0) {
818 this.renumberRows_(0, bottom);
819 this.clearHome(screen);
820 }
821 }.bind(this));
822
823 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -0700824 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -0800825};
826
827/**
rgindac9bc5502012-01-18 11:48:44 -0800828 * Full terminal reset.
829 */
rginda87b86462011-12-14 13:48:03 -0800830hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800831 this.clearAllTabStops();
832 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -0700833
834 this.clearHome(this.primaryScreen_);
835 this.primaryScreen_.textAttributes.reset();
836
837 this.clearHome(this.alternateScreen_);
838 this.alternateScreen_.textAttributes.reset();
839
rgindab8bc8932012-04-27 12:45:03 -0700840 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
841
Robert Ginda92e18102013-03-14 13:56:37 -0700842 this.vt.reset();
843
rgindac9bc5502012-01-18 11:48:44 -0800844 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800845};
846
rgindac9bc5502012-01-18 11:48:44 -0800847/**
848 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -0700849 *
850 * Perform a soft reset to the default values listed in
851 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -0800852 */
rginda0f5c0292012-01-13 11:00:13 -0800853hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -0700854 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -0800855 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -0700856
rgindab8bc8932012-04-27 12:45:03 -0700857 // Xterm also resets the color palette on soft reset, even though it doesn't
858 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -0700859 this.primaryScreen_.textAttributes.resetColorPalette();
860 this.alternateScreen_.textAttributes.resetColorPalette();
861
rgindab8bc8932012-04-27 12:45:03 -0700862 // The xterm man page explicitly says this will happen on soft reset.
863 this.setVTScrollRegion(null, null);
864
865 // Xterm also shows the cursor on soft reset, but does not alter the blink
866 // state.
rgindaa19afe22012-01-25 15:40:22 -0800867 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -0800868};
869
rgindac9bc5502012-01-18 11:48:44 -0800870/**
871 * Move the cursor forward to the next tab stop, or to the last column
872 * if no more tab stops are set.
873 */
874hterm.Terminal.prototype.forwardTabStop = function() {
875 var column = this.screen_.cursorPosition.column;
876
877 for (var i = 0; i < this.tabStops_.length; i++) {
878 if (this.tabStops_[i] > column) {
879 this.setCursorColumn(this.tabStops_[i]);
880 return;
881 }
882 }
883
David Benjamin66e954d2012-05-05 21:08:12 -0400884 // xterm does not clear the overflow flag on HT or CHT.
885 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -0800886 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -0400887 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -0800888};
889
rgindac9bc5502012-01-18 11:48:44 -0800890/**
891 * Move the cursor backward to the previous tab stop, or to the first column
892 * if no previous tab stops are set.
893 */
894hterm.Terminal.prototype.backwardTabStop = function() {
895 var column = this.screen_.cursorPosition.column;
896
897 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
898 if (this.tabStops_[i] < column) {
899 this.setCursorColumn(this.tabStops_[i]);
900 return;
901 }
902 }
903
904 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800905};
906
rgindac9bc5502012-01-18 11:48:44 -0800907/**
908 * Set a tab stop at the given column.
909 *
910 * @param {int} column Zero based column.
911 */
912hterm.Terminal.prototype.setTabStop = function(column) {
913 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
914 if (this.tabStops_[i] == column)
915 return;
916
917 if (this.tabStops_[i] < column) {
918 this.tabStops_.splice(i + 1, 0, column);
919 return;
920 }
921 }
922
923 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800924};
925
rgindac9bc5502012-01-18 11:48:44 -0800926/**
927 * Clear the tab stop at the current cursor position.
928 *
929 * No effect if there is no tab stop at the current cursor position.
930 */
931hterm.Terminal.prototype.clearTabStopAtCursor = function() {
932 var column = this.screen_.cursorPosition.column;
933
934 var i = this.tabStops_.indexOf(column);
935 if (i == -1)
936 return;
937
938 this.tabStops_.splice(i, 1);
939};
940
941/**
942 * Clear all tab stops.
943 */
944hterm.Terminal.prototype.clearAllTabStops = function() {
945 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -0400946 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -0800947};
948
949/**
950 * Set up the default tab stops, starting from a given column.
951 *
952 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -0400953 * from the specified column, or 0 if no column is provided. It also flags
954 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -0800955 *
956 * This does not clear the existing tab stops first, use clearAllTabStops
957 * for that.
958 *
959 * @param {int} opt_start Optional starting zero based starting column, useful
960 * for filling out missing tab stops when the terminal is resized.
961 */
962hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
963 var start = opt_start || 0;
964 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -0400965 // Round start up to a default tab stop.
966 start = start - 1 - ((start - 1) % w) + w;
967 for (var i = start; i < this.screenSize.width; i += w) {
968 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -0800969 }
David Benjamin66e954d2012-05-05 21:08:12 -0400970
971 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -0800972};
973
rginda6d397402012-01-17 10:58:29 -0800974/**
rginda8ba33642011-12-14 12:31:31 -0800975 * Interpret a sequence of characters.
976 *
977 * Incomplete escape sequences are buffered until the next call.
978 *
979 * @param {string} str Sequence of characters to interpret or pass through.
980 */
981hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -0800982 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -0800983 this.scheduleSyncCursorPosition_();
984};
985
986/**
987 * Take over the given DIV for use as the terminal display.
988 *
989 * @param {HTMLDivElement} div The div to use as the terminal display.
990 */
991hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -0800992 this.div_ = div;
993
rginda8ba33642011-12-14 12:31:31 -0800994 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -0700995 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -0400996 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
997 this.scrollPort_.setBackgroundPosition(
998 this.prefs_.get('background-position'));
rginda30f20f62012-04-05 16:36:19 -0700999
rginda0918b652012-04-04 11:26:24 -07001000 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001001
rginda9f5222b2012-03-05 11:53:28 -08001002 this.setFontSize(this.prefs_.get('font-size'));
1003 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001004
David Reveman8f552492012-03-28 12:18:41 -04001005 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
1006
rginda8ba33642011-12-14 12:31:31 -08001007 this.document_ = this.scrollPort_.getDocument();
1008
rginda4bba5e12012-06-20 16:15:30 -07001009 this.document_.body.oncontextmenu = function() { return false };
1010
1011 var onMouse = this.onMouse_.bind(this);
1012 this.document_.body.firstChild.addEventListener('mousedown', onMouse);
1013 this.document_.body.firstChild.addEventListener('mouseup', onMouse);
1014 this.document_.body.firstChild.addEventListener('mousemove', onMouse);
1015 this.scrollPort_.onScrollWheel = onMouse;
1016
rginda8e92a692012-05-20 19:37:20 -07001017 this.document_.body.firstChild.addEventListener(
1018 'focus', this.onFocusChange_.bind(this, true));
1019 this.document_.body.firstChild.addEventListener(
1020 'blur', this.onFocusChange_.bind(this, false));
1021
1022 var style = this.document_.createElement('style');
1023 style.textContent =
1024 ('.cursor-node[focus="false"] {' +
1025 ' box-sizing: border-box;' +
1026 ' background-color: transparent !important;' +
1027 ' border-width: 2px;' +
1028 ' border-style: solid;' +
1029 '}');
1030 this.document_.head.appendChild(style);
1031
rginda8ba33642011-12-14 12:31:31 -08001032 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001033 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001034 this.cursorNode_.style.cssText =
1035 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001036 'top: -99px;' +
1037 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001038 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1039 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001040 '-webkit-transition: opacity, background-color 100ms linear;');
1041 this.setCursorColor(this.prefs_.get('cursor-color'));
rgindad5613292012-06-19 15:40:37 -07001042
rginda8ba33642011-12-14 12:31:31 -08001043 this.document_.body.appendChild(this.cursorNode_);
1044
rgindad5613292012-06-19 15:40:37 -07001045 // When 'enableMouseDragScroll' is off we reposition this element directly
1046 // under the mouse cursor after a click. This makes Chrome associate
1047 // subsequent mousemove events with the scroll-blocker. Since the
1048 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1049 // events do not cause the scrollport to scroll.
1050 //
1051 // It's a hack, but it's the cleanest way I could find.
1052 this.scrollBlockerNode_ = this.document_.createElement('div');
1053 this.scrollBlockerNode_.style.cssText =
1054 ('position: absolute;' +
1055 'top: -99px;' +
1056 'display: block;' +
1057 'width: 10px;' +
1058 'height: 10px;');
1059 this.document_.body.appendChild(this.scrollBlockerNode_);
1060
1061 var onMouse = this.onMouse_.bind(this);
1062 this.scrollPort_.onScrollWheel = onMouse;
1063 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1064 ].forEach(function(event) {
1065 this.scrollBlockerNode_.addEventListener(event, onMouse);
1066 this.cursorNode_.addEventListener(event, onMouse);
1067 this.document_.addEventListener(event, onMouse);
1068 }.bind(this));
1069
1070 this.cursorNode_.addEventListener('mousedown', function() {
1071 setTimeout(this.focus.bind(this));
1072 }.bind(this));
1073
rgindade84e382012-04-20 15:39:31 -07001074 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
rginda8ba33642011-12-14 12:31:31 -08001075 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001076
rginda87b86462011-12-14 13:48:03 -08001077 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001078 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001079};
1080
rginda0918b652012-04-04 11:26:24 -07001081/**
1082 * Return the HTML document that contains the terminal DOM nodes.
1083 */
rginda87b86462011-12-14 13:48:03 -08001084hterm.Terminal.prototype.getDocument = function() {
1085 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001086};
1087
1088/**
rginda0918b652012-04-04 11:26:24 -07001089 * Focus the terminal.
1090 */
1091hterm.Terminal.prototype.focus = function() {
1092 this.scrollPort_.focus();
1093};
1094
1095/**
rginda8ba33642011-12-14 12:31:31 -08001096 * Return the HTML Element for a given row index.
1097 *
1098 * This is a method from the RowProvider interface. The ScrollPort uses
1099 * it to fetch rows on demand as they are scrolled into view.
1100 *
1101 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1102 * pairs to conserve memory.
1103 *
1104 * @param {integer} index The zero-based row index, measured relative to the
1105 * start of the scrollback buffer. On-screen rows will always have the
1106 * largest indicies.
1107 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1108 */
1109hterm.Terminal.prototype.getRowNode = function(index) {
1110 if (index < this.scrollbackRows_.length)
1111 return this.scrollbackRows_[index];
1112
1113 var screenIndex = index - this.scrollbackRows_.length;
1114 return this.screen_.rowsArray[screenIndex];
1115};
1116
1117/**
1118 * Return the text content for a given range of rows.
1119 *
1120 * This is a method from the RowProvider interface. The ScrollPort uses
1121 * it to fetch text content on demand when the user attempts to copy their
1122 * selection to the clipboard.
1123 *
1124 * @param {integer} start The zero-based row index to start from, measured
1125 * relative to the start of the scrollback buffer. On-screen rows will
1126 * always have the largest indicies.
1127 * @param {integer} end The zero-based row index to end on, measured
1128 * relative to the start of the scrollback buffer.
1129 * @return {string} A single string containing the text value of the range of
1130 * rows. Lines will be newline delimited, with no trailing newline.
1131 */
1132hterm.Terminal.prototype.getRowsText = function(start, end) {
1133 var ary = [];
1134 for (var i = start; i < end; i++) {
1135 var node = this.getRowNode(i);
1136 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001137 if (i < end - 1 && !node.getAttribute('line-overflow'))
1138 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001139 }
1140
rgindaa09e7332012-08-17 12:49:51 -07001141 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001142};
1143
1144/**
1145 * Return the text content for a given row.
1146 *
1147 * This is a method from the RowProvider interface. The ScrollPort uses
1148 * it to fetch text content on demand when the user attempts to copy their
1149 * selection to the clipboard.
1150 *
1151 * @param {integer} index The zero-based row index to return, measured
1152 * relative to the start of the scrollback buffer. On-screen rows will
1153 * always have the largest indicies.
1154 * @return {string} A string containing the text value of the selected row.
1155 */
1156hterm.Terminal.prototype.getRowText = function(index) {
1157 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001158 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001159};
1160
1161/**
1162 * Return the total number of rows in the addressable screen and in the
1163 * scrollback buffer of this terminal.
1164 *
1165 * This is a method from the RowProvider interface. The ScrollPort uses
1166 * it to compute the size of the scrollbar.
1167 *
1168 * @return {integer} The number of rows in this terminal.
1169 */
1170hterm.Terminal.prototype.getRowCount = function() {
1171 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1172};
1173
1174/**
1175 * Create DOM nodes for new rows and append them to the end of the terminal.
1176 *
1177 * This is the only correct way to add a new DOM node for a row. Notice that
1178 * the new row is appended to the bottom of the list of rows, and does not
1179 * require renumbering (of the rowIndex property) of previous rows.
1180 *
1181 * If you think you want a new blank row somewhere in the middle of the
1182 * terminal, look into moveRows_().
1183 *
1184 * This method does not pay attention to vtScrollTop/Bottom, since you should
1185 * be using moveRows() in cases where they would matter.
1186 *
1187 * The cursor will be positioned at column 0 of the first inserted line.
1188 */
1189hterm.Terminal.prototype.appendRows_ = function(count) {
1190 var cursorRow = this.screen_.rowsArray.length;
1191 var offset = this.scrollbackRows_.length + cursorRow;
1192 for (var i = 0; i < count; i++) {
1193 var row = this.document_.createElement('x-row');
1194 row.appendChild(this.document_.createTextNode(''));
1195 row.rowIndex = offset + i;
1196 this.screen_.pushRow(row);
1197 }
1198
1199 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1200 if (extraRows > 0) {
1201 var ary = this.screen_.shiftRows(extraRows);
1202 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001203 if (this.scrollPort_.isScrolledEnd)
1204 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001205 }
1206
1207 if (cursorRow >= this.screen_.rowsArray.length)
1208 cursorRow = this.screen_.rowsArray.length - 1;
1209
rginda87b86462011-12-14 13:48:03 -08001210 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001211};
1212
1213/**
1214 * Relocate rows from one part of the addressable screen to another.
1215 *
1216 * This is used to recycle rows during VT scrolls (those which are driven
1217 * by VT commands, rather than by the user manipulating the scrollbar.)
1218 *
1219 * In this case, the blank lines scrolled into the scroll region are made of
1220 * the nodes we scrolled off. These have their rowIndex properties carefully
1221 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001222 */
1223hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1224 var ary = this.screen_.removeRows(fromIndex, count);
1225 this.screen_.insertRows(toIndex, ary);
1226
1227 var start, end;
1228 if (fromIndex < toIndex) {
1229 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001230 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001231 } else {
1232 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001233 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001234 }
1235
1236 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001237 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001238};
1239
1240/**
1241 * Renumber the rowIndex property of the given range of rows.
1242 *
1243 * The start and end indicies are relative to the screen, not the scrollback.
1244 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001245 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001246 * no need to renumber scrollback rows.
1247 */
Robert Ginda40932892012-12-10 17:26:40 -08001248hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1249 var screen = opt_screen || this.screen_;
1250
rginda8ba33642011-12-14 12:31:31 -08001251 var offset = this.scrollbackRows_.length;
1252 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001253 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001254 }
1255};
1256
1257/**
1258 * Print a string to the terminal.
1259 *
1260 * This respects the current insert and wraparound modes. It will add new lines
1261 * to the end of the terminal, scrolling off the top into the scrollback buffer
1262 * if necessary.
1263 *
1264 * The string is *not* parsed for escape codes. Use the interpret() method if
1265 * that's what you're after.
1266 *
1267 * @param{string} str The string to print.
1268 */
1269hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001270 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001271
rgindaa9abdd82012-08-06 18:05:09 -07001272 while (startOffset < str.length) {
rgindaa09e7332012-08-17 12:49:51 -07001273 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1274 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001275 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001276 }
rgindaa19afe22012-01-25 15:40:22 -08001277
rgindaa9abdd82012-08-06 18:05:09 -07001278 var count = str.length - startOffset;
1279 var didOverflow = false;
1280 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001281
rgindaa9abdd82012-08-06 18:05:09 -07001282 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1283 didOverflow = true;
1284 count = this.screenSize.width - this.screen_.cursorPosition.column;
1285 }
rgindaa19afe22012-01-25 15:40:22 -08001286
rgindaa9abdd82012-08-06 18:05:09 -07001287 if (didOverflow && !this.options_.wraparound) {
1288 // If the string overflowed the line but wraparound is off, then the
1289 // last printed character should be the last of the string.
1290 // TODO: This will add to our problems with multibyte UTF-16 characters.
1291 substr = str.substr(startOffset, count - 1) +
1292 str.substr(str.length - 1);
1293 count = str.length;
1294 } else {
1295 substr = str.substr(startOffset, count);
1296 }
rgindaa19afe22012-01-25 15:40:22 -08001297
rgindaa9abdd82012-08-06 18:05:09 -07001298 if (this.options_.insertMode) {
1299 this.screen_.insertString(substr);
1300 } else {
1301 this.screen_.overwriteString(substr);
1302 }
1303
1304 this.screen_.maybeClipCurrentRow();
1305 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001306 }
rginda8ba33642011-12-14 12:31:31 -08001307
1308 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001309
rginda9f5222b2012-03-05 11:53:28 -08001310 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001311 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001312};
1313
1314/**
rginda87b86462011-12-14 13:48:03 -08001315 * Set the VT scroll region.
1316 *
rginda87b86462011-12-14 13:48:03 -08001317 * This also resets the cursor position to the absolute (0, 0) position, since
1318 * that's what xterm appears to do.
1319 *
1320 * @param {integer} scrollTop The zero-based top of the scroll region.
1321 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1322 * inclusive.
1323 */
1324hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
1325 this.vtScrollTop_ = scrollTop;
1326 this.vtScrollBottom_ = scrollBottom;
rginda87b86462011-12-14 13:48:03 -08001327};
1328
1329/**
rginda8ba33642011-12-14 12:31:31 -08001330 * Return the top row index according to the VT.
1331 *
1332 * This will return 0 unless the terminal has been told to restrict scrolling
1333 * to some lower row. It is used for some VT cursor positioning and scrolling
1334 * commands.
1335 *
1336 * @return {integer} The topmost row in the terminal's scroll region.
1337 */
1338hterm.Terminal.prototype.getVTScrollTop = function() {
1339 if (this.vtScrollTop_ != null)
1340 return this.vtScrollTop_;
1341
1342 return 0;
rginda87b86462011-12-14 13:48:03 -08001343};
rginda8ba33642011-12-14 12:31:31 -08001344
1345/**
1346 * Return the bottom row index according to the VT.
1347 *
1348 * This will return the height of the terminal unless the it has been told to
1349 * restrict scrolling to some higher row. It is used for some VT cursor
1350 * positioning and scrolling commands.
1351 *
1352 * @return {integer} The bottommost row in the terminal's scroll region.
1353 */
1354hterm.Terminal.prototype.getVTScrollBottom = function() {
1355 if (this.vtScrollBottom_ != null)
1356 return this.vtScrollBottom_;
1357
rginda87b86462011-12-14 13:48:03 -08001358 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001359}
1360
1361/**
1362 * Process a '\n' character.
1363 *
1364 * If the cursor is on the final row of the terminal this will append a new
1365 * blank row to the screen and scroll the topmost row into the scrollback
1366 * buffer.
1367 *
1368 * Otherwise, this moves the cursor to column zero of the next row.
1369 */
1370hterm.Terminal.prototype.newLine = function() {
1371 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -08001372 // If we're at the end of the screen we need to append a new line and
1373 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -08001374 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -08001375 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
1376 // End of the scroll region does not affect the scrollback buffer.
1377 this.vtScrollUp(1);
1378 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -08001379 } else {
rginda87b86462011-12-14 13:48:03 -08001380 // Anywhere else in the screen just moves the cursor.
1381 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001382 }
1383};
1384
1385/**
1386 * Like newLine(), except maintain the cursor column.
1387 */
1388hterm.Terminal.prototype.lineFeed = function() {
1389 var column = this.screen_.cursorPosition.column;
1390 this.newLine();
1391 this.setCursorColumn(column);
1392};
1393
1394/**
rginda87b86462011-12-14 13:48:03 -08001395 * If autoCarriageReturn is set then newLine(), else lineFeed().
1396 */
1397hterm.Terminal.prototype.formFeed = function() {
1398 if (this.options_.autoCarriageReturn) {
1399 this.newLine();
1400 } else {
1401 this.lineFeed();
1402 }
1403};
1404
1405/**
1406 * Move the cursor up one row, possibly inserting a blank line.
1407 *
1408 * The cursor column is not changed.
1409 */
1410hterm.Terminal.prototype.reverseLineFeed = function() {
1411 var scrollTop = this.getVTScrollTop();
1412 var currentRow = this.screen_.cursorPosition.row;
1413
1414 if (currentRow == scrollTop) {
1415 this.insertLines(1);
1416 } else {
1417 this.setAbsoluteCursorRow(currentRow - 1);
1418 }
1419};
1420
1421/**
rginda8ba33642011-12-14 12:31:31 -08001422 * Replace all characters to the left of the current cursor with the space
1423 * character.
1424 *
1425 * TODO(rginda): This should probably *remove* the characters (not just replace
1426 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001427 * position.
rginda8ba33642011-12-14 12:31:31 -08001428 */
1429hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001430 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001431 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001432 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001433 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001434};
1435
1436/**
David Benjamin684a9b72012-05-01 17:19:58 -04001437 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001438 *
1439 * The cursor position is unchanged.
1440 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001441 * If the current background color is not the default background color this
1442 * will insert spaces rather than delete. This is unfortunate because the
1443 * trailing space will affect text selection, but it's difficult to come up
1444 * with a way to style empty space that wouldn't trip up the hterm.Screen
1445 * code.
rginda8ba33642011-12-14 12:31:31 -08001446 */
1447hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001448 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1449 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001450
1451 if (this.screen_.textAttributes.background ===
1452 this.screen_.textAttributes.DEFAULT_COLOR) {
1453 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
1454 if (cursorRow.textContent.length <=
1455 this.screen_.cursorPosition.column + count) {
1456 this.screen_.deleteChars(count);
1457 this.clearCursorOverflow();
1458 return;
1459 }
1460 }
1461
rginda87b86462011-12-14 13:48:03 -08001462 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001463 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001464 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001465 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001466};
1467
1468/**
1469 * Erase the current line.
1470 *
1471 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001472 */
1473hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001474 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001475 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001476 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001477 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001478};
1479
1480/**
David Benjamina08d78f2012-05-05 00:28:49 -04001481 * Erase all characters from the start of the screen to the current cursor
1482 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001483 *
1484 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001485 */
1486hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001487 var cursor = this.saveCursor();
1488
1489 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001490
David Benjamina08d78f2012-05-05 00:28:49 -04001491 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001492 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001493 this.screen_.clearCursorRow();
1494 }
1495
rginda87b86462011-12-14 13:48:03 -08001496 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001497 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001498};
1499
1500/**
1501 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001502 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001503 *
1504 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001505 */
1506hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001507 var cursor = this.saveCursor();
1508
1509 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001510
David Benjamina08d78f2012-05-05 00:28:49 -04001511 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001512 for (var i = cursor.row + 1; i <= bottom; i++) {
1513 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001514 this.screen_.clearCursorRow();
1515 }
1516
rginda87b86462011-12-14 13:48:03 -08001517 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001518 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001519};
1520
1521/**
1522 * Fill the terminal with a given character.
1523 *
1524 * This methods does not respect the VT scroll region.
1525 *
1526 * @param {string} ch The character to use for the fill.
1527 */
1528hterm.Terminal.prototype.fill = function(ch) {
1529 var cursor = this.saveCursor();
1530
1531 this.setAbsoluteCursorPosition(0, 0);
1532 for (var row = 0; row < this.screenSize.height; row++) {
1533 for (var col = 0; col < this.screenSize.width; col++) {
1534 this.setAbsoluteCursorPosition(row, col);
1535 this.screen_.overwriteString(ch);
1536 }
1537 }
1538
1539 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001540};
1541
1542/**
rginda9ea433c2012-03-16 11:57:00 -07001543 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001544 *
rginda9ea433c2012-03-16 11:57:00 -07001545 * This does not respect the scroll region.
1546 *
1547 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1548 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001549 */
rginda9ea433c2012-03-16 11:57:00 -07001550hterm.Terminal.prototype.clearHome = function(opt_screen) {
1551 var screen = opt_screen || this.screen_;
1552 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001553
rginda11057d52012-04-25 12:29:56 -07001554 if (bottom == 0) {
1555 // Empty screen, nothing to do.
1556 return;
1557 }
1558
rgindae4d29232012-01-19 10:47:13 -08001559 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001560 screen.setCursorPosition(i, 0);
1561 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001562 }
1563
rginda9ea433c2012-03-16 11:57:00 -07001564 screen.setCursorPosition(0, 0);
1565};
1566
1567/**
1568 * Erase the entire display without changing the cursor position.
1569 *
1570 * The cursor position is unchanged. This does not respect the scroll
1571 * region.
1572 *
1573 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1574 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001575 */
1576hterm.Terminal.prototype.clear = function(opt_screen) {
1577 var screen = opt_screen || this.screen_;
1578 var cursor = screen.cursorPosition.clone();
1579 this.clearHome(screen);
1580 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001581};
1582
1583/**
1584 * VT command to insert lines at the current cursor row.
1585 *
1586 * This respects the current scroll region. Rows pushed off the bottom are
1587 * lost (they won't show up in the scrollback buffer).
1588 *
rginda8ba33642011-12-14 12:31:31 -08001589 * @param {integer} count The number of lines to insert.
1590 */
1591hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07001592 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08001593
1594 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07001595 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08001596
Robert Ginda579186b2012-09-26 11:40:04 -07001597 // The moveCount is the number of rows we need to relocate to make room for
1598 // the new row(s). The count is the distance to move them.
1599 var moveCount = bottom - cursorRow - count + 1;
1600 if (moveCount)
1601 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08001602
Robert Ginda579186b2012-09-26 11:40:04 -07001603 for (var i = count - 1; i >= 0; i--) {
1604 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001605 this.screen_.clearCursorRow();
1606 }
rginda8ba33642011-12-14 12:31:31 -08001607};
1608
1609/**
1610 * VT command to delete lines at the current cursor row.
1611 *
1612 * New rows are added to the bottom of scroll region to take their place. New
1613 * rows are strictly there to take up space and have no content or style.
1614 */
1615hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001616 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001617
rginda87b86462011-12-14 13:48:03 -08001618 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001619 var bottom = this.getVTScrollBottom();
1620
rginda87b86462011-12-14 13:48:03 -08001621 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001622 count = Math.min(count, maxCount);
1623
rginda87b86462011-12-14 13:48:03 -08001624 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001625 if (count != maxCount)
1626 this.moveRows_(top, count, moveStart);
1627
1628 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001629 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001630 this.screen_.clearCursorRow();
1631 }
1632
rginda87b86462011-12-14 13:48:03 -08001633 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001634 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001635};
1636
1637/**
1638 * Inserts the given number of spaces at the current cursor position.
1639 *
rginda87b86462011-12-14 13:48:03 -08001640 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001641 */
1642hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001643 var cursor = this.saveCursor();
1644
rgindacbbd7482012-06-13 15:06:16 -07001645 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001646 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001647 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001648
1649 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001650 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001651};
1652
1653/**
1654 * Forward-delete the specified number of characters starting at the cursor
1655 * position.
1656 *
1657 * @param {integer} count The number of characters to delete.
1658 */
1659hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001660 var deleted = this.screen_.deleteChars(count);
1661 if (deleted && !this.screen_.textAttributes.isDefault()) {
1662 var cursor = this.saveCursor();
1663 this.setCursorColumn(this.screenSize.width - deleted);
1664 this.screen_.insertString(lib.f.getWhitespace(deleted));
1665 this.restoreCursor(cursor);
1666 }
1667
David Benjamin54e8bf62012-06-01 22:31:40 -04001668 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001669};
1670
1671/**
1672 * Shift rows in the scroll region upwards by a given number of lines.
1673 *
1674 * New rows are inserted at the bottom of the scroll region to fill the
1675 * vacated rows. The new rows not filled out with the current text attributes.
1676 *
1677 * This function does not affect the scrollback rows at all. Rows shifted
1678 * off the top are lost.
1679 *
rginda87b86462011-12-14 13:48:03 -08001680 * The cursor position is not altered.
1681 *
rginda8ba33642011-12-14 12:31:31 -08001682 * @param {integer} count The number of rows to scroll.
1683 */
1684hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001685 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001686
rginda87b86462011-12-14 13:48:03 -08001687 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001688 this.deleteLines(count);
1689
rginda87b86462011-12-14 13:48:03 -08001690 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001691};
1692
1693/**
1694 * Shift rows below the cursor down by a given number of lines.
1695 *
1696 * This function respects the current scroll region.
1697 *
1698 * New rows are inserted at the top of the scroll region to fill the
1699 * vacated rows. The new rows not filled out with the current text attributes.
1700 *
1701 * This function does not affect the scrollback rows at all. Rows shifted
1702 * off the bottom are lost.
1703 *
1704 * @param {integer} count The number of rows to scroll.
1705 */
1706hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001707 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001708
rginda87b86462011-12-14 13:48:03 -08001709 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001710 this.insertLines(opt_count);
1711
rginda87b86462011-12-14 13:48:03 -08001712 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001713};
1714
rginda87b86462011-12-14 13:48:03 -08001715
rginda8ba33642011-12-14 12:31:31 -08001716/**
1717 * Set the cursor position.
1718 *
1719 * The cursor row is relative to the scroll region if the terminal has
1720 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1721 *
1722 * @param {integer} row The new zero-based cursor row.
1723 * @param {integer} row The new zero-based cursor column.
1724 */
1725hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1726 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001727 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001728 } else {
rginda87b86462011-12-14 13:48:03 -08001729 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001730 }
rginda87b86462011-12-14 13:48:03 -08001731};
rginda8ba33642011-12-14 12:31:31 -08001732
rginda87b86462011-12-14 13:48:03 -08001733hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1734 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07001735 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
1736 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001737 this.screen_.setCursorPosition(row, column);
1738};
1739
1740hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07001741 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
1742 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001743 this.screen_.setCursorPosition(row, column);
1744};
1745
1746/**
1747 * Set the cursor column.
1748 *
1749 * @param {integer} column The new zero-based cursor column.
1750 */
1751hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001752 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001753};
1754
1755/**
1756 * Return the cursor column.
1757 *
1758 * @return {integer} The zero-based cursor column.
1759 */
1760hterm.Terminal.prototype.getCursorColumn = function() {
1761 return this.screen_.cursorPosition.column;
1762};
1763
1764/**
1765 * Set the cursor row.
1766 *
1767 * The cursor row is relative to the scroll region if the terminal has
1768 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1769 *
1770 * @param {integer} row The new cursor row.
1771 */
rginda87b86462011-12-14 13:48:03 -08001772hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1773 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001774};
1775
1776/**
1777 * Return the cursor row.
1778 *
1779 * @return {integer} The zero-based cursor row.
1780 */
1781hterm.Terminal.prototype.getCursorRow = function(row) {
1782 return this.screen_.cursorPosition.row;
1783};
1784
1785/**
1786 * Request that the ScrollPort redraw itself soon.
1787 *
1788 * The redraw will happen asynchronously, soon after the call stack winds down.
1789 * Multiple calls will be coalesced into a single redraw.
1790 */
1791hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001792 if (this.timeouts_.redraw)
1793 return;
rginda8ba33642011-12-14 12:31:31 -08001794
1795 var self = this;
rginda87b86462011-12-14 13:48:03 -08001796 this.timeouts_.redraw = setTimeout(function() {
1797 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001798 self.scrollPort_.redraw_();
1799 }, 0);
1800};
1801
1802/**
1803 * Request that the ScrollPort be scrolled to the bottom.
1804 *
1805 * The scroll will happen asynchronously, soon after the call stack winds down.
1806 * Multiple calls will be coalesced into a single scroll.
1807 *
1808 * This affects the scrollbar position of the ScrollPort, and has nothing to
1809 * do with the VT scroll commands.
1810 */
1811hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1812 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001813 return;
rginda8ba33642011-12-14 12:31:31 -08001814
1815 var self = this;
1816 this.timeouts_.scrollDown = setTimeout(function() {
1817 delete self.timeouts_.scrollDown;
1818 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1819 }, 10);
1820};
1821
1822/**
1823 * Move the cursor up a specified number of rows.
1824 *
1825 * @param {integer} count The number of rows to move the cursor.
1826 */
1827hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001828 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001829};
1830
1831/**
1832 * Move the cursor down a specified number of rows.
1833 *
1834 * @param {integer} count The number of rows to move the cursor.
1835 */
1836hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001837 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001838 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1839 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1840 this.screenSize.height - 1);
1841
rgindacbbd7482012-06-13 15:06:16 -07001842 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08001843 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001844 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001845};
1846
1847/**
1848 * Move the cursor left a specified number of columns.
1849 *
1850 * @param {integer} count The number of columns to move the cursor.
1851 */
1852hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001853 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001854};
1855
1856/**
1857 * Move the cursor right a specified number of columns.
1858 *
1859 * @param {integer} count The number of columns to move the cursor.
1860 */
1861hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001862 count = count || 1;
rgindacbbd7482012-06-13 15:06:16 -07001863 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001864 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001865 this.setCursorColumn(column);
1866};
1867
1868/**
1869 * Reverse the foreground and background colors of the terminal.
1870 *
1871 * This only affects text that was drawn with no attributes.
1872 *
1873 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1874 * been drawn with attributes that happen to coincide with the default
1875 * 'no-attribute' colors. My guess is probably not.
1876 */
1877hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001878 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001879 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08001880 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
1881 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08001882 } else {
rginda9f5222b2012-03-05 11:53:28 -08001883 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
1884 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08001885 }
1886};
1887
1888/**
rginda87b86462011-12-14 13:48:03 -08001889 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07001890 *
1891 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08001892 */
1893hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08001894 this.cursorNode_.style.backgroundColor =
1895 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001896
1897 var self = this;
1898 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08001899 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08001900 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07001901
1902 if (this.bellAudio_.getAttribute('src')) {
Robert Gindaa6331372013-03-19 10:35:39 -07001903 if (this.bellSquelchTimeout_)
Robert Ginda92e18102013-03-14 13:56:37 -07001904 return;
1905
1906 this.bellAudio_.play();
1907
1908 this.bellSequelchTimeout_ = setTimeout(function() {
1909 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07001910 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07001911 } else {
1912 delete this.bellSquelchTimeout_;
1913 }
rginda87b86462011-12-14 13:48:03 -08001914};
1915
1916/**
rginda8ba33642011-12-14 12:31:31 -08001917 * Set the origin mode bit.
1918 *
1919 * If origin mode is on, certain VT cursor and scrolling commands measure their
1920 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1921 * to the top of the addressable screen.
1922 *
1923 * Defaults to off.
1924 *
1925 * @param {boolean} state True to set origin mode, false to unset.
1926 */
1927hterm.Terminal.prototype.setOriginMode = function(state) {
1928 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08001929 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08001930};
1931
1932/**
1933 * Set the insert mode bit.
1934 *
1935 * If insert mode is on, existing text beyond the cursor position will be
1936 * shifted right to make room for new text. Otherwise, new text overwrites
1937 * any existing text.
1938 *
1939 * Defaults to off.
1940 *
1941 * @param {boolean} state True to set insert mode, false to unset.
1942 */
1943hterm.Terminal.prototype.setInsertMode = function(state) {
1944 this.options_.insertMode = state;
1945};
1946
1947/**
rginda87b86462011-12-14 13:48:03 -08001948 * Set the auto carriage return bit.
1949 *
1950 * If auto carriage return is on then a formfeed character is interpreted
1951 * as a newline, otherwise it's the same as a linefeed. The difference boils
1952 * down to whether or not the cursor column is reset.
1953 */
1954hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1955 this.options_.autoCarriageReturn = state;
1956};
1957
1958/**
rginda8ba33642011-12-14 12:31:31 -08001959 * Set the wraparound mode bit.
1960 *
1961 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1962 * to the start of the following row. Otherwise, the cursor is clamped to the
1963 * end of the screen and attempts to write past it are ignored.
1964 *
1965 * Defaults to on.
1966 *
1967 * @param {boolean} state True to set wraparound mode, false to unset.
1968 */
1969hterm.Terminal.prototype.setWraparound = function(state) {
1970 this.options_.wraparound = state;
1971};
1972
1973/**
1974 * Set the reverse-wraparound mode bit.
1975 *
1976 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1977 * to the end of the previous row. Otherwise, the cursor is clamped to column
1978 * 0.
1979 *
1980 * Defaults to off.
1981 *
1982 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
1983 */
1984hterm.Terminal.prototype.setReverseWraparound = function(state) {
1985 this.options_.reverseWraparound = state;
1986};
1987
1988/**
1989 * Selects between the primary and alternate screens.
1990 *
1991 * If alternate mode is on, the alternate screen is active. Otherwise the
1992 * primary screen is active.
1993 *
1994 * Swapping screens has no effect on the scrollback buffer.
1995 *
1996 * Each screen maintains its own cursor position.
1997 *
1998 * Defaults to off.
1999 *
2000 * @param {boolean} state True to set alternate mode, false to unset.
2001 */
2002hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002003 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002004 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2005
rginda35c456b2012-02-09 17:29:05 -08002006 if (this.screen_.rowsArray.length &&
2007 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2008 // If the screen changed sizes while we were away, our rowIndexes may
2009 // be incorrect.
2010 var offset = this.scrollbackRows_.length;
2011 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002012 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002013 ary[i].rowIndex = offset + i;
2014 }
2015 }
rginda8ba33642011-12-14 12:31:31 -08002016
rginda35c456b2012-02-09 17:29:05 -08002017 this.realizeWidth_(this.screenSize.width);
2018 this.realizeHeight_(this.screenSize.height);
2019 this.scrollPort_.syncScrollHeight();
2020 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002021
rginda6d397402012-01-17 10:58:29 -08002022 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002023 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002024};
2025
2026/**
2027 * Set the cursor-blink mode bit.
2028 *
2029 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2030 * a visible cursor does not blink.
2031 *
2032 * You should make sure to turn blinking off if you're going to dispose of a
2033 * terminal, otherwise you'll leak a timeout.
2034 *
2035 * Defaults to on.
2036 *
2037 * @param {boolean} state True to set cursor-blink mode, false to unset.
2038 */
2039hterm.Terminal.prototype.setCursorBlink = function(state) {
2040 this.options_.cursorBlink = state;
2041
2042 if (!state && this.timeouts_.cursorBlink) {
2043 clearTimeout(this.timeouts_.cursorBlink);
2044 delete this.timeouts_.cursorBlink;
2045 }
2046
2047 if (this.options_.cursorVisible)
2048 this.setCursorVisible(true);
2049};
2050
2051/**
2052 * Set the cursor-visible mode bit.
2053 *
2054 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2055 *
2056 * Defaults to on.
2057 *
2058 * @param {boolean} state True to set cursor-visible mode, false to unset.
2059 */
2060hterm.Terminal.prototype.setCursorVisible = function(state) {
2061 this.options_.cursorVisible = state;
2062
2063 if (!state) {
rginda87b86462011-12-14 13:48:03 -08002064 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002065 return;
2066 }
2067
rginda87b86462011-12-14 13:48:03 -08002068 this.syncCursorPosition_();
2069
2070 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002071
2072 if (this.options_.cursorBlink) {
2073 if (this.timeouts_.cursorBlink)
2074 return;
2075
2076 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
2077 500);
2078 } else {
2079 if (this.timeouts_.cursorBlink) {
2080 clearTimeout(this.timeouts_.cursorBlink);
2081 delete this.timeouts_.cursorBlink;
2082 }
2083 }
2084};
2085
2086/**
rginda87b86462011-12-14 13:48:03 -08002087 * Synchronizes the visible cursor and document selection with the current
2088 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002089 */
2090hterm.Terminal.prototype.syncCursorPosition_ = function() {
2091 var topRowIndex = this.scrollPort_.getTopRowIndex();
2092 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2093 var cursorRowIndex = this.scrollbackRows_.length +
2094 this.screen_.cursorPosition.row;
2095
2096 if (cursorRowIndex > bottomRowIndex) {
2097 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002098 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002099 return;
2100 }
2101
rginda35c456b2012-02-09 17:29:05 -08002102 this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px';
2103 this.cursorNode_.style.height = this.scrollPort_.characterSize.height + 'px';
2104
rginda8ba33642011-12-14 12:31:31 -08002105 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002106 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2107 'px';
2108 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2109 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002110
2111 this.cursorNode_.setAttribute('title',
2112 '(' + this.screen_.cursorPosition.row +
2113 ', ' + this.screen_.cursorPosition.column +
2114 ')');
2115
2116 // Update the caret for a11y purposes.
2117 var selection = this.document_.getSelection();
2118 if (selection && selection.isCollapsed)
2119 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002120};
2121
2122/**
2123 * Synchronizes the visible cursor with the current cursor coordinates.
2124 *
2125 * The sync will happen asynchronously, soon after the call stack winds down.
2126 * Multiple calls will be coalesced into a single sync.
2127 */
2128hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2129 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002130 return;
rginda8ba33642011-12-14 12:31:31 -08002131
2132 var self = this;
2133 this.timeouts_.syncCursor = setTimeout(function() {
2134 self.syncCursorPosition_();
2135 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002136 }, 0);
2137};
2138
rgindacc2996c2012-02-24 14:59:31 -08002139/**
rgindaf522ce02012-04-17 17:49:17 -07002140 * Show or hide the zoom warning.
2141 *
2142 * The zoom warning is a message warning the user that their browser zoom must
2143 * be set to 100% in order for hterm to function properly.
2144 *
2145 * @param {boolean} state True to show the message, false to hide it.
2146 */
2147hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2148 if (!this.zoomWarningNode_) {
2149 if (!state)
2150 return;
2151
2152 this.zoomWarningNode_ = this.document_.createElement('div');
2153 this.zoomWarningNode_.style.cssText = (
2154 'color: black;' +
2155 'background-color: #ff2222;' +
2156 'font-size: large;' +
2157 'border-radius: 8px;' +
2158 'opacity: 0.75;' +
2159 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2160 'top: 0.5em;' +
2161 'right: 1.2em;' +
2162 'position: absolute;' +
2163 '-webkit-text-size-adjust: none;' +
2164 '-webkit-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002165 }
2166
Robert Gindab4839c22013-02-28 16:52:10 -08002167 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2168 hterm.zoomWarningMessage,
2169 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2170
rgindaf522ce02012-04-17 17:49:17 -07002171 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2172
2173 if (state) {
2174 if (!this.zoomWarningNode_.parentNode)
2175 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2176 } else if (this.zoomWarningNode_.parentNode) {
2177 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2178 }
2179};
2180
2181/**
rgindacc2996c2012-02-24 14:59:31 -08002182 * Show the terminal overlay for a given amount of time.
2183 *
2184 * The terminal overlay appears in inverse video in a large font, centered
2185 * over the terminal. You should probably keep the overlay message brief,
2186 * since it's in a large font and you probably aren't going to check the size
2187 * of the terminal first.
2188 *
2189 * @param {string} msg The text (not HTML) message to display in the overlay.
2190 * @param {number} opt_timeout The amount of time to wait before fading out
2191 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2192 * stay up forever (or until the next overlay).
2193 */
2194hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002195 if (!this.overlayNode_) {
2196 if (!this.div_)
2197 return;
2198
2199 this.overlayNode_ = this.document_.createElement('div');
2200 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002201 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002202 'font-size: xx-large;' +
2203 'opacity: 0.75;' +
2204 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2205 'position: absolute;' +
2206 '-webkit-user-select: none;' +
2207 '-webkit-transition: opacity 180ms ease-in;');
2208 }
2209
rginda9f5222b2012-03-05 11:53:28 -08002210 this.overlayNode_.style.color = this.prefs_.get('background-color');
2211 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2212 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2213
rgindaf0090c92012-02-10 14:58:52 -08002214 this.overlayNode_.textContent = msg;
2215 this.overlayNode_.style.opacity = '0.75';
2216
2217 if (!this.overlayNode_.parentNode)
2218 this.div_.appendChild(this.overlayNode_);
2219
Robert Ginda97769282013-02-01 15:30:30 -08002220 var divSize = hterm.getClientSize(this.div_);
2221 var overlaySize = hterm.getClientSize(this.overlayNode_);
2222
2223 this.overlayNode_.style.top = (divSize.height - overlaySize.height) / 2;
2224 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
2225 this.scrollPort_.currentScrollbarWidthPx) / 2;
rgindaf0090c92012-02-10 14:58:52 -08002226
2227 var self = this;
2228
2229 if (this.overlayTimeout_)
2230 clearTimeout(this.overlayTimeout_);
2231
rgindacc2996c2012-02-24 14:59:31 -08002232 if (opt_timeout === null)
2233 return;
2234
rgindaf0090c92012-02-10 14:58:52 -08002235 this.overlayTimeout_ = setTimeout(function() {
2236 self.overlayNode_.style.opacity = '0';
2237 setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002238 if (self.overlayNode_.parentNode)
2239 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002240 self.overlayTimeout_ = null;
2241 self.overlayNode_.style.opacity = '0.75';
2242 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002243 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002244};
2245
rginda4bba5e12012-06-20 16:15:30 -07002246/**
2247 * Paste from the system clipboard to the terminal.
2248 */
2249hterm.Terminal.prototype.paste = function() {
2250 hterm.pasteFromClipboard(this.document_);
2251};
2252
2253/**
2254 * Copy a string to the system clipboard.
2255 *
2256 * Note: If there is a selected range in the terminal, it'll be cleared.
2257 */
2258hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda9fb38222012-09-11 14:19:12 -07002259 if (this.prefs_.get('enable-clipboard-notice'))
Robert Gindab4839c22013-02-28 16:52:10 -08002260 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
rgindaa09e7332012-08-17 12:49:51 -07002261
2262 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002263 copySource.textContent = str;
2264 copySource.style.cssText = (
2265 '-webkit-user-select: text;' +
2266 'position: absolute;' +
2267 'top: -99px');
2268
2269 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002270
rginda4bba5e12012-06-20 16:15:30 -07002271 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002272 var anchorNode = selection.anchorNode;
2273 var anchorOffset = selection.anchorOffset;
2274 var focusNode = selection.focusNode;
2275 var focusOffset = selection.focusOffset;
2276
rginda4bba5e12012-06-20 16:15:30 -07002277 selection.selectAllChildren(copySource);
2278
rgindaa09e7332012-08-17 12:49:51 -07002279 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002280
rgindafaa74742012-08-21 13:34:03 -07002281 selection.collapse(anchorNode, anchorOffset);
2282 selection.extend(focusNode, focusOffset);
2283
rginda4bba5e12012-06-20 16:15:30 -07002284 copySource.parentNode.removeChild(copySource);
2285};
2286
rgindaa09e7332012-08-17 12:49:51 -07002287hterm.Terminal.prototype.getSelectionText = function() {
2288 var selection = this.scrollPort_.selection;
2289 selection.sync();
2290
2291 if (selection.isCollapsed)
2292 return null;
2293
2294
2295 // Start offset measures from the beginning of the line.
2296 var startOffset = selection.startOffset;
2297 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002298
Robert Gindafdbb3f22012-09-06 20:23:06 -07002299 if (node.nodeName != 'X-ROW') {
2300 // If the selection doesn't start on an x-row node, then it must be
2301 // somewhere inside the x-row. Add any characters from previous siblings
2302 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002303
2304 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2305 // If node is the text node in a styled span, move up to the span node.
2306 node = node.parentNode;
2307 }
2308
Robert Gindafdbb3f22012-09-06 20:23:06 -07002309 while (node.previousSibling) {
2310 node = node.previousSibling;
2311 startOffset += node.textContent.length;
2312 }
rgindaa09e7332012-08-17 12:49:51 -07002313 }
2314
2315 // End offset measures from the end of the line.
2316 var endOffset = selection.endNode.textContent.length - selection.endOffset;
2317 var node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002318
Robert Gindafdbb3f22012-09-06 20:23:06 -07002319 if (node.nodeName != 'X-ROW') {
2320 // If the selection doesn't end on an x-row node, then it must be
2321 // somewhere inside the x-row. Add any characters from following siblings
2322 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002323
2324 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2325 // If node is the text node in a styled span, move up to the span node.
2326 node = node.parentNode;
2327 }
2328
Robert Gindafdbb3f22012-09-06 20:23:06 -07002329 while (node.nextSibling) {
2330 node = node.nextSibling;
2331 endOffset += node.textContent.length;
2332 }
rgindaa09e7332012-08-17 12:49:51 -07002333 }
2334
2335 var rv = this.getRowsText(selection.startRow.rowIndex,
2336 selection.endRow.rowIndex + 1);
2337 return rv.substring(startOffset, rv.length - endOffset);
2338};
2339
rginda4bba5e12012-06-20 16:15:30 -07002340/**
2341 * Copy the current selection to the system clipboard, then clear it after a
2342 * short delay.
2343 */
2344hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002345 var text = this.getSelectionText();
2346 if (text != null)
2347 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002348};
2349
rgindaf0090c92012-02-10 14:58:52 -08002350hterm.Terminal.prototype.overlaySize = function() {
2351 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2352};
2353
rginda87b86462011-12-14 13:48:03 -08002354/**
2355 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2356 *
2357 * @param {string} string The VT string representing the keystroke.
2358 */
2359hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002360 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002361 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2362
2363 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08002364};
2365
2366/**
rgindad5613292012-06-19 15:40:37 -07002367 * Add the terminalRow and terminalColumn properties to mouse events and
2368 * then forward on to onMouse().
2369 *
2370 * The terminalRow and terminalColumn properties contain the (row, column)
2371 * coordinates for the mouse event.
2372 */
2373hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002374 if (e.processedByTerminalHandler_) {
2375 // We register our event handlers on the document, as well as the cursor
2376 // and the scroll blocker. Mouse events that occur on the cursor or
2377 // scroll blocker will also appear on the document, but we don't want to
2378 // process them twice.
2379 //
2380 // We can't just prevent bubbling because that has other side effects, so
2381 // we decorate the event object with this property instead.
2382 return;
2383 }
2384
2385 e.processedByTerminalHandler_ = true;
2386
rginda4bba5e12012-06-20 16:15:30 -07002387 if (e.type == 'mousedown' && e.which == this.mousePasteButton) {
2388 this.paste();
2389 return;
2390 }
2391
2392 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2393 !this.document_.getSelection().isCollapsed) {
rgindafaa74742012-08-21 13:34:03 -07002394 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002395 return;
2396 }
2397
rgindad5613292012-06-19 15:40:37 -07002398 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2399 this.scrollPort_.characterSize.height) + 1;
2400 e.terminalColumn = parseInt(e.clientX /
2401 this.scrollPort_.characterSize.width) + 1;
2402
2403 if (e.type == 'mousedown') {
2404 if (e.terminalColumn > this.screenSize.width) {
2405 // Mousedown in the scrollbar area.
2406 return;
2407 }
2408
2409 if (!this.enableMouseDragScroll) {
2410 // Move the scroll-blocker into place if we want to keep the scrollport
2411 // from scrolling.
2412 this.scrollBlockerNode_.engaged = true;
2413 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2414 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2415 }
2416 } else if (this.scrollBlockerNode_.engaged &&
2417 (e.type == 'mousemove' || e.type == 'mouseup')) {
2418 // Disengage the scroll-blocker after one of these events.
2419 this.scrollBlockerNode_.engaged = false;
2420 this.scrollBlockerNode_.style.top = '-99px';
2421 }
2422
rgindafaa74742012-08-21 13:34:03 -07002423 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07002424};
2425
2426/**
2427 * Clients should override this if they care to know about mouse events.
2428 *
2429 * The event parameter will be a normal DOM mouse click event with additional
2430 * 'terminalRow' and 'terminalColumn' properties.
2431 */
2432hterm.Terminal.prototype.onMouse = function(e) { };
2433
2434/**
rginda8e92a692012-05-20 19:37:20 -07002435 * React when focus changes.
2436 */
2437hterm.Terminal.prototype.onFocusChange_ = function(state) {
2438 this.cursorNode_.setAttribute('focus', state ? 'true' : 'false');
2439};
2440
2441/**
rginda8ba33642011-12-14 12:31:31 -08002442 * React when the ScrollPort is scrolled.
2443 */
2444hterm.Terminal.prototype.onScroll_ = function() {
2445 this.scheduleSyncCursorPosition_();
2446};
2447
2448/**
rginda9846e2f2012-01-27 13:53:33 -08002449 * React when text is pasted into the scrollPort.
2450 */
2451hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaf2547f12012-10-25 20:36:21 -07002452 var text = this.vt.encodeUTF8(e.text);
2453 text = text.replace(/\n/mg, '\r');
2454 this.io.onVTKeystroke(text);
rginda9846e2f2012-01-27 13:53:33 -08002455};
2456
2457/**
rgindaa09e7332012-08-17 12:49:51 -07002458 * React when the user tries to copy from the scrollPort.
2459 */
2460hterm.Terminal.prototype.onCopy_ = function(e) {
2461 e.preventDefault();
rgindafaa74742012-08-21 13:34:03 -07002462 this.copySelectionToClipboard();
rgindaa09e7332012-08-17 12:49:51 -07002463};
2464
2465/**
rginda8ba33642011-12-14 12:31:31 -08002466 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002467 *
2468 * Note: This function should not directly contain code that alters the internal
2469 * state of the terminal. That kind of code belongs in realizeWidth or
2470 * realizeHeight, so that it can be executed synchronously in the case of a
2471 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002472 */
2473hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08002474 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002475 this.scrollPort_.characterSize.width);
2476 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
2477 this.scrollPort_.characterSize.height);
2478
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002479 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08002480 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002481 // gets removed from the document or during the initial load, and we can't
2482 // deal with that.
rginda35c456b2012-02-09 17:29:05 -08002483 return;
2484 }
2485
rgindaa8ba17d2012-08-15 14:41:10 -07002486 var isNewSize = (columnCount != this.screenSize.width ||
2487 rowCount != this.screenSize.height);
2488
2489 // We do this even if the size didn't change, just to be sure everything is
2490 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002491 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07002492 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07002493
2494 if (isNewSize)
2495 this.overlaySize();
2496
2497 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08002498};
2499
2500/**
2501 * Service the cursor blink timeout.
2502 */
2503hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08002504 if (this.cursorNode_.style.opacity == '0') {
2505 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002506 } else {
rginda87b86462011-12-14 13:48:03 -08002507 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002508 }
2509};
David Reveman8f552492012-03-28 12:18:41 -04002510
2511/**
2512 * Set the scrollbar-visible mode bit.
2513 *
2514 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2515 * Otherwise it will not.
2516 *
2517 * Defaults to on.
2518 *
2519 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2520 */
2521hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2522 this.scrollPort_.setScrollbarVisible(state);
2523};