blob: e05e4f61ac8287ef4e6f69a9aaa07b47c9580439 [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
Robert Ginda830583c2013-08-07 13:20:46 -070080 // The current cursor shape of the terminal.
81 this.cursorShape_ = hterm.Terminal.cursorShape.BLOCK;
82
83 // The current color of the cursor.
84 this.cursorColor_ = null;
85
rginda9f5222b2012-03-05 11:53:28 -080086 // These prefs are cached so we don't have to read from local storage with
Robert Ginda57f03b42012-09-13 11:02:48 -070087 // each output and keystroke. They are initialized by the preference manager.
Robert Ginda8cb7d902013-06-20 14:37:18 -070088 this.backgroundColor_ = null;
89 this.foregroundColor_ = null;
Robert Ginda57f03b42012-09-13 11:02:48 -070090 this.scrollOnOutput_ = null;
91 this.scrollOnKeystroke_ = null;
rginda9f5222b2012-03-05 11:53:28 -080092
rgindaf0090c92012-02-10 14:58:52 -080093 // Terminal bell sound.
94 this.bellAudio_ = this.document_.createElement('audio');
rgindaf0090c92012-02-10 14:58:52 -080095 this.bellAudio_.setAttribute('preload', 'auto');
96
rginda6d397402012-01-17 10:58:29 -080097 // Cursor position and attributes saved with DECSC.
98 this.savedOptions_ = {};
99
rginda8ba33642011-12-14 12:31:31 -0800100 // The current mode bits for the terminal.
101 this.options_ = new hterm.Options();
102
103 // Timeouts we might need to clear.
104 this.timeouts_ = {};
rginda87b86462011-12-14 13:48:03 -0800105
106 // The VT escape sequence interpreter.
rginda0f5c0292012-01-13 11:00:13 -0800107 this.vt = new hterm.VT(this);
rginda87b86462011-12-14 13:48:03 -0800108
rgindafeaf3142012-01-31 15:14:20 -0800109 // The keyboard hander.
110 this.keyboard = new hterm.Keyboard(this);
111
rginda87b86462011-12-14 13:48:03 -0800112 // General IO interface that can be given to third parties without exposing
113 // the entire terminal object.
114 this.io = new hterm.Terminal.IO(this);
rgindac9bc5502012-01-18 11:48:44 -0800115
rgindad5613292012-06-19 15:40:37 -0700116 // True if mouse-click-drag should scroll the terminal.
117 this.enableMouseDragScroll = true;
118
Robert Ginda57f03b42012-09-13 11:02:48 -0700119 this.copyOnSelect = null;
rginda4bba5e12012-06-20 16:15:30 -0700120 this.mousePasteButton = null;
rginda4bba5e12012-06-20 16:15:30 -0700121
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400122 this.realizeSize_(80, 24);
rgindac9bc5502012-01-18 11:48:44 -0800123 this.setDefaultTabStops();
Robert Ginda57f03b42012-09-13 11:02:48 -0700124
125 this.setProfile(opt_profileId || 'default',
126 function() { this.onTerminalReady() }.bind(this));
rginda87b86462011-12-14 13:48:03 -0800127};
128
129/**
Robert Ginda830583c2013-08-07 13:20:46 -0700130 * Possible cursor shapes.
131 */
132hterm.Terminal.cursorShape = {
133 BLOCK: 'BLOCK',
134 BEAM: 'BEAM',
135 UNDERLINE: 'UNDERLINE'
136};
137
138/**
Robert Ginda57f03b42012-09-13 11:02:48 -0700139 * Clients should override this to be notified when the terminal is ready
140 * for use.
141 *
142 * The terminal initialization is asynchronous, and shouldn't be used before
143 * this method is called.
144 */
145hterm.Terminal.prototype.onTerminalReady = function() { };
146
147/**
rginda35c456b2012-02-09 17:29:05 -0800148 * Default tab with of 8 to match xterm.
149 */
150hterm.Terminal.prototype.tabWidth = 8;
151
152/**
rginda9f5222b2012-03-05 11:53:28 -0800153 * Select a preference profile.
154 *
155 * This will load the terminal preferences for the given profile name and
156 * associate subsequent preference changes with the new preference profile.
157 *
158 * @param {string} newName The name of the preference profile. Forward slash
159 * characters will be removed from the name.
Robert Ginda57f03b42012-09-13 11:02:48 -0700160 * @param {function} opt_callback Optional callback to invoke when the profile
161 * transition is complete.
rginda9f5222b2012-03-05 11:53:28 -0800162 */
Robert Ginda57f03b42012-09-13 11:02:48 -0700163hterm.Terminal.prototype.setProfile = function(profileId, opt_callback) {
164 this.profileId_ = profileId.replace(/\//g, '');
rginda9f5222b2012-03-05 11:53:28 -0800165
Robert Ginda57f03b42012-09-13 11:02:48 -0700166 var terminal = this;
rginda9f5222b2012-03-05 11:53:28 -0800167
Robert Ginda57f03b42012-09-13 11:02:48 -0700168 if (this.prefs_)
169 this.prefs_.deactivate();
rginda9f5222b2012-03-05 11:53:28 -0800170
Robert Ginda57f03b42012-09-13 11:02:48 -0700171 this.prefs_ = new hterm.PreferenceManager(this.profileId_);
172 this.prefs_.addObservers(null, {
Andrew de los Reyes574e10e2013-04-04 09:31:57 -0700173 'alt-backspace-is-meta-backspace': function(v) {
174 terminal.keyboard.altBackspaceIsMetaBackspace = v;
175 },
176
Robert Ginda57f03b42012-09-13 11:02:48 -0700177 'alt-is-meta': function(v) {
178 terminal.keyboard.altIsMeta = v;
179 },
180
181 'alt-sends-what': function(v) {
182 if (!/^(escape|8-bit|browser-key)$/.test(v))
183 v = 'escape';
184
185 terminal.keyboard.altSendsWhat = v;
186 },
187
188 'audible-bell-sound': function(v) {
Robert Gindab4839c22013-02-28 16:52:10 -0800189 var ary = v.match(/^lib-resource:(\S+)/);
190 if (ary) {
191 terminal.bellAudio_.setAttribute('src',
192 lib.resource.getDataUrl(ary[1]));
193 } else {
194 terminal.bellAudio_.setAttribute('src', v);
195 }
Robert Ginda57f03b42012-09-13 11:02:48 -0700196 },
197
198 'background-color': function(v) {
199 terminal.setBackgroundColor(v);
200 },
201
202 'background-image': function(v) {
203 terminal.scrollPort_.setBackgroundImage(v);
204 },
205
206 'background-size': function(v) {
207 terminal.scrollPort_.setBackgroundSize(v);
208 },
209
210 'background-position': function(v) {
211 terminal.scrollPort_.setBackgroundPosition(v);
212 },
213
214 'backspace-sends-backspace': function(v) {
215 terminal.keyboard.backspaceSendsBackspace = v;
216 },
217
218 'cursor-blink': function(v) {
219 terminal.setCursorBlink(!!v);
220 },
221
222 'cursor-color': function(v) {
223 terminal.setCursorColor(v);
224 },
225
226 'color-palette-overrides': function(v) {
227 if (!(v == null || v instanceof Object || v instanceof Array)) {
228 console.warn('Preference color-palette-overrides is not an array or ' +
229 'object: ' + v);
230 return;
rginda9f5222b2012-03-05 11:53:28 -0800231 }
rginda9f5222b2012-03-05 11:53:28 -0800232
Robert Ginda57f03b42012-09-13 11:02:48 -0700233 lib.colors.colorPalette = lib.colors.stockColorPalette.concat();
rginda39bdf6f2012-04-10 16:50:55 -0700234
Robert Ginda57f03b42012-09-13 11:02:48 -0700235 if (v) {
236 for (var key in v) {
237 var i = parseInt(key);
238 if (isNaN(i) || i < 0 || i > 255) {
239 console.log('Invalid value in palette: ' + key + ': ' + v[key]);
240 continue;
241 }
242
243 if (v[i]) {
244 var rgb = lib.colors.normalizeCSS(v[i]);
245 if (rgb)
246 lib.colors.colorPalette[i] = rgb;
247 }
248 }
rginda30f20f62012-04-05 16:36:19 -0700249 }
rginda30f20f62012-04-05 16:36:19 -0700250
Robert Ginda57f03b42012-09-13 11:02:48 -0700251 terminal.primaryScreen_.textAttributes.resetColorPalette()
252 terminal.alternateScreen_.textAttributes.resetColorPalette();
253 },
rginda30f20f62012-04-05 16:36:19 -0700254
Robert Ginda57f03b42012-09-13 11:02:48 -0700255 'copy-on-select': function(v) {
256 terminal.copyOnSelect = !!v;
257 },
rginda9f5222b2012-03-05 11:53:28 -0800258
Leonardo Mesquita61e7c312014-01-04 12:53:12 +0100259 'ctrl-v-paste': function(v) {
260 terminal.keyboard.ctrlVPaste = v;
261 },
262
Robert Ginda57f03b42012-09-13 11:02:48 -0700263 'enable-8-bit-control': function(v) {
264 terminal.vt.enable8BitControl = !!v;
265 },
rginda30f20f62012-04-05 16:36:19 -0700266
Robert Ginda57f03b42012-09-13 11:02:48 -0700267 'enable-bold': function(v) {
268 terminal.syncBoldSafeState();
269 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400270
Robert Ginda57f03b42012-09-13 11:02:48 -0700271 'enable-clipboard-write': function(v) {
272 terminal.vt.enableClipboardWrite = !!v;
273 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400274
Robert Ginda3755e752013-05-31 13:34:09 -0700275 'enable-dec12': function(v) {
276 terminal.vt.enableDec12 = !!v;
277 },
278
Robert Ginda57f03b42012-09-13 11:02:48 -0700279 'font-family': function(v) {
280 terminal.syncFontFamily();
281 },
rginda30f20f62012-04-05 16:36:19 -0700282
Robert Ginda57f03b42012-09-13 11:02:48 -0700283 'font-size': function(v) {
284 terminal.setFontSize(v);
285 },
rginda9875d902012-08-20 16:21:57 -0700286
Robert Ginda57f03b42012-09-13 11:02:48 -0700287 'font-smoothing': function(v) {
288 terminal.syncFontFamily();
289 },
rgindade84e382012-04-20 15:39:31 -0700290
Robert Ginda57f03b42012-09-13 11:02:48 -0700291 'foreground-color': function(v) {
292 terminal.setForegroundColor(v);
293 },
rginda30f20f62012-04-05 16:36:19 -0700294
Robert Ginda57f03b42012-09-13 11:02:48 -0700295 'home-keys-scroll': function(v) {
296 terminal.keyboard.homeKeysScroll = v;
297 },
rginda4bba5e12012-06-20 16:15:30 -0700298
Robert Ginda57f03b42012-09-13 11:02:48 -0700299 'max-string-sequence': function(v) {
300 terminal.vt.maxStringSequence = v;
301 },
rginda11057d52012-04-25 12:29:56 -0700302
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700303 'media-keys-are-fkeys': function(v) {
304 terminal.keyboard.mediaKeysAreFKeys = v;
305 },
306
Robert Ginda57f03b42012-09-13 11:02:48 -0700307 'meta-sends-escape': function(v) {
308 terminal.keyboard.metaSendsEscape = v;
309 },
rginda30f20f62012-04-05 16:36:19 -0700310
Robert Ginda57f03b42012-09-13 11:02:48 -0700311 'mouse-cell-motion-trick': function(v) {
312 terminal.vt.setMouseCellMotionTrick(v);
313 },
Robert Ginda9fb38222012-09-11 14:19:12 -0700314
Robert Ginda57f03b42012-09-13 11:02:48 -0700315 'mouse-paste-button': function(v) {
316 terminal.syncMousePasteButton();
317 },
rgindaa8ba17d2012-08-15 14:41:10 -0700318
Robert Ginda40932892012-12-10 17:26:40 -0800319 'pass-alt-number': function(v) {
320 if (v == null) {
321 var osx = window.navigator.userAgent.match(/Mac OS X/);
322
323 // Let Alt-1..9 pass to the browser (to control tab switching) on
324 // non-OS X systems, or if hterm is not opened in an app window.
325 v = (!osx && hterm.windowType != 'popup');
326 }
327
328 terminal.passAltNumber = v;
329 },
330
331 'pass-ctrl-number': function(v) {
332 if (v == null) {
333 var osx = window.navigator.userAgent.match(/Mac OS X/);
334
335 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
336 // non-OS X systems, or if hterm is not opened in an app window.
337 v = (!osx && hterm.windowType != 'popup');
338 }
339
340 terminal.passCtrlNumber = v;
341 },
342
343 'pass-meta-number': function(v) {
344 if (v == null) {
345 var osx = window.navigator.userAgent.match(/Mac OS X/);
346
347 // Let Meta-1..9 pass to the browser (to control tab switching) on
348 // OS X systems, or if hterm is not opened in an app window.
349 v = (osx && hterm.windowType != 'popup');
350 }
351
352 terminal.passMetaNumber = v;
353 },
354
Robert Ginda8cb7d902013-06-20 14:37:18 -0700355 'receive-encoding': function(v) {
356 if (!(/^(utf-8|raw)$/).test(v)) {
357 console.warn('Invalid value for "receive-encoding": ' + v);
358 v = 'utf-8';
359 }
360
361 terminal.vt.characterEncoding = v;
362 },
363
Robert Ginda57f03b42012-09-13 11:02:48 -0700364 'scroll-on-keystroke': function(v) {
365 terminal.scrollOnKeystroke_ = v;
366 },
rginda9f5222b2012-03-05 11:53:28 -0800367
Robert Ginda57f03b42012-09-13 11:02:48 -0700368 'scroll-on-output': function(v) {
369 terminal.scrollOnOutput_ = v;
370 },
rginda30f20f62012-04-05 16:36:19 -0700371
Robert Ginda57f03b42012-09-13 11:02:48 -0700372 'scrollbar-visible': function(v) {
373 terminal.setScrollbarVisible(v);
374 },
rginda9f5222b2012-03-05 11:53:28 -0800375
Robert Ginda8cb7d902013-06-20 14:37:18 -0700376 'send-encoding': function(v) {
377 if (!(/^(utf-8|raw)$/).test(v)) {
378 console.warn('Invalid value for "send-encoding": ' + v);
379 v = 'utf-8';
380 }
381
382 terminal.keyboard.characterEncoding = v;
383 },
384
Robert Ginda57f03b42012-09-13 11:02:48 -0700385 'shift-insert-paste': function(v) {
386 terminal.keyboard.shiftInsertPaste = v;
387 },
rginda9f5222b2012-03-05 11:53:28 -0800388
Robert Ginda57f03b42012-09-13 11:02:48 -0700389 'page-keys-scroll': function(v) {
390 terminal.keyboard.pageKeysScroll = v;
391 }
392 });
rginda30f20f62012-04-05 16:36:19 -0700393
Robert Ginda57f03b42012-09-13 11:02:48 -0700394 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800395 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700396
397 if (opt_callback)
398 opt_callback();
399 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800400};
401
rginda8e92a692012-05-20 19:37:20 -0700402/**
403 * Set the color for the cursor.
404 *
405 * If you want this setting to persist, set it through prefs_, rather than
406 * with this method.
407 */
408hterm.Terminal.prototype.setCursorColor = function(color) {
Robert Ginda830583c2013-08-07 13:20:46 -0700409 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700410 this.cursorNode_.style.backgroundColor = color;
411 this.cursorNode_.style.borderColor = color;
412};
413
414/**
415 * Return the current cursor color as a string.
416 */
417hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700418 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700419};
420
421/**
rgindad5613292012-06-19 15:40:37 -0700422 * Enable or disable mouse based text selection in the terminal.
423 */
424hterm.Terminal.prototype.setSelectionEnabled = function(state) {
425 this.enableMouseDragScroll = state;
426 this.scrollPort_.setSelectionEnabled(state);
427};
428
429/**
rginda8e92a692012-05-20 19:37:20 -0700430 * Set the background color.
431 *
432 * If you want this setting to persist, set it through prefs_, rather than
433 * with this method.
434 */
435hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700436 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700437 this.primaryScreen_.textAttributes.setDefaults(
438 this.foregroundColor_, this.backgroundColor_);
439 this.alternateScreen_.textAttributes.setDefaults(
440 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700441 this.scrollPort_.setBackgroundColor(color);
442};
443
rginda9f5222b2012-03-05 11:53:28 -0800444/**
445 * Return the current terminal background color.
446 *
447 * Intended for use by other classes, so we don't have to expose the entire
448 * prefs_ object.
449 */
450hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700451 return this.backgroundColor_;
452};
453
454/**
455 * Set the foreground color.
456 *
457 * If you want this setting to persist, set it through prefs_, rather than
458 * with this method.
459 */
460hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700461 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700462 this.primaryScreen_.textAttributes.setDefaults(
463 this.foregroundColor_, this.backgroundColor_);
464 this.alternateScreen_.textAttributes.setDefaults(
465 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700466 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800467};
468
469/**
470 * Return the current terminal foreground color.
471 *
472 * Intended for use by other classes, so we don't have to expose the entire
473 * prefs_ object.
474 */
475hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700476 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800477};
478
479/**
rginda87b86462011-12-14 13:48:03 -0800480 * Create a new instance of a terminal command and run it with a given
481 * argument string.
482 *
483 * @param {function} commandClass The constructor for a terminal command.
484 * @param {string} argString The argument string to pass to the command.
485 */
486hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700487 var environment = this.prefs_.get('environment');
488 if (typeof environment != 'object' || environment == null)
489 environment = {};
490
rginda87b86462011-12-14 13:48:03 -0800491 var self = this;
492 this.command = new commandClass(
493 { argString: argString || '',
494 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700495 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800496 onExit: function(code) {
497 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800498 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700499 if (self.prefs_.get('close-on-exit'))
500 window.close();
rginda87b86462011-12-14 13:48:03 -0800501 }
502 });
503
rgindafeaf3142012-01-31 15:14:20 -0800504 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800505 this.command.run();
506};
507
508/**
rgindafeaf3142012-01-31 15:14:20 -0800509 * Returns true if the current screen is the primary screen, false otherwise.
510 */
511hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700512 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800513};
514
515/**
516 * Install the keyboard handler for this terminal.
517 *
518 * This will prevent the browser from seeing any keystrokes sent to the
519 * terminal.
520 */
521hterm.Terminal.prototype.installKeyboard = function() {
Toni Barzic0bfa8922013-11-22 11:18:35 -0800522 this.keyboard.installKeyboard(this.scrollPort_.getScreenNode());
rgindafeaf3142012-01-31 15:14:20 -0800523}
524
525/**
526 * Uninstall the keyboard handler for this terminal.
527 */
528hterm.Terminal.prototype.uninstallKeyboard = function() {
529 this.keyboard.installKeyboard(null);
530}
531
532/**
rginda35c456b2012-02-09 17:29:05 -0800533 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800534 *
535 * Call setFontSize(0) to reset to the default font size.
536 *
537 * This function does not modify the font-size preference.
538 *
539 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800540 */
541hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800542 if (px === 0)
543 px = this.prefs_.get('font-size');
544
rginda35c456b2012-02-09 17:29:05 -0800545 this.scrollPort_.setFontSize(px);
546};
547
548/**
549 * Get the current font size.
550 */
551hterm.Terminal.prototype.getFontSize = function() {
552 return this.scrollPort_.getFontSize();
553};
554
555/**
rginda8e92a692012-05-20 19:37:20 -0700556 * Get the current font family.
557 */
558hterm.Terminal.prototype.getFontFamily = function() {
559 return this.scrollPort_.getFontFamily();
560};
561
562/**
rginda35c456b2012-02-09 17:29:05 -0800563 * Set the CSS "font-family" for this terminal.
564 */
rginda9f5222b2012-03-05 11:53:28 -0800565hterm.Terminal.prototype.syncFontFamily = function() {
566 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
567 this.prefs_.get('font-smoothing'));
568 this.syncBoldSafeState();
569};
570
rginda4bba5e12012-06-20 16:15:30 -0700571/**
572 * Set this.mousePasteButton based on the mouse-paste-button pref,
573 * autodetecting if necessary.
574 */
575hterm.Terminal.prototype.syncMousePasteButton = function() {
576 var button = this.prefs_.get('mouse-paste-button');
577 if (typeof button == 'number') {
578 this.mousePasteButton = button;
579 return;
580 }
581
582 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
583 if (!ary || ary[2] == 'CrOS') {
584 this.mousePasteButton = 2;
585 } else {
586 this.mousePasteButton = 3;
587 }
588};
589
590/**
591 * Enable or disable bold based on the enable-bold pref, autodetecting if
592 * necessary.
593 */
rginda9f5222b2012-03-05 11:53:28 -0800594hterm.Terminal.prototype.syncBoldSafeState = function() {
595 var enableBold = this.prefs_.get('enable-bold');
596 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700597 this.primaryScreen_.textAttributes.enableBold = enableBold;
598 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800599 return;
600 }
601
rgindaf7521392012-02-28 17:20:34 -0800602 var normalSize = this.scrollPort_.measureCharacterSize();
603 var boldSize = this.scrollPort_.measureCharacterSize('bold');
604
605 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800606 if (!isBoldSafe) {
607 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700608 'from normal. Font family is: ' +
609 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800610 }
rginda9f5222b2012-03-05 11:53:28 -0800611
Robert Gindaed016262012-10-26 16:27:09 -0700612 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
613 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800614};
615
616/**
rginda87b86462011-12-14 13:48:03 -0800617 * Return a copy of the current cursor position.
618 *
619 * @return {hterm.RowCol} The RowCol object representing the current position.
620 */
621hterm.Terminal.prototype.saveCursor = function() {
622 return this.screen_.cursorPosition.clone();
623};
624
rgindaa19afe22012-01-25 15:40:22 -0800625hterm.Terminal.prototype.getTextAttributes = function() {
626 return this.screen_.textAttributes;
627};
628
rginda1a09aa02012-06-18 21:11:25 -0700629hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
630 this.screen_.textAttributes = textAttributes;
631};
632
rginda87b86462011-12-14 13:48:03 -0800633/**
rgindaf522ce02012-04-17 17:49:17 -0700634 * Return the current browser zoom factor applied to the terminal.
635 *
636 * @return {number} The current browser zoom factor.
637 */
638hterm.Terminal.prototype.getZoomFactor = function() {
639 return this.scrollPort_.characterSize.zoomFactor;
640};
641
642/**
rginda9846e2f2012-01-27 13:53:33 -0800643 * Change the title of this terminal's window.
644 */
645hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800646 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800647};
648
649/**
rginda87b86462011-12-14 13:48:03 -0800650 * Restore a previously saved cursor position.
651 *
652 * @param {hterm.RowCol} cursor The position to restore.
653 */
654hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700655 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
656 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800657 this.screen_.setCursorPosition(row, column);
658 if (cursor.column > column ||
659 cursor.column == column && cursor.overflow) {
660 this.screen_.cursorPosition.overflow = true;
661 }
rginda87b86462011-12-14 13:48:03 -0800662};
663
664/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400665 * Clear the cursor's overflow flag.
666 */
667hterm.Terminal.prototype.clearCursorOverflow = function() {
668 this.screen_.cursorPosition.overflow = false;
669};
670
671/**
Robert Ginda830583c2013-08-07 13:20:46 -0700672 * Sets the cursor shape
673 */
674hterm.Terminal.prototype.setCursorShape = function(shape) {
675 this.cursorShape_ = shape;
Robert Gindafb1be6a2013-12-11 11:56:22 -0800676 this.restyleCursor_();
Robert Ginda830583c2013-08-07 13:20:46 -0700677}
678
679/**
680 * Get the cursor shape
681 */
682hterm.Terminal.prototype.getCursorShape = function() {
683 return this.cursorShape_;
684}
685
686/**
rginda87b86462011-12-14 13:48:03 -0800687 * Set the width of the terminal, resizing the UI to match.
688 */
689hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800690 if (columnCount == null) {
691 this.div_.style.width = '100%';
692 return;
693 }
694
rginda35c456b2012-02-09 17:29:05 -0800695 this.div_.style.width = this.scrollPort_.characterSize.width *
Robert Ginda97769282013-02-01 15:30:30 -0800696 columnCount + this.scrollPort_.currentScrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400697 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800698 this.scheduleSyncCursorPosition_();
699};
rginda87b86462011-12-14 13:48:03 -0800700
rgindac9bc5502012-01-18 11:48:44 -0800701/**
rginda35c456b2012-02-09 17:29:05 -0800702 * Set the height of the terminal, resizing the UI to match.
703 */
704hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800705 if (rowCount == null) {
706 this.div_.style.height = '100%';
707 return;
708 }
709
rginda35c456b2012-02-09 17:29:05 -0800710 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700711 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800712 this.realizeSize_(this.screenSize.width, rowCount);
713 this.scheduleSyncCursorPosition_();
714};
715
716/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400717 * Deal with terminal size changes.
718 *
719 */
720hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
721 if (columnCount != this.screenSize.width)
722 this.realizeWidth_(columnCount);
723
724 if (rowCount != this.screenSize.height)
725 this.realizeHeight_(rowCount);
726
727 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -0700728 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400729};
730
731/**
rgindac9bc5502012-01-18 11:48:44 -0800732 * Deal with terminal width changes.
733 *
734 * This function does what needs to be done when the terminal width changes
735 * out from under us. It happens here rather than in onResize_() because this
736 * code may need to run synchronously to handle programmatic changes of
737 * terminal width.
738 *
739 * Relying on the browser to send us an async resize event means we may not be
740 * in the correct state yet when the next escape sequence hits.
741 */
742hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700743 if (columnCount <= 0)
744 throw new Error('Attempt to realize bad width: ' + columnCount);
745
rgindac9bc5502012-01-18 11:48:44 -0800746 var deltaColumns = columnCount - this.screen_.getWidth();
747
rginda87b86462011-12-14 13:48:03 -0800748 this.screenSize.width = columnCount;
749 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800750
751 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400752 if (this.defaultTabStops)
753 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800754 } else {
755 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400756 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800757 break;
758
759 this.tabStops_.pop();
760 }
761 }
762
763 this.screen_.setColumnCount(this.screenSize.width);
764};
765
766/**
767 * Deal with terminal height changes.
768 *
769 * This function does what needs to be done when the terminal height changes
770 * out from under us. It happens here rather than in onResize_() because this
771 * code may need to run synchronously to handle programmatic changes of
772 * terminal height.
773 *
774 * Relying on the browser to send us an async resize event means we may not be
775 * in the correct state yet when the next escape sequence hits.
776 */
777hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700778 if (rowCount <= 0)
779 throw new Error('Attempt to realize bad height: ' + rowCount);
780
rgindac9bc5502012-01-18 11:48:44 -0800781 var deltaRows = rowCount - this.screen_.getHeight();
782
783 this.screenSize.height = rowCount;
784
785 var cursor = this.saveCursor();
786
787 if (deltaRows < 0) {
788 // Screen got smaller.
789 deltaRows *= -1;
790 while (deltaRows) {
791 var lastRow = this.getRowCount() - 1;
792 if (lastRow - this.scrollbackRows_.length == cursor.row)
793 break;
794
795 if (this.getRowText(lastRow))
796 break;
797
798 this.screen_.popRow();
799 deltaRows--;
800 }
801
802 var ary = this.screen_.shiftRows(deltaRows);
803 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
804
805 // We just removed rows from the top of the screen, we need to update
806 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800807 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800808 } else if (deltaRows > 0) {
809 // Screen got larger.
810
811 if (deltaRows <= this.scrollbackRows_.length) {
812 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
813 var rows = this.scrollbackRows_.splice(
814 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
815 this.screen_.unshiftRows(rows);
816 deltaRows -= scrollbackCount;
817 cursor.row += scrollbackCount;
818 }
819
820 if (deltaRows)
821 this.appendRows_(deltaRows);
822 }
823
rginda35c456b2012-02-09 17:29:05 -0800824 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800825 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800826};
827
828/**
829 * Scroll the terminal to the top of the scrollback buffer.
830 */
831hterm.Terminal.prototype.scrollHome = function() {
832 this.scrollPort_.scrollRowToTop(0);
833};
834
835/**
836 * Scroll the terminal to the end.
837 */
838hterm.Terminal.prototype.scrollEnd = function() {
839 this.scrollPort_.scrollRowToBottom(this.getRowCount());
840};
841
842/**
843 * Scroll the terminal one page up (minus one line) relative to the current
844 * position.
845 */
846hterm.Terminal.prototype.scrollPageUp = function() {
847 var i = this.scrollPort_.getTopRowIndex();
848 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
849};
850
851/**
852 * Scroll the terminal one page down (minus one line) relative to the current
853 * position.
854 */
855hterm.Terminal.prototype.scrollPageDown = function() {
856 var i = this.scrollPort_.getTopRowIndex();
857 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800858};
859
rgindac9bc5502012-01-18 11:48:44 -0800860/**
Robert Ginda40932892012-12-10 17:26:40 -0800861 * Clear primary screen, secondary screen, and the scrollback buffer.
862 */
863hterm.Terminal.prototype.wipeContents = function() {
864 this.scrollbackRows_.length = 0;
865 this.scrollPort_.resetCache();
866
867 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
868 var bottom = screen.getHeight();
869 if (bottom > 0) {
870 this.renumberRows_(0, bottom);
871 this.clearHome(screen);
872 }
873 }.bind(this));
874
875 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -0700876 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -0800877};
878
879/**
rgindac9bc5502012-01-18 11:48:44 -0800880 * Full terminal reset.
881 */
rginda87b86462011-12-14 13:48:03 -0800882hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800883 this.clearAllTabStops();
884 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -0700885
886 this.clearHome(this.primaryScreen_);
887 this.primaryScreen_.textAttributes.reset();
888
889 this.clearHome(this.alternateScreen_);
890 this.alternateScreen_.textAttributes.reset();
891
rgindab8bc8932012-04-27 12:45:03 -0700892 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
893
Robert Ginda92e18102013-03-14 13:56:37 -0700894 this.vt.reset();
895
rgindac9bc5502012-01-18 11:48:44 -0800896 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800897};
898
rgindac9bc5502012-01-18 11:48:44 -0800899/**
900 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -0700901 *
902 * Perform a soft reset to the default values listed in
903 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -0800904 */
rginda0f5c0292012-01-13 11:00:13 -0800905hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -0700906 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -0800907 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -0700908
rgindab8bc8932012-04-27 12:45:03 -0700909 // Xterm also resets the color palette on soft reset, even though it doesn't
910 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -0700911 this.primaryScreen_.textAttributes.resetColorPalette();
912 this.alternateScreen_.textAttributes.resetColorPalette();
913
rgindab8bc8932012-04-27 12:45:03 -0700914 // The xterm man page explicitly says this will happen on soft reset.
915 this.setVTScrollRegion(null, null);
916
917 // Xterm also shows the cursor on soft reset, but does not alter the blink
918 // state.
rgindaa19afe22012-01-25 15:40:22 -0800919 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -0800920};
921
rgindac9bc5502012-01-18 11:48:44 -0800922/**
923 * Move the cursor forward to the next tab stop, or to the last column
924 * if no more tab stops are set.
925 */
926hterm.Terminal.prototype.forwardTabStop = function() {
927 var column = this.screen_.cursorPosition.column;
928
929 for (var i = 0; i < this.tabStops_.length; i++) {
930 if (this.tabStops_[i] > column) {
931 this.setCursorColumn(this.tabStops_[i]);
932 return;
933 }
934 }
935
David Benjamin66e954d2012-05-05 21:08:12 -0400936 // xterm does not clear the overflow flag on HT or CHT.
937 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -0800938 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -0400939 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -0800940};
941
rgindac9bc5502012-01-18 11:48:44 -0800942/**
943 * Move the cursor backward to the previous tab stop, or to the first column
944 * if no previous tab stops are set.
945 */
946hterm.Terminal.prototype.backwardTabStop = function() {
947 var column = this.screen_.cursorPosition.column;
948
949 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
950 if (this.tabStops_[i] < column) {
951 this.setCursorColumn(this.tabStops_[i]);
952 return;
953 }
954 }
955
956 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800957};
958
rgindac9bc5502012-01-18 11:48:44 -0800959/**
960 * Set a tab stop at the given column.
961 *
962 * @param {int} column Zero based column.
963 */
964hterm.Terminal.prototype.setTabStop = function(column) {
965 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
966 if (this.tabStops_[i] == column)
967 return;
968
969 if (this.tabStops_[i] < column) {
970 this.tabStops_.splice(i + 1, 0, column);
971 return;
972 }
973 }
974
975 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800976};
977
rgindac9bc5502012-01-18 11:48:44 -0800978/**
979 * Clear the tab stop at the current cursor position.
980 *
981 * No effect if there is no tab stop at the current cursor position.
982 */
983hterm.Terminal.prototype.clearTabStopAtCursor = function() {
984 var column = this.screen_.cursorPosition.column;
985
986 var i = this.tabStops_.indexOf(column);
987 if (i == -1)
988 return;
989
990 this.tabStops_.splice(i, 1);
991};
992
993/**
994 * Clear all tab stops.
995 */
996hterm.Terminal.prototype.clearAllTabStops = function() {
997 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -0400998 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -0800999};
1000
1001/**
1002 * Set up the default tab stops, starting from a given column.
1003 *
1004 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001005 * from the specified column, or 0 if no column is provided. It also flags
1006 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001007 *
1008 * This does not clear the existing tab stops first, use clearAllTabStops
1009 * for that.
1010 *
1011 * @param {int} opt_start Optional starting zero based starting column, useful
1012 * for filling out missing tab stops when the terminal is resized.
1013 */
1014hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1015 var start = opt_start || 0;
1016 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001017 // Round start up to a default tab stop.
1018 start = start - 1 - ((start - 1) % w) + w;
1019 for (var i = start; i < this.screenSize.width; i += w) {
1020 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001021 }
David Benjamin66e954d2012-05-05 21:08:12 -04001022
1023 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001024};
1025
rginda6d397402012-01-17 10:58:29 -08001026/**
rginda8ba33642011-12-14 12:31:31 -08001027 * Interpret a sequence of characters.
1028 *
1029 * Incomplete escape sequences are buffered until the next call.
1030 *
1031 * @param {string} str Sequence of characters to interpret or pass through.
1032 */
1033hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001034 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001035 this.scheduleSyncCursorPosition_();
1036};
1037
1038/**
1039 * Take over the given DIV for use as the terminal display.
1040 *
1041 * @param {HTMLDivElement} div The div to use as the terminal display.
1042 */
1043hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001044 this.div_ = div;
1045
rginda8ba33642011-12-14 12:31:31 -08001046 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001047 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001048 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1049 this.scrollPort_.setBackgroundPosition(
1050 this.prefs_.get('background-position'));
rginda30f20f62012-04-05 16:36:19 -07001051
rginda0918b652012-04-04 11:26:24 -07001052 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001053
rginda9f5222b2012-03-05 11:53:28 -08001054 this.setFontSize(this.prefs_.get('font-size'));
1055 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001056
David Reveman8f552492012-03-28 12:18:41 -04001057 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
1058
rginda8ba33642011-12-14 12:31:31 -08001059 this.document_ = this.scrollPort_.getDocument();
1060
rginda4bba5e12012-06-20 16:15:30 -07001061 this.document_.body.oncontextmenu = function() { return false };
1062
1063 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001064 var screenNode = this.scrollPort_.getScreenNode();
1065 screenNode.addEventListener('mousedown', onMouse);
1066 screenNode.addEventListener('mouseup', onMouse);
1067 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001068 this.scrollPort_.onScrollWheel = onMouse;
1069
Toni Barzic0bfa8922013-11-22 11:18:35 -08001070 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001071 'focus', this.onFocusChange_.bind(this, true));
Toni Barzic0bfa8922013-11-22 11:18:35 -08001072 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001073 'blur', this.onFocusChange_.bind(this, false));
1074
1075 var style = this.document_.createElement('style');
1076 style.textContent =
1077 ('.cursor-node[focus="false"] {' +
1078 ' box-sizing: border-box;' +
1079 ' background-color: transparent !important;' +
1080 ' border-width: 2px;' +
1081 ' border-style: solid;' +
1082 '}');
1083 this.document_.head.appendChild(style);
1084
rginda8ba33642011-12-14 12:31:31 -08001085 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001086 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001087 this.cursorNode_.style.cssText =
1088 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001089 'top: -99px;' +
1090 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001091 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1092 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001093 '-webkit-transition: opacity, background-color 100ms linear;');
Robert Gindafb1be6a2013-12-11 11:56:22 -08001094
rginda8e92a692012-05-20 19:37:20 -07001095 this.setCursorColor(this.prefs_.get('cursor-color'));
Robert Gindafb1be6a2013-12-11 11:56:22 -08001096 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
1097 this.restyleCursor_();
rgindad5613292012-06-19 15:40:37 -07001098
rginda8ba33642011-12-14 12:31:31 -08001099 this.document_.body.appendChild(this.cursorNode_);
1100
rgindad5613292012-06-19 15:40:37 -07001101 // When 'enableMouseDragScroll' is off we reposition this element directly
1102 // under the mouse cursor after a click. This makes Chrome associate
1103 // subsequent mousemove events with the scroll-blocker. Since the
1104 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1105 // events do not cause the scrollport to scroll.
1106 //
1107 // It's a hack, but it's the cleanest way I could find.
1108 this.scrollBlockerNode_ = this.document_.createElement('div');
1109 this.scrollBlockerNode_.style.cssText =
1110 ('position: absolute;' +
1111 'top: -99px;' +
1112 'display: block;' +
1113 'width: 10px;' +
1114 'height: 10px;');
1115 this.document_.body.appendChild(this.scrollBlockerNode_);
1116
1117 var onMouse = this.onMouse_.bind(this);
1118 this.scrollPort_.onScrollWheel = onMouse;
1119 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1120 ].forEach(function(event) {
1121 this.scrollBlockerNode_.addEventListener(event, onMouse);
1122 this.cursorNode_.addEventListener(event, onMouse);
1123 this.document_.addEventListener(event, onMouse);
1124 }.bind(this));
1125
1126 this.cursorNode_.addEventListener('mousedown', function() {
1127 setTimeout(this.focus.bind(this));
1128 }.bind(this));
1129
rginda8ba33642011-12-14 12:31:31 -08001130 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001131
rginda87b86462011-12-14 13:48:03 -08001132 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001133 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001134};
1135
rginda0918b652012-04-04 11:26:24 -07001136/**
1137 * Return the HTML document that contains the terminal DOM nodes.
1138 */
rginda87b86462011-12-14 13:48:03 -08001139hterm.Terminal.prototype.getDocument = function() {
1140 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001141};
1142
1143/**
rginda0918b652012-04-04 11:26:24 -07001144 * Focus the terminal.
1145 */
1146hterm.Terminal.prototype.focus = function() {
1147 this.scrollPort_.focus();
1148};
1149
1150/**
rginda8ba33642011-12-14 12:31:31 -08001151 * Return the HTML Element for a given row index.
1152 *
1153 * This is a method from the RowProvider interface. The ScrollPort uses
1154 * it to fetch rows on demand as they are scrolled into view.
1155 *
1156 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1157 * pairs to conserve memory.
1158 *
1159 * @param {integer} index The zero-based row index, measured relative to the
1160 * start of the scrollback buffer. On-screen rows will always have the
1161 * largest indicies.
1162 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1163 */
1164hterm.Terminal.prototype.getRowNode = function(index) {
1165 if (index < this.scrollbackRows_.length)
1166 return this.scrollbackRows_[index];
1167
1168 var screenIndex = index - this.scrollbackRows_.length;
1169 return this.screen_.rowsArray[screenIndex];
1170};
1171
1172/**
1173 * Return the text content for a given range of rows.
1174 *
1175 * This is a method from the RowProvider interface. The ScrollPort uses
1176 * it to fetch text content on demand when the user attempts to copy their
1177 * selection to the clipboard.
1178 *
1179 * @param {integer} start The zero-based row index to start from, measured
1180 * relative to the start of the scrollback buffer. On-screen rows will
1181 * always have the largest indicies.
1182 * @param {integer} end The zero-based row index to end on, measured
1183 * relative to the start of the scrollback buffer.
1184 * @return {string} A single string containing the text value of the range of
1185 * rows. Lines will be newline delimited, with no trailing newline.
1186 */
1187hterm.Terminal.prototype.getRowsText = function(start, end) {
1188 var ary = [];
1189 for (var i = start; i < end; i++) {
1190 var node = this.getRowNode(i);
1191 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001192 if (i < end - 1 && !node.getAttribute('line-overflow'))
1193 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001194 }
1195
rgindaa09e7332012-08-17 12:49:51 -07001196 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001197};
1198
1199/**
1200 * Return the text content for a given row.
1201 *
1202 * This is a method from the RowProvider interface. The ScrollPort uses
1203 * it to fetch text content on demand when the user attempts to copy their
1204 * selection to the clipboard.
1205 *
1206 * @param {integer} index The zero-based row index to return, measured
1207 * relative to the start of the scrollback buffer. On-screen rows will
1208 * always have the largest indicies.
1209 * @return {string} A string containing the text value of the selected row.
1210 */
1211hterm.Terminal.prototype.getRowText = function(index) {
1212 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001213 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001214};
1215
1216/**
1217 * Return the total number of rows in the addressable screen and in the
1218 * scrollback buffer of this terminal.
1219 *
1220 * This is a method from the RowProvider interface. The ScrollPort uses
1221 * it to compute the size of the scrollbar.
1222 *
1223 * @return {integer} The number of rows in this terminal.
1224 */
1225hterm.Terminal.prototype.getRowCount = function() {
1226 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1227};
1228
1229/**
1230 * Create DOM nodes for new rows and append them to the end of the terminal.
1231 *
1232 * This is the only correct way to add a new DOM node for a row. Notice that
1233 * the new row is appended to the bottom of the list of rows, and does not
1234 * require renumbering (of the rowIndex property) of previous rows.
1235 *
1236 * If you think you want a new blank row somewhere in the middle of the
1237 * terminal, look into moveRows_().
1238 *
1239 * This method does not pay attention to vtScrollTop/Bottom, since you should
1240 * be using moveRows() in cases where they would matter.
1241 *
1242 * The cursor will be positioned at column 0 of the first inserted line.
1243 */
1244hterm.Terminal.prototype.appendRows_ = function(count) {
1245 var cursorRow = this.screen_.rowsArray.length;
1246 var offset = this.scrollbackRows_.length + cursorRow;
1247 for (var i = 0; i < count; i++) {
1248 var row = this.document_.createElement('x-row');
1249 row.appendChild(this.document_.createTextNode(''));
1250 row.rowIndex = offset + i;
1251 this.screen_.pushRow(row);
1252 }
1253
1254 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1255 if (extraRows > 0) {
1256 var ary = this.screen_.shiftRows(extraRows);
1257 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001258 if (this.scrollPort_.isScrolledEnd)
1259 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001260 }
1261
1262 if (cursorRow >= this.screen_.rowsArray.length)
1263 cursorRow = this.screen_.rowsArray.length - 1;
1264
rginda87b86462011-12-14 13:48:03 -08001265 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001266};
1267
1268/**
1269 * Relocate rows from one part of the addressable screen to another.
1270 *
1271 * This is used to recycle rows during VT scrolls (those which are driven
1272 * by VT commands, rather than by the user manipulating the scrollbar.)
1273 *
1274 * In this case, the blank lines scrolled into the scroll region are made of
1275 * the nodes we scrolled off. These have their rowIndex properties carefully
1276 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001277 */
1278hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1279 var ary = this.screen_.removeRows(fromIndex, count);
1280 this.screen_.insertRows(toIndex, ary);
1281
1282 var start, end;
1283 if (fromIndex < toIndex) {
1284 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001285 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001286 } else {
1287 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001288 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001289 }
1290
1291 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001292 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001293};
1294
1295/**
1296 * Renumber the rowIndex property of the given range of rows.
1297 *
1298 * The start and end indicies are relative to the screen, not the scrollback.
1299 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001300 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001301 * no need to renumber scrollback rows.
1302 */
Robert Ginda40932892012-12-10 17:26:40 -08001303hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1304 var screen = opt_screen || this.screen_;
1305
rginda8ba33642011-12-14 12:31:31 -08001306 var offset = this.scrollbackRows_.length;
1307 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001308 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001309 }
1310};
1311
1312/**
1313 * Print a string to the terminal.
1314 *
1315 * This respects the current insert and wraparound modes. It will add new lines
1316 * to the end of the terminal, scrolling off the top into the scrollback buffer
1317 * if necessary.
1318 *
1319 * The string is *not* parsed for escape codes. Use the interpret() method if
1320 * that's what you're after.
1321 *
1322 * @param{string} str The string to print.
1323 */
1324hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001325 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001326
rgindaa9abdd82012-08-06 18:05:09 -07001327 while (startOffset < str.length) {
rgindaa09e7332012-08-17 12:49:51 -07001328 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1329 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001330 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001331 }
rgindaa19afe22012-01-25 15:40:22 -08001332
rgindaa9abdd82012-08-06 18:05:09 -07001333 var count = str.length - startOffset;
1334 var didOverflow = false;
1335 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001336
rgindaa9abdd82012-08-06 18:05:09 -07001337 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1338 didOverflow = true;
1339 count = this.screenSize.width - this.screen_.cursorPosition.column;
1340 }
rgindaa19afe22012-01-25 15:40:22 -08001341
rgindaa9abdd82012-08-06 18:05:09 -07001342 if (didOverflow && !this.options_.wraparound) {
1343 // If the string overflowed the line but wraparound is off, then the
1344 // last printed character should be the last of the string.
1345 // TODO: This will add to our problems with multibyte UTF-16 characters.
1346 substr = str.substr(startOffset, count - 1) +
1347 str.substr(str.length - 1);
1348 count = str.length;
1349 } else {
1350 substr = str.substr(startOffset, count);
1351 }
rgindaa19afe22012-01-25 15:40:22 -08001352
rgindaa9abdd82012-08-06 18:05:09 -07001353 if (this.options_.insertMode) {
1354 this.screen_.insertString(substr);
1355 } else {
1356 this.screen_.overwriteString(substr);
1357 }
1358
1359 this.screen_.maybeClipCurrentRow();
1360 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001361 }
rginda8ba33642011-12-14 12:31:31 -08001362
1363 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001364
rginda9f5222b2012-03-05 11:53:28 -08001365 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001366 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001367};
1368
1369/**
rginda87b86462011-12-14 13:48:03 -08001370 * Set the VT scroll region.
1371 *
rginda87b86462011-12-14 13:48:03 -08001372 * This also resets the cursor position to the absolute (0, 0) position, since
1373 * that's what xterm appears to do.
1374 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001375 * Setting the scroll region to the full height of the terminal will clear
1376 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1377 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1378 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1379 * continue to work as most users would expect.
1380 *
rginda87b86462011-12-14 13:48:03 -08001381 * @param {integer} scrollTop The zero-based top of the scroll region.
1382 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1383 * inclusive.
1384 */
1385hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001386 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001387 this.vtScrollTop_ = null;
1388 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001389 } else {
1390 this.vtScrollTop_ = scrollTop;
1391 this.vtScrollBottom_ = scrollBottom;
1392 }
rginda87b86462011-12-14 13:48:03 -08001393};
1394
1395/**
rginda8ba33642011-12-14 12:31:31 -08001396 * Return the top row index according to the VT.
1397 *
1398 * This will return 0 unless the terminal has been told to restrict scrolling
1399 * to some lower row. It is used for some VT cursor positioning and scrolling
1400 * commands.
1401 *
1402 * @return {integer} The topmost row in the terminal's scroll region.
1403 */
1404hterm.Terminal.prototype.getVTScrollTop = function() {
1405 if (this.vtScrollTop_ != null)
1406 return this.vtScrollTop_;
1407
1408 return 0;
rginda87b86462011-12-14 13:48:03 -08001409};
rginda8ba33642011-12-14 12:31:31 -08001410
1411/**
1412 * Return the bottom row index according to the VT.
1413 *
1414 * This will return the height of the terminal unless the it has been told to
1415 * restrict scrolling to some higher row. It is used for some VT cursor
1416 * positioning and scrolling commands.
1417 *
1418 * @return {integer} The bottommost row in the terminal's scroll region.
1419 */
1420hterm.Terminal.prototype.getVTScrollBottom = function() {
1421 if (this.vtScrollBottom_ != null)
1422 return this.vtScrollBottom_;
1423
rginda87b86462011-12-14 13:48:03 -08001424 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001425}
1426
1427/**
1428 * Process a '\n' character.
1429 *
1430 * If the cursor is on the final row of the terminal this will append a new
1431 * blank row to the screen and scroll the topmost row into the scrollback
1432 * buffer.
1433 *
1434 * Otherwise, this moves the cursor to column zero of the next row.
1435 */
1436hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001437 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1438 this.screen_.rowsArray.length - 1);
1439
1440 if (this.vtScrollBottom_ != null) {
1441 // A VT Scroll region is active, we never append new rows.
1442 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1443 // We're at the end of the VT Scroll Region, perform a VT scroll.
1444 this.vtScrollUp(1);
1445 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1446 } else if (cursorAtEndOfScreen) {
1447 // We're at the end of the screen, the only thing to do is put the
1448 // cursor to column 0.
1449 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1450 } else {
1451 // Anywhere else, advance the cursor row, and reset the column.
1452 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1453 }
1454 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001455 // We're at the end of the screen. Append a new row to the terminal,
1456 // shifting the top row into the scrollback.
1457 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001458 } else {
rginda87b86462011-12-14 13:48:03 -08001459 // Anywhere else in the screen just moves the cursor.
1460 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001461 }
1462};
1463
1464/**
1465 * Like newLine(), except maintain the cursor column.
1466 */
1467hterm.Terminal.prototype.lineFeed = function() {
1468 var column = this.screen_.cursorPosition.column;
1469 this.newLine();
1470 this.setCursorColumn(column);
1471};
1472
1473/**
rginda87b86462011-12-14 13:48:03 -08001474 * If autoCarriageReturn is set then newLine(), else lineFeed().
1475 */
1476hterm.Terminal.prototype.formFeed = function() {
1477 if (this.options_.autoCarriageReturn) {
1478 this.newLine();
1479 } else {
1480 this.lineFeed();
1481 }
1482};
1483
1484/**
1485 * Move the cursor up one row, possibly inserting a blank line.
1486 *
1487 * The cursor column is not changed.
1488 */
1489hterm.Terminal.prototype.reverseLineFeed = function() {
1490 var scrollTop = this.getVTScrollTop();
1491 var currentRow = this.screen_.cursorPosition.row;
1492
1493 if (currentRow == scrollTop) {
1494 this.insertLines(1);
1495 } else {
1496 this.setAbsoluteCursorRow(currentRow - 1);
1497 }
1498};
1499
1500/**
rginda8ba33642011-12-14 12:31:31 -08001501 * Replace all characters to the left of the current cursor with the space
1502 * character.
1503 *
1504 * TODO(rginda): This should probably *remove* the characters (not just replace
1505 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001506 * position.
rginda8ba33642011-12-14 12:31:31 -08001507 */
1508hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001509 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001510 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001511 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001512 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001513};
1514
1515/**
David Benjamin684a9b72012-05-01 17:19:58 -04001516 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001517 *
1518 * The cursor position is unchanged.
1519 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001520 * If the current background color is not the default background color this
1521 * will insert spaces rather than delete. This is unfortunate because the
1522 * trailing space will affect text selection, but it's difficult to come up
1523 * with a way to style empty space that wouldn't trip up the hterm.Screen
1524 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001525 *
1526 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1527 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1528 * crbug.com/232390 for details.
rginda8ba33642011-12-14 12:31:31 -08001529 */
1530hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001531 if (this.screen_.cursorPosition.overflow)
1532 return;
1533
Robert Ginda7fd57082012-09-25 14:41:47 -07001534 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1535 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001536
1537 if (this.screen_.textAttributes.background ===
1538 this.screen_.textAttributes.DEFAULT_COLOR) {
1539 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
1540 if (cursorRow.textContent.length <=
1541 this.screen_.cursorPosition.column + count) {
1542 this.screen_.deleteChars(count);
1543 this.clearCursorOverflow();
1544 return;
1545 }
1546 }
1547
rginda87b86462011-12-14 13:48:03 -08001548 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001549 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001550 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001551 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001552};
1553
1554/**
1555 * Erase the current line.
1556 *
1557 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001558 */
1559hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001560 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001561 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001562 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001563 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001564};
1565
1566/**
David Benjamina08d78f2012-05-05 00:28:49 -04001567 * Erase all characters from the start of the screen to the current cursor
1568 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001569 *
1570 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001571 */
1572hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001573 var cursor = this.saveCursor();
1574
1575 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001576
David Benjamina08d78f2012-05-05 00:28:49 -04001577 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001578 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001579 this.screen_.clearCursorRow();
1580 }
1581
rginda87b86462011-12-14 13:48:03 -08001582 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001583 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001584};
1585
1586/**
1587 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001588 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001589 *
1590 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001591 */
1592hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001593 var cursor = this.saveCursor();
1594
1595 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001596
David Benjamina08d78f2012-05-05 00:28:49 -04001597 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001598 for (var i = cursor.row + 1; i <= bottom; i++) {
1599 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001600 this.screen_.clearCursorRow();
1601 }
1602
rginda87b86462011-12-14 13:48:03 -08001603 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001604 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001605};
1606
1607/**
1608 * Fill the terminal with a given character.
1609 *
1610 * This methods does not respect the VT scroll region.
1611 *
1612 * @param {string} ch The character to use for the fill.
1613 */
1614hterm.Terminal.prototype.fill = function(ch) {
1615 var cursor = this.saveCursor();
1616
1617 this.setAbsoluteCursorPosition(0, 0);
1618 for (var row = 0; row < this.screenSize.height; row++) {
1619 for (var col = 0; col < this.screenSize.width; col++) {
1620 this.setAbsoluteCursorPosition(row, col);
1621 this.screen_.overwriteString(ch);
1622 }
1623 }
1624
1625 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001626};
1627
1628/**
rginda9ea433c2012-03-16 11:57:00 -07001629 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001630 *
rginda9ea433c2012-03-16 11:57:00 -07001631 * This does not respect the scroll region.
1632 *
1633 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1634 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001635 */
rginda9ea433c2012-03-16 11:57:00 -07001636hterm.Terminal.prototype.clearHome = function(opt_screen) {
1637 var screen = opt_screen || this.screen_;
1638 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001639
rginda11057d52012-04-25 12:29:56 -07001640 if (bottom == 0) {
1641 // Empty screen, nothing to do.
1642 return;
1643 }
1644
rgindae4d29232012-01-19 10:47:13 -08001645 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001646 screen.setCursorPosition(i, 0);
1647 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001648 }
1649
rginda9ea433c2012-03-16 11:57:00 -07001650 screen.setCursorPosition(0, 0);
1651};
1652
1653/**
1654 * Erase the entire display without changing the cursor position.
1655 *
1656 * The cursor position is unchanged. This does not respect the scroll
1657 * region.
1658 *
1659 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1660 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001661 */
1662hterm.Terminal.prototype.clear = function(opt_screen) {
1663 var screen = opt_screen || this.screen_;
1664 var cursor = screen.cursorPosition.clone();
1665 this.clearHome(screen);
1666 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001667};
1668
1669/**
1670 * VT command to insert lines at the current cursor row.
1671 *
1672 * This respects the current scroll region. Rows pushed off the bottom are
1673 * lost (they won't show up in the scrollback buffer).
1674 *
rginda8ba33642011-12-14 12:31:31 -08001675 * @param {integer} count The number of lines to insert.
1676 */
1677hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07001678 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08001679
1680 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07001681 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08001682
Robert Ginda579186b2012-09-26 11:40:04 -07001683 // The moveCount is the number of rows we need to relocate to make room for
1684 // the new row(s). The count is the distance to move them.
1685 var moveCount = bottom - cursorRow - count + 1;
1686 if (moveCount)
1687 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08001688
Robert Ginda579186b2012-09-26 11:40:04 -07001689 for (var i = count - 1; i >= 0; i--) {
1690 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001691 this.screen_.clearCursorRow();
1692 }
rginda8ba33642011-12-14 12:31:31 -08001693};
1694
1695/**
1696 * VT command to delete lines at the current cursor row.
1697 *
1698 * New rows are added to the bottom of scroll region to take their place. New
1699 * rows are strictly there to take up space and have no content or style.
1700 */
1701hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001702 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001703
rginda87b86462011-12-14 13:48:03 -08001704 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001705 var bottom = this.getVTScrollBottom();
1706
rginda87b86462011-12-14 13:48:03 -08001707 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001708 count = Math.min(count, maxCount);
1709
rginda87b86462011-12-14 13:48:03 -08001710 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001711 if (count != maxCount)
1712 this.moveRows_(top, count, moveStart);
1713
1714 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001715 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001716 this.screen_.clearCursorRow();
1717 }
1718
rginda87b86462011-12-14 13:48:03 -08001719 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001720 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001721};
1722
1723/**
1724 * Inserts the given number of spaces at the current cursor position.
1725 *
rginda87b86462011-12-14 13:48:03 -08001726 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001727 */
1728hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001729 var cursor = this.saveCursor();
1730
rgindacbbd7482012-06-13 15:06:16 -07001731 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001732 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001733 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001734
1735 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001736 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001737};
1738
1739/**
1740 * Forward-delete the specified number of characters starting at the cursor
1741 * position.
1742 *
1743 * @param {integer} count The number of characters to delete.
1744 */
1745hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001746 var deleted = this.screen_.deleteChars(count);
1747 if (deleted && !this.screen_.textAttributes.isDefault()) {
1748 var cursor = this.saveCursor();
1749 this.setCursorColumn(this.screenSize.width - deleted);
1750 this.screen_.insertString(lib.f.getWhitespace(deleted));
1751 this.restoreCursor(cursor);
1752 }
1753
David Benjamin54e8bf62012-06-01 22:31:40 -04001754 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001755};
1756
1757/**
1758 * Shift rows in the scroll region upwards by a given number of lines.
1759 *
1760 * New rows are inserted at the bottom of the scroll region to fill the
1761 * vacated rows. The new rows not filled out with the current text attributes.
1762 *
1763 * This function does not affect the scrollback rows at all. Rows shifted
1764 * off the top are lost.
1765 *
rginda87b86462011-12-14 13:48:03 -08001766 * The cursor position is not altered.
1767 *
rginda8ba33642011-12-14 12:31:31 -08001768 * @param {integer} count The number of rows to scroll.
1769 */
1770hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001771 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001772
rginda87b86462011-12-14 13:48:03 -08001773 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001774 this.deleteLines(count);
1775
rginda87b86462011-12-14 13:48:03 -08001776 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001777};
1778
1779/**
1780 * Shift rows below the cursor down by a given number of lines.
1781 *
1782 * This function respects the current scroll region.
1783 *
1784 * New rows are inserted at the top of the scroll region to fill the
1785 * vacated rows. The new rows not filled out with the current text attributes.
1786 *
1787 * This function does not affect the scrollback rows at all. Rows shifted
1788 * off the bottom are lost.
1789 *
1790 * @param {integer} count The number of rows to scroll.
1791 */
1792hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001793 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001794
rginda87b86462011-12-14 13:48:03 -08001795 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001796 this.insertLines(opt_count);
1797
rginda87b86462011-12-14 13:48:03 -08001798 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001799};
1800
rginda87b86462011-12-14 13:48:03 -08001801
rginda8ba33642011-12-14 12:31:31 -08001802/**
1803 * Set the cursor position.
1804 *
1805 * The cursor row is relative to the scroll region if the terminal has
1806 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1807 *
1808 * @param {integer} row The new zero-based cursor row.
1809 * @param {integer} row The new zero-based cursor column.
1810 */
1811hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1812 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001813 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001814 } else {
rginda87b86462011-12-14 13:48:03 -08001815 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001816 }
rginda87b86462011-12-14 13:48:03 -08001817};
rginda8ba33642011-12-14 12:31:31 -08001818
rginda87b86462011-12-14 13:48:03 -08001819hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1820 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07001821 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
1822 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001823 this.screen_.setCursorPosition(row, column);
1824};
1825
1826hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07001827 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
1828 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001829 this.screen_.setCursorPosition(row, column);
1830};
1831
1832/**
1833 * Set the cursor column.
1834 *
1835 * @param {integer} column The new zero-based cursor column.
1836 */
1837hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001838 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001839};
1840
1841/**
1842 * Return the cursor column.
1843 *
1844 * @return {integer} The zero-based cursor column.
1845 */
1846hterm.Terminal.prototype.getCursorColumn = function() {
1847 return this.screen_.cursorPosition.column;
1848};
1849
1850/**
1851 * Set the cursor row.
1852 *
1853 * The cursor row is relative to the scroll region if the terminal has
1854 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1855 *
1856 * @param {integer} row The new cursor row.
1857 */
rginda87b86462011-12-14 13:48:03 -08001858hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1859 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001860};
1861
1862/**
1863 * Return the cursor row.
1864 *
1865 * @return {integer} The zero-based cursor row.
1866 */
1867hterm.Terminal.prototype.getCursorRow = function(row) {
1868 return this.screen_.cursorPosition.row;
1869};
1870
1871/**
1872 * Request that the ScrollPort redraw itself soon.
1873 *
1874 * The redraw will happen asynchronously, soon after the call stack winds down.
1875 * Multiple calls will be coalesced into a single redraw.
1876 */
1877hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001878 if (this.timeouts_.redraw)
1879 return;
rginda8ba33642011-12-14 12:31:31 -08001880
1881 var self = this;
rginda87b86462011-12-14 13:48:03 -08001882 this.timeouts_.redraw = setTimeout(function() {
1883 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001884 self.scrollPort_.redraw_();
1885 }, 0);
1886};
1887
1888/**
1889 * Request that the ScrollPort be scrolled to the bottom.
1890 *
1891 * The scroll will happen asynchronously, soon after the call stack winds down.
1892 * Multiple calls will be coalesced into a single scroll.
1893 *
1894 * This affects the scrollbar position of the ScrollPort, and has nothing to
1895 * do with the VT scroll commands.
1896 */
1897hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1898 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001899 return;
rginda8ba33642011-12-14 12:31:31 -08001900
1901 var self = this;
1902 this.timeouts_.scrollDown = setTimeout(function() {
1903 delete self.timeouts_.scrollDown;
1904 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1905 }, 10);
1906};
1907
1908/**
1909 * Move the cursor up a specified number of rows.
1910 *
1911 * @param {integer} count The number of rows to move the cursor.
1912 */
1913hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001914 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001915};
1916
1917/**
1918 * Move the cursor down a specified number of rows.
1919 *
1920 * @param {integer} count The number of rows to move the cursor.
1921 */
1922hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001923 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001924 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1925 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1926 this.screenSize.height - 1);
1927
rgindacbbd7482012-06-13 15:06:16 -07001928 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08001929 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001930 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001931};
1932
1933/**
1934 * Move the cursor left a specified number of columns.
1935 *
1936 * @param {integer} count The number of columns to move the cursor.
1937 */
1938hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001939 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001940};
1941
1942/**
1943 * Move the cursor right a specified number of columns.
1944 *
1945 * @param {integer} count The number of columns to move the cursor.
1946 */
1947hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001948 count = count || 1;
rgindacbbd7482012-06-13 15:06:16 -07001949 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001950 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001951 this.setCursorColumn(column);
1952};
1953
1954/**
1955 * Reverse the foreground and background colors of the terminal.
1956 *
1957 * This only affects text that was drawn with no attributes.
1958 *
1959 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1960 * been drawn with attributes that happen to coincide with the default
1961 * 'no-attribute' colors. My guess is probably not.
1962 */
1963hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001964 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001965 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08001966 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
1967 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08001968 } else {
rginda9f5222b2012-03-05 11:53:28 -08001969 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
1970 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08001971 }
1972};
1973
1974/**
rginda87b86462011-12-14 13:48:03 -08001975 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07001976 *
1977 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08001978 */
1979hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08001980 this.cursorNode_.style.backgroundColor =
1981 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001982
1983 var self = this;
1984 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08001985 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08001986 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07001987
1988 if (this.bellAudio_.getAttribute('src')) {
Robert Gindaa6331372013-03-19 10:35:39 -07001989 if (this.bellSquelchTimeout_)
Robert Ginda92e18102013-03-14 13:56:37 -07001990 return;
1991
1992 this.bellAudio_.play();
1993
1994 this.bellSequelchTimeout_ = setTimeout(function() {
1995 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07001996 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07001997 } else {
1998 delete this.bellSquelchTimeout_;
1999 }
rginda87b86462011-12-14 13:48:03 -08002000};
2001
2002/**
rginda8ba33642011-12-14 12:31:31 -08002003 * Set the origin mode bit.
2004 *
2005 * If origin mode is on, certain VT cursor and scrolling commands measure their
2006 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2007 * to the top of the addressable screen.
2008 *
2009 * Defaults to off.
2010 *
2011 * @param {boolean} state True to set origin mode, false to unset.
2012 */
2013hterm.Terminal.prototype.setOriginMode = function(state) {
2014 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002015 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002016};
2017
2018/**
2019 * Set the insert mode bit.
2020 *
2021 * If insert mode is on, existing text beyond the cursor position will be
2022 * shifted right to make room for new text. Otherwise, new text overwrites
2023 * any existing text.
2024 *
2025 * Defaults to off.
2026 *
2027 * @param {boolean} state True to set insert mode, false to unset.
2028 */
2029hterm.Terminal.prototype.setInsertMode = function(state) {
2030 this.options_.insertMode = state;
2031};
2032
2033/**
rginda87b86462011-12-14 13:48:03 -08002034 * Set the auto carriage return bit.
2035 *
2036 * If auto carriage return is on then a formfeed character is interpreted
2037 * as a newline, otherwise it's the same as a linefeed. The difference boils
2038 * down to whether or not the cursor column is reset.
2039 */
2040hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2041 this.options_.autoCarriageReturn = state;
2042};
2043
2044/**
rginda8ba33642011-12-14 12:31:31 -08002045 * Set the wraparound mode bit.
2046 *
2047 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2048 * to the start of the following row. Otherwise, the cursor is clamped to the
2049 * end of the screen and attempts to write past it are ignored.
2050 *
2051 * Defaults to on.
2052 *
2053 * @param {boolean} state True to set wraparound mode, false to unset.
2054 */
2055hterm.Terminal.prototype.setWraparound = function(state) {
2056 this.options_.wraparound = state;
2057};
2058
2059/**
2060 * Set the reverse-wraparound mode bit.
2061 *
2062 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2063 * to the end of the previous row. Otherwise, the cursor is clamped to column
2064 * 0.
2065 *
2066 * Defaults to off.
2067 *
2068 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2069 */
2070hterm.Terminal.prototype.setReverseWraparound = function(state) {
2071 this.options_.reverseWraparound = state;
2072};
2073
2074/**
2075 * Selects between the primary and alternate screens.
2076 *
2077 * If alternate mode is on, the alternate screen is active. Otherwise the
2078 * primary screen is active.
2079 *
2080 * Swapping screens has no effect on the scrollback buffer.
2081 *
2082 * Each screen maintains its own cursor position.
2083 *
2084 * Defaults to off.
2085 *
2086 * @param {boolean} state True to set alternate mode, false to unset.
2087 */
2088hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002089 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002090 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2091
rginda35c456b2012-02-09 17:29:05 -08002092 if (this.screen_.rowsArray.length &&
2093 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2094 // If the screen changed sizes while we were away, our rowIndexes may
2095 // be incorrect.
2096 var offset = this.scrollbackRows_.length;
2097 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002098 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002099 ary[i].rowIndex = offset + i;
2100 }
2101 }
rginda8ba33642011-12-14 12:31:31 -08002102
rginda35c456b2012-02-09 17:29:05 -08002103 this.realizeWidth_(this.screenSize.width);
2104 this.realizeHeight_(this.screenSize.height);
2105 this.scrollPort_.syncScrollHeight();
2106 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002107
rginda6d397402012-01-17 10:58:29 -08002108 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002109 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002110};
2111
2112/**
2113 * Set the cursor-blink mode bit.
2114 *
2115 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2116 * a visible cursor does not blink.
2117 *
2118 * You should make sure to turn blinking off if you're going to dispose of a
2119 * terminal, otherwise you'll leak a timeout.
2120 *
2121 * Defaults to on.
2122 *
2123 * @param {boolean} state True to set cursor-blink mode, false to unset.
2124 */
2125hterm.Terminal.prototype.setCursorBlink = function(state) {
2126 this.options_.cursorBlink = state;
2127
2128 if (!state && this.timeouts_.cursorBlink) {
2129 clearTimeout(this.timeouts_.cursorBlink);
2130 delete this.timeouts_.cursorBlink;
2131 }
2132
2133 if (this.options_.cursorVisible)
2134 this.setCursorVisible(true);
2135};
2136
2137/**
2138 * Set the cursor-visible mode bit.
2139 *
2140 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2141 *
2142 * Defaults to on.
2143 *
2144 * @param {boolean} state True to set cursor-visible mode, false to unset.
2145 */
2146hterm.Terminal.prototype.setCursorVisible = function(state) {
2147 this.options_.cursorVisible = state;
2148
2149 if (!state) {
rginda87b86462011-12-14 13:48:03 -08002150 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002151 return;
2152 }
2153
rginda87b86462011-12-14 13:48:03 -08002154 this.syncCursorPosition_();
2155
2156 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002157
2158 if (this.options_.cursorBlink) {
2159 if (this.timeouts_.cursorBlink)
2160 return;
2161
2162 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
2163 500);
2164 } else {
2165 if (this.timeouts_.cursorBlink) {
2166 clearTimeout(this.timeouts_.cursorBlink);
2167 delete this.timeouts_.cursorBlink;
2168 }
2169 }
2170};
2171
2172/**
rginda87b86462011-12-14 13:48:03 -08002173 * Synchronizes the visible cursor and document selection with the current
2174 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002175 */
2176hterm.Terminal.prototype.syncCursorPosition_ = function() {
2177 var topRowIndex = this.scrollPort_.getTopRowIndex();
2178 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2179 var cursorRowIndex = this.scrollbackRows_.length +
2180 this.screen_.cursorPosition.row;
2181
2182 if (cursorRowIndex > bottomRowIndex) {
2183 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002184 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002185 return;
2186 }
2187
2188 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002189 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2190 'px';
2191 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2192 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002193
2194 this.cursorNode_.setAttribute('title',
2195 '(' + this.screen_.cursorPosition.row +
2196 ', ' + this.screen_.cursorPosition.column +
2197 ')');
2198
2199 // Update the caret for a11y purposes.
2200 var selection = this.document_.getSelection();
2201 if (selection && selection.isCollapsed)
2202 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002203};
2204
Robert Gindafb1be6a2013-12-11 11:56:22 -08002205/**
2206 * Adjusts the style of this.cursorNode_ according to the current cursor shape
2207 * and character cell dimensions.
2208 */
Robert Ginda830583c2013-08-07 13:20:46 -07002209hterm.Terminal.prototype.restyleCursor_ = function() {
2210 var shape = this.cursorShape_;
2211
2212 if (this.cursorNode_.getAttribute('focus') == 'false') {
2213 // Always show a block cursor when unfocused.
2214 shape = hterm.Terminal.cursorShape.BLOCK;
2215 }
2216
2217 var style = this.cursorNode_.style;
2218
Robert Gindafb1be6a2013-12-11 11:56:22 -08002219 style.width = this.scrollPort_.characterSize.width + 'px';
2220
Robert Ginda830583c2013-08-07 13:20:46 -07002221 switch (shape) {
2222 case hterm.Terminal.cursorShape.BEAM:
2223 style.height = this.scrollPort_.characterSize.height + 'px';
2224 style.backgroundColor = 'transparent';
2225 style.borderBottomStyle = null;
2226 style.borderLeftStyle = 'solid';
2227 break;
2228
2229 case hterm.Terminal.cursorShape.UNDERLINE:
2230 style.height = this.scrollPort_.characterSize.baseline + 'px';
2231 style.backgroundColor = 'transparent';
2232 style.borderBottomStyle = 'solid';
2233 // correct the size to put it exactly at the baseline
2234 style.borderLeftStyle = null;
2235 break;
2236
2237 default:
2238 style.height = this.scrollPort_.characterSize.height + 'px';
2239 style.backgroundColor = this.cursorColor_;
2240 style.borderBottomStyle = null;
2241 style.borderLeftStyle = null;
2242 break;
2243 }
2244};
2245
rginda8ba33642011-12-14 12:31:31 -08002246/**
2247 * Synchronizes the visible cursor with the current cursor coordinates.
2248 *
2249 * The sync will happen asynchronously, soon after the call stack winds down.
2250 * Multiple calls will be coalesced into a single sync.
2251 */
2252hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2253 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002254 return;
rginda8ba33642011-12-14 12:31:31 -08002255
2256 var self = this;
2257 this.timeouts_.syncCursor = setTimeout(function() {
2258 self.syncCursorPosition_();
2259 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002260 }, 0);
2261};
2262
rgindacc2996c2012-02-24 14:59:31 -08002263/**
rgindaf522ce02012-04-17 17:49:17 -07002264 * Show or hide the zoom warning.
2265 *
2266 * The zoom warning is a message warning the user that their browser zoom must
2267 * be set to 100% in order for hterm to function properly.
2268 *
2269 * @param {boolean} state True to show the message, false to hide it.
2270 */
2271hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2272 if (!this.zoomWarningNode_) {
2273 if (!state)
2274 return;
2275
2276 this.zoomWarningNode_ = this.document_.createElement('div');
2277 this.zoomWarningNode_.style.cssText = (
2278 'color: black;' +
2279 'background-color: #ff2222;' +
2280 'font-size: large;' +
2281 'border-radius: 8px;' +
2282 'opacity: 0.75;' +
2283 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2284 'top: 0.5em;' +
2285 'right: 1.2em;' +
2286 'position: absolute;' +
2287 '-webkit-text-size-adjust: none;' +
2288 '-webkit-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002289 }
2290
Robert Gindab4839c22013-02-28 16:52:10 -08002291 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2292 hterm.zoomWarningMessage,
2293 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2294
rgindaf522ce02012-04-17 17:49:17 -07002295 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2296
2297 if (state) {
2298 if (!this.zoomWarningNode_.parentNode)
2299 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2300 } else if (this.zoomWarningNode_.parentNode) {
2301 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2302 }
2303};
2304
2305/**
rgindacc2996c2012-02-24 14:59:31 -08002306 * Show the terminal overlay for a given amount of time.
2307 *
2308 * The terminal overlay appears in inverse video in a large font, centered
2309 * over the terminal. You should probably keep the overlay message brief,
2310 * since it's in a large font and you probably aren't going to check the size
2311 * of the terminal first.
2312 *
2313 * @param {string} msg The text (not HTML) message to display in the overlay.
2314 * @param {number} opt_timeout The amount of time to wait before fading out
2315 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2316 * stay up forever (or until the next overlay).
2317 */
2318hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002319 if (!this.overlayNode_) {
2320 if (!this.div_)
2321 return;
2322
2323 this.overlayNode_ = this.document_.createElement('div');
2324 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002325 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002326 'font-size: xx-large;' +
2327 'opacity: 0.75;' +
2328 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2329 'position: absolute;' +
2330 '-webkit-user-select: none;' +
2331 '-webkit-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002332
2333 this.overlayNode_.addEventListener('mousedown', function(e) {
2334 e.preventDefault();
2335 e.stopPropagation();
2336 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002337 }
2338
rginda9f5222b2012-03-05 11:53:28 -08002339 this.overlayNode_.style.color = this.prefs_.get('background-color');
2340 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2341 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2342
rgindaf0090c92012-02-10 14:58:52 -08002343 this.overlayNode_.textContent = msg;
2344 this.overlayNode_.style.opacity = '0.75';
2345
2346 if (!this.overlayNode_.parentNode)
2347 this.div_.appendChild(this.overlayNode_);
2348
Robert Ginda97769282013-02-01 15:30:30 -08002349 var divSize = hterm.getClientSize(this.div_);
2350 var overlaySize = hterm.getClientSize(this.overlayNode_);
2351
2352 this.overlayNode_.style.top = (divSize.height - overlaySize.height) / 2;
2353 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
2354 this.scrollPort_.currentScrollbarWidthPx) / 2;
rgindaf0090c92012-02-10 14:58:52 -08002355
2356 var self = this;
2357
2358 if (this.overlayTimeout_)
2359 clearTimeout(this.overlayTimeout_);
2360
rgindacc2996c2012-02-24 14:59:31 -08002361 if (opt_timeout === null)
2362 return;
2363
rgindaf0090c92012-02-10 14:58:52 -08002364 this.overlayTimeout_ = setTimeout(function() {
2365 self.overlayNode_.style.opacity = '0';
Robert Ginda70926e42013-11-25 14:56:36 -08002366 self.overlayTimeout_ = setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002367 if (self.overlayNode_.parentNode)
2368 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002369 self.overlayTimeout_ = null;
2370 self.overlayNode_.style.opacity = '0.75';
2371 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002372 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002373};
2374
rginda4bba5e12012-06-20 16:15:30 -07002375/**
2376 * Paste from the system clipboard to the terminal.
2377 */
2378hterm.Terminal.prototype.paste = function() {
2379 hterm.pasteFromClipboard(this.document_);
2380};
2381
2382/**
2383 * Copy a string to the system clipboard.
2384 *
2385 * Note: If there is a selected range in the terminal, it'll be cleared.
2386 */
2387hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda9fb38222012-09-11 14:19:12 -07002388 if (this.prefs_.get('enable-clipboard-notice'))
Robert Gindab4839c22013-02-28 16:52:10 -08002389 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
rgindaa09e7332012-08-17 12:49:51 -07002390
2391 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002392 copySource.textContent = str;
2393 copySource.style.cssText = (
2394 '-webkit-user-select: text;' +
2395 'position: absolute;' +
2396 'top: -99px');
2397
2398 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002399
rginda4bba5e12012-06-20 16:15:30 -07002400 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002401 var anchorNode = selection.anchorNode;
2402 var anchorOffset = selection.anchorOffset;
2403 var focusNode = selection.focusNode;
2404 var focusOffset = selection.focusOffset;
2405
rginda4bba5e12012-06-20 16:15:30 -07002406 selection.selectAllChildren(copySource);
2407
rgindaa09e7332012-08-17 12:49:51 -07002408 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002409
rgindafaa74742012-08-21 13:34:03 -07002410 selection.collapse(anchorNode, anchorOffset);
2411 selection.extend(focusNode, focusOffset);
2412
rginda4bba5e12012-06-20 16:15:30 -07002413 copySource.parentNode.removeChild(copySource);
2414};
2415
rgindaa09e7332012-08-17 12:49:51 -07002416hterm.Terminal.prototype.getSelectionText = function() {
2417 var selection = this.scrollPort_.selection;
2418 selection.sync();
2419
2420 if (selection.isCollapsed)
2421 return null;
2422
2423
2424 // Start offset measures from the beginning of the line.
2425 var startOffset = selection.startOffset;
2426 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002427
Robert Gindafdbb3f22012-09-06 20:23:06 -07002428 if (node.nodeName != 'X-ROW') {
2429 // If the selection doesn't start on an x-row node, then it must be
2430 // somewhere inside the x-row. Add any characters from previous siblings
2431 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002432
2433 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2434 // If node is the text node in a styled span, move up to the span node.
2435 node = node.parentNode;
2436 }
2437
Robert Gindafdbb3f22012-09-06 20:23:06 -07002438 while (node.previousSibling) {
2439 node = node.previousSibling;
2440 startOffset += node.textContent.length;
2441 }
rgindaa09e7332012-08-17 12:49:51 -07002442 }
2443
2444 // End offset measures from the end of the line.
2445 var endOffset = selection.endNode.textContent.length - selection.endOffset;
2446 var node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002447
Robert Gindafdbb3f22012-09-06 20:23:06 -07002448 if (node.nodeName != 'X-ROW') {
2449 // If the selection doesn't end on an x-row node, then it must be
2450 // somewhere inside the x-row. Add any characters from following siblings
2451 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002452
2453 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2454 // If node is the text node in a styled span, move up to the span node.
2455 node = node.parentNode;
2456 }
2457
Robert Gindafdbb3f22012-09-06 20:23:06 -07002458 while (node.nextSibling) {
2459 node = node.nextSibling;
2460 endOffset += node.textContent.length;
2461 }
rgindaa09e7332012-08-17 12:49:51 -07002462 }
2463
2464 var rv = this.getRowsText(selection.startRow.rowIndex,
2465 selection.endRow.rowIndex + 1);
2466 return rv.substring(startOffset, rv.length - endOffset);
2467};
2468
rginda4bba5e12012-06-20 16:15:30 -07002469/**
2470 * Copy the current selection to the system clipboard, then clear it after a
2471 * short delay.
2472 */
2473hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002474 var text = this.getSelectionText();
2475 if (text != null)
2476 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002477};
2478
rgindaf0090c92012-02-10 14:58:52 -08002479hterm.Terminal.prototype.overlaySize = function() {
2480 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2481};
2482
rginda87b86462011-12-14 13:48:03 -08002483/**
2484 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2485 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07002486 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08002487 */
2488hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002489 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002490 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2491
Robert Ginda8cb7d902013-06-20 14:37:18 -07002492 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08002493};
2494
2495/**
rgindad5613292012-06-19 15:40:37 -07002496 * Add the terminalRow and terminalColumn properties to mouse events and
2497 * then forward on to onMouse().
2498 *
2499 * The terminalRow and terminalColumn properties contain the (row, column)
2500 * coordinates for the mouse event.
2501 */
2502hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002503 if (e.processedByTerminalHandler_) {
2504 // We register our event handlers on the document, as well as the cursor
2505 // and the scroll blocker. Mouse events that occur on the cursor or
2506 // scroll blocker will also appear on the document, but we don't want to
2507 // process them twice.
2508 //
2509 // We can't just prevent bubbling because that has other side effects, so
2510 // we decorate the event object with this property instead.
2511 return;
2512 }
2513
2514 e.processedByTerminalHandler_ = true;
2515
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002516 if (e.type == 'mousedown' && e.terminalColumn > this.screenSize.width) {
2517 // Mousedown in the scrollbar area.
rginda4bba5e12012-06-20 16:15:30 -07002518 return;
2519 }
2520
rgindad5613292012-06-19 15:40:37 -07002521 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2522 this.scrollPort_.characterSize.height) + 1;
2523 e.terminalColumn = parseInt(e.clientX /
2524 this.scrollPort_.characterSize.width) + 1;
2525
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002526 if (this.enableMouseDragScroll) {
2527 if (e.type == 'dblclick') {
2528 this.screen_.expandSelection(this.document_.getSelection());
2529 hterm.copySelectionToClipboard(this.document_);
rgindad5613292012-06-19 15:40:37 -07002530 return;
2531 }
2532
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002533 if (e.type == 'mousedown' && e.which == this.mousePasteButton) {
2534 this.paste();
2535 return;
rgindad5613292012-06-19 15:40:37 -07002536 }
Robert Ginda4e24f8f2014-01-08 14:45:06 -08002537
2538 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2539 !this.document_.getSelection().isCollapsed) {
2540 hterm.copySelectionToClipboard(this.document_);
2541 return;
2542 }
2543
2544 if ((e.type == 'mousemove' || e.type == 'mouseup') &&
2545 this.scrollBlockerNode_.engaged) {
2546 // Disengage the scroll-blocker after one of these events.
2547 this.scrollBlockerNode_.engaged = false;
2548 this.scrollBlockerNode_.style.top = '-99px';
2549 }
2550
2551 } else /* if (!this.enableMouseDragScroll) */ {
2552 if (!this.scrollBlockerNode_.engaged) {
2553 if (e.type == 'mousedown') {
2554 // Move the scroll-blocker into place if we want to keep the scrollport
2555 // from scrolling.
2556 this.scrollBlockerNode_.engaged = true;
2557 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2558 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2559 } else if (e.type == 'mousemove') {
2560 // Oh. This means that drag-scroll was disabled AFTER the mouse down,
2561 // in which case it's too late to engage the scroll-blocker.
2562 this.document_.getSelection().collapse();
2563 e.preventDefault();
2564 }
2565 }
rgindad5613292012-06-19 15:40:37 -07002566 }
2567
rgindafaa74742012-08-21 13:34:03 -07002568 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07002569};
2570
2571/**
2572 * Clients should override this if they care to know about mouse events.
2573 *
2574 * The event parameter will be a normal DOM mouse click event with additional
2575 * 'terminalRow' and 'terminalColumn' properties.
2576 */
2577hterm.Terminal.prototype.onMouse = function(e) { };
2578
2579/**
rginda8e92a692012-05-20 19:37:20 -07002580 * React when focus changes.
2581 */
2582hterm.Terminal.prototype.onFocusChange_ = function(state) {
2583 this.cursorNode_.setAttribute('focus', state ? 'true' : 'false');
Robert Ginda830583c2013-08-07 13:20:46 -07002584 this.restyleCursor_();
rginda8e92a692012-05-20 19:37:20 -07002585};
2586
2587/**
rginda8ba33642011-12-14 12:31:31 -08002588 * React when the ScrollPort is scrolled.
2589 */
2590hterm.Terminal.prototype.onScroll_ = function() {
2591 this.scheduleSyncCursorPosition_();
2592};
2593
2594/**
rginda9846e2f2012-01-27 13:53:33 -08002595 * React when text is pasted into the scrollPort.
2596 */
2597hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Ginda8cb7d902013-06-20 14:37:18 -07002598 this.onVTKeystroke(e.text.replace(/\n/mg, '\r'));
rginda9846e2f2012-01-27 13:53:33 -08002599};
2600
2601/**
rgindaa09e7332012-08-17 12:49:51 -07002602 * React when the user tries to copy from the scrollPort.
2603 */
2604hterm.Terminal.prototype.onCopy_ = function(e) {
2605 e.preventDefault();
rgindafaa74742012-08-21 13:34:03 -07002606 this.copySelectionToClipboard();
rgindaa09e7332012-08-17 12:49:51 -07002607};
2608
2609/**
rginda8ba33642011-12-14 12:31:31 -08002610 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002611 *
2612 * Note: This function should not directly contain code that alters the internal
2613 * state of the terminal. That kind of code belongs in realizeWidth or
2614 * realizeHeight, so that it can be executed synchronously in the case of a
2615 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002616 */
2617hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08002618 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002619 this.scrollPort_.characterSize.width);
2620 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
2621 this.scrollPort_.characterSize.height);
2622
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002623 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08002624 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002625 // gets removed from the document or during the initial load, and we can't
2626 // deal with that.
rginda35c456b2012-02-09 17:29:05 -08002627 return;
2628 }
2629
rgindaa8ba17d2012-08-15 14:41:10 -07002630 var isNewSize = (columnCount != this.screenSize.width ||
2631 rowCount != this.screenSize.height);
2632
2633 // We do this even if the size didn't change, just to be sure everything is
2634 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002635 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07002636 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07002637
2638 if (isNewSize)
2639 this.overlaySize();
2640
Robert Gindafb1be6a2013-12-11 11:56:22 -08002641 this.restyleCursor_();
rgindaa8ba17d2012-08-15 14:41:10 -07002642 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08002643};
2644
2645/**
2646 * Service the cursor blink timeout.
2647 */
2648hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Ginda830583c2013-08-07 13:20:46 -07002649 if (this.cursorNode_.getAttribute('focus') == 'false' ||
2650 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08002651 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002652 } else {
rginda87b86462011-12-14 13:48:03 -08002653 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002654 }
2655};
David Reveman8f552492012-03-28 12:18:41 -04002656
2657/**
2658 * Set the scrollbar-visible mode bit.
2659 *
2660 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2661 * Otherwise it will not.
2662 *
2663 * Defaults to on.
2664 *
2665 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2666 */
2667hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2668 this.scrollPort_.setScrollbarVisible(state);
2669};