blob: c91254e5b0bd46cf9a8be5e71ec1cfbc445e7470 [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 Ginda57f03b42012-09-13 11:02:48 -0700256 'font-family': function(v) {
257 terminal.syncFontFamily();
258 },
rginda30f20f62012-04-05 16:36:19 -0700259
Robert Ginda57f03b42012-09-13 11:02:48 -0700260 'font-size': function(v) {
261 terminal.setFontSize(v);
262 },
rginda9875d902012-08-20 16:21:57 -0700263
Robert Ginda57f03b42012-09-13 11:02:48 -0700264 'font-smoothing': function(v) {
265 terminal.syncFontFamily();
266 },
rgindade84e382012-04-20 15:39:31 -0700267
Robert Ginda57f03b42012-09-13 11:02:48 -0700268 'foreground-color': function(v) {
269 terminal.setForegroundColor(v);
270 },
rginda30f20f62012-04-05 16:36:19 -0700271
Robert Ginda57f03b42012-09-13 11:02:48 -0700272 'home-keys-scroll': function(v) {
273 terminal.keyboard.homeKeysScroll = v;
274 },
rginda4bba5e12012-06-20 16:15:30 -0700275
Robert Ginda57f03b42012-09-13 11:02:48 -0700276 'max-string-sequence': function(v) {
277 terminal.vt.maxStringSequence = v;
278 },
rginda11057d52012-04-25 12:29:56 -0700279
Robert Ginda57f03b42012-09-13 11:02:48 -0700280 'meta-sends-escape': function(v) {
281 terminal.keyboard.metaSendsEscape = v;
282 },
rginda30f20f62012-04-05 16:36:19 -0700283
Robert Ginda57f03b42012-09-13 11:02:48 -0700284 'mouse-cell-motion-trick': function(v) {
285 terminal.vt.setMouseCellMotionTrick(v);
286 },
Robert Ginda9fb38222012-09-11 14:19:12 -0700287
Robert Ginda57f03b42012-09-13 11:02:48 -0700288 'mouse-paste-button': function(v) {
289 terminal.syncMousePasteButton();
290 },
rgindaa8ba17d2012-08-15 14:41:10 -0700291
Robert Ginda40932892012-12-10 17:26:40 -0800292 'pass-alt-number': function(v) {
293 if (v == null) {
294 var osx = window.navigator.userAgent.match(/Mac OS X/);
295
296 // Let Alt-1..9 pass to the browser (to control tab switching) on
297 // non-OS X systems, or if hterm is not opened in an app window.
298 v = (!osx && hterm.windowType != 'popup');
299 }
300
301 terminal.passAltNumber = v;
302 },
303
304 'pass-ctrl-number': function(v) {
305 if (v == null) {
306 var osx = window.navigator.userAgent.match(/Mac OS X/);
307
308 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
309 // non-OS X systems, or if hterm is not opened in an app window.
310 v = (!osx && hterm.windowType != 'popup');
311 }
312
313 terminal.passCtrlNumber = v;
314 },
315
316 'pass-meta-number': function(v) {
317 if (v == null) {
318 var osx = window.navigator.userAgent.match(/Mac OS X/);
319
320 // Let Meta-1..9 pass to the browser (to control tab switching) on
321 // OS X systems, or if hterm is not opened in an app window.
322 v = (osx && hterm.windowType != 'popup');
323 }
324
325 terminal.passMetaNumber = v;
326 },
327
Robert Ginda57f03b42012-09-13 11:02:48 -0700328 'scroll-on-keystroke': function(v) {
329 terminal.scrollOnKeystroke_ = v;
330 },
rginda9f5222b2012-03-05 11:53:28 -0800331
Robert Ginda57f03b42012-09-13 11:02:48 -0700332 'scroll-on-output': function(v) {
333 terminal.scrollOnOutput_ = v;
334 },
rginda30f20f62012-04-05 16:36:19 -0700335
Robert Ginda57f03b42012-09-13 11:02:48 -0700336 'scrollbar-visible': function(v) {
337 terminal.setScrollbarVisible(v);
338 },
rginda9f5222b2012-03-05 11:53:28 -0800339
Robert Ginda57f03b42012-09-13 11:02:48 -0700340 'shift-insert-paste': function(v) {
341 terminal.keyboard.shiftInsertPaste = v;
342 },
rginda9f5222b2012-03-05 11:53:28 -0800343
Robert Ginda57f03b42012-09-13 11:02:48 -0700344 'page-keys-scroll': function(v) {
345 terminal.keyboard.pageKeysScroll = v;
346 }
347 });
rginda30f20f62012-04-05 16:36:19 -0700348
Robert Ginda57f03b42012-09-13 11:02:48 -0700349 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800350 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700351
352 if (opt_callback)
353 opt_callback();
354 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800355};
356
rginda8e92a692012-05-20 19:37:20 -0700357
358/**
359 * Set the color for the cursor.
360 *
361 * If you want this setting to persist, set it through prefs_, rather than
362 * with this method.
363 */
364hterm.Terminal.prototype.setCursorColor = function(color) {
365 this.cursorNode_.style.backgroundColor = color;
366 this.cursorNode_.style.borderColor = color;
367};
368
369/**
370 * Return the current cursor color as a string.
371 */
372hterm.Terminal.prototype.getCursorColor = function() {
373 return this.cursorNode_.style.backgroundColor;
374};
375
376/**
rgindad5613292012-06-19 15:40:37 -0700377 * Enable or disable mouse based text selection in the terminal.
378 */
379hterm.Terminal.prototype.setSelectionEnabled = function(state) {
380 this.enableMouseDragScroll = state;
381 this.scrollPort_.setSelectionEnabled(state);
382};
383
384/**
rginda8e92a692012-05-20 19:37:20 -0700385 * Set the background color.
386 *
387 * If you want this setting to persist, set it through prefs_, rather than
388 * with this method.
389 */
390hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700391 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700392 this.primaryScreen_.textAttributes.setDefaults(
393 this.foregroundColor_, this.backgroundColor_);
394 this.alternateScreen_.textAttributes.setDefaults(
395 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700396 this.scrollPort_.setBackgroundColor(color);
397};
398
rginda9f5222b2012-03-05 11:53:28 -0800399/**
400 * Return the current terminal background color.
401 *
402 * Intended for use by other classes, so we don't have to expose the entire
403 * prefs_ object.
404 */
405hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700406 return this.backgroundColor_;
407};
408
409/**
410 * Set the foreground color.
411 *
412 * If you want this setting to persist, set it through prefs_, rather than
413 * with this method.
414 */
415hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700416 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700417 this.primaryScreen_.textAttributes.setDefaults(
418 this.foregroundColor_, this.backgroundColor_);
419 this.alternateScreen_.textAttributes.setDefaults(
420 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700421 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800422};
423
424/**
425 * Return the current terminal foreground color.
426 *
427 * Intended for use by other classes, so we don't have to expose the entire
428 * prefs_ object.
429 */
430hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700431 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800432};
433
434/**
rginda87b86462011-12-14 13:48:03 -0800435 * Create a new instance of a terminal command and run it with a given
436 * argument string.
437 *
438 * @param {function} commandClass The constructor for a terminal command.
439 * @param {string} argString The argument string to pass to the command.
440 */
441hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700442 var environment = this.prefs_.get('environment');
443 if (typeof environment != 'object' || environment == null)
444 environment = {};
445
rginda87b86462011-12-14 13:48:03 -0800446 var self = this;
447 this.command = new commandClass(
448 { argString: argString || '',
449 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700450 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800451 onExit: function(code) {
452 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800453 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700454 if (self.prefs_.get('close-on-exit'))
455 window.close();
rginda87b86462011-12-14 13:48:03 -0800456 }
457 });
458
rgindafeaf3142012-01-31 15:14:20 -0800459 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800460 this.command.run();
461};
462
463/**
rgindafeaf3142012-01-31 15:14:20 -0800464 * Returns true if the current screen is the primary screen, false otherwise.
465 */
466hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700467 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800468};
469
470/**
471 * Install the keyboard handler for this terminal.
472 *
473 * This will prevent the browser from seeing any keystrokes sent to the
474 * terminal.
475 */
476hterm.Terminal.prototype.installKeyboard = function() {
477 this.keyboard.installKeyboard(this.document_.body.firstChild);
478}
479
480/**
481 * Uninstall the keyboard handler for this terminal.
482 */
483hterm.Terminal.prototype.uninstallKeyboard = function() {
484 this.keyboard.installKeyboard(null);
485}
486
487/**
rginda35c456b2012-02-09 17:29:05 -0800488 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800489 *
490 * Call setFontSize(0) to reset to the default font size.
491 *
492 * This function does not modify the font-size preference.
493 *
494 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800495 */
496hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800497 if (px === 0)
498 px = this.prefs_.get('font-size');
499
rginda35c456b2012-02-09 17:29:05 -0800500 this.scrollPort_.setFontSize(px);
501};
502
503/**
504 * Get the current font size.
505 */
506hterm.Terminal.prototype.getFontSize = function() {
507 return this.scrollPort_.getFontSize();
508};
509
510/**
rginda8e92a692012-05-20 19:37:20 -0700511 * Get the current font family.
512 */
513hterm.Terminal.prototype.getFontFamily = function() {
514 return this.scrollPort_.getFontFamily();
515};
516
517/**
rginda35c456b2012-02-09 17:29:05 -0800518 * Set the CSS "font-family" for this terminal.
519 */
rginda9f5222b2012-03-05 11:53:28 -0800520hterm.Terminal.prototype.syncFontFamily = function() {
521 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
522 this.prefs_.get('font-smoothing'));
523 this.syncBoldSafeState();
524};
525
rginda4bba5e12012-06-20 16:15:30 -0700526/**
527 * Set this.mousePasteButton based on the mouse-paste-button pref,
528 * autodetecting if necessary.
529 */
530hterm.Terminal.prototype.syncMousePasteButton = function() {
531 var button = this.prefs_.get('mouse-paste-button');
532 if (typeof button == 'number') {
533 this.mousePasteButton = button;
534 return;
535 }
536
537 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
538 if (!ary || ary[2] == 'CrOS') {
539 this.mousePasteButton = 2;
540 } else {
541 this.mousePasteButton = 3;
542 }
543};
544
545/**
546 * Enable or disable bold based on the enable-bold pref, autodetecting if
547 * necessary.
548 */
rginda9f5222b2012-03-05 11:53:28 -0800549hterm.Terminal.prototype.syncBoldSafeState = function() {
550 var enableBold = this.prefs_.get('enable-bold');
551 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700552 this.primaryScreen_.textAttributes.enableBold = enableBold;
553 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800554 return;
555 }
556
rgindaf7521392012-02-28 17:20:34 -0800557 var normalSize = this.scrollPort_.measureCharacterSize();
558 var boldSize = this.scrollPort_.measureCharacterSize('bold');
559
560 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800561 if (!isBoldSafe) {
562 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700563 'from normal. Font family is: ' +
564 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800565 }
rginda9f5222b2012-03-05 11:53:28 -0800566
Robert Gindaed016262012-10-26 16:27:09 -0700567 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
568 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800569};
570
571/**
rginda87b86462011-12-14 13:48:03 -0800572 * Return a copy of the current cursor position.
573 *
574 * @return {hterm.RowCol} The RowCol object representing the current position.
575 */
576hterm.Terminal.prototype.saveCursor = function() {
577 return this.screen_.cursorPosition.clone();
578};
579
rgindaa19afe22012-01-25 15:40:22 -0800580hterm.Terminal.prototype.getTextAttributes = function() {
581 return this.screen_.textAttributes;
582};
583
rginda1a09aa02012-06-18 21:11:25 -0700584hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
585 this.screen_.textAttributes = textAttributes;
586};
587
rginda87b86462011-12-14 13:48:03 -0800588/**
rgindaf522ce02012-04-17 17:49:17 -0700589 * Return the current browser zoom factor applied to the terminal.
590 *
591 * @return {number} The current browser zoom factor.
592 */
593hterm.Terminal.prototype.getZoomFactor = function() {
594 return this.scrollPort_.characterSize.zoomFactor;
595};
596
597/**
rginda9846e2f2012-01-27 13:53:33 -0800598 * Change the title of this terminal's window.
599 */
600hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800601 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800602};
603
604/**
rginda87b86462011-12-14 13:48:03 -0800605 * Restore a previously saved cursor position.
606 *
607 * @param {hterm.RowCol} cursor The position to restore.
608 */
609hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700610 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
611 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800612 this.screen_.setCursorPosition(row, column);
613 if (cursor.column > column ||
614 cursor.column == column && cursor.overflow) {
615 this.screen_.cursorPosition.overflow = true;
616 }
rginda87b86462011-12-14 13:48:03 -0800617};
618
619/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400620 * Clear the cursor's overflow flag.
621 */
622hterm.Terminal.prototype.clearCursorOverflow = function() {
623 this.screen_.cursorPosition.overflow = false;
624};
625
626/**
rginda87b86462011-12-14 13:48:03 -0800627 * Set the width of the terminal, resizing the UI to match.
628 */
629hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800630 if (columnCount == null) {
631 this.div_.style.width = '100%';
632 return;
633 }
634
rginda35c456b2012-02-09 17:29:05 -0800635 this.div_.style.width = this.scrollPort_.characterSize.width *
Robert Ginda97769282013-02-01 15:30:30 -0800636 columnCount + this.scrollPort_.currentScrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400637 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800638 this.scheduleSyncCursorPosition_();
639};
rginda87b86462011-12-14 13:48:03 -0800640
rgindac9bc5502012-01-18 11:48:44 -0800641/**
rginda35c456b2012-02-09 17:29:05 -0800642 * Set the height of the terminal, resizing the UI to match.
643 */
644hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800645 if (rowCount == null) {
646 this.div_.style.height = '100%';
647 return;
648 }
649
rginda35c456b2012-02-09 17:29:05 -0800650 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700651 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800652 this.realizeSize_(this.screenSize.width, rowCount);
653 this.scheduleSyncCursorPosition_();
654};
655
656/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400657 * Deal with terminal size changes.
658 *
659 */
660hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
661 if (columnCount != this.screenSize.width)
662 this.realizeWidth_(columnCount);
663
664 if (rowCount != this.screenSize.height)
665 this.realizeHeight_(rowCount);
666
667 // Send new terminal size to plugin.
668 this.io.onTerminalResize(columnCount, rowCount);
669};
670
671/**
rgindac9bc5502012-01-18 11:48:44 -0800672 * Deal with terminal width changes.
673 *
674 * This function does what needs to be done when the terminal width changes
675 * out from under us. It happens here rather than in onResize_() because this
676 * code may need to run synchronously to handle programmatic changes of
677 * terminal width.
678 *
679 * Relying on the browser to send us an async resize event means we may not be
680 * in the correct state yet when the next escape sequence hits.
681 */
682hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700683 if (columnCount <= 0)
684 throw new Error('Attempt to realize bad width: ' + columnCount);
685
rgindac9bc5502012-01-18 11:48:44 -0800686 var deltaColumns = columnCount - this.screen_.getWidth();
687
rginda87b86462011-12-14 13:48:03 -0800688 this.screenSize.width = columnCount;
689 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800690
691 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400692 if (this.defaultTabStops)
693 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800694 } else {
695 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400696 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800697 break;
698
699 this.tabStops_.pop();
700 }
701 }
702
703 this.screen_.setColumnCount(this.screenSize.width);
704};
705
706/**
707 * Deal with terminal height changes.
708 *
709 * This function does what needs to be done when the terminal height changes
710 * out from under us. It happens here rather than in onResize_() because this
711 * code may need to run synchronously to handle programmatic changes of
712 * terminal height.
713 *
714 * Relying on the browser to send us an async resize event means we may not be
715 * in the correct state yet when the next escape sequence hits.
716 */
717hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700718 if (rowCount <= 0)
719 throw new Error('Attempt to realize bad height: ' + rowCount);
720
rgindac9bc5502012-01-18 11:48:44 -0800721 var deltaRows = rowCount - this.screen_.getHeight();
722
723 this.screenSize.height = rowCount;
724
725 var cursor = this.saveCursor();
726
727 if (deltaRows < 0) {
728 // Screen got smaller.
729 deltaRows *= -1;
730 while (deltaRows) {
731 var lastRow = this.getRowCount() - 1;
732 if (lastRow - this.scrollbackRows_.length == cursor.row)
733 break;
734
735 if (this.getRowText(lastRow))
736 break;
737
738 this.screen_.popRow();
739 deltaRows--;
740 }
741
742 var ary = this.screen_.shiftRows(deltaRows);
743 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
744
745 // We just removed rows from the top of the screen, we need to update
746 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800747 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800748 } else if (deltaRows > 0) {
749 // Screen got larger.
750
751 if (deltaRows <= this.scrollbackRows_.length) {
752 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
753 var rows = this.scrollbackRows_.splice(
754 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
755 this.screen_.unshiftRows(rows);
756 deltaRows -= scrollbackCount;
757 cursor.row += scrollbackCount;
758 }
759
760 if (deltaRows)
761 this.appendRows_(deltaRows);
762 }
763
rginda35c456b2012-02-09 17:29:05 -0800764 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800765 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800766};
767
768/**
769 * Scroll the terminal to the top of the scrollback buffer.
770 */
771hterm.Terminal.prototype.scrollHome = function() {
772 this.scrollPort_.scrollRowToTop(0);
773};
774
775/**
776 * Scroll the terminal to the end.
777 */
778hterm.Terminal.prototype.scrollEnd = function() {
779 this.scrollPort_.scrollRowToBottom(this.getRowCount());
780};
781
782/**
783 * Scroll the terminal one page up (minus one line) relative to the current
784 * position.
785 */
786hterm.Terminal.prototype.scrollPageUp = function() {
787 var i = this.scrollPort_.getTopRowIndex();
788 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
789};
790
791/**
792 * Scroll the terminal one page down (minus one line) relative to the current
793 * position.
794 */
795hterm.Terminal.prototype.scrollPageDown = function() {
796 var i = this.scrollPort_.getTopRowIndex();
797 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800798};
799
rgindac9bc5502012-01-18 11:48:44 -0800800/**
Robert Ginda40932892012-12-10 17:26:40 -0800801 * Clear primary screen, secondary screen, and the scrollback buffer.
802 */
803hterm.Terminal.prototype.wipeContents = function() {
804 this.scrollbackRows_.length = 0;
805 this.scrollPort_.resetCache();
806
807 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
808 var bottom = screen.getHeight();
809 if (bottom > 0) {
810 this.renumberRows_(0, bottom);
811 this.clearHome(screen);
812 }
813 }.bind(this));
814
815 this.syncCursorPosition_();
816};
817
818/**
rgindac9bc5502012-01-18 11:48:44 -0800819 * Full terminal reset.
820 */
rginda87b86462011-12-14 13:48:03 -0800821hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800822 this.clearAllTabStops();
823 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -0700824
825 this.clearHome(this.primaryScreen_);
826 this.primaryScreen_.textAttributes.reset();
827
828 this.clearHome(this.alternateScreen_);
829 this.alternateScreen_.textAttributes.reset();
830
rgindab8bc8932012-04-27 12:45:03 -0700831 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
832
Robert Ginda92e18102013-03-14 13:56:37 -0700833 this.vt.reset();
834
rgindac9bc5502012-01-18 11:48:44 -0800835 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800836};
837
rgindac9bc5502012-01-18 11:48:44 -0800838/**
839 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -0700840 *
841 * Perform a soft reset to the default values listed in
842 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -0800843 */
rginda0f5c0292012-01-13 11:00:13 -0800844hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -0700845 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -0800846 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -0700847
rgindab8bc8932012-04-27 12:45:03 -0700848 // Xterm also resets the color palette on soft reset, even though it doesn't
849 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -0700850 this.primaryScreen_.textAttributes.resetColorPalette();
851 this.alternateScreen_.textAttributes.resetColorPalette();
852
rgindab8bc8932012-04-27 12:45:03 -0700853 // The xterm man page explicitly says this will happen on soft reset.
854 this.setVTScrollRegion(null, null);
855
856 // Xterm also shows the cursor on soft reset, but does not alter the blink
857 // state.
rgindaa19afe22012-01-25 15:40:22 -0800858 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -0800859};
860
rgindac9bc5502012-01-18 11:48:44 -0800861/**
862 * Move the cursor forward to the next tab stop, or to the last column
863 * if no more tab stops are set.
864 */
865hterm.Terminal.prototype.forwardTabStop = function() {
866 var column = this.screen_.cursorPosition.column;
867
868 for (var i = 0; i < this.tabStops_.length; i++) {
869 if (this.tabStops_[i] > column) {
870 this.setCursorColumn(this.tabStops_[i]);
871 return;
872 }
873 }
874
David Benjamin66e954d2012-05-05 21:08:12 -0400875 // xterm does not clear the overflow flag on HT or CHT.
876 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -0800877 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -0400878 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -0800879};
880
rgindac9bc5502012-01-18 11:48:44 -0800881/**
882 * Move the cursor backward to the previous tab stop, or to the first column
883 * if no previous tab stops are set.
884 */
885hterm.Terminal.prototype.backwardTabStop = function() {
886 var column = this.screen_.cursorPosition.column;
887
888 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
889 if (this.tabStops_[i] < column) {
890 this.setCursorColumn(this.tabStops_[i]);
891 return;
892 }
893 }
894
895 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800896};
897
rgindac9bc5502012-01-18 11:48:44 -0800898/**
899 * Set a tab stop at the given column.
900 *
901 * @param {int} column Zero based column.
902 */
903hterm.Terminal.prototype.setTabStop = function(column) {
904 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
905 if (this.tabStops_[i] == column)
906 return;
907
908 if (this.tabStops_[i] < column) {
909 this.tabStops_.splice(i + 1, 0, column);
910 return;
911 }
912 }
913
914 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800915};
916
rgindac9bc5502012-01-18 11:48:44 -0800917/**
918 * Clear the tab stop at the current cursor position.
919 *
920 * No effect if there is no tab stop at the current cursor position.
921 */
922hterm.Terminal.prototype.clearTabStopAtCursor = function() {
923 var column = this.screen_.cursorPosition.column;
924
925 var i = this.tabStops_.indexOf(column);
926 if (i == -1)
927 return;
928
929 this.tabStops_.splice(i, 1);
930};
931
932/**
933 * Clear all tab stops.
934 */
935hterm.Terminal.prototype.clearAllTabStops = function() {
936 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -0400937 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -0800938};
939
940/**
941 * Set up the default tab stops, starting from a given column.
942 *
943 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -0400944 * from the specified column, or 0 if no column is provided. It also flags
945 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -0800946 *
947 * This does not clear the existing tab stops first, use clearAllTabStops
948 * for that.
949 *
950 * @param {int} opt_start Optional starting zero based starting column, useful
951 * for filling out missing tab stops when the terminal is resized.
952 */
953hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
954 var start = opt_start || 0;
955 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -0400956 // Round start up to a default tab stop.
957 start = start - 1 - ((start - 1) % w) + w;
958 for (var i = start; i < this.screenSize.width; i += w) {
959 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -0800960 }
David Benjamin66e954d2012-05-05 21:08:12 -0400961
962 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -0800963};
964
rginda6d397402012-01-17 10:58:29 -0800965/**
rginda8ba33642011-12-14 12:31:31 -0800966 * Interpret a sequence of characters.
967 *
968 * Incomplete escape sequences are buffered until the next call.
969 *
970 * @param {string} str Sequence of characters to interpret or pass through.
971 */
972hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -0800973 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -0800974 this.scheduleSyncCursorPosition_();
975};
976
977/**
978 * Take over the given DIV for use as the terminal display.
979 *
980 * @param {HTMLDivElement} div The div to use as the terminal display.
981 */
982hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -0800983 this.div_ = div;
984
rginda8ba33642011-12-14 12:31:31 -0800985 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -0700986 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -0400987 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
988 this.scrollPort_.setBackgroundPosition(
989 this.prefs_.get('background-position'));
rginda30f20f62012-04-05 16:36:19 -0700990
rginda0918b652012-04-04 11:26:24 -0700991 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -0800992
rginda9f5222b2012-03-05 11:53:28 -0800993 this.setFontSize(this.prefs_.get('font-size'));
994 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -0800995
David Reveman8f552492012-03-28 12:18:41 -0400996 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
997
rginda8ba33642011-12-14 12:31:31 -0800998 this.document_ = this.scrollPort_.getDocument();
999
rginda4bba5e12012-06-20 16:15:30 -07001000 this.document_.body.oncontextmenu = function() { return false };
1001
1002 var onMouse = this.onMouse_.bind(this);
1003 this.document_.body.firstChild.addEventListener('mousedown', onMouse);
1004 this.document_.body.firstChild.addEventListener('mouseup', onMouse);
1005 this.document_.body.firstChild.addEventListener('mousemove', onMouse);
1006 this.scrollPort_.onScrollWheel = onMouse;
1007
rginda8e92a692012-05-20 19:37:20 -07001008 this.document_.body.firstChild.addEventListener(
1009 'focus', this.onFocusChange_.bind(this, true));
1010 this.document_.body.firstChild.addEventListener(
1011 'blur', this.onFocusChange_.bind(this, false));
1012
1013 var style = this.document_.createElement('style');
1014 style.textContent =
1015 ('.cursor-node[focus="false"] {' +
1016 ' box-sizing: border-box;' +
1017 ' background-color: transparent !important;' +
1018 ' border-width: 2px;' +
1019 ' border-style: solid;' +
1020 '}');
1021 this.document_.head.appendChild(style);
1022
rginda8ba33642011-12-14 12:31:31 -08001023 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001024 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001025 this.cursorNode_.style.cssText =
1026 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001027 'top: -99px;' +
1028 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001029 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1030 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001031 '-webkit-transition: opacity, background-color 100ms linear;');
1032 this.setCursorColor(this.prefs_.get('cursor-color'));
rgindad5613292012-06-19 15:40:37 -07001033
rginda8ba33642011-12-14 12:31:31 -08001034 this.document_.body.appendChild(this.cursorNode_);
1035
rgindad5613292012-06-19 15:40:37 -07001036 // When 'enableMouseDragScroll' is off we reposition this element directly
1037 // under the mouse cursor after a click. This makes Chrome associate
1038 // subsequent mousemove events with the scroll-blocker. Since the
1039 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1040 // events do not cause the scrollport to scroll.
1041 //
1042 // It's a hack, but it's the cleanest way I could find.
1043 this.scrollBlockerNode_ = this.document_.createElement('div');
1044 this.scrollBlockerNode_.style.cssText =
1045 ('position: absolute;' +
1046 'top: -99px;' +
1047 'display: block;' +
1048 'width: 10px;' +
1049 'height: 10px;');
1050 this.document_.body.appendChild(this.scrollBlockerNode_);
1051
1052 var onMouse = this.onMouse_.bind(this);
1053 this.scrollPort_.onScrollWheel = onMouse;
1054 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1055 ].forEach(function(event) {
1056 this.scrollBlockerNode_.addEventListener(event, onMouse);
1057 this.cursorNode_.addEventListener(event, onMouse);
1058 this.document_.addEventListener(event, onMouse);
1059 }.bind(this));
1060
1061 this.cursorNode_.addEventListener('mousedown', function() {
1062 setTimeout(this.focus.bind(this));
1063 }.bind(this));
1064
rgindade84e382012-04-20 15:39:31 -07001065 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
rginda8ba33642011-12-14 12:31:31 -08001066 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001067
rginda87b86462011-12-14 13:48:03 -08001068 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001069 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001070};
1071
rginda0918b652012-04-04 11:26:24 -07001072/**
1073 * Return the HTML document that contains the terminal DOM nodes.
1074 */
rginda87b86462011-12-14 13:48:03 -08001075hterm.Terminal.prototype.getDocument = function() {
1076 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001077};
1078
1079/**
rginda0918b652012-04-04 11:26:24 -07001080 * Focus the terminal.
1081 */
1082hterm.Terminal.prototype.focus = function() {
1083 this.scrollPort_.focus();
1084};
1085
1086/**
rginda8ba33642011-12-14 12:31:31 -08001087 * Return the HTML Element for a given row index.
1088 *
1089 * This is a method from the RowProvider interface. The ScrollPort uses
1090 * it to fetch rows on demand as they are scrolled into view.
1091 *
1092 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1093 * pairs to conserve memory.
1094 *
1095 * @param {integer} index The zero-based row index, measured relative to the
1096 * start of the scrollback buffer. On-screen rows will always have the
1097 * largest indicies.
1098 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1099 */
1100hterm.Terminal.prototype.getRowNode = function(index) {
1101 if (index < this.scrollbackRows_.length)
1102 return this.scrollbackRows_[index];
1103
1104 var screenIndex = index - this.scrollbackRows_.length;
1105 return this.screen_.rowsArray[screenIndex];
1106};
1107
1108/**
1109 * Return the text content for a given range of rows.
1110 *
1111 * This is a method from the RowProvider interface. The ScrollPort uses
1112 * it to fetch text content on demand when the user attempts to copy their
1113 * selection to the clipboard.
1114 *
1115 * @param {integer} start The zero-based row index to start from, measured
1116 * relative to the start of the scrollback buffer. On-screen rows will
1117 * always have the largest indicies.
1118 * @param {integer} end The zero-based row index to end on, measured
1119 * relative to the start of the scrollback buffer.
1120 * @return {string} A single string containing the text value of the range of
1121 * rows. Lines will be newline delimited, with no trailing newline.
1122 */
1123hterm.Terminal.prototype.getRowsText = function(start, end) {
1124 var ary = [];
1125 for (var i = start; i < end; i++) {
1126 var node = this.getRowNode(i);
1127 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001128 if (i < end - 1 && !node.getAttribute('line-overflow'))
1129 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001130 }
1131
rgindaa09e7332012-08-17 12:49:51 -07001132 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001133};
1134
1135/**
1136 * Return the text content for a given row.
1137 *
1138 * This is a method from the RowProvider interface. The ScrollPort uses
1139 * it to fetch text content on demand when the user attempts to copy their
1140 * selection to the clipboard.
1141 *
1142 * @param {integer} index The zero-based row index to return, measured
1143 * relative to the start of the scrollback buffer. On-screen rows will
1144 * always have the largest indicies.
1145 * @return {string} A string containing the text value of the selected row.
1146 */
1147hterm.Terminal.prototype.getRowText = function(index) {
1148 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001149 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001150};
1151
1152/**
1153 * Return the total number of rows in the addressable screen and in the
1154 * scrollback buffer of this terminal.
1155 *
1156 * This is a method from the RowProvider interface. The ScrollPort uses
1157 * it to compute the size of the scrollbar.
1158 *
1159 * @return {integer} The number of rows in this terminal.
1160 */
1161hterm.Terminal.prototype.getRowCount = function() {
1162 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1163};
1164
1165/**
1166 * Create DOM nodes for new rows and append them to the end of the terminal.
1167 *
1168 * This is the only correct way to add a new DOM node for a row. Notice that
1169 * the new row is appended to the bottom of the list of rows, and does not
1170 * require renumbering (of the rowIndex property) of previous rows.
1171 *
1172 * If you think you want a new blank row somewhere in the middle of the
1173 * terminal, look into moveRows_().
1174 *
1175 * This method does not pay attention to vtScrollTop/Bottom, since you should
1176 * be using moveRows() in cases where they would matter.
1177 *
1178 * The cursor will be positioned at column 0 of the first inserted line.
1179 */
1180hterm.Terminal.prototype.appendRows_ = function(count) {
1181 var cursorRow = this.screen_.rowsArray.length;
1182 var offset = this.scrollbackRows_.length + cursorRow;
1183 for (var i = 0; i < count; i++) {
1184 var row = this.document_.createElement('x-row');
1185 row.appendChild(this.document_.createTextNode(''));
1186 row.rowIndex = offset + i;
1187 this.screen_.pushRow(row);
1188 }
1189
1190 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1191 if (extraRows > 0) {
1192 var ary = this.screen_.shiftRows(extraRows);
1193 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001194 if (this.scrollPort_.isScrolledEnd)
1195 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001196 }
1197
1198 if (cursorRow >= this.screen_.rowsArray.length)
1199 cursorRow = this.screen_.rowsArray.length - 1;
1200
rginda87b86462011-12-14 13:48:03 -08001201 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001202};
1203
1204/**
1205 * Relocate rows from one part of the addressable screen to another.
1206 *
1207 * This is used to recycle rows during VT scrolls (those which are driven
1208 * by VT commands, rather than by the user manipulating the scrollbar.)
1209 *
1210 * In this case, the blank lines scrolled into the scroll region are made of
1211 * the nodes we scrolled off. These have their rowIndex properties carefully
1212 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001213 */
1214hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1215 var ary = this.screen_.removeRows(fromIndex, count);
1216 this.screen_.insertRows(toIndex, ary);
1217
1218 var start, end;
1219 if (fromIndex < toIndex) {
1220 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001221 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001222 } else {
1223 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001224 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001225 }
1226
1227 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001228 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001229};
1230
1231/**
1232 * Renumber the rowIndex property of the given range of rows.
1233 *
1234 * The start and end indicies are relative to the screen, not the scrollback.
1235 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001236 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001237 * no need to renumber scrollback rows.
1238 */
Robert Ginda40932892012-12-10 17:26:40 -08001239hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1240 var screen = opt_screen || this.screen_;
1241
rginda8ba33642011-12-14 12:31:31 -08001242 var offset = this.scrollbackRows_.length;
1243 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001244 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001245 }
1246};
1247
1248/**
1249 * Print a string to the terminal.
1250 *
1251 * This respects the current insert and wraparound modes. It will add new lines
1252 * to the end of the terminal, scrolling off the top into the scrollback buffer
1253 * if necessary.
1254 *
1255 * The string is *not* parsed for escape codes. Use the interpret() method if
1256 * that's what you're after.
1257 *
1258 * @param{string} str The string to print.
1259 */
1260hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001261 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001262
rgindaa9abdd82012-08-06 18:05:09 -07001263 while (startOffset < str.length) {
rgindaa09e7332012-08-17 12:49:51 -07001264 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1265 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001266 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001267 }
rgindaa19afe22012-01-25 15:40:22 -08001268
rgindaa9abdd82012-08-06 18:05:09 -07001269 var count = str.length - startOffset;
1270 var didOverflow = false;
1271 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001272
rgindaa9abdd82012-08-06 18:05:09 -07001273 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1274 didOverflow = true;
1275 count = this.screenSize.width - this.screen_.cursorPosition.column;
1276 }
rgindaa19afe22012-01-25 15:40:22 -08001277
rgindaa9abdd82012-08-06 18:05:09 -07001278 if (didOverflow && !this.options_.wraparound) {
1279 // If the string overflowed the line but wraparound is off, then the
1280 // last printed character should be the last of the string.
1281 // TODO: This will add to our problems with multibyte UTF-16 characters.
1282 substr = str.substr(startOffset, count - 1) +
1283 str.substr(str.length - 1);
1284 count = str.length;
1285 } else {
1286 substr = str.substr(startOffset, count);
1287 }
rgindaa19afe22012-01-25 15:40:22 -08001288
rgindaa9abdd82012-08-06 18:05:09 -07001289 if (this.options_.insertMode) {
1290 this.screen_.insertString(substr);
1291 } else {
1292 this.screen_.overwriteString(substr);
1293 }
1294
1295 this.screen_.maybeClipCurrentRow();
1296 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001297 }
rginda8ba33642011-12-14 12:31:31 -08001298
1299 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001300
rginda9f5222b2012-03-05 11:53:28 -08001301 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001302 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001303};
1304
1305/**
rginda87b86462011-12-14 13:48:03 -08001306 * Set the VT scroll region.
1307 *
rginda87b86462011-12-14 13:48:03 -08001308 * This also resets the cursor position to the absolute (0, 0) position, since
1309 * that's what xterm appears to do.
1310 *
1311 * @param {integer} scrollTop The zero-based top of the scroll region.
1312 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1313 * inclusive.
1314 */
1315hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
1316 this.vtScrollTop_ = scrollTop;
1317 this.vtScrollBottom_ = scrollBottom;
rginda87b86462011-12-14 13:48:03 -08001318};
1319
1320/**
rginda8ba33642011-12-14 12:31:31 -08001321 * Return the top row index according to the VT.
1322 *
1323 * This will return 0 unless the terminal has been told to restrict scrolling
1324 * to some lower row. It is used for some VT cursor positioning and scrolling
1325 * commands.
1326 *
1327 * @return {integer} The topmost row in the terminal's scroll region.
1328 */
1329hterm.Terminal.prototype.getVTScrollTop = function() {
1330 if (this.vtScrollTop_ != null)
1331 return this.vtScrollTop_;
1332
1333 return 0;
rginda87b86462011-12-14 13:48:03 -08001334};
rginda8ba33642011-12-14 12:31:31 -08001335
1336/**
1337 * Return the bottom row index according to the VT.
1338 *
1339 * This will return the height of the terminal unless the it has been told to
1340 * restrict scrolling to some higher row. It is used for some VT cursor
1341 * positioning and scrolling commands.
1342 *
1343 * @return {integer} The bottommost row in the terminal's scroll region.
1344 */
1345hterm.Terminal.prototype.getVTScrollBottom = function() {
1346 if (this.vtScrollBottom_ != null)
1347 return this.vtScrollBottom_;
1348
rginda87b86462011-12-14 13:48:03 -08001349 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001350}
1351
1352/**
1353 * Process a '\n' character.
1354 *
1355 * If the cursor is on the final row of the terminal this will append a new
1356 * blank row to the screen and scroll the topmost row into the scrollback
1357 * buffer.
1358 *
1359 * Otherwise, this moves the cursor to column zero of the next row.
1360 */
1361hterm.Terminal.prototype.newLine = function() {
1362 if (this.screen_.cursorPosition.row == this.screen_.rowsArray.length - 1) {
rginda87b86462011-12-14 13:48:03 -08001363 // If we're at the end of the screen we need to append a new line and
1364 // scroll the top line into the scrollback buffer.
rginda8ba33642011-12-14 12:31:31 -08001365 this.appendRows_(1);
rginda87b86462011-12-14 13:48:03 -08001366 } else if (this.screen_.cursorPosition.row == this.getVTScrollBottom()) {
1367 // End of the scroll region does not affect the scrollback buffer.
1368 this.vtScrollUp(1);
1369 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
rginda8ba33642011-12-14 12:31:31 -08001370 } else {
rginda87b86462011-12-14 13:48:03 -08001371 // Anywhere else in the screen just moves the cursor.
1372 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001373 }
1374};
1375
1376/**
1377 * Like newLine(), except maintain the cursor column.
1378 */
1379hterm.Terminal.prototype.lineFeed = function() {
1380 var column = this.screen_.cursorPosition.column;
1381 this.newLine();
1382 this.setCursorColumn(column);
1383};
1384
1385/**
rginda87b86462011-12-14 13:48:03 -08001386 * If autoCarriageReturn is set then newLine(), else lineFeed().
1387 */
1388hterm.Terminal.prototype.formFeed = function() {
1389 if (this.options_.autoCarriageReturn) {
1390 this.newLine();
1391 } else {
1392 this.lineFeed();
1393 }
1394};
1395
1396/**
1397 * Move the cursor up one row, possibly inserting a blank line.
1398 *
1399 * The cursor column is not changed.
1400 */
1401hterm.Terminal.prototype.reverseLineFeed = function() {
1402 var scrollTop = this.getVTScrollTop();
1403 var currentRow = this.screen_.cursorPosition.row;
1404
1405 if (currentRow == scrollTop) {
1406 this.insertLines(1);
1407 } else {
1408 this.setAbsoluteCursorRow(currentRow - 1);
1409 }
1410};
1411
1412/**
rginda8ba33642011-12-14 12:31:31 -08001413 * Replace all characters to the left of the current cursor with the space
1414 * character.
1415 *
1416 * TODO(rginda): This should probably *remove* the characters (not just replace
1417 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001418 * position.
rginda8ba33642011-12-14 12:31:31 -08001419 */
1420hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001421 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001422 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001423 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001424 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001425};
1426
1427/**
David Benjamin684a9b72012-05-01 17:19:58 -04001428 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001429 *
1430 * The cursor position is unchanged.
1431 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001432 * If the current background color is not the default background color this
1433 * will insert spaces rather than delete. This is unfortunate because the
1434 * trailing space will affect text selection, but it's difficult to come up
1435 * with a way to style empty space that wouldn't trip up the hterm.Screen
1436 * code.
rginda8ba33642011-12-14 12:31:31 -08001437 */
1438hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001439 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1440 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001441
1442 if (this.screen_.textAttributes.background ===
1443 this.screen_.textAttributes.DEFAULT_COLOR) {
1444 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
1445 if (cursorRow.textContent.length <=
1446 this.screen_.cursorPosition.column + count) {
1447 this.screen_.deleteChars(count);
1448 this.clearCursorOverflow();
1449 return;
1450 }
1451 }
1452
rginda87b86462011-12-14 13:48:03 -08001453 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001454 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001455 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001456 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001457};
1458
1459/**
1460 * Erase the current line.
1461 *
1462 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001463 */
1464hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001465 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001466 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001467 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001468 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001469};
1470
1471/**
David Benjamina08d78f2012-05-05 00:28:49 -04001472 * Erase all characters from the start of the screen to the current cursor
1473 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001474 *
1475 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001476 */
1477hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001478 var cursor = this.saveCursor();
1479
1480 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001481
David Benjamina08d78f2012-05-05 00:28:49 -04001482 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001483 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001484 this.screen_.clearCursorRow();
1485 }
1486
rginda87b86462011-12-14 13:48:03 -08001487 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001488 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001489};
1490
1491/**
1492 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001493 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001494 *
1495 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001496 */
1497hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001498 var cursor = this.saveCursor();
1499
1500 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001501
David Benjamina08d78f2012-05-05 00:28:49 -04001502 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001503 for (var i = cursor.row + 1; i <= bottom; i++) {
1504 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001505 this.screen_.clearCursorRow();
1506 }
1507
rginda87b86462011-12-14 13:48:03 -08001508 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001509 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001510};
1511
1512/**
1513 * Fill the terminal with a given character.
1514 *
1515 * This methods does not respect the VT scroll region.
1516 *
1517 * @param {string} ch The character to use for the fill.
1518 */
1519hterm.Terminal.prototype.fill = function(ch) {
1520 var cursor = this.saveCursor();
1521
1522 this.setAbsoluteCursorPosition(0, 0);
1523 for (var row = 0; row < this.screenSize.height; row++) {
1524 for (var col = 0; col < this.screenSize.width; col++) {
1525 this.setAbsoluteCursorPosition(row, col);
1526 this.screen_.overwriteString(ch);
1527 }
1528 }
1529
1530 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001531};
1532
1533/**
rginda9ea433c2012-03-16 11:57:00 -07001534 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001535 *
rginda9ea433c2012-03-16 11:57:00 -07001536 * This does not respect the scroll region.
1537 *
1538 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1539 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001540 */
rginda9ea433c2012-03-16 11:57:00 -07001541hterm.Terminal.prototype.clearHome = function(opt_screen) {
1542 var screen = opt_screen || this.screen_;
1543 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001544
rginda11057d52012-04-25 12:29:56 -07001545 if (bottom == 0) {
1546 // Empty screen, nothing to do.
1547 return;
1548 }
1549
rgindae4d29232012-01-19 10:47:13 -08001550 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001551 screen.setCursorPosition(i, 0);
1552 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001553 }
1554
rginda9ea433c2012-03-16 11:57:00 -07001555 screen.setCursorPosition(0, 0);
1556};
1557
1558/**
1559 * Erase the entire display without changing the cursor position.
1560 *
1561 * The cursor position is unchanged. This does not respect the scroll
1562 * region.
1563 *
1564 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1565 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001566 */
1567hterm.Terminal.prototype.clear = function(opt_screen) {
1568 var screen = opt_screen || this.screen_;
1569 var cursor = screen.cursorPosition.clone();
1570 this.clearHome(screen);
1571 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001572};
1573
1574/**
1575 * VT command to insert lines at the current cursor row.
1576 *
1577 * This respects the current scroll region. Rows pushed off the bottom are
1578 * lost (they won't show up in the scrollback buffer).
1579 *
rginda8ba33642011-12-14 12:31:31 -08001580 * @param {integer} count The number of lines to insert.
1581 */
1582hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07001583 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08001584
1585 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07001586 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08001587
Robert Ginda579186b2012-09-26 11:40:04 -07001588 // The moveCount is the number of rows we need to relocate to make room for
1589 // the new row(s). The count is the distance to move them.
1590 var moveCount = bottom - cursorRow - count + 1;
1591 if (moveCount)
1592 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08001593
Robert Ginda579186b2012-09-26 11:40:04 -07001594 for (var i = count - 1; i >= 0; i--) {
1595 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001596 this.screen_.clearCursorRow();
1597 }
rginda8ba33642011-12-14 12:31:31 -08001598};
1599
1600/**
1601 * VT command to delete lines at the current cursor row.
1602 *
1603 * New rows are added to the bottom of scroll region to take their place. New
1604 * rows are strictly there to take up space and have no content or style.
1605 */
1606hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001607 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001608
rginda87b86462011-12-14 13:48:03 -08001609 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001610 var bottom = this.getVTScrollBottom();
1611
rginda87b86462011-12-14 13:48:03 -08001612 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001613 count = Math.min(count, maxCount);
1614
rginda87b86462011-12-14 13:48:03 -08001615 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001616 if (count != maxCount)
1617 this.moveRows_(top, count, moveStart);
1618
1619 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001620 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001621 this.screen_.clearCursorRow();
1622 }
1623
rginda87b86462011-12-14 13:48:03 -08001624 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001625 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001626};
1627
1628/**
1629 * Inserts the given number of spaces at the current cursor position.
1630 *
rginda87b86462011-12-14 13:48:03 -08001631 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001632 */
1633hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001634 var cursor = this.saveCursor();
1635
rgindacbbd7482012-06-13 15:06:16 -07001636 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001637 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001638 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001639
1640 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001641 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001642};
1643
1644/**
1645 * Forward-delete the specified number of characters starting at the cursor
1646 * position.
1647 *
1648 * @param {integer} count The number of characters to delete.
1649 */
1650hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001651 var deleted = this.screen_.deleteChars(count);
1652 if (deleted && !this.screen_.textAttributes.isDefault()) {
1653 var cursor = this.saveCursor();
1654 this.setCursorColumn(this.screenSize.width - deleted);
1655 this.screen_.insertString(lib.f.getWhitespace(deleted));
1656 this.restoreCursor(cursor);
1657 }
1658
David Benjamin54e8bf62012-06-01 22:31:40 -04001659 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001660};
1661
1662/**
1663 * Shift rows in the scroll region upwards by a given number of lines.
1664 *
1665 * New rows are inserted at the bottom of the scroll region to fill the
1666 * vacated rows. The new rows not filled out with the current text attributes.
1667 *
1668 * This function does not affect the scrollback rows at all. Rows shifted
1669 * off the top are lost.
1670 *
rginda87b86462011-12-14 13:48:03 -08001671 * The cursor position is not altered.
1672 *
rginda8ba33642011-12-14 12:31:31 -08001673 * @param {integer} count The number of rows to scroll.
1674 */
1675hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001676 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001677
rginda87b86462011-12-14 13:48:03 -08001678 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001679 this.deleteLines(count);
1680
rginda87b86462011-12-14 13:48:03 -08001681 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001682};
1683
1684/**
1685 * Shift rows below the cursor down by a given number of lines.
1686 *
1687 * This function respects the current scroll region.
1688 *
1689 * New rows are inserted at the top of the scroll region to fill the
1690 * vacated rows. The new rows not filled out with the current text attributes.
1691 *
1692 * This function does not affect the scrollback rows at all. Rows shifted
1693 * off the bottom are lost.
1694 *
1695 * @param {integer} count The number of rows to scroll.
1696 */
1697hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001698 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001699
rginda87b86462011-12-14 13:48:03 -08001700 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001701 this.insertLines(opt_count);
1702
rginda87b86462011-12-14 13:48:03 -08001703 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001704};
1705
rginda87b86462011-12-14 13:48:03 -08001706
rginda8ba33642011-12-14 12:31:31 -08001707/**
1708 * Set the cursor position.
1709 *
1710 * The cursor row is relative to the scroll region if the terminal has
1711 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1712 *
1713 * @param {integer} row The new zero-based cursor row.
1714 * @param {integer} row The new zero-based cursor column.
1715 */
1716hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1717 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001718 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001719 } else {
rginda87b86462011-12-14 13:48:03 -08001720 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001721 }
rginda87b86462011-12-14 13:48:03 -08001722};
rginda8ba33642011-12-14 12:31:31 -08001723
rginda87b86462011-12-14 13:48:03 -08001724hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1725 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07001726 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
1727 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001728 this.screen_.setCursorPosition(row, column);
1729};
1730
1731hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07001732 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
1733 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001734 this.screen_.setCursorPosition(row, column);
1735};
1736
1737/**
1738 * Set the cursor column.
1739 *
1740 * @param {integer} column The new zero-based cursor column.
1741 */
1742hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001743 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001744};
1745
1746/**
1747 * Return the cursor column.
1748 *
1749 * @return {integer} The zero-based cursor column.
1750 */
1751hterm.Terminal.prototype.getCursorColumn = function() {
1752 return this.screen_.cursorPosition.column;
1753};
1754
1755/**
1756 * Set the cursor row.
1757 *
1758 * The cursor row is relative to the scroll region if the terminal has
1759 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1760 *
1761 * @param {integer} row The new cursor row.
1762 */
rginda87b86462011-12-14 13:48:03 -08001763hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1764 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001765};
1766
1767/**
1768 * Return the cursor row.
1769 *
1770 * @return {integer} The zero-based cursor row.
1771 */
1772hterm.Terminal.prototype.getCursorRow = function(row) {
1773 return this.screen_.cursorPosition.row;
1774};
1775
1776/**
1777 * Request that the ScrollPort redraw itself soon.
1778 *
1779 * The redraw will happen asynchronously, soon after the call stack winds down.
1780 * Multiple calls will be coalesced into a single redraw.
1781 */
1782hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001783 if (this.timeouts_.redraw)
1784 return;
rginda8ba33642011-12-14 12:31:31 -08001785
1786 var self = this;
rginda87b86462011-12-14 13:48:03 -08001787 this.timeouts_.redraw = setTimeout(function() {
1788 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001789 self.scrollPort_.redraw_();
1790 }, 0);
1791};
1792
1793/**
1794 * Request that the ScrollPort be scrolled to the bottom.
1795 *
1796 * The scroll will happen asynchronously, soon after the call stack winds down.
1797 * Multiple calls will be coalesced into a single scroll.
1798 *
1799 * This affects the scrollbar position of the ScrollPort, and has nothing to
1800 * do with the VT scroll commands.
1801 */
1802hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1803 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001804 return;
rginda8ba33642011-12-14 12:31:31 -08001805
1806 var self = this;
1807 this.timeouts_.scrollDown = setTimeout(function() {
1808 delete self.timeouts_.scrollDown;
1809 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1810 }, 10);
1811};
1812
1813/**
1814 * Move the cursor up a specified number of rows.
1815 *
1816 * @param {integer} count The number of rows to move the cursor.
1817 */
1818hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001819 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001820};
1821
1822/**
1823 * Move the cursor down a specified number of rows.
1824 *
1825 * @param {integer} count The number of rows to move the cursor.
1826 */
1827hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001828 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001829 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1830 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1831 this.screenSize.height - 1);
1832
rgindacbbd7482012-06-13 15:06:16 -07001833 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08001834 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001835 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001836};
1837
1838/**
1839 * Move the cursor left a specified number of columns.
1840 *
1841 * @param {integer} count The number of columns to move the cursor.
1842 */
1843hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001844 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001845};
1846
1847/**
1848 * Move the cursor right a specified number of columns.
1849 *
1850 * @param {integer} count The number of columns to move the cursor.
1851 */
1852hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001853 count = count || 1;
rgindacbbd7482012-06-13 15:06:16 -07001854 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001855 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001856 this.setCursorColumn(column);
1857};
1858
1859/**
1860 * Reverse the foreground and background colors of the terminal.
1861 *
1862 * This only affects text that was drawn with no attributes.
1863 *
1864 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1865 * been drawn with attributes that happen to coincide with the default
1866 * 'no-attribute' colors. My guess is probably not.
1867 */
1868hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001869 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001870 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08001871 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
1872 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08001873 } else {
rginda9f5222b2012-03-05 11:53:28 -08001874 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
1875 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08001876 }
1877};
1878
1879/**
rginda87b86462011-12-14 13:48:03 -08001880 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07001881 *
1882 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08001883 */
1884hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08001885 this.cursorNode_.style.backgroundColor =
1886 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001887
1888 var self = this;
1889 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08001890 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08001891 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07001892
1893 if (this.bellAudio_.getAttribute('src')) {
Robert Gindaa6331372013-03-19 10:35:39 -07001894 if (this.bellSquelchTimeout_)
Robert Ginda92e18102013-03-14 13:56:37 -07001895 return;
1896
1897 this.bellAudio_.play();
1898
1899 this.bellSequelchTimeout_ = setTimeout(function() {
1900 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07001901 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07001902 } else {
1903 delete this.bellSquelchTimeout_;
1904 }
rginda87b86462011-12-14 13:48:03 -08001905};
1906
1907/**
rginda8ba33642011-12-14 12:31:31 -08001908 * Set the origin mode bit.
1909 *
1910 * If origin mode is on, certain VT cursor and scrolling commands measure their
1911 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
1912 * to the top of the addressable screen.
1913 *
1914 * Defaults to off.
1915 *
1916 * @param {boolean} state True to set origin mode, false to unset.
1917 */
1918hterm.Terminal.prototype.setOriginMode = function(state) {
1919 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08001920 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08001921};
1922
1923/**
1924 * Set the insert mode bit.
1925 *
1926 * If insert mode is on, existing text beyond the cursor position will be
1927 * shifted right to make room for new text. Otherwise, new text overwrites
1928 * any existing text.
1929 *
1930 * Defaults to off.
1931 *
1932 * @param {boolean} state True to set insert mode, false to unset.
1933 */
1934hterm.Terminal.prototype.setInsertMode = function(state) {
1935 this.options_.insertMode = state;
1936};
1937
1938/**
rginda87b86462011-12-14 13:48:03 -08001939 * Set the auto carriage return bit.
1940 *
1941 * If auto carriage return is on then a formfeed character is interpreted
1942 * as a newline, otherwise it's the same as a linefeed. The difference boils
1943 * down to whether or not the cursor column is reset.
1944 */
1945hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
1946 this.options_.autoCarriageReturn = state;
1947};
1948
1949/**
rginda8ba33642011-12-14 12:31:31 -08001950 * Set the wraparound mode bit.
1951 *
1952 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
1953 * to the start of the following row. Otherwise, the cursor is clamped to the
1954 * end of the screen and attempts to write past it are ignored.
1955 *
1956 * Defaults to on.
1957 *
1958 * @param {boolean} state True to set wraparound mode, false to unset.
1959 */
1960hterm.Terminal.prototype.setWraparound = function(state) {
1961 this.options_.wraparound = state;
1962};
1963
1964/**
1965 * Set the reverse-wraparound mode bit.
1966 *
1967 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
1968 * to the end of the previous row. Otherwise, the cursor is clamped to column
1969 * 0.
1970 *
1971 * Defaults to off.
1972 *
1973 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
1974 */
1975hterm.Terminal.prototype.setReverseWraparound = function(state) {
1976 this.options_.reverseWraparound = state;
1977};
1978
1979/**
1980 * Selects between the primary and alternate screens.
1981 *
1982 * If alternate mode is on, the alternate screen is active. Otherwise the
1983 * primary screen is active.
1984 *
1985 * Swapping screens has no effect on the scrollback buffer.
1986 *
1987 * Each screen maintains its own cursor position.
1988 *
1989 * Defaults to off.
1990 *
1991 * @param {boolean} state True to set alternate mode, false to unset.
1992 */
1993hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08001994 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001995 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
1996
rginda35c456b2012-02-09 17:29:05 -08001997 if (this.screen_.rowsArray.length &&
1998 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
1999 // If the screen changed sizes while we were away, our rowIndexes may
2000 // be incorrect.
2001 var offset = this.scrollbackRows_.length;
2002 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002003 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002004 ary[i].rowIndex = offset + i;
2005 }
2006 }
rginda8ba33642011-12-14 12:31:31 -08002007
rginda35c456b2012-02-09 17:29:05 -08002008 this.realizeWidth_(this.screenSize.width);
2009 this.realizeHeight_(this.screenSize.height);
2010 this.scrollPort_.syncScrollHeight();
2011 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002012
rginda6d397402012-01-17 10:58:29 -08002013 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002014 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002015};
2016
2017/**
2018 * Set the cursor-blink mode bit.
2019 *
2020 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2021 * a visible cursor does not blink.
2022 *
2023 * You should make sure to turn blinking off if you're going to dispose of a
2024 * terminal, otherwise you'll leak a timeout.
2025 *
2026 * Defaults to on.
2027 *
2028 * @param {boolean} state True to set cursor-blink mode, false to unset.
2029 */
2030hterm.Terminal.prototype.setCursorBlink = function(state) {
2031 this.options_.cursorBlink = state;
2032
2033 if (!state && this.timeouts_.cursorBlink) {
2034 clearTimeout(this.timeouts_.cursorBlink);
2035 delete this.timeouts_.cursorBlink;
2036 }
2037
2038 if (this.options_.cursorVisible)
2039 this.setCursorVisible(true);
2040};
2041
2042/**
2043 * Set the cursor-visible mode bit.
2044 *
2045 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2046 *
2047 * Defaults to on.
2048 *
2049 * @param {boolean} state True to set cursor-visible mode, false to unset.
2050 */
2051hterm.Terminal.prototype.setCursorVisible = function(state) {
2052 this.options_.cursorVisible = state;
2053
2054 if (!state) {
rginda87b86462011-12-14 13:48:03 -08002055 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002056 return;
2057 }
2058
rginda87b86462011-12-14 13:48:03 -08002059 this.syncCursorPosition_();
2060
2061 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002062
2063 if (this.options_.cursorBlink) {
2064 if (this.timeouts_.cursorBlink)
2065 return;
2066
2067 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
2068 500);
2069 } else {
2070 if (this.timeouts_.cursorBlink) {
2071 clearTimeout(this.timeouts_.cursorBlink);
2072 delete this.timeouts_.cursorBlink;
2073 }
2074 }
2075};
2076
2077/**
rginda87b86462011-12-14 13:48:03 -08002078 * Synchronizes the visible cursor and document selection with the current
2079 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002080 */
2081hterm.Terminal.prototype.syncCursorPosition_ = function() {
2082 var topRowIndex = this.scrollPort_.getTopRowIndex();
2083 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2084 var cursorRowIndex = this.scrollbackRows_.length +
2085 this.screen_.cursorPosition.row;
2086
2087 if (cursorRowIndex > bottomRowIndex) {
2088 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002089 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002090 return;
2091 }
2092
rginda35c456b2012-02-09 17:29:05 -08002093 this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px';
2094 this.cursorNode_.style.height = this.scrollPort_.characterSize.height + 'px';
2095
rginda8ba33642011-12-14 12:31:31 -08002096 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002097 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2098 'px';
2099 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2100 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002101
2102 this.cursorNode_.setAttribute('title',
2103 '(' + this.screen_.cursorPosition.row +
2104 ', ' + this.screen_.cursorPosition.column +
2105 ')');
2106
2107 // Update the caret for a11y purposes.
2108 var selection = this.document_.getSelection();
2109 if (selection && selection.isCollapsed)
2110 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002111};
2112
2113/**
2114 * Synchronizes the visible cursor with the current cursor coordinates.
2115 *
2116 * The sync will happen asynchronously, soon after the call stack winds down.
2117 * Multiple calls will be coalesced into a single sync.
2118 */
2119hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2120 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002121 return;
rginda8ba33642011-12-14 12:31:31 -08002122
2123 var self = this;
2124 this.timeouts_.syncCursor = setTimeout(function() {
2125 self.syncCursorPosition_();
2126 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002127 }, 0);
2128};
2129
rgindacc2996c2012-02-24 14:59:31 -08002130/**
rgindaf522ce02012-04-17 17:49:17 -07002131 * Show or hide the zoom warning.
2132 *
2133 * The zoom warning is a message warning the user that their browser zoom must
2134 * be set to 100% in order for hterm to function properly.
2135 *
2136 * @param {boolean} state True to show the message, false to hide it.
2137 */
2138hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2139 if (!this.zoomWarningNode_) {
2140 if (!state)
2141 return;
2142
2143 this.zoomWarningNode_ = this.document_.createElement('div');
2144 this.zoomWarningNode_.style.cssText = (
2145 'color: black;' +
2146 'background-color: #ff2222;' +
2147 'font-size: large;' +
2148 'border-radius: 8px;' +
2149 'opacity: 0.75;' +
2150 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2151 'top: 0.5em;' +
2152 'right: 1.2em;' +
2153 'position: absolute;' +
2154 '-webkit-text-size-adjust: none;' +
2155 '-webkit-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002156 }
2157
Robert Gindab4839c22013-02-28 16:52:10 -08002158 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2159 hterm.zoomWarningMessage,
2160 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2161
rgindaf522ce02012-04-17 17:49:17 -07002162 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2163
2164 if (state) {
2165 if (!this.zoomWarningNode_.parentNode)
2166 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2167 } else if (this.zoomWarningNode_.parentNode) {
2168 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2169 }
2170};
2171
2172/**
rgindacc2996c2012-02-24 14:59:31 -08002173 * Show the terminal overlay for a given amount of time.
2174 *
2175 * The terminal overlay appears in inverse video in a large font, centered
2176 * over the terminal. You should probably keep the overlay message brief,
2177 * since it's in a large font and you probably aren't going to check the size
2178 * of the terminal first.
2179 *
2180 * @param {string} msg The text (not HTML) message to display in the overlay.
2181 * @param {number} opt_timeout The amount of time to wait before fading out
2182 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2183 * stay up forever (or until the next overlay).
2184 */
2185hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002186 if (!this.overlayNode_) {
2187 if (!this.div_)
2188 return;
2189
2190 this.overlayNode_ = this.document_.createElement('div');
2191 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002192 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002193 'font-size: xx-large;' +
2194 'opacity: 0.75;' +
2195 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2196 'position: absolute;' +
2197 '-webkit-user-select: none;' +
2198 '-webkit-transition: opacity 180ms ease-in;');
2199 }
2200
rginda9f5222b2012-03-05 11:53:28 -08002201 this.overlayNode_.style.color = this.prefs_.get('background-color');
2202 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2203 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2204
rgindaf0090c92012-02-10 14:58:52 -08002205 this.overlayNode_.textContent = msg;
2206 this.overlayNode_.style.opacity = '0.75';
2207
2208 if (!this.overlayNode_.parentNode)
2209 this.div_.appendChild(this.overlayNode_);
2210
Robert Ginda97769282013-02-01 15:30:30 -08002211 var divSize = hterm.getClientSize(this.div_);
2212 var overlaySize = hterm.getClientSize(this.overlayNode_);
2213
2214 this.overlayNode_.style.top = (divSize.height - overlaySize.height) / 2;
2215 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
2216 this.scrollPort_.currentScrollbarWidthPx) / 2;
rgindaf0090c92012-02-10 14:58:52 -08002217
2218 var self = this;
2219
2220 if (this.overlayTimeout_)
2221 clearTimeout(this.overlayTimeout_);
2222
rgindacc2996c2012-02-24 14:59:31 -08002223 if (opt_timeout === null)
2224 return;
2225
rgindaf0090c92012-02-10 14:58:52 -08002226 this.overlayTimeout_ = setTimeout(function() {
2227 self.overlayNode_.style.opacity = '0';
2228 setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002229 if (self.overlayNode_.parentNode)
2230 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002231 self.overlayTimeout_ = null;
2232 self.overlayNode_.style.opacity = '0.75';
2233 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002234 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002235};
2236
rginda4bba5e12012-06-20 16:15:30 -07002237/**
2238 * Paste from the system clipboard to the terminal.
2239 */
2240hterm.Terminal.prototype.paste = function() {
2241 hterm.pasteFromClipboard(this.document_);
2242};
2243
2244/**
2245 * Copy a string to the system clipboard.
2246 *
2247 * Note: If there is a selected range in the terminal, it'll be cleared.
2248 */
2249hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda9fb38222012-09-11 14:19:12 -07002250 if (this.prefs_.get('enable-clipboard-notice'))
Robert Gindab4839c22013-02-28 16:52:10 -08002251 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
rgindaa09e7332012-08-17 12:49:51 -07002252
2253 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002254 copySource.textContent = str;
2255 copySource.style.cssText = (
2256 '-webkit-user-select: text;' +
2257 'position: absolute;' +
2258 'top: -99px');
2259
2260 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002261
rginda4bba5e12012-06-20 16:15:30 -07002262 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002263 var anchorNode = selection.anchorNode;
2264 var anchorOffset = selection.anchorOffset;
2265 var focusNode = selection.focusNode;
2266 var focusOffset = selection.focusOffset;
2267
rginda4bba5e12012-06-20 16:15:30 -07002268 selection.selectAllChildren(copySource);
2269
rgindaa09e7332012-08-17 12:49:51 -07002270 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002271
rgindafaa74742012-08-21 13:34:03 -07002272 selection.collapse(anchorNode, anchorOffset);
2273 selection.extend(focusNode, focusOffset);
2274
rginda4bba5e12012-06-20 16:15:30 -07002275 copySource.parentNode.removeChild(copySource);
2276};
2277
rgindaa09e7332012-08-17 12:49:51 -07002278hterm.Terminal.prototype.getSelectionText = function() {
2279 var selection = this.scrollPort_.selection;
2280 selection.sync();
2281
2282 if (selection.isCollapsed)
2283 return null;
2284
2285
2286 // Start offset measures from the beginning of the line.
2287 var startOffset = selection.startOffset;
2288 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002289
Robert Gindafdbb3f22012-09-06 20:23:06 -07002290 if (node.nodeName != 'X-ROW') {
2291 // If the selection doesn't start on an x-row node, then it must be
2292 // somewhere inside the x-row. Add any characters from previous siblings
2293 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002294
2295 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2296 // If node is the text node in a styled span, move up to the span node.
2297 node = node.parentNode;
2298 }
2299
Robert Gindafdbb3f22012-09-06 20:23:06 -07002300 while (node.previousSibling) {
2301 node = node.previousSibling;
2302 startOffset += node.textContent.length;
2303 }
rgindaa09e7332012-08-17 12:49:51 -07002304 }
2305
2306 // End offset measures from the end of the line.
2307 var endOffset = selection.endNode.textContent.length - selection.endOffset;
2308 var node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002309
Robert Gindafdbb3f22012-09-06 20:23:06 -07002310 if (node.nodeName != 'X-ROW') {
2311 // If the selection doesn't end on an x-row node, then it must be
2312 // somewhere inside the x-row. Add any characters from following siblings
2313 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002314
2315 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2316 // If node is the text node in a styled span, move up to the span node.
2317 node = node.parentNode;
2318 }
2319
Robert Gindafdbb3f22012-09-06 20:23:06 -07002320 while (node.nextSibling) {
2321 node = node.nextSibling;
2322 endOffset += node.textContent.length;
2323 }
rgindaa09e7332012-08-17 12:49:51 -07002324 }
2325
2326 var rv = this.getRowsText(selection.startRow.rowIndex,
2327 selection.endRow.rowIndex + 1);
2328 return rv.substring(startOffset, rv.length - endOffset);
2329};
2330
rginda4bba5e12012-06-20 16:15:30 -07002331/**
2332 * Copy the current selection to the system clipboard, then clear it after a
2333 * short delay.
2334 */
2335hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002336 var text = this.getSelectionText();
2337 if (text != null)
2338 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002339};
2340
rgindaf0090c92012-02-10 14:58:52 -08002341hterm.Terminal.prototype.overlaySize = function() {
2342 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2343};
2344
rginda87b86462011-12-14 13:48:03 -08002345/**
2346 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2347 *
2348 * @param {string} string The VT string representing the keystroke.
2349 */
2350hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002351 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002352 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2353
2354 this.io.onVTKeystroke(string);
rginda8ba33642011-12-14 12:31:31 -08002355};
2356
2357/**
rgindad5613292012-06-19 15:40:37 -07002358 * Add the terminalRow and terminalColumn properties to mouse events and
2359 * then forward on to onMouse().
2360 *
2361 * The terminalRow and terminalColumn properties contain the (row, column)
2362 * coordinates for the mouse event.
2363 */
2364hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002365 if (e.processedByTerminalHandler_) {
2366 // We register our event handlers on the document, as well as the cursor
2367 // and the scroll blocker. Mouse events that occur on the cursor or
2368 // scroll blocker will also appear on the document, but we don't want to
2369 // process them twice.
2370 //
2371 // We can't just prevent bubbling because that has other side effects, so
2372 // we decorate the event object with this property instead.
2373 return;
2374 }
2375
2376 e.processedByTerminalHandler_ = true;
2377
rginda4bba5e12012-06-20 16:15:30 -07002378 if (e.type == 'mousedown' && e.which == this.mousePasteButton) {
2379 this.paste();
2380 return;
2381 }
2382
2383 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2384 !this.document_.getSelection().isCollapsed) {
rgindafaa74742012-08-21 13:34:03 -07002385 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002386 return;
2387 }
2388
rgindad5613292012-06-19 15:40:37 -07002389 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2390 this.scrollPort_.characterSize.height) + 1;
2391 e.terminalColumn = parseInt(e.clientX /
2392 this.scrollPort_.characterSize.width) + 1;
2393
2394 if (e.type == 'mousedown') {
2395 if (e.terminalColumn > this.screenSize.width) {
2396 // Mousedown in the scrollbar area.
2397 return;
2398 }
2399
2400 if (!this.enableMouseDragScroll) {
2401 // Move the scroll-blocker into place if we want to keep the scrollport
2402 // from scrolling.
2403 this.scrollBlockerNode_.engaged = true;
2404 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2405 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2406 }
2407 } else if (this.scrollBlockerNode_.engaged &&
2408 (e.type == 'mousemove' || e.type == 'mouseup')) {
2409 // Disengage the scroll-blocker after one of these events.
2410 this.scrollBlockerNode_.engaged = false;
2411 this.scrollBlockerNode_.style.top = '-99px';
2412 }
2413
rgindafaa74742012-08-21 13:34:03 -07002414 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07002415};
2416
2417/**
2418 * Clients should override this if they care to know about mouse events.
2419 *
2420 * The event parameter will be a normal DOM mouse click event with additional
2421 * 'terminalRow' and 'terminalColumn' properties.
2422 */
2423hterm.Terminal.prototype.onMouse = function(e) { };
2424
2425/**
rginda8e92a692012-05-20 19:37:20 -07002426 * React when focus changes.
2427 */
2428hterm.Terminal.prototype.onFocusChange_ = function(state) {
2429 this.cursorNode_.setAttribute('focus', state ? 'true' : 'false');
2430};
2431
2432/**
rginda8ba33642011-12-14 12:31:31 -08002433 * React when the ScrollPort is scrolled.
2434 */
2435hterm.Terminal.prototype.onScroll_ = function() {
2436 this.scheduleSyncCursorPosition_();
2437};
2438
2439/**
rginda9846e2f2012-01-27 13:53:33 -08002440 * React when text is pasted into the scrollPort.
2441 */
2442hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Gindaf2547f12012-10-25 20:36:21 -07002443 var text = this.vt.encodeUTF8(e.text);
2444 text = text.replace(/\n/mg, '\r');
2445 this.io.onVTKeystroke(text);
rginda9846e2f2012-01-27 13:53:33 -08002446};
2447
2448/**
rgindaa09e7332012-08-17 12:49:51 -07002449 * React when the user tries to copy from the scrollPort.
2450 */
2451hterm.Terminal.prototype.onCopy_ = function(e) {
2452 e.preventDefault();
rgindafaa74742012-08-21 13:34:03 -07002453 this.copySelectionToClipboard();
rgindaa09e7332012-08-17 12:49:51 -07002454};
2455
2456/**
rginda8ba33642011-12-14 12:31:31 -08002457 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002458 *
2459 * Note: This function should not directly contain code that alters the internal
2460 * state of the terminal. That kind of code belongs in realizeWidth or
2461 * realizeHeight, so that it can be executed synchronously in the case of a
2462 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002463 */
2464hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08002465 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002466 this.scrollPort_.characterSize.width);
2467 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
2468 this.scrollPort_.characterSize.height);
2469
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002470 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08002471 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002472 // gets removed from the document or during the initial load, and we can't
2473 // deal with that.
rginda35c456b2012-02-09 17:29:05 -08002474 return;
2475 }
2476
rgindaa8ba17d2012-08-15 14:41:10 -07002477 var isNewSize = (columnCount != this.screenSize.width ||
2478 rowCount != this.screenSize.height);
2479
2480 // We do this even if the size didn't change, just to be sure everything is
2481 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002482 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07002483 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07002484
2485 if (isNewSize)
2486 this.overlaySize();
2487
2488 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08002489};
2490
2491/**
2492 * Service the cursor blink timeout.
2493 */
2494hterm.Terminal.prototype.onCursorBlink_ = function() {
rginda87b86462011-12-14 13:48:03 -08002495 if (this.cursorNode_.style.opacity == '0') {
2496 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002497 } else {
rginda87b86462011-12-14 13:48:03 -08002498 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002499 }
2500};
David Reveman8f552492012-03-28 12:18:41 -04002501
2502/**
2503 * Set the scrollbar-visible mode bit.
2504 *
2505 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2506 * Otherwise it will not.
2507 *
2508 * Defaults to on.
2509 *
2510 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2511 */
2512hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2513 this.scrollPort_.setScrollbarVisible(state);
2514};