blob: 5cda73d95b486ef1f2d65592ee63471bffb1aa8c [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
Robert Ginda57f03b42012-09-13 11:02:48 -0700259 'enable-8-bit-control': function(v) {
260 terminal.vt.enable8BitControl = !!v;
261 },
rginda30f20f62012-04-05 16:36:19 -0700262
Robert Ginda57f03b42012-09-13 11:02:48 -0700263 'enable-bold': function(v) {
264 terminal.syncBoldSafeState();
265 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400266
Robert Ginda57f03b42012-09-13 11:02:48 -0700267 'enable-clipboard-write': function(v) {
268 terminal.vt.enableClipboardWrite = !!v;
269 },
Philip Douglass959b49d2012-05-30 13:29:29 -0400270
Robert Ginda3755e752013-05-31 13:34:09 -0700271 'enable-dec12': function(v) {
272 terminal.vt.enableDec12 = !!v;
273 },
274
Robert Ginda57f03b42012-09-13 11:02:48 -0700275 'font-family': function(v) {
276 terminal.syncFontFamily();
277 },
rginda30f20f62012-04-05 16:36:19 -0700278
Robert Ginda57f03b42012-09-13 11:02:48 -0700279 'font-size': function(v) {
280 terminal.setFontSize(v);
281 },
rginda9875d902012-08-20 16:21:57 -0700282
Robert Ginda57f03b42012-09-13 11:02:48 -0700283 'font-smoothing': function(v) {
284 terminal.syncFontFamily();
285 },
rgindade84e382012-04-20 15:39:31 -0700286
Robert Ginda57f03b42012-09-13 11:02:48 -0700287 'foreground-color': function(v) {
288 terminal.setForegroundColor(v);
289 },
rginda30f20f62012-04-05 16:36:19 -0700290
Robert Ginda57f03b42012-09-13 11:02:48 -0700291 'home-keys-scroll': function(v) {
292 terminal.keyboard.homeKeysScroll = v;
293 },
rginda4bba5e12012-06-20 16:15:30 -0700294
Robert Ginda57f03b42012-09-13 11:02:48 -0700295 'max-string-sequence': function(v) {
296 terminal.vt.maxStringSequence = v;
297 },
rginda11057d52012-04-25 12:29:56 -0700298
Andrew de los Reyes6af23ae2013-04-04 14:17:50 -0700299 'media-keys-are-fkeys': function(v) {
300 terminal.keyboard.mediaKeysAreFKeys = v;
301 },
302
Robert Ginda57f03b42012-09-13 11:02:48 -0700303 'meta-sends-escape': function(v) {
304 terminal.keyboard.metaSendsEscape = v;
305 },
rginda30f20f62012-04-05 16:36:19 -0700306
Robert Ginda57f03b42012-09-13 11:02:48 -0700307 'mouse-cell-motion-trick': function(v) {
308 terminal.vt.setMouseCellMotionTrick(v);
309 },
Robert Ginda9fb38222012-09-11 14:19:12 -0700310
Robert Ginda57f03b42012-09-13 11:02:48 -0700311 'mouse-paste-button': function(v) {
312 terminal.syncMousePasteButton();
313 },
rgindaa8ba17d2012-08-15 14:41:10 -0700314
Robert Ginda40932892012-12-10 17:26:40 -0800315 'pass-alt-number': function(v) {
316 if (v == null) {
317 var osx = window.navigator.userAgent.match(/Mac OS X/);
318
319 // Let Alt-1..9 pass to the browser (to control tab switching) on
320 // non-OS X systems, or if hterm is not opened in an app window.
321 v = (!osx && hterm.windowType != 'popup');
322 }
323
324 terminal.passAltNumber = v;
325 },
326
327 'pass-ctrl-number': function(v) {
328 if (v == null) {
329 var osx = window.navigator.userAgent.match(/Mac OS X/);
330
331 // Let Ctrl-1..9 pass to the browser (to control tab switching) on
332 // non-OS X systems, or if hterm is not opened in an app window.
333 v = (!osx && hterm.windowType != 'popup');
334 }
335
336 terminal.passCtrlNumber = v;
337 },
338
339 'pass-meta-number': function(v) {
340 if (v == null) {
341 var osx = window.navigator.userAgent.match(/Mac OS X/);
342
343 // Let Meta-1..9 pass to the browser (to control tab switching) on
344 // OS X systems, or if hterm is not opened in an app window.
345 v = (osx && hterm.windowType != 'popup');
346 }
347
348 terminal.passMetaNumber = v;
349 },
350
Robert Ginda8cb7d902013-06-20 14:37:18 -0700351 'receive-encoding': function(v) {
352 if (!(/^(utf-8|raw)$/).test(v)) {
353 console.warn('Invalid value for "receive-encoding": ' + v);
354 v = 'utf-8';
355 }
356
357 terminal.vt.characterEncoding = v;
358 },
359
Robert Ginda57f03b42012-09-13 11:02:48 -0700360 'scroll-on-keystroke': function(v) {
361 terminal.scrollOnKeystroke_ = v;
362 },
rginda9f5222b2012-03-05 11:53:28 -0800363
Robert Ginda57f03b42012-09-13 11:02:48 -0700364 'scroll-on-output': function(v) {
365 terminal.scrollOnOutput_ = v;
366 },
rginda30f20f62012-04-05 16:36:19 -0700367
Robert Ginda57f03b42012-09-13 11:02:48 -0700368 'scrollbar-visible': function(v) {
369 terminal.setScrollbarVisible(v);
370 },
rginda9f5222b2012-03-05 11:53:28 -0800371
Robert Ginda8cb7d902013-06-20 14:37:18 -0700372 'send-encoding': function(v) {
373 if (!(/^(utf-8|raw)$/).test(v)) {
374 console.warn('Invalid value for "send-encoding": ' + v);
375 v = 'utf-8';
376 }
377
378 terminal.keyboard.characterEncoding = v;
379 },
380
Robert Ginda57f03b42012-09-13 11:02:48 -0700381 'shift-insert-paste': function(v) {
382 terminal.keyboard.shiftInsertPaste = v;
383 },
rginda9f5222b2012-03-05 11:53:28 -0800384
Robert Ginda57f03b42012-09-13 11:02:48 -0700385 'page-keys-scroll': function(v) {
386 terminal.keyboard.pageKeysScroll = v;
387 }
388 });
rginda30f20f62012-04-05 16:36:19 -0700389
Robert Ginda57f03b42012-09-13 11:02:48 -0700390 this.prefs_.readStorage(function() {
rginda9f5222b2012-03-05 11:53:28 -0800391 this.prefs_.notifyAll();
Robert Ginda57f03b42012-09-13 11:02:48 -0700392
393 if (opt_callback)
394 opt_callback();
395 }.bind(this));
rginda9f5222b2012-03-05 11:53:28 -0800396};
397
rginda8e92a692012-05-20 19:37:20 -0700398/**
399 * Set the color for the cursor.
400 *
401 * If you want this setting to persist, set it through prefs_, rather than
402 * with this method.
403 */
404hterm.Terminal.prototype.setCursorColor = function(color) {
Robert Ginda830583c2013-08-07 13:20:46 -0700405 this.cursorColor_ = color;
rginda8e92a692012-05-20 19:37:20 -0700406 this.cursorNode_.style.backgroundColor = color;
407 this.cursorNode_.style.borderColor = color;
408};
409
410/**
411 * Return the current cursor color as a string.
412 */
413hterm.Terminal.prototype.getCursorColor = function() {
Robert Ginda830583c2013-08-07 13:20:46 -0700414 return this.cursorColor_;
rginda8e92a692012-05-20 19:37:20 -0700415};
416
417/**
rgindad5613292012-06-19 15:40:37 -0700418 * Enable or disable mouse based text selection in the terminal.
419 */
420hterm.Terminal.prototype.setSelectionEnabled = function(state) {
421 this.enableMouseDragScroll = state;
422 this.scrollPort_.setSelectionEnabled(state);
423};
424
425/**
rginda8e92a692012-05-20 19:37:20 -0700426 * Set the background color.
427 *
428 * If you want this setting to persist, set it through prefs_, rather than
429 * with this method.
430 */
431hterm.Terminal.prototype.setBackgroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700432 this.backgroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700433 this.primaryScreen_.textAttributes.setDefaults(
434 this.foregroundColor_, this.backgroundColor_);
435 this.alternateScreen_.textAttributes.setDefaults(
436 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700437 this.scrollPort_.setBackgroundColor(color);
438};
439
rginda9f5222b2012-03-05 11:53:28 -0800440/**
441 * Return the current terminal background color.
442 *
443 * Intended for use by other classes, so we don't have to expose the entire
444 * prefs_ object.
445 */
446hterm.Terminal.prototype.getBackgroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700447 return this.backgroundColor_;
448};
449
450/**
451 * Set the foreground color.
452 *
453 * If you want this setting to persist, set it through prefs_, rather than
454 * with this method.
455 */
456hterm.Terminal.prototype.setForegroundColor = function(color) {
rgindacbbd7482012-06-13 15:06:16 -0700457 this.foregroundColor_ = lib.colors.normalizeCSS(color);
Robert Ginda57f03b42012-09-13 11:02:48 -0700458 this.primaryScreen_.textAttributes.setDefaults(
459 this.foregroundColor_, this.backgroundColor_);
460 this.alternateScreen_.textAttributes.setDefaults(
461 this.foregroundColor_, this.backgroundColor_);
rginda8e92a692012-05-20 19:37:20 -0700462 this.scrollPort_.setForegroundColor(color);
rginda9f5222b2012-03-05 11:53:28 -0800463};
464
465/**
466 * Return the current terminal foreground color.
467 *
468 * Intended for use by other classes, so we don't have to expose the entire
469 * prefs_ object.
470 */
471hterm.Terminal.prototype.getForegroundColor = function() {
rginda8e92a692012-05-20 19:37:20 -0700472 return this.foregroundColor_;
rginda9f5222b2012-03-05 11:53:28 -0800473};
474
475/**
rginda87b86462011-12-14 13:48:03 -0800476 * Create a new instance of a terminal command and run it with a given
477 * argument string.
478 *
479 * @param {function} commandClass The constructor for a terminal command.
480 * @param {string} argString The argument string to pass to the command.
481 */
482hterm.Terminal.prototype.runCommandClass = function(commandClass, argString) {
rgindaf522ce02012-04-17 17:49:17 -0700483 var environment = this.prefs_.get('environment');
484 if (typeof environment != 'object' || environment == null)
485 environment = {};
486
rginda87b86462011-12-14 13:48:03 -0800487 var self = this;
488 this.command = new commandClass(
489 { argString: argString || '',
490 io: this.io.push(),
rgindaf522ce02012-04-17 17:49:17 -0700491 environment: environment,
rginda87b86462011-12-14 13:48:03 -0800492 onExit: function(code) {
493 self.io.pop();
rgindafeaf3142012-01-31 15:14:20 -0800494 self.uninstallKeyboard();
rginda9875d902012-08-20 16:21:57 -0700495 if (self.prefs_.get('close-on-exit'))
496 window.close();
rginda87b86462011-12-14 13:48:03 -0800497 }
498 });
499
rgindafeaf3142012-01-31 15:14:20 -0800500 this.installKeyboard();
rginda87b86462011-12-14 13:48:03 -0800501 this.command.run();
502};
503
504/**
rgindafeaf3142012-01-31 15:14:20 -0800505 * Returns true if the current screen is the primary screen, false otherwise.
506 */
507hterm.Terminal.prototype.isPrimaryScreen = function() {
rgindaf522ce02012-04-17 17:49:17 -0700508 return this.screen_ == this.primaryScreen_;
rgindafeaf3142012-01-31 15:14:20 -0800509};
510
511/**
512 * Install the keyboard handler for this terminal.
513 *
514 * This will prevent the browser from seeing any keystrokes sent to the
515 * terminal.
516 */
517hterm.Terminal.prototype.installKeyboard = function() {
Toni Barzic0bfa8922013-11-22 11:18:35 -0800518 this.keyboard.installKeyboard(this.scrollPort_.getScreenNode());
rgindafeaf3142012-01-31 15:14:20 -0800519}
520
521/**
522 * Uninstall the keyboard handler for this terminal.
523 */
524hterm.Terminal.prototype.uninstallKeyboard = function() {
525 this.keyboard.installKeyboard(null);
526}
527
528/**
rginda35c456b2012-02-09 17:29:05 -0800529 * Set the font size for this terminal.
rginda9f5222b2012-03-05 11:53:28 -0800530 *
531 * Call setFontSize(0) to reset to the default font size.
532 *
533 * This function does not modify the font-size preference.
534 *
535 * @param {number} px The desired font size, in pixels.
rginda35c456b2012-02-09 17:29:05 -0800536 */
537hterm.Terminal.prototype.setFontSize = function(px) {
rginda9f5222b2012-03-05 11:53:28 -0800538 if (px === 0)
539 px = this.prefs_.get('font-size');
540
rginda35c456b2012-02-09 17:29:05 -0800541 this.scrollPort_.setFontSize(px);
542};
543
544/**
545 * Get the current font size.
546 */
547hterm.Terminal.prototype.getFontSize = function() {
548 return this.scrollPort_.getFontSize();
549};
550
551/**
rginda8e92a692012-05-20 19:37:20 -0700552 * Get the current font family.
553 */
554hterm.Terminal.prototype.getFontFamily = function() {
555 return this.scrollPort_.getFontFamily();
556};
557
558/**
rginda35c456b2012-02-09 17:29:05 -0800559 * Set the CSS "font-family" for this terminal.
560 */
rginda9f5222b2012-03-05 11:53:28 -0800561hterm.Terminal.prototype.syncFontFamily = function() {
562 this.scrollPort_.setFontFamily(this.prefs_.get('font-family'),
563 this.prefs_.get('font-smoothing'));
564 this.syncBoldSafeState();
565};
566
rginda4bba5e12012-06-20 16:15:30 -0700567/**
568 * Set this.mousePasteButton based on the mouse-paste-button pref,
569 * autodetecting if necessary.
570 */
571hterm.Terminal.prototype.syncMousePasteButton = function() {
572 var button = this.prefs_.get('mouse-paste-button');
573 if (typeof button == 'number') {
574 this.mousePasteButton = button;
575 return;
576 }
577
578 var ary = navigator.userAgent.match(/\(X11;\s+(\S+)/);
579 if (!ary || ary[2] == 'CrOS') {
580 this.mousePasteButton = 2;
581 } else {
582 this.mousePasteButton = 3;
583 }
584};
585
586/**
587 * Enable or disable bold based on the enable-bold pref, autodetecting if
588 * necessary.
589 */
rginda9f5222b2012-03-05 11:53:28 -0800590hterm.Terminal.prototype.syncBoldSafeState = function() {
591 var enableBold = this.prefs_.get('enable-bold');
592 if (enableBold !== null) {
Robert Gindaed016262012-10-26 16:27:09 -0700593 this.primaryScreen_.textAttributes.enableBold = enableBold;
594 this.alternateScreen_.textAttributes.enableBold = enableBold;
rginda9f5222b2012-03-05 11:53:28 -0800595 return;
596 }
597
rgindaf7521392012-02-28 17:20:34 -0800598 var normalSize = this.scrollPort_.measureCharacterSize();
599 var boldSize = this.scrollPort_.measureCharacterSize('bold');
600
601 var isBoldSafe = normalSize.equals(boldSize);
rgindaf7521392012-02-28 17:20:34 -0800602 if (!isBoldSafe) {
603 console.warn('Bold characters disabled: Size of bold weight differs ' +
rgindac9759de2012-03-19 13:21:41 -0700604 'from normal. Font family is: ' +
605 this.scrollPort_.getFontFamily());
rgindaf7521392012-02-28 17:20:34 -0800606 }
rginda9f5222b2012-03-05 11:53:28 -0800607
Robert Gindaed016262012-10-26 16:27:09 -0700608 this.primaryScreen_.textAttributes.enableBold = isBoldSafe;
609 this.alternateScreen_.textAttributes.enableBold = isBoldSafe;
rginda35c456b2012-02-09 17:29:05 -0800610};
611
612/**
rginda87b86462011-12-14 13:48:03 -0800613 * Return a copy of the current cursor position.
614 *
615 * @return {hterm.RowCol} The RowCol object representing the current position.
616 */
617hterm.Terminal.prototype.saveCursor = function() {
618 return this.screen_.cursorPosition.clone();
619};
620
rgindaa19afe22012-01-25 15:40:22 -0800621hterm.Terminal.prototype.getTextAttributes = function() {
622 return this.screen_.textAttributes;
623};
624
rginda1a09aa02012-06-18 21:11:25 -0700625hterm.Terminal.prototype.setTextAttributes = function(textAttributes) {
626 this.screen_.textAttributes = textAttributes;
627};
628
rginda87b86462011-12-14 13:48:03 -0800629/**
rgindaf522ce02012-04-17 17:49:17 -0700630 * Return the current browser zoom factor applied to the terminal.
631 *
632 * @return {number} The current browser zoom factor.
633 */
634hterm.Terminal.prototype.getZoomFactor = function() {
635 return this.scrollPort_.characterSize.zoomFactor;
636};
637
638/**
rginda9846e2f2012-01-27 13:53:33 -0800639 * Change the title of this terminal's window.
640 */
641hterm.Terminal.prototype.setWindowTitle = function(title) {
rgindafeaf3142012-01-31 15:14:20 -0800642 window.document.title = title;
rginda9846e2f2012-01-27 13:53:33 -0800643};
644
645/**
rginda87b86462011-12-14 13:48:03 -0800646 * Restore a previously saved cursor position.
647 *
648 * @param {hterm.RowCol} cursor The position to restore.
649 */
650hterm.Terminal.prototype.restoreCursor = function(cursor) {
rgindacbbd7482012-06-13 15:06:16 -0700651 var row = lib.f.clamp(cursor.row, 0, this.screenSize.height - 1);
652 var column = lib.f.clamp(cursor.column, 0, this.screenSize.width - 1);
rginda35c456b2012-02-09 17:29:05 -0800653 this.screen_.setCursorPosition(row, column);
654 if (cursor.column > column ||
655 cursor.column == column && cursor.overflow) {
656 this.screen_.cursorPosition.overflow = true;
657 }
rginda87b86462011-12-14 13:48:03 -0800658};
659
660/**
David Benjamin54e8bf62012-06-01 22:31:40 -0400661 * Clear the cursor's overflow flag.
662 */
663hterm.Terminal.prototype.clearCursorOverflow = function() {
664 this.screen_.cursorPosition.overflow = false;
665};
666
667/**
Robert Ginda830583c2013-08-07 13:20:46 -0700668 * Sets the cursor shape
669 */
670hterm.Terminal.prototype.setCursorShape = function(shape) {
671 this.cursorShape_ = shape;
672 this.restyleCursor_(shape);
673}
674
675/**
676 * Get the cursor shape
677 */
678hterm.Terminal.prototype.getCursorShape = function() {
679 return this.cursorShape_;
680}
681
682/**
rginda87b86462011-12-14 13:48:03 -0800683 * Set the width of the terminal, resizing the UI to match.
684 */
685hterm.Terminal.prototype.setWidth = function(columnCount) {
rgindaf0090c92012-02-10 14:58:52 -0800686 if (columnCount == null) {
687 this.div_.style.width = '100%';
688 return;
689 }
690
rginda35c456b2012-02-09 17:29:05 -0800691 this.div_.style.width = this.scrollPort_.characterSize.width *
Robert Ginda97769282013-02-01 15:30:30 -0800692 columnCount + this.scrollPort_.currentScrollbarWidthPx + 'px';
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400693 this.realizeSize_(columnCount, this.screenSize.height);
rgindac9bc5502012-01-18 11:48:44 -0800694 this.scheduleSyncCursorPosition_();
695};
rginda87b86462011-12-14 13:48:03 -0800696
rgindac9bc5502012-01-18 11:48:44 -0800697/**
rginda35c456b2012-02-09 17:29:05 -0800698 * Set the height of the terminal, resizing the UI to match.
699 */
700hterm.Terminal.prototype.setHeight = function(rowCount) {
rgindaf0090c92012-02-10 14:58:52 -0800701 if (rowCount == null) {
702 this.div_.style.height = '100%';
703 return;
704 }
705
rginda35c456b2012-02-09 17:29:05 -0800706 this.div_.style.height =
rginda30f20f62012-04-05 16:36:19 -0700707 this.scrollPort_.characterSize.height * rowCount + 'px';
rginda35c456b2012-02-09 17:29:05 -0800708 this.realizeSize_(this.screenSize.width, rowCount);
709 this.scheduleSyncCursorPosition_();
710};
711
712/**
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400713 * Deal with terminal size changes.
714 *
715 */
716hterm.Terminal.prototype.realizeSize_ = function(columnCount, rowCount) {
717 if (columnCount != this.screenSize.width)
718 this.realizeWidth_(columnCount);
719
720 if (rowCount != this.screenSize.height)
721 this.realizeHeight_(rowCount);
722
723 // Send new terminal size to plugin.
Robert Gindae81427f2013-05-24 10:34:46 -0700724 this.io.onTerminalResize_(columnCount, rowCount);
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +0400725};
726
727/**
rgindac9bc5502012-01-18 11:48:44 -0800728 * Deal with terminal width changes.
729 *
730 * This function does what needs to be done when the terminal width changes
731 * out from under us. It happens here rather than in onResize_() because this
732 * code may need to run synchronously to handle programmatic changes of
733 * terminal width.
734 *
735 * Relying on the browser to send us an async resize event means we may not be
736 * in the correct state yet when the next escape sequence hits.
737 */
738hterm.Terminal.prototype.realizeWidth_ = function(columnCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700739 if (columnCount <= 0)
740 throw new Error('Attempt to realize bad width: ' + columnCount);
741
rgindac9bc5502012-01-18 11:48:44 -0800742 var deltaColumns = columnCount - this.screen_.getWidth();
743
rginda87b86462011-12-14 13:48:03 -0800744 this.screenSize.width = columnCount;
745 this.screen_.setColumnCount(columnCount);
rgindac9bc5502012-01-18 11:48:44 -0800746
747 if (deltaColumns > 0) {
David Benjamin66e954d2012-05-05 21:08:12 -0400748 if (this.defaultTabStops)
749 this.setDefaultTabStops(this.screenSize.width - deltaColumns);
rgindac9bc5502012-01-18 11:48:44 -0800750 } else {
751 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
David Benjamin66e954d2012-05-05 21:08:12 -0400752 if (this.tabStops_[i] < columnCount)
rgindac9bc5502012-01-18 11:48:44 -0800753 break;
754
755 this.tabStops_.pop();
756 }
757 }
758
759 this.screen_.setColumnCount(this.screenSize.width);
760};
761
762/**
763 * Deal with terminal height changes.
764 *
765 * This function does what needs to be done when the terminal height changes
766 * out from under us. It happens here rather than in onResize_() because this
767 * code may need to run synchronously to handle programmatic changes of
768 * terminal height.
769 *
770 * Relying on the browser to send us an async resize event means we may not be
771 * in the correct state yet when the next escape sequence hits.
772 */
773hterm.Terminal.prototype.realizeHeight_ = function(rowCount) {
Robert Ginda4e83f3a2012-09-04 15:25:25 -0700774 if (rowCount <= 0)
775 throw new Error('Attempt to realize bad height: ' + rowCount);
776
rgindac9bc5502012-01-18 11:48:44 -0800777 var deltaRows = rowCount - this.screen_.getHeight();
778
779 this.screenSize.height = rowCount;
780
781 var cursor = this.saveCursor();
782
783 if (deltaRows < 0) {
784 // Screen got smaller.
785 deltaRows *= -1;
786 while (deltaRows) {
787 var lastRow = this.getRowCount() - 1;
788 if (lastRow - this.scrollbackRows_.length == cursor.row)
789 break;
790
791 if (this.getRowText(lastRow))
792 break;
793
794 this.screen_.popRow();
795 deltaRows--;
796 }
797
798 var ary = this.screen_.shiftRows(deltaRows);
799 this.scrollbackRows_.push.apply(this.scrollbackRows_, ary);
800
801 // We just removed rows from the top of the screen, we need to update
802 // the cursor to match.
rginda35c456b2012-02-09 17:29:05 -0800803 cursor.row = Math.max(cursor.row - deltaRows, 0);
rgindac9bc5502012-01-18 11:48:44 -0800804 } else if (deltaRows > 0) {
805 // Screen got larger.
806
807 if (deltaRows <= this.scrollbackRows_.length) {
808 var scrollbackCount = Math.min(deltaRows, this.scrollbackRows_.length);
809 var rows = this.scrollbackRows_.splice(
810 this.scrollbackRows_.length - scrollbackCount, scrollbackCount);
811 this.screen_.unshiftRows(rows);
812 deltaRows -= scrollbackCount;
813 cursor.row += scrollbackCount;
814 }
815
816 if (deltaRows)
817 this.appendRows_(deltaRows);
818 }
819
rginda35c456b2012-02-09 17:29:05 -0800820 this.setVTScrollRegion(null, null);
rgindac9bc5502012-01-18 11:48:44 -0800821 this.restoreCursor(cursor);
rginda87b86462011-12-14 13:48:03 -0800822};
823
824/**
825 * Scroll the terminal to the top of the scrollback buffer.
826 */
827hterm.Terminal.prototype.scrollHome = function() {
828 this.scrollPort_.scrollRowToTop(0);
829};
830
831/**
832 * Scroll the terminal to the end.
833 */
834hterm.Terminal.prototype.scrollEnd = function() {
835 this.scrollPort_.scrollRowToBottom(this.getRowCount());
836};
837
838/**
839 * Scroll the terminal one page up (minus one line) relative to the current
840 * position.
841 */
842hterm.Terminal.prototype.scrollPageUp = function() {
843 var i = this.scrollPort_.getTopRowIndex();
844 this.scrollPort_.scrollRowToTop(i - this.screenSize.height + 1);
845};
846
847/**
848 * Scroll the terminal one page down (minus one line) relative to the current
849 * position.
850 */
851hterm.Terminal.prototype.scrollPageDown = function() {
852 var i = this.scrollPort_.getTopRowIndex();
853 this.scrollPort_.scrollRowToTop(i + this.screenSize.height - 1);
rginda8ba33642011-12-14 12:31:31 -0800854};
855
rgindac9bc5502012-01-18 11:48:44 -0800856/**
Robert Ginda40932892012-12-10 17:26:40 -0800857 * Clear primary screen, secondary screen, and the scrollback buffer.
858 */
859hterm.Terminal.prototype.wipeContents = function() {
860 this.scrollbackRows_.length = 0;
861 this.scrollPort_.resetCache();
862
863 [this.primaryScreen_, this.alternateScreen_].forEach(function(screen) {
864 var bottom = screen.getHeight();
865 if (bottom > 0) {
866 this.renumberRows_(0, bottom);
867 this.clearHome(screen);
868 }
869 }.bind(this));
870
871 this.syncCursorPosition_();
Andrew de los Reyes68e07802013-04-04 15:38:55 -0700872 this.scrollPort_.invalidate();
Robert Ginda40932892012-12-10 17:26:40 -0800873};
874
875/**
rgindac9bc5502012-01-18 11:48:44 -0800876 * Full terminal reset.
877 */
rginda87b86462011-12-14 13:48:03 -0800878hterm.Terminal.prototype.reset = function() {
rgindac9bc5502012-01-18 11:48:44 -0800879 this.clearAllTabStops();
880 this.setDefaultTabStops();
rginda9ea433c2012-03-16 11:57:00 -0700881
882 this.clearHome(this.primaryScreen_);
883 this.primaryScreen_.textAttributes.reset();
884
885 this.clearHome(this.alternateScreen_);
886 this.alternateScreen_.textAttributes.reset();
887
rgindab8bc8932012-04-27 12:45:03 -0700888 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
889
Robert Ginda92e18102013-03-14 13:56:37 -0700890 this.vt.reset();
891
rgindac9bc5502012-01-18 11:48:44 -0800892 this.softReset();
rginda87b86462011-12-14 13:48:03 -0800893};
894
rgindac9bc5502012-01-18 11:48:44 -0800895/**
896 * Soft terminal reset.
rgindab8bc8932012-04-27 12:45:03 -0700897 *
898 * Perform a soft reset to the default values listed in
899 * http://www.vt100.net/docs/vt510-rm/DECSTR#T5-9
rgindac9bc5502012-01-18 11:48:44 -0800900 */
rginda0f5c0292012-01-13 11:00:13 -0800901hterm.Terminal.prototype.softReset = function() {
rgindab8bc8932012-04-27 12:45:03 -0700902 // Reset terminal options to their default values.
rgindac9bc5502012-01-18 11:48:44 -0800903 this.options_ = new hterm.Options();
rgindaf522ce02012-04-17 17:49:17 -0700904
rgindab8bc8932012-04-27 12:45:03 -0700905 // Xterm also resets the color palette on soft reset, even though it doesn't
906 // seem to be documented anywhere.
rgindaf522ce02012-04-17 17:49:17 -0700907 this.primaryScreen_.textAttributes.resetColorPalette();
908 this.alternateScreen_.textAttributes.resetColorPalette();
909
rgindab8bc8932012-04-27 12:45:03 -0700910 // The xterm man page explicitly says this will happen on soft reset.
911 this.setVTScrollRegion(null, null);
912
913 // Xterm also shows the cursor on soft reset, but does not alter the blink
914 // state.
rgindaa19afe22012-01-25 15:40:22 -0800915 this.setCursorVisible(true);
rginda0f5c0292012-01-13 11:00:13 -0800916};
917
rgindac9bc5502012-01-18 11:48:44 -0800918/**
919 * Move the cursor forward to the next tab stop, or to the last column
920 * if no more tab stops are set.
921 */
922hterm.Terminal.prototype.forwardTabStop = function() {
923 var column = this.screen_.cursorPosition.column;
924
925 for (var i = 0; i < this.tabStops_.length; i++) {
926 if (this.tabStops_[i] > column) {
927 this.setCursorColumn(this.tabStops_[i]);
928 return;
929 }
930 }
931
David Benjamin66e954d2012-05-05 21:08:12 -0400932 // xterm does not clear the overflow flag on HT or CHT.
933 var overflow = this.screen_.cursorPosition.overflow;
rgindac9bc5502012-01-18 11:48:44 -0800934 this.setCursorColumn(this.screenSize.width - 1);
David Benjamin66e954d2012-05-05 21:08:12 -0400935 this.screen_.cursorPosition.overflow = overflow;
rginda0f5c0292012-01-13 11:00:13 -0800936};
937
rgindac9bc5502012-01-18 11:48:44 -0800938/**
939 * Move the cursor backward to the previous tab stop, or to the first column
940 * if no previous tab stops are set.
941 */
942hterm.Terminal.prototype.backwardTabStop = function() {
943 var column = this.screen_.cursorPosition.column;
944
945 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
946 if (this.tabStops_[i] < column) {
947 this.setCursorColumn(this.tabStops_[i]);
948 return;
949 }
950 }
951
952 this.setCursorColumn(1);
rginda0f5c0292012-01-13 11:00:13 -0800953};
954
rgindac9bc5502012-01-18 11:48:44 -0800955/**
956 * Set a tab stop at the given column.
957 *
958 * @param {int} column Zero based column.
959 */
960hterm.Terminal.prototype.setTabStop = function(column) {
961 for (var i = this.tabStops_.length - 1; i >= 0; i--) {
962 if (this.tabStops_[i] == column)
963 return;
964
965 if (this.tabStops_[i] < column) {
966 this.tabStops_.splice(i + 1, 0, column);
967 return;
968 }
969 }
970
971 this.tabStops_.splice(0, 0, column);
rginda87b86462011-12-14 13:48:03 -0800972};
973
rgindac9bc5502012-01-18 11:48:44 -0800974/**
975 * Clear the tab stop at the current cursor position.
976 *
977 * No effect if there is no tab stop at the current cursor position.
978 */
979hterm.Terminal.prototype.clearTabStopAtCursor = function() {
980 var column = this.screen_.cursorPosition.column;
981
982 var i = this.tabStops_.indexOf(column);
983 if (i == -1)
984 return;
985
986 this.tabStops_.splice(i, 1);
987};
988
989/**
990 * Clear all tab stops.
991 */
992hterm.Terminal.prototype.clearAllTabStops = function() {
993 this.tabStops_.length = 0;
David Benjamin66e954d2012-05-05 21:08:12 -0400994 this.defaultTabStops = false;
rgindac9bc5502012-01-18 11:48:44 -0800995};
996
997/**
998 * Set up the default tab stops, starting from a given column.
999 *
1000 * This sets a tabstop every (column % this.tabWidth) column, starting
David Benjamin66e954d2012-05-05 21:08:12 -04001001 * from the specified column, or 0 if no column is provided. It also flags
1002 * future resizes to set them up.
rgindac9bc5502012-01-18 11:48:44 -08001003 *
1004 * This does not clear the existing tab stops first, use clearAllTabStops
1005 * for that.
1006 *
1007 * @param {int} opt_start Optional starting zero based starting column, useful
1008 * for filling out missing tab stops when the terminal is resized.
1009 */
1010hterm.Terminal.prototype.setDefaultTabStops = function(opt_start) {
1011 var start = opt_start || 0;
1012 var w = this.tabWidth;
David Benjamin66e954d2012-05-05 21:08:12 -04001013 // Round start up to a default tab stop.
1014 start = start - 1 - ((start - 1) % w) + w;
1015 for (var i = start; i < this.screenSize.width; i += w) {
1016 this.setTabStop(i);
rgindac9bc5502012-01-18 11:48:44 -08001017 }
David Benjamin66e954d2012-05-05 21:08:12 -04001018
1019 this.defaultTabStops = true;
rginda87b86462011-12-14 13:48:03 -08001020};
1021
rginda6d397402012-01-17 10:58:29 -08001022/**
rginda8ba33642011-12-14 12:31:31 -08001023 * Interpret a sequence of characters.
1024 *
1025 * Incomplete escape sequences are buffered until the next call.
1026 *
1027 * @param {string} str Sequence of characters to interpret or pass through.
1028 */
1029hterm.Terminal.prototype.interpret = function(str) {
rginda0f5c0292012-01-13 11:00:13 -08001030 this.vt.interpret(str);
rginda8ba33642011-12-14 12:31:31 -08001031 this.scheduleSyncCursorPosition_();
1032};
1033
1034/**
1035 * Take over the given DIV for use as the terminal display.
1036 *
1037 * @param {HTMLDivElement} div The div to use as the terminal display.
1038 */
1039hterm.Terminal.prototype.decorate = function(div) {
rginda87b86462011-12-14 13:48:03 -08001040 this.div_ = div;
1041
rginda8ba33642011-12-14 12:31:31 -08001042 this.scrollPort_.decorate(div);
rginda30f20f62012-04-05 16:36:19 -07001043 this.scrollPort_.setBackgroundImage(this.prefs_.get('background-image'));
Philip Douglass959b49d2012-05-30 13:29:29 -04001044 this.scrollPort_.setBackgroundSize(this.prefs_.get('background-size'));
1045 this.scrollPort_.setBackgroundPosition(
1046 this.prefs_.get('background-position'));
rginda30f20f62012-04-05 16:36:19 -07001047
rginda0918b652012-04-04 11:26:24 -07001048 this.div_.focus = this.focus.bind(this);
rgindaf7521392012-02-28 17:20:34 -08001049
rginda9f5222b2012-03-05 11:53:28 -08001050 this.setFontSize(this.prefs_.get('font-size'));
1051 this.syncFontFamily();
rgindaa19afe22012-01-25 15:40:22 -08001052
David Reveman8f552492012-03-28 12:18:41 -04001053 this.setScrollbarVisible(this.prefs_.get('scrollbar-visible'));
1054
rginda8ba33642011-12-14 12:31:31 -08001055 this.document_ = this.scrollPort_.getDocument();
1056
rginda4bba5e12012-06-20 16:15:30 -07001057 this.document_.body.oncontextmenu = function() { return false };
1058
1059 var onMouse = this.onMouse_.bind(this);
Toni Barzic0bfa8922013-11-22 11:18:35 -08001060 var screenNode = this.scrollPort_.getScreenNode();
1061 screenNode.addEventListener('mousedown', onMouse);
1062 screenNode.addEventListener('mouseup', onMouse);
1063 screenNode.addEventListener('mousemove', onMouse);
rginda4bba5e12012-06-20 16:15:30 -07001064 this.scrollPort_.onScrollWheel = onMouse;
1065
Toni Barzic0bfa8922013-11-22 11:18:35 -08001066 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001067 'focus', this.onFocusChange_.bind(this, true));
Toni Barzic0bfa8922013-11-22 11:18:35 -08001068 screenNode.addEventListener(
rginda8e92a692012-05-20 19:37:20 -07001069 'blur', this.onFocusChange_.bind(this, false));
1070
1071 var style = this.document_.createElement('style');
1072 style.textContent =
1073 ('.cursor-node[focus="false"] {' +
1074 ' box-sizing: border-box;' +
1075 ' background-color: transparent !important;' +
1076 ' border-width: 2px;' +
1077 ' border-style: solid;' +
1078 '}');
1079 this.document_.head.appendChild(style);
1080
rginda8ba33642011-12-14 12:31:31 -08001081 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001082 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001083 this.cursorNode_.style.cssText =
1084 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001085 'top: -99px;' +
1086 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001087 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1088 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001089 '-webkit-transition: opacity, background-color 100ms linear;');
1090 this.setCursorColor(this.prefs_.get('cursor-color'));
rgindad5613292012-06-19 15:40:37 -07001091
rginda8ba33642011-12-14 12:31:31 -08001092 this.document_.body.appendChild(this.cursorNode_);
1093
rgindad5613292012-06-19 15:40:37 -07001094 // When 'enableMouseDragScroll' is off we reposition this element directly
1095 // under the mouse cursor after a click. This makes Chrome associate
1096 // subsequent mousemove events with the scroll-blocker. Since the
1097 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1098 // events do not cause the scrollport to scroll.
1099 //
1100 // It's a hack, but it's the cleanest way I could find.
1101 this.scrollBlockerNode_ = this.document_.createElement('div');
1102 this.scrollBlockerNode_.style.cssText =
1103 ('position: absolute;' +
1104 'top: -99px;' +
1105 'display: block;' +
1106 'width: 10px;' +
1107 'height: 10px;');
1108 this.document_.body.appendChild(this.scrollBlockerNode_);
1109
1110 var onMouse = this.onMouse_.bind(this);
1111 this.scrollPort_.onScrollWheel = onMouse;
1112 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1113 ].forEach(function(event) {
1114 this.scrollBlockerNode_.addEventListener(event, onMouse);
1115 this.cursorNode_.addEventListener(event, onMouse);
1116 this.document_.addEventListener(event, onMouse);
1117 }.bind(this));
1118
1119 this.cursorNode_.addEventListener('mousedown', function() {
1120 setTimeout(this.focus.bind(this));
1121 }.bind(this));
1122
rgindade84e382012-04-20 15:39:31 -07001123 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
rginda8ba33642011-12-14 12:31:31 -08001124 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001125
rginda87b86462011-12-14 13:48:03 -08001126 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001127 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001128};
1129
rginda0918b652012-04-04 11:26:24 -07001130/**
1131 * Return the HTML document that contains the terminal DOM nodes.
1132 */
rginda87b86462011-12-14 13:48:03 -08001133hterm.Terminal.prototype.getDocument = function() {
1134 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001135};
1136
1137/**
rginda0918b652012-04-04 11:26:24 -07001138 * Focus the terminal.
1139 */
1140hterm.Terminal.prototype.focus = function() {
1141 this.scrollPort_.focus();
1142};
1143
1144/**
rginda8ba33642011-12-14 12:31:31 -08001145 * Return the HTML Element for a given row index.
1146 *
1147 * This is a method from the RowProvider interface. The ScrollPort uses
1148 * it to fetch rows on demand as they are scrolled into view.
1149 *
1150 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1151 * pairs to conserve memory.
1152 *
1153 * @param {integer} index The zero-based row index, measured relative to the
1154 * start of the scrollback buffer. On-screen rows will always have the
1155 * largest indicies.
1156 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1157 */
1158hterm.Terminal.prototype.getRowNode = function(index) {
1159 if (index < this.scrollbackRows_.length)
1160 return this.scrollbackRows_[index];
1161
1162 var screenIndex = index - this.scrollbackRows_.length;
1163 return this.screen_.rowsArray[screenIndex];
1164};
1165
1166/**
1167 * Return the text content for a given range of rows.
1168 *
1169 * This is a method from the RowProvider interface. The ScrollPort uses
1170 * it to fetch text content on demand when the user attempts to copy their
1171 * selection to the clipboard.
1172 *
1173 * @param {integer} start The zero-based row index to start from, measured
1174 * relative to the start of the scrollback buffer. On-screen rows will
1175 * always have the largest indicies.
1176 * @param {integer} end The zero-based row index to end on, measured
1177 * relative to the start of the scrollback buffer.
1178 * @return {string} A single string containing the text value of the range of
1179 * rows. Lines will be newline delimited, with no trailing newline.
1180 */
1181hterm.Terminal.prototype.getRowsText = function(start, end) {
1182 var ary = [];
1183 for (var i = start; i < end; i++) {
1184 var node = this.getRowNode(i);
1185 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001186 if (i < end - 1 && !node.getAttribute('line-overflow'))
1187 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001188 }
1189
rgindaa09e7332012-08-17 12:49:51 -07001190 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001191};
1192
1193/**
1194 * Return the text content for a given row.
1195 *
1196 * This is a method from the RowProvider interface. The ScrollPort uses
1197 * it to fetch text content on demand when the user attempts to copy their
1198 * selection to the clipboard.
1199 *
1200 * @param {integer} index The zero-based row index to return, measured
1201 * relative to the start of the scrollback buffer. On-screen rows will
1202 * always have the largest indicies.
1203 * @return {string} A string containing the text value of the selected row.
1204 */
1205hterm.Terminal.prototype.getRowText = function(index) {
1206 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001207 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001208};
1209
1210/**
1211 * Return the total number of rows in the addressable screen and in the
1212 * scrollback buffer of this terminal.
1213 *
1214 * This is a method from the RowProvider interface. The ScrollPort uses
1215 * it to compute the size of the scrollbar.
1216 *
1217 * @return {integer} The number of rows in this terminal.
1218 */
1219hterm.Terminal.prototype.getRowCount = function() {
1220 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1221};
1222
1223/**
1224 * Create DOM nodes for new rows and append them to the end of the terminal.
1225 *
1226 * This is the only correct way to add a new DOM node for a row. Notice that
1227 * the new row is appended to the bottom of the list of rows, and does not
1228 * require renumbering (of the rowIndex property) of previous rows.
1229 *
1230 * If you think you want a new blank row somewhere in the middle of the
1231 * terminal, look into moveRows_().
1232 *
1233 * This method does not pay attention to vtScrollTop/Bottom, since you should
1234 * be using moveRows() in cases where they would matter.
1235 *
1236 * The cursor will be positioned at column 0 of the first inserted line.
1237 */
1238hterm.Terminal.prototype.appendRows_ = function(count) {
1239 var cursorRow = this.screen_.rowsArray.length;
1240 var offset = this.scrollbackRows_.length + cursorRow;
1241 for (var i = 0; i < count; i++) {
1242 var row = this.document_.createElement('x-row');
1243 row.appendChild(this.document_.createTextNode(''));
1244 row.rowIndex = offset + i;
1245 this.screen_.pushRow(row);
1246 }
1247
1248 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1249 if (extraRows > 0) {
1250 var ary = this.screen_.shiftRows(extraRows);
1251 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001252 if (this.scrollPort_.isScrolledEnd)
1253 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001254 }
1255
1256 if (cursorRow >= this.screen_.rowsArray.length)
1257 cursorRow = this.screen_.rowsArray.length - 1;
1258
rginda87b86462011-12-14 13:48:03 -08001259 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001260};
1261
1262/**
1263 * Relocate rows from one part of the addressable screen to another.
1264 *
1265 * This is used to recycle rows during VT scrolls (those which are driven
1266 * by VT commands, rather than by the user manipulating the scrollbar.)
1267 *
1268 * In this case, the blank lines scrolled into the scroll region are made of
1269 * the nodes we scrolled off. These have their rowIndex properties carefully
1270 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001271 */
1272hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1273 var ary = this.screen_.removeRows(fromIndex, count);
1274 this.screen_.insertRows(toIndex, ary);
1275
1276 var start, end;
1277 if (fromIndex < toIndex) {
1278 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001279 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001280 } else {
1281 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001282 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001283 }
1284
1285 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001286 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001287};
1288
1289/**
1290 * Renumber the rowIndex property of the given range of rows.
1291 *
1292 * The start and end indicies are relative to the screen, not the scrollback.
1293 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001294 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001295 * no need to renumber scrollback rows.
1296 */
Robert Ginda40932892012-12-10 17:26:40 -08001297hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1298 var screen = opt_screen || this.screen_;
1299
rginda8ba33642011-12-14 12:31:31 -08001300 var offset = this.scrollbackRows_.length;
1301 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001302 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001303 }
1304};
1305
1306/**
1307 * Print a string to the terminal.
1308 *
1309 * This respects the current insert and wraparound modes. It will add new lines
1310 * to the end of the terminal, scrolling off the top into the scrollback buffer
1311 * if necessary.
1312 *
1313 * The string is *not* parsed for escape codes. Use the interpret() method if
1314 * that's what you're after.
1315 *
1316 * @param{string} str The string to print.
1317 */
1318hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001319 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001320
rgindaa9abdd82012-08-06 18:05:09 -07001321 while (startOffset < str.length) {
rgindaa09e7332012-08-17 12:49:51 -07001322 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1323 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001324 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001325 }
rgindaa19afe22012-01-25 15:40:22 -08001326
rgindaa9abdd82012-08-06 18:05:09 -07001327 var count = str.length - startOffset;
1328 var didOverflow = false;
1329 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001330
rgindaa9abdd82012-08-06 18:05:09 -07001331 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1332 didOverflow = true;
1333 count = this.screenSize.width - this.screen_.cursorPosition.column;
1334 }
rgindaa19afe22012-01-25 15:40:22 -08001335
rgindaa9abdd82012-08-06 18:05:09 -07001336 if (didOverflow && !this.options_.wraparound) {
1337 // If the string overflowed the line but wraparound is off, then the
1338 // last printed character should be the last of the string.
1339 // TODO: This will add to our problems with multibyte UTF-16 characters.
1340 substr = str.substr(startOffset, count - 1) +
1341 str.substr(str.length - 1);
1342 count = str.length;
1343 } else {
1344 substr = str.substr(startOffset, count);
1345 }
rgindaa19afe22012-01-25 15:40:22 -08001346
rgindaa9abdd82012-08-06 18:05:09 -07001347 if (this.options_.insertMode) {
1348 this.screen_.insertString(substr);
1349 } else {
1350 this.screen_.overwriteString(substr);
1351 }
1352
1353 this.screen_.maybeClipCurrentRow();
1354 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001355 }
rginda8ba33642011-12-14 12:31:31 -08001356
1357 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001358
rginda9f5222b2012-03-05 11:53:28 -08001359 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001360 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001361};
1362
1363/**
rginda87b86462011-12-14 13:48:03 -08001364 * Set the VT scroll region.
1365 *
rginda87b86462011-12-14 13:48:03 -08001366 * This also resets the cursor position to the absolute (0, 0) position, since
1367 * that's what xterm appears to do.
1368 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001369 * Setting the scroll region to the full height of the terminal will clear
1370 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1371 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1372 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1373 * continue to work as most users would expect.
1374 *
rginda87b86462011-12-14 13:48:03 -08001375 * @param {integer} scrollTop The zero-based top of the scroll region.
1376 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1377 * inclusive.
1378 */
1379hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001380 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001381 this.vtScrollTop_ = null;
1382 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001383 } else {
1384 this.vtScrollTop_ = scrollTop;
1385 this.vtScrollBottom_ = scrollBottom;
1386 }
rginda87b86462011-12-14 13:48:03 -08001387};
1388
1389/**
rginda8ba33642011-12-14 12:31:31 -08001390 * Return the top row index according to the VT.
1391 *
1392 * This will return 0 unless the terminal has been told to restrict scrolling
1393 * to some lower row. It is used for some VT cursor positioning and scrolling
1394 * commands.
1395 *
1396 * @return {integer} The topmost row in the terminal's scroll region.
1397 */
1398hterm.Terminal.prototype.getVTScrollTop = function() {
1399 if (this.vtScrollTop_ != null)
1400 return this.vtScrollTop_;
1401
1402 return 0;
rginda87b86462011-12-14 13:48:03 -08001403};
rginda8ba33642011-12-14 12:31:31 -08001404
1405/**
1406 * Return the bottom row index according to the VT.
1407 *
1408 * This will return the height of the terminal unless the it has been told to
1409 * restrict scrolling to some higher row. It is used for some VT cursor
1410 * positioning and scrolling commands.
1411 *
1412 * @return {integer} The bottommost row in the terminal's scroll region.
1413 */
1414hterm.Terminal.prototype.getVTScrollBottom = function() {
1415 if (this.vtScrollBottom_ != null)
1416 return this.vtScrollBottom_;
1417
rginda87b86462011-12-14 13:48:03 -08001418 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001419}
1420
1421/**
1422 * Process a '\n' character.
1423 *
1424 * If the cursor is on the final row of the terminal this will append a new
1425 * blank row to the screen and scroll the topmost row into the scrollback
1426 * buffer.
1427 *
1428 * Otherwise, this moves the cursor to column zero of the next row.
1429 */
1430hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001431 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1432 this.screen_.rowsArray.length - 1);
1433
1434 if (this.vtScrollBottom_ != null) {
1435 // A VT Scroll region is active, we never append new rows.
1436 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1437 // We're at the end of the VT Scroll Region, perform a VT scroll.
1438 this.vtScrollUp(1);
1439 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1440 } else if (cursorAtEndOfScreen) {
1441 // We're at the end of the screen, the only thing to do is put the
1442 // cursor to column 0.
1443 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1444 } else {
1445 // Anywhere else, advance the cursor row, and reset the column.
1446 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1447 }
1448 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001449 // We're at the end of the screen. Append a new row to the terminal,
1450 // shifting the top row into the scrollback.
1451 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001452 } else {
rginda87b86462011-12-14 13:48:03 -08001453 // Anywhere else in the screen just moves the cursor.
1454 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001455 }
1456};
1457
1458/**
1459 * Like newLine(), except maintain the cursor column.
1460 */
1461hterm.Terminal.prototype.lineFeed = function() {
1462 var column = this.screen_.cursorPosition.column;
1463 this.newLine();
1464 this.setCursorColumn(column);
1465};
1466
1467/**
rginda87b86462011-12-14 13:48:03 -08001468 * If autoCarriageReturn is set then newLine(), else lineFeed().
1469 */
1470hterm.Terminal.prototype.formFeed = function() {
1471 if (this.options_.autoCarriageReturn) {
1472 this.newLine();
1473 } else {
1474 this.lineFeed();
1475 }
1476};
1477
1478/**
1479 * Move the cursor up one row, possibly inserting a blank line.
1480 *
1481 * The cursor column is not changed.
1482 */
1483hterm.Terminal.prototype.reverseLineFeed = function() {
1484 var scrollTop = this.getVTScrollTop();
1485 var currentRow = this.screen_.cursorPosition.row;
1486
1487 if (currentRow == scrollTop) {
1488 this.insertLines(1);
1489 } else {
1490 this.setAbsoluteCursorRow(currentRow - 1);
1491 }
1492};
1493
1494/**
rginda8ba33642011-12-14 12:31:31 -08001495 * Replace all characters to the left of the current cursor with the space
1496 * character.
1497 *
1498 * TODO(rginda): This should probably *remove* the characters (not just replace
1499 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001500 * position.
rginda8ba33642011-12-14 12:31:31 -08001501 */
1502hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001503 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001504 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001505 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001506 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001507};
1508
1509/**
David Benjamin684a9b72012-05-01 17:19:58 -04001510 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001511 *
1512 * The cursor position is unchanged.
1513 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001514 * If the current background color is not the default background color this
1515 * will insert spaces rather than delete. This is unfortunate because the
1516 * trailing space will affect text selection, but it's difficult to come up
1517 * with a way to style empty space that wouldn't trip up the hterm.Screen
1518 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001519 *
1520 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1521 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1522 * crbug.com/232390 for details.
rginda8ba33642011-12-14 12:31:31 -08001523 */
1524hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001525 if (this.screen_.cursorPosition.overflow)
1526 return;
1527
Robert Ginda7fd57082012-09-25 14:41:47 -07001528 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1529 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001530
1531 if (this.screen_.textAttributes.background ===
1532 this.screen_.textAttributes.DEFAULT_COLOR) {
1533 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
1534 if (cursorRow.textContent.length <=
1535 this.screen_.cursorPosition.column + count) {
1536 this.screen_.deleteChars(count);
1537 this.clearCursorOverflow();
1538 return;
1539 }
1540 }
1541
rginda87b86462011-12-14 13:48:03 -08001542 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001543 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001544 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001545 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001546};
1547
1548/**
1549 * Erase the current line.
1550 *
1551 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001552 */
1553hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001554 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001555 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001556 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001557 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001558};
1559
1560/**
David Benjamina08d78f2012-05-05 00:28:49 -04001561 * Erase all characters from the start of the screen to the current cursor
1562 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001563 *
1564 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001565 */
1566hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001567 var cursor = this.saveCursor();
1568
1569 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001570
David Benjamina08d78f2012-05-05 00:28:49 -04001571 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001572 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001573 this.screen_.clearCursorRow();
1574 }
1575
rginda87b86462011-12-14 13:48:03 -08001576 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001577 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001578};
1579
1580/**
1581 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001582 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001583 *
1584 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001585 */
1586hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001587 var cursor = this.saveCursor();
1588
1589 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001590
David Benjamina08d78f2012-05-05 00:28:49 -04001591 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001592 for (var i = cursor.row + 1; i <= bottom; i++) {
1593 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001594 this.screen_.clearCursorRow();
1595 }
1596
rginda87b86462011-12-14 13:48:03 -08001597 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001598 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001599};
1600
1601/**
1602 * Fill the terminal with a given character.
1603 *
1604 * This methods does not respect the VT scroll region.
1605 *
1606 * @param {string} ch The character to use for the fill.
1607 */
1608hterm.Terminal.prototype.fill = function(ch) {
1609 var cursor = this.saveCursor();
1610
1611 this.setAbsoluteCursorPosition(0, 0);
1612 for (var row = 0; row < this.screenSize.height; row++) {
1613 for (var col = 0; col < this.screenSize.width; col++) {
1614 this.setAbsoluteCursorPosition(row, col);
1615 this.screen_.overwriteString(ch);
1616 }
1617 }
1618
1619 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001620};
1621
1622/**
rginda9ea433c2012-03-16 11:57:00 -07001623 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001624 *
rginda9ea433c2012-03-16 11:57:00 -07001625 * This does not respect the scroll region.
1626 *
1627 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1628 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001629 */
rginda9ea433c2012-03-16 11:57:00 -07001630hterm.Terminal.prototype.clearHome = function(opt_screen) {
1631 var screen = opt_screen || this.screen_;
1632 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001633
rginda11057d52012-04-25 12:29:56 -07001634 if (bottom == 0) {
1635 // Empty screen, nothing to do.
1636 return;
1637 }
1638
rgindae4d29232012-01-19 10:47:13 -08001639 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001640 screen.setCursorPosition(i, 0);
1641 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001642 }
1643
rginda9ea433c2012-03-16 11:57:00 -07001644 screen.setCursorPosition(0, 0);
1645};
1646
1647/**
1648 * Erase the entire display without changing the cursor position.
1649 *
1650 * The cursor position is unchanged. This does not respect the scroll
1651 * region.
1652 *
1653 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1654 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001655 */
1656hterm.Terminal.prototype.clear = function(opt_screen) {
1657 var screen = opt_screen || this.screen_;
1658 var cursor = screen.cursorPosition.clone();
1659 this.clearHome(screen);
1660 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001661};
1662
1663/**
1664 * VT command to insert lines at the current cursor row.
1665 *
1666 * This respects the current scroll region. Rows pushed off the bottom are
1667 * lost (they won't show up in the scrollback buffer).
1668 *
rginda8ba33642011-12-14 12:31:31 -08001669 * @param {integer} count The number of lines to insert.
1670 */
1671hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07001672 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08001673
1674 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07001675 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08001676
Robert Ginda579186b2012-09-26 11:40:04 -07001677 // The moveCount is the number of rows we need to relocate to make room for
1678 // the new row(s). The count is the distance to move them.
1679 var moveCount = bottom - cursorRow - count + 1;
1680 if (moveCount)
1681 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08001682
Robert Ginda579186b2012-09-26 11:40:04 -07001683 for (var i = count - 1; i >= 0; i--) {
1684 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001685 this.screen_.clearCursorRow();
1686 }
rginda8ba33642011-12-14 12:31:31 -08001687};
1688
1689/**
1690 * VT command to delete lines at the current cursor row.
1691 *
1692 * New rows are added to the bottom of scroll region to take their place. New
1693 * rows are strictly there to take up space and have no content or style.
1694 */
1695hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001696 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001697
rginda87b86462011-12-14 13:48:03 -08001698 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001699 var bottom = this.getVTScrollBottom();
1700
rginda87b86462011-12-14 13:48:03 -08001701 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001702 count = Math.min(count, maxCount);
1703
rginda87b86462011-12-14 13:48:03 -08001704 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001705 if (count != maxCount)
1706 this.moveRows_(top, count, moveStart);
1707
1708 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001709 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001710 this.screen_.clearCursorRow();
1711 }
1712
rginda87b86462011-12-14 13:48:03 -08001713 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001714 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001715};
1716
1717/**
1718 * Inserts the given number of spaces at the current cursor position.
1719 *
rginda87b86462011-12-14 13:48:03 -08001720 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001721 */
1722hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001723 var cursor = this.saveCursor();
1724
rgindacbbd7482012-06-13 15:06:16 -07001725 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001726 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001727 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001728
1729 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001730 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001731};
1732
1733/**
1734 * Forward-delete the specified number of characters starting at the cursor
1735 * position.
1736 *
1737 * @param {integer} count The number of characters to delete.
1738 */
1739hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001740 var deleted = this.screen_.deleteChars(count);
1741 if (deleted && !this.screen_.textAttributes.isDefault()) {
1742 var cursor = this.saveCursor();
1743 this.setCursorColumn(this.screenSize.width - deleted);
1744 this.screen_.insertString(lib.f.getWhitespace(deleted));
1745 this.restoreCursor(cursor);
1746 }
1747
David Benjamin54e8bf62012-06-01 22:31:40 -04001748 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001749};
1750
1751/**
1752 * Shift rows in the scroll region upwards by a given number of lines.
1753 *
1754 * New rows are inserted at the bottom of the scroll region to fill the
1755 * vacated rows. The new rows not filled out with the current text attributes.
1756 *
1757 * This function does not affect the scrollback rows at all. Rows shifted
1758 * off the top are lost.
1759 *
rginda87b86462011-12-14 13:48:03 -08001760 * The cursor position is not altered.
1761 *
rginda8ba33642011-12-14 12:31:31 -08001762 * @param {integer} count The number of rows to scroll.
1763 */
1764hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001765 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001766
rginda87b86462011-12-14 13:48:03 -08001767 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001768 this.deleteLines(count);
1769
rginda87b86462011-12-14 13:48:03 -08001770 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001771};
1772
1773/**
1774 * Shift rows below the cursor down by a given number of lines.
1775 *
1776 * This function respects the current scroll region.
1777 *
1778 * New rows are inserted at the top of the scroll region to fill the
1779 * vacated rows. The new rows not filled out with the current text attributes.
1780 *
1781 * This function does not affect the scrollback rows at all. Rows shifted
1782 * off the bottom are lost.
1783 *
1784 * @param {integer} count The number of rows to scroll.
1785 */
1786hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001787 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001788
rginda87b86462011-12-14 13:48:03 -08001789 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001790 this.insertLines(opt_count);
1791
rginda87b86462011-12-14 13:48:03 -08001792 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001793};
1794
rginda87b86462011-12-14 13:48:03 -08001795
rginda8ba33642011-12-14 12:31:31 -08001796/**
1797 * Set the cursor position.
1798 *
1799 * The cursor row is relative to the scroll region if the terminal has
1800 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1801 *
1802 * @param {integer} row The new zero-based cursor row.
1803 * @param {integer} row The new zero-based cursor column.
1804 */
1805hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1806 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001807 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001808 } else {
rginda87b86462011-12-14 13:48:03 -08001809 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001810 }
rginda87b86462011-12-14 13:48:03 -08001811};
rginda8ba33642011-12-14 12:31:31 -08001812
rginda87b86462011-12-14 13:48:03 -08001813hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1814 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07001815 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
1816 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001817 this.screen_.setCursorPosition(row, column);
1818};
1819
1820hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07001821 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
1822 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001823 this.screen_.setCursorPosition(row, column);
1824};
1825
1826/**
1827 * Set the cursor column.
1828 *
1829 * @param {integer} column The new zero-based cursor column.
1830 */
1831hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001832 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001833};
1834
1835/**
1836 * Return the cursor column.
1837 *
1838 * @return {integer} The zero-based cursor column.
1839 */
1840hterm.Terminal.prototype.getCursorColumn = function() {
1841 return this.screen_.cursorPosition.column;
1842};
1843
1844/**
1845 * Set the cursor row.
1846 *
1847 * The cursor row is relative to the scroll region if the terminal has
1848 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1849 *
1850 * @param {integer} row The new cursor row.
1851 */
rginda87b86462011-12-14 13:48:03 -08001852hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1853 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001854};
1855
1856/**
1857 * Return the cursor row.
1858 *
1859 * @return {integer} The zero-based cursor row.
1860 */
1861hterm.Terminal.prototype.getCursorRow = function(row) {
1862 return this.screen_.cursorPosition.row;
1863};
1864
1865/**
1866 * Request that the ScrollPort redraw itself soon.
1867 *
1868 * The redraw will happen asynchronously, soon after the call stack winds down.
1869 * Multiple calls will be coalesced into a single redraw.
1870 */
1871hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001872 if (this.timeouts_.redraw)
1873 return;
rginda8ba33642011-12-14 12:31:31 -08001874
1875 var self = this;
rginda87b86462011-12-14 13:48:03 -08001876 this.timeouts_.redraw = setTimeout(function() {
1877 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001878 self.scrollPort_.redraw_();
1879 }, 0);
1880};
1881
1882/**
1883 * Request that the ScrollPort be scrolled to the bottom.
1884 *
1885 * The scroll will happen asynchronously, soon after the call stack winds down.
1886 * Multiple calls will be coalesced into a single scroll.
1887 *
1888 * This affects the scrollbar position of the ScrollPort, and has nothing to
1889 * do with the VT scroll commands.
1890 */
1891hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1892 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001893 return;
rginda8ba33642011-12-14 12:31:31 -08001894
1895 var self = this;
1896 this.timeouts_.scrollDown = setTimeout(function() {
1897 delete self.timeouts_.scrollDown;
1898 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1899 }, 10);
1900};
1901
1902/**
1903 * Move the cursor up a specified number of rows.
1904 *
1905 * @param {integer} count The number of rows to move the cursor.
1906 */
1907hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001908 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001909};
1910
1911/**
1912 * Move the cursor down a specified number of rows.
1913 *
1914 * @param {integer} count The number of rows to move the cursor.
1915 */
1916hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001917 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001918 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1919 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1920 this.screenSize.height - 1);
1921
rgindacbbd7482012-06-13 15:06:16 -07001922 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08001923 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001924 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001925};
1926
1927/**
1928 * Move the cursor left a specified number of columns.
1929 *
1930 * @param {integer} count The number of columns to move the cursor.
1931 */
1932hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001933 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001934};
1935
1936/**
1937 * Move the cursor right a specified number of columns.
1938 *
1939 * @param {integer} count The number of columns to move the cursor.
1940 */
1941hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001942 count = count || 1;
rgindacbbd7482012-06-13 15:06:16 -07001943 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001944 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001945 this.setCursorColumn(column);
1946};
1947
1948/**
1949 * Reverse the foreground and background colors of the terminal.
1950 *
1951 * This only affects text that was drawn with no attributes.
1952 *
1953 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1954 * been drawn with attributes that happen to coincide with the default
1955 * 'no-attribute' colors. My guess is probably not.
1956 */
1957hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001958 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001959 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08001960 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
1961 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08001962 } else {
rginda9f5222b2012-03-05 11:53:28 -08001963 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
1964 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08001965 }
1966};
1967
1968/**
rginda87b86462011-12-14 13:48:03 -08001969 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07001970 *
1971 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08001972 */
1973hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08001974 this.cursorNode_.style.backgroundColor =
1975 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001976
1977 var self = this;
1978 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08001979 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08001980 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07001981
1982 if (this.bellAudio_.getAttribute('src')) {
Robert Gindaa6331372013-03-19 10:35:39 -07001983 if (this.bellSquelchTimeout_)
Robert Ginda92e18102013-03-14 13:56:37 -07001984 return;
1985
1986 this.bellAudio_.play();
1987
1988 this.bellSequelchTimeout_ = setTimeout(function() {
1989 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07001990 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07001991 } else {
1992 delete this.bellSquelchTimeout_;
1993 }
rginda87b86462011-12-14 13:48:03 -08001994};
1995
1996/**
rginda8ba33642011-12-14 12:31:31 -08001997 * Set the origin mode bit.
1998 *
1999 * If origin mode is on, certain VT cursor and scrolling commands measure their
2000 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2001 * to the top of the addressable screen.
2002 *
2003 * Defaults to off.
2004 *
2005 * @param {boolean} state True to set origin mode, false to unset.
2006 */
2007hterm.Terminal.prototype.setOriginMode = function(state) {
2008 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002009 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002010};
2011
2012/**
2013 * Set the insert mode bit.
2014 *
2015 * If insert mode is on, existing text beyond the cursor position will be
2016 * shifted right to make room for new text. Otherwise, new text overwrites
2017 * any existing text.
2018 *
2019 * Defaults to off.
2020 *
2021 * @param {boolean} state True to set insert mode, false to unset.
2022 */
2023hterm.Terminal.prototype.setInsertMode = function(state) {
2024 this.options_.insertMode = state;
2025};
2026
2027/**
rginda87b86462011-12-14 13:48:03 -08002028 * Set the auto carriage return bit.
2029 *
2030 * If auto carriage return is on then a formfeed character is interpreted
2031 * as a newline, otherwise it's the same as a linefeed. The difference boils
2032 * down to whether or not the cursor column is reset.
2033 */
2034hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2035 this.options_.autoCarriageReturn = state;
2036};
2037
2038/**
rginda8ba33642011-12-14 12:31:31 -08002039 * Set the wraparound mode bit.
2040 *
2041 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2042 * to the start of the following row. Otherwise, the cursor is clamped to the
2043 * end of the screen and attempts to write past it are ignored.
2044 *
2045 * Defaults to on.
2046 *
2047 * @param {boolean} state True to set wraparound mode, false to unset.
2048 */
2049hterm.Terminal.prototype.setWraparound = function(state) {
2050 this.options_.wraparound = state;
2051};
2052
2053/**
2054 * Set the reverse-wraparound mode bit.
2055 *
2056 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2057 * to the end of the previous row. Otherwise, the cursor is clamped to column
2058 * 0.
2059 *
2060 * Defaults to off.
2061 *
2062 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2063 */
2064hterm.Terminal.prototype.setReverseWraparound = function(state) {
2065 this.options_.reverseWraparound = state;
2066};
2067
2068/**
2069 * Selects between the primary and alternate screens.
2070 *
2071 * If alternate mode is on, the alternate screen is active. Otherwise the
2072 * primary screen is active.
2073 *
2074 * Swapping screens has no effect on the scrollback buffer.
2075 *
2076 * Each screen maintains its own cursor position.
2077 *
2078 * Defaults to off.
2079 *
2080 * @param {boolean} state True to set alternate mode, false to unset.
2081 */
2082hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002083 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002084 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2085
rginda35c456b2012-02-09 17:29:05 -08002086 if (this.screen_.rowsArray.length &&
2087 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2088 // If the screen changed sizes while we were away, our rowIndexes may
2089 // be incorrect.
2090 var offset = this.scrollbackRows_.length;
2091 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002092 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002093 ary[i].rowIndex = offset + i;
2094 }
2095 }
rginda8ba33642011-12-14 12:31:31 -08002096
rginda35c456b2012-02-09 17:29:05 -08002097 this.realizeWidth_(this.screenSize.width);
2098 this.realizeHeight_(this.screenSize.height);
2099 this.scrollPort_.syncScrollHeight();
2100 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002101
rginda6d397402012-01-17 10:58:29 -08002102 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002103 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002104};
2105
2106/**
2107 * Set the cursor-blink mode bit.
2108 *
2109 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2110 * a visible cursor does not blink.
2111 *
2112 * You should make sure to turn blinking off if you're going to dispose of a
2113 * terminal, otherwise you'll leak a timeout.
2114 *
2115 * Defaults to on.
2116 *
2117 * @param {boolean} state True to set cursor-blink mode, false to unset.
2118 */
2119hterm.Terminal.prototype.setCursorBlink = function(state) {
2120 this.options_.cursorBlink = state;
2121
2122 if (!state && this.timeouts_.cursorBlink) {
2123 clearTimeout(this.timeouts_.cursorBlink);
2124 delete this.timeouts_.cursorBlink;
2125 }
2126
2127 if (this.options_.cursorVisible)
2128 this.setCursorVisible(true);
2129};
2130
2131/**
2132 * Set the cursor-visible mode bit.
2133 *
2134 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2135 *
2136 * Defaults to on.
2137 *
2138 * @param {boolean} state True to set cursor-visible mode, false to unset.
2139 */
2140hterm.Terminal.prototype.setCursorVisible = function(state) {
2141 this.options_.cursorVisible = state;
2142
2143 if (!state) {
rginda87b86462011-12-14 13:48:03 -08002144 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002145 return;
2146 }
2147
rginda87b86462011-12-14 13:48:03 -08002148 this.syncCursorPosition_();
2149
2150 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002151
2152 if (this.options_.cursorBlink) {
2153 if (this.timeouts_.cursorBlink)
2154 return;
2155
2156 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
2157 500);
2158 } else {
2159 if (this.timeouts_.cursorBlink) {
2160 clearTimeout(this.timeouts_.cursorBlink);
2161 delete this.timeouts_.cursorBlink;
2162 }
2163 }
2164};
2165
2166/**
rginda87b86462011-12-14 13:48:03 -08002167 * Synchronizes the visible cursor and document selection with the current
2168 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002169 */
2170hterm.Terminal.prototype.syncCursorPosition_ = function() {
2171 var topRowIndex = this.scrollPort_.getTopRowIndex();
2172 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2173 var cursorRowIndex = this.scrollbackRows_.length +
2174 this.screen_.cursorPosition.row;
2175
2176 if (cursorRowIndex > bottomRowIndex) {
2177 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002178 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002179 return;
2180 }
2181
rginda35c456b2012-02-09 17:29:05 -08002182 this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px';
rginda35c456b2012-02-09 17:29:05 -08002183
rginda8ba33642011-12-14 12:31:31 -08002184 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002185 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2186 'px';
2187 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2188 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002189
2190 this.cursorNode_.setAttribute('title',
2191 '(' + this.screen_.cursorPosition.row +
2192 ', ' + this.screen_.cursorPosition.column +
2193 ')');
2194
2195 // Update the caret for a11y purposes.
2196 var selection = this.document_.getSelection();
2197 if (selection && selection.isCollapsed)
2198 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002199};
2200
Robert Ginda830583c2013-08-07 13:20:46 -07002201hterm.Terminal.prototype.restyleCursor_ = function() {
2202 var shape = this.cursorShape_;
2203
2204 if (this.cursorNode_.getAttribute('focus') == 'false') {
2205 // Always show a block cursor when unfocused.
2206 shape = hterm.Terminal.cursorShape.BLOCK;
2207 }
2208
2209 var style = this.cursorNode_.style;
2210
2211 switch (shape) {
2212 case hterm.Terminal.cursorShape.BEAM:
2213 style.height = this.scrollPort_.characterSize.height + 'px';
2214 style.backgroundColor = 'transparent';
2215 style.borderBottomStyle = null;
2216 style.borderLeftStyle = 'solid';
2217 break;
2218
2219 case hterm.Terminal.cursorShape.UNDERLINE:
2220 style.height = this.scrollPort_.characterSize.baseline + 'px';
2221 style.backgroundColor = 'transparent';
2222 style.borderBottomStyle = 'solid';
2223 // correct the size to put it exactly at the baseline
2224 style.borderLeftStyle = null;
2225 break;
2226
2227 default:
2228 style.height = this.scrollPort_.characterSize.height + 'px';
2229 style.backgroundColor = this.cursorColor_;
2230 style.borderBottomStyle = null;
2231 style.borderLeftStyle = null;
2232 break;
2233 }
2234};
2235
rginda8ba33642011-12-14 12:31:31 -08002236/**
2237 * Synchronizes the visible cursor with the current cursor coordinates.
2238 *
2239 * The sync will happen asynchronously, soon after the call stack winds down.
2240 * Multiple calls will be coalesced into a single sync.
2241 */
2242hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2243 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002244 return;
rginda8ba33642011-12-14 12:31:31 -08002245
2246 var self = this;
2247 this.timeouts_.syncCursor = setTimeout(function() {
2248 self.syncCursorPosition_();
2249 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002250 }, 0);
2251};
2252
rgindacc2996c2012-02-24 14:59:31 -08002253/**
rgindaf522ce02012-04-17 17:49:17 -07002254 * Show or hide the zoom warning.
2255 *
2256 * The zoom warning is a message warning the user that their browser zoom must
2257 * be set to 100% in order for hterm to function properly.
2258 *
2259 * @param {boolean} state True to show the message, false to hide it.
2260 */
2261hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2262 if (!this.zoomWarningNode_) {
2263 if (!state)
2264 return;
2265
2266 this.zoomWarningNode_ = this.document_.createElement('div');
2267 this.zoomWarningNode_.style.cssText = (
2268 'color: black;' +
2269 'background-color: #ff2222;' +
2270 'font-size: large;' +
2271 'border-radius: 8px;' +
2272 'opacity: 0.75;' +
2273 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2274 'top: 0.5em;' +
2275 'right: 1.2em;' +
2276 'position: absolute;' +
2277 '-webkit-text-size-adjust: none;' +
2278 '-webkit-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002279 }
2280
Robert Gindab4839c22013-02-28 16:52:10 -08002281 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2282 hterm.zoomWarningMessage,
2283 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2284
rgindaf522ce02012-04-17 17:49:17 -07002285 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2286
2287 if (state) {
2288 if (!this.zoomWarningNode_.parentNode)
2289 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2290 } else if (this.zoomWarningNode_.parentNode) {
2291 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2292 }
2293};
2294
2295/**
rgindacc2996c2012-02-24 14:59:31 -08002296 * Show the terminal overlay for a given amount of time.
2297 *
2298 * The terminal overlay appears in inverse video in a large font, centered
2299 * over the terminal. You should probably keep the overlay message brief,
2300 * since it's in a large font and you probably aren't going to check the size
2301 * of the terminal first.
2302 *
2303 * @param {string} msg The text (not HTML) message to display in the overlay.
2304 * @param {number} opt_timeout The amount of time to wait before fading out
2305 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2306 * stay up forever (or until the next overlay).
2307 */
2308hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002309 if (!this.overlayNode_) {
2310 if (!this.div_)
2311 return;
2312
2313 this.overlayNode_ = this.document_.createElement('div');
2314 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002315 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002316 'font-size: xx-large;' +
2317 'opacity: 0.75;' +
2318 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2319 'position: absolute;' +
2320 '-webkit-user-select: none;' +
2321 '-webkit-transition: opacity 180ms ease-in;');
Robert Ginda70926e42013-11-25 14:56:36 -08002322
2323 this.overlayNode_.addEventListener('mousedown', function(e) {
2324 e.preventDefault();
2325 e.stopPropagation();
2326 }, true);
rgindaf0090c92012-02-10 14:58:52 -08002327 }
2328
rginda9f5222b2012-03-05 11:53:28 -08002329 this.overlayNode_.style.color = this.prefs_.get('background-color');
2330 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2331 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2332
rgindaf0090c92012-02-10 14:58:52 -08002333 this.overlayNode_.textContent = msg;
2334 this.overlayNode_.style.opacity = '0.75';
2335
2336 if (!this.overlayNode_.parentNode)
2337 this.div_.appendChild(this.overlayNode_);
2338
Robert Ginda97769282013-02-01 15:30:30 -08002339 var divSize = hterm.getClientSize(this.div_);
2340 var overlaySize = hterm.getClientSize(this.overlayNode_);
2341
2342 this.overlayNode_.style.top = (divSize.height - overlaySize.height) / 2;
2343 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
2344 this.scrollPort_.currentScrollbarWidthPx) / 2;
rgindaf0090c92012-02-10 14:58:52 -08002345
2346 var self = this;
2347
2348 if (this.overlayTimeout_)
2349 clearTimeout(this.overlayTimeout_);
2350
rgindacc2996c2012-02-24 14:59:31 -08002351 if (opt_timeout === null)
2352 return;
2353
rgindaf0090c92012-02-10 14:58:52 -08002354 this.overlayTimeout_ = setTimeout(function() {
2355 self.overlayNode_.style.opacity = '0';
Robert Ginda70926e42013-11-25 14:56:36 -08002356 self.overlayTimeout_ = setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002357 if (self.overlayNode_.parentNode)
2358 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002359 self.overlayTimeout_ = null;
2360 self.overlayNode_.style.opacity = '0.75';
2361 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002362 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002363};
2364
rginda4bba5e12012-06-20 16:15:30 -07002365/**
2366 * Paste from the system clipboard to the terminal.
2367 */
2368hterm.Terminal.prototype.paste = function() {
2369 hterm.pasteFromClipboard(this.document_);
2370};
2371
2372/**
2373 * Copy a string to the system clipboard.
2374 *
2375 * Note: If there is a selected range in the terminal, it'll be cleared.
2376 */
2377hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda9fb38222012-09-11 14:19:12 -07002378 if (this.prefs_.get('enable-clipboard-notice'))
Robert Gindab4839c22013-02-28 16:52:10 -08002379 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
rgindaa09e7332012-08-17 12:49:51 -07002380
2381 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002382 copySource.textContent = str;
2383 copySource.style.cssText = (
2384 '-webkit-user-select: text;' +
2385 'position: absolute;' +
2386 'top: -99px');
2387
2388 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002389
rginda4bba5e12012-06-20 16:15:30 -07002390 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002391 var anchorNode = selection.anchorNode;
2392 var anchorOffset = selection.anchorOffset;
2393 var focusNode = selection.focusNode;
2394 var focusOffset = selection.focusOffset;
2395
rginda4bba5e12012-06-20 16:15:30 -07002396 selection.selectAllChildren(copySource);
2397
rgindaa09e7332012-08-17 12:49:51 -07002398 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002399
rgindafaa74742012-08-21 13:34:03 -07002400 selection.collapse(anchorNode, anchorOffset);
2401 selection.extend(focusNode, focusOffset);
2402
rginda4bba5e12012-06-20 16:15:30 -07002403 copySource.parentNode.removeChild(copySource);
2404};
2405
rgindaa09e7332012-08-17 12:49:51 -07002406hterm.Terminal.prototype.getSelectionText = function() {
2407 var selection = this.scrollPort_.selection;
2408 selection.sync();
2409
2410 if (selection.isCollapsed)
2411 return null;
2412
2413
2414 // Start offset measures from the beginning of the line.
2415 var startOffset = selection.startOffset;
2416 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002417
Robert Gindafdbb3f22012-09-06 20:23:06 -07002418 if (node.nodeName != 'X-ROW') {
2419 // If the selection doesn't start on an x-row node, then it must be
2420 // somewhere inside the x-row. Add any characters from previous siblings
2421 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002422
2423 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2424 // If node is the text node in a styled span, move up to the span node.
2425 node = node.parentNode;
2426 }
2427
Robert Gindafdbb3f22012-09-06 20:23:06 -07002428 while (node.previousSibling) {
2429 node = node.previousSibling;
2430 startOffset += node.textContent.length;
2431 }
rgindaa09e7332012-08-17 12:49:51 -07002432 }
2433
2434 // End offset measures from the end of the line.
2435 var endOffset = selection.endNode.textContent.length - selection.endOffset;
2436 var node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002437
Robert Gindafdbb3f22012-09-06 20:23:06 -07002438 if (node.nodeName != 'X-ROW') {
2439 // If the selection doesn't end on an x-row node, then it must be
2440 // somewhere inside the x-row. Add any characters from following siblings
2441 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002442
2443 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2444 // If node is the text node in a styled span, move up to the span node.
2445 node = node.parentNode;
2446 }
2447
Robert Gindafdbb3f22012-09-06 20:23:06 -07002448 while (node.nextSibling) {
2449 node = node.nextSibling;
2450 endOffset += node.textContent.length;
2451 }
rgindaa09e7332012-08-17 12:49:51 -07002452 }
2453
2454 var rv = this.getRowsText(selection.startRow.rowIndex,
2455 selection.endRow.rowIndex + 1);
2456 return rv.substring(startOffset, rv.length - endOffset);
2457};
2458
rginda4bba5e12012-06-20 16:15:30 -07002459/**
2460 * Copy the current selection to the system clipboard, then clear it after a
2461 * short delay.
2462 */
2463hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002464 var text = this.getSelectionText();
2465 if (text != null)
2466 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002467};
2468
rgindaf0090c92012-02-10 14:58:52 -08002469hterm.Terminal.prototype.overlaySize = function() {
2470 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2471};
2472
rginda87b86462011-12-14 13:48:03 -08002473/**
2474 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2475 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07002476 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08002477 */
2478hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002479 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002480 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2481
Robert Ginda8cb7d902013-06-20 14:37:18 -07002482 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08002483};
2484
2485/**
rgindad5613292012-06-19 15:40:37 -07002486 * Add the terminalRow and terminalColumn properties to mouse events and
2487 * then forward on to onMouse().
2488 *
2489 * The terminalRow and terminalColumn properties contain the (row, column)
2490 * coordinates for the mouse event.
2491 */
2492hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002493 if (e.processedByTerminalHandler_) {
2494 // We register our event handlers on the document, as well as the cursor
2495 // and the scroll blocker. Mouse events that occur on the cursor or
2496 // scroll blocker will also appear on the document, but we don't want to
2497 // process them twice.
2498 //
2499 // We can't just prevent bubbling because that has other side effects, so
2500 // we decorate the event object with this property instead.
2501 return;
2502 }
2503
2504 e.processedByTerminalHandler_ = true;
2505
John Macinnesfb683832013-07-22 14:46:30 -04002506 if (e.type == 'dblclick') {
2507 this.screen_.expandSelection(this.document_.getSelection());
2508 hterm.copySelectionToClipboard(this.document_);
2509 return;
2510 }
2511
rginda4bba5e12012-06-20 16:15:30 -07002512 if (e.type == 'mousedown' && e.which == this.mousePasteButton) {
2513 this.paste();
2514 return;
2515 }
2516
2517 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2518 !this.document_.getSelection().isCollapsed) {
rgindafaa74742012-08-21 13:34:03 -07002519 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002520 return;
2521 }
2522
rgindad5613292012-06-19 15:40:37 -07002523 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2524 this.scrollPort_.characterSize.height) + 1;
2525 e.terminalColumn = parseInt(e.clientX /
2526 this.scrollPort_.characterSize.width) + 1;
2527
2528 if (e.type == 'mousedown') {
2529 if (e.terminalColumn > this.screenSize.width) {
2530 // Mousedown in the scrollbar area.
2531 return;
2532 }
2533
2534 if (!this.enableMouseDragScroll) {
2535 // Move the scroll-blocker into place if we want to keep the scrollport
2536 // from scrolling.
2537 this.scrollBlockerNode_.engaged = true;
2538 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2539 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2540 }
2541 } else if (this.scrollBlockerNode_.engaged &&
2542 (e.type == 'mousemove' || e.type == 'mouseup')) {
2543 // Disengage the scroll-blocker after one of these events.
2544 this.scrollBlockerNode_.engaged = false;
2545 this.scrollBlockerNode_.style.top = '-99px';
2546 }
2547
rgindafaa74742012-08-21 13:34:03 -07002548 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07002549};
2550
2551/**
2552 * Clients should override this if they care to know about mouse events.
2553 *
2554 * The event parameter will be a normal DOM mouse click event with additional
2555 * 'terminalRow' and 'terminalColumn' properties.
2556 */
2557hterm.Terminal.prototype.onMouse = function(e) { };
2558
2559/**
rginda8e92a692012-05-20 19:37:20 -07002560 * React when focus changes.
2561 */
2562hterm.Terminal.prototype.onFocusChange_ = function(state) {
2563 this.cursorNode_.setAttribute('focus', state ? 'true' : 'false');
Robert Ginda830583c2013-08-07 13:20:46 -07002564 this.restyleCursor_();
rginda8e92a692012-05-20 19:37:20 -07002565};
2566
2567/**
rginda8ba33642011-12-14 12:31:31 -08002568 * React when the ScrollPort is scrolled.
2569 */
2570hterm.Terminal.prototype.onScroll_ = function() {
2571 this.scheduleSyncCursorPosition_();
2572};
2573
2574/**
rginda9846e2f2012-01-27 13:53:33 -08002575 * React when text is pasted into the scrollPort.
2576 */
2577hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Ginda8cb7d902013-06-20 14:37:18 -07002578 this.onVTKeystroke(e.text.replace(/\n/mg, '\r'));
rginda9846e2f2012-01-27 13:53:33 -08002579};
2580
2581/**
rgindaa09e7332012-08-17 12:49:51 -07002582 * React when the user tries to copy from the scrollPort.
2583 */
2584hterm.Terminal.prototype.onCopy_ = function(e) {
2585 e.preventDefault();
rgindafaa74742012-08-21 13:34:03 -07002586 this.copySelectionToClipboard();
rgindaa09e7332012-08-17 12:49:51 -07002587};
2588
2589/**
rginda8ba33642011-12-14 12:31:31 -08002590 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002591 *
2592 * Note: This function should not directly contain code that alters the internal
2593 * state of the terminal. That kind of code belongs in realizeWidth or
2594 * realizeHeight, so that it can be executed synchronously in the case of a
2595 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002596 */
2597hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08002598 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002599 this.scrollPort_.characterSize.width);
2600 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
2601 this.scrollPort_.characterSize.height);
2602
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002603 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08002604 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002605 // gets removed from the document or during the initial load, and we can't
2606 // deal with that.
rginda35c456b2012-02-09 17:29:05 -08002607 return;
2608 }
2609
rgindaa8ba17d2012-08-15 14:41:10 -07002610 var isNewSize = (columnCount != this.screenSize.width ||
2611 rowCount != this.screenSize.height);
2612
2613 // We do this even if the size didn't change, just to be sure everything is
2614 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002615 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07002616 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07002617
2618 if (isNewSize)
2619 this.overlaySize();
2620
2621 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08002622};
2623
2624/**
2625 * Service the cursor blink timeout.
2626 */
2627hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Ginda830583c2013-08-07 13:20:46 -07002628 if (this.cursorNode_.getAttribute('focus') == 'false' ||
2629 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08002630 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002631 } else {
rginda87b86462011-12-14 13:48:03 -08002632 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002633 }
2634};
David Reveman8f552492012-03-28 12:18:41 -04002635
2636/**
2637 * Set the scrollbar-visible mode bit.
2638 *
2639 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2640 * Otherwise it will not.
2641 *
2642 * Defaults to on.
2643 *
2644 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2645 */
2646hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2647 this.scrollPort_.setScrollbarVisible(state);
2648};