blob: aaf05e6f5da0d635a62b1207810c5d5acfb868bc [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() {
518 this.keyboard.installKeyboard(this.document_.body.firstChild);
519}
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);
1060 this.document_.body.firstChild.addEventListener('mousedown', onMouse);
1061 this.document_.body.firstChild.addEventListener('mouseup', onMouse);
1062 this.document_.body.firstChild.addEventListener('mousemove', onMouse);
1063 this.scrollPort_.onScrollWheel = onMouse;
1064
rginda8e92a692012-05-20 19:37:20 -07001065 this.document_.body.firstChild.addEventListener(
1066 'focus', this.onFocusChange_.bind(this, true));
1067 this.document_.body.firstChild.addEventListener(
1068 'blur', this.onFocusChange_.bind(this, false));
1069
1070 var style = this.document_.createElement('style');
1071 style.textContent =
1072 ('.cursor-node[focus="false"] {' +
1073 ' box-sizing: border-box;' +
1074 ' background-color: transparent !important;' +
1075 ' border-width: 2px;' +
1076 ' border-style: solid;' +
1077 '}');
1078 this.document_.head.appendChild(style);
1079
rginda8ba33642011-12-14 12:31:31 -08001080 this.cursorNode_ = this.document_.createElement('div');
rginda8e92a692012-05-20 19:37:20 -07001081 this.cursorNode_.className = 'cursor-node';
rginda8ba33642011-12-14 12:31:31 -08001082 this.cursorNode_.style.cssText =
1083 ('position: absolute;' +
rginda87b86462011-12-14 13:48:03 -08001084 'top: -99px;' +
1085 'display: block;' +
rginda35c456b2012-02-09 17:29:05 -08001086 'width: ' + this.scrollPort_.characterSize.width + 'px;' +
1087 'height: ' + this.scrollPort_.characterSize.height + 'px;' +
rginda8e92a692012-05-20 19:37:20 -07001088 '-webkit-transition: opacity, background-color 100ms linear;');
1089 this.setCursorColor(this.prefs_.get('cursor-color'));
rgindad5613292012-06-19 15:40:37 -07001090
rginda8ba33642011-12-14 12:31:31 -08001091 this.document_.body.appendChild(this.cursorNode_);
1092
rgindad5613292012-06-19 15:40:37 -07001093 // When 'enableMouseDragScroll' is off we reposition this element directly
1094 // under the mouse cursor after a click. This makes Chrome associate
1095 // subsequent mousemove events with the scroll-blocker. Since the
1096 // scroll-blocker is a peer (not a child) of the scrollport, the mousemove
1097 // events do not cause the scrollport to scroll.
1098 //
1099 // It's a hack, but it's the cleanest way I could find.
1100 this.scrollBlockerNode_ = this.document_.createElement('div');
1101 this.scrollBlockerNode_.style.cssText =
1102 ('position: absolute;' +
1103 'top: -99px;' +
1104 'display: block;' +
1105 'width: 10px;' +
1106 'height: 10px;');
1107 this.document_.body.appendChild(this.scrollBlockerNode_);
1108
1109 var onMouse = this.onMouse_.bind(this);
1110 this.scrollPort_.onScrollWheel = onMouse;
1111 ['mousedown', 'mouseup', 'mousemove', 'click', 'dblclick',
1112 ].forEach(function(event) {
1113 this.scrollBlockerNode_.addEventListener(event, onMouse);
1114 this.cursorNode_.addEventListener(event, onMouse);
1115 this.document_.addEventListener(event, onMouse);
1116 }.bind(this));
1117
1118 this.cursorNode_.addEventListener('mousedown', function() {
1119 setTimeout(this.focus.bind(this));
1120 }.bind(this));
1121
rgindade84e382012-04-20 15:39:31 -07001122 this.setCursorBlink(!!this.prefs_.get('cursor-blink'));
rginda8ba33642011-12-14 12:31:31 -08001123 this.setReverseVideo(false);
rginda87b86462011-12-14 13:48:03 -08001124
rginda87b86462011-12-14 13:48:03 -08001125 this.scrollPort_.focus();
rginda6d397402012-01-17 10:58:29 -08001126 this.scrollPort_.scheduleRedraw();
rginda87b86462011-12-14 13:48:03 -08001127};
1128
rginda0918b652012-04-04 11:26:24 -07001129/**
1130 * Return the HTML document that contains the terminal DOM nodes.
1131 */
rginda87b86462011-12-14 13:48:03 -08001132hterm.Terminal.prototype.getDocument = function() {
1133 return this.document_;
rginda8ba33642011-12-14 12:31:31 -08001134};
1135
1136/**
rginda0918b652012-04-04 11:26:24 -07001137 * Focus the terminal.
1138 */
1139hterm.Terminal.prototype.focus = function() {
1140 this.scrollPort_.focus();
1141};
1142
1143/**
rginda8ba33642011-12-14 12:31:31 -08001144 * Return the HTML Element for a given row index.
1145 *
1146 * This is a method from the RowProvider interface. The ScrollPort uses
1147 * it to fetch rows on demand as they are scrolled into view.
1148 *
1149 * TODO(rginda): Consider saving scrollback rows as (HTML source, text content)
1150 * pairs to conserve memory.
1151 *
1152 * @param {integer} index The zero-based row index, measured relative to the
1153 * start of the scrollback buffer. On-screen rows will always have the
1154 * largest indicies.
1155 * @return {HTMLElement} The 'x-row' element containing for the requested row.
1156 */
1157hterm.Terminal.prototype.getRowNode = function(index) {
1158 if (index < this.scrollbackRows_.length)
1159 return this.scrollbackRows_[index];
1160
1161 var screenIndex = index - this.scrollbackRows_.length;
1162 return this.screen_.rowsArray[screenIndex];
1163};
1164
1165/**
1166 * Return the text content for a given range of rows.
1167 *
1168 * This is a method from the RowProvider interface. The ScrollPort uses
1169 * it to fetch text content on demand when the user attempts to copy their
1170 * selection to the clipboard.
1171 *
1172 * @param {integer} start The zero-based row index to start from, measured
1173 * relative to the start of the scrollback buffer. On-screen rows will
1174 * always have the largest indicies.
1175 * @param {integer} end The zero-based row index to end on, measured
1176 * relative to the start of the scrollback buffer.
1177 * @return {string} A single string containing the text value of the range of
1178 * rows. Lines will be newline delimited, with no trailing newline.
1179 */
1180hterm.Terminal.prototype.getRowsText = function(start, end) {
1181 var ary = [];
1182 for (var i = start; i < end; i++) {
1183 var node = this.getRowNode(i);
1184 ary.push(node.textContent);
rgindaa09e7332012-08-17 12:49:51 -07001185 if (i < end - 1 && !node.getAttribute('line-overflow'))
1186 ary.push('\n');
rginda8ba33642011-12-14 12:31:31 -08001187 }
1188
rgindaa09e7332012-08-17 12:49:51 -07001189 return ary.join('');
rginda8ba33642011-12-14 12:31:31 -08001190};
1191
1192/**
1193 * Return the text content for a given row.
1194 *
1195 * This is a method from the RowProvider interface. The ScrollPort uses
1196 * it to fetch text content on demand when the user attempts to copy their
1197 * selection to the clipboard.
1198 *
1199 * @param {integer} index The zero-based row index to return, measured
1200 * relative to the start of the scrollback buffer. On-screen rows will
1201 * always have the largest indicies.
1202 * @return {string} A string containing the text value of the selected row.
1203 */
1204hterm.Terminal.prototype.getRowText = function(index) {
1205 var node = this.getRowNode(index);
rginda87b86462011-12-14 13:48:03 -08001206 return node.textContent;
rginda8ba33642011-12-14 12:31:31 -08001207};
1208
1209/**
1210 * Return the total number of rows in the addressable screen and in the
1211 * scrollback buffer of this terminal.
1212 *
1213 * This is a method from the RowProvider interface. The ScrollPort uses
1214 * it to compute the size of the scrollbar.
1215 *
1216 * @return {integer} The number of rows in this terminal.
1217 */
1218hterm.Terminal.prototype.getRowCount = function() {
1219 return this.scrollbackRows_.length + this.screen_.rowsArray.length;
1220};
1221
1222/**
1223 * Create DOM nodes for new rows and append them to the end of the terminal.
1224 *
1225 * This is the only correct way to add a new DOM node for a row. Notice that
1226 * the new row is appended to the bottom of the list of rows, and does not
1227 * require renumbering (of the rowIndex property) of previous rows.
1228 *
1229 * If you think you want a new blank row somewhere in the middle of the
1230 * terminal, look into moveRows_().
1231 *
1232 * This method does not pay attention to vtScrollTop/Bottom, since you should
1233 * be using moveRows() in cases where they would matter.
1234 *
1235 * The cursor will be positioned at column 0 of the first inserted line.
1236 */
1237hterm.Terminal.prototype.appendRows_ = function(count) {
1238 var cursorRow = this.screen_.rowsArray.length;
1239 var offset = this.scrollbackRows_.length + cursorRow;
1240 for (var i = 0; i < count; i++) {
1241 var row = this.document_.createElement('x-row');
1242 row.appendChild(this.document_.createTextNode(''));
1243 row.rowIndex = offset + i;
1244 this.screen_.pushRow(row);
1245 }
1246
1247 var extraRows = this.screen_.rowsArray.length - this.screenSize.height;
1248 if (extraRows > 0) {
1249 var ary = this.screen_.shiftRows(extraRows);
1250 Array.prototype.push.apply(this.scrollbackRows_, ary);
Robert Ginda36c5aa62012-10-15 11:17:47 -07001251 if (this.scrollPort_.isScrolledEnd)
1252 this.scheduleScrollDown_();
rginda8ba33642011-12-14 12:31:31 -08001253 }
1254
1255 if (cursorRow >= this.screen_.rowsArray.length)
1256 cursorRow = this.screen_.rowsArray.length - 1;
1257
rginda87b86462011-12-14 13:48:03 -08001258 this.setAbsoluteCursorPosition(cursorRow, 0);
rginda8ba33642011-12-14 12:31:31 -08001259};
1260
1261/**
1262 * Relocate rows from one part of the addressable screen to another.
1263 *
1264 * This is used to recycle rows during VT scrolls (those which are driven
1265 * by VT commands, rather than by the user manipulating the scrollbar.)
1266 *
1267 * In this case, the blank lines scrolled into the scroll region are made of
1268 * the nodes we scrolled off. These have their rowIndex properties carefully
1269 * renumbered so as not to confuse the ScrollPort.
rginda8ba33642011-12-14 12:31:31 -08001270 */
1271hterm.Terminal.prototype.moveRows_ = function(fromIndex, count, toIndex) {
1272 var ary = this.screen_.removeRows(fromIndex, count);
1273 this.screen_.insertRows(toIndex, ary);
1274
1275 var start, end;
1276 if (fromIndex < toIndex) {
1277 start = fromIndex;
rginda87b86462011-12-14 13:48:03 -08001278 end = toIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001279 } else {
1280 start = toIndex;
rginda87b86462011-12-14 13:48:03 -08001281 end = fromIndex + count;
rginda8ba33642011-12-14 12:31:31 -08001282 }
1283
1284 this.renumberRows_(start, end);
rginda2312fff2012-01-05 16:20:52 -08001285 this.scrollPort_.scheduleInvalidate();
rginda8ba33642011-12-14 12:31:31 -08001286};
1287
1288/**
1289 * Renumber the rowIndex property of the given range of rows.
1290 *
1291 * The start and end indicies are relative to the screen, not the scrollback.
1292 * Rows in the scrollback buffer cannot be renumbered. Since they are not
rginda2312fff2012-01-05 16:20:52 -08001293 * addressable (you can't delete them, scroll them, etc), you should have
rginda8ba33642011-12-14 12:31:31 -08001294 * no need to renumber scrollback rows.
1295 */
Robert Ginda40932892012-12-10 17:26:40 -08001296hterm.Terminal.prototype.renumberRows_ = function(start, end, opt_screen) {
1297 var screen = opt_screen || this.screen_;
1298
rginda8ba33642011-12-14 12:31:31 -08001299 var offset = this.scrollbackRows_.length;
1300 for (var i = start; i < end; i++) {
Robert Ginda40932892012-12-10 17:26:40 -08001301 screen.rowsArray[i].rowIndex = offset + i;
rginda8ba33642011-12-14 12:31:31 -08001302 }
1303};
1304
1305/**
1306 * Print a string to the terminal.
1307 *
1308 * This respects the current insert and wraparound modes. It will add new lines
1309 * to the end of the terminal, scrolling off the top into the scrollback buffer
1310 * if necessary.
1311 *
1312 * The string is *not* parsed for escape codes. Use the interpret() method if
1313 * that's what you're after.
1314 *
1315 * @param{string} str The string to print.
1316 */
1317hterm.Terminal.prototype.print = function(str) {
rgindaa9abdd82012-08-06 18:05:09 -07001318 var startOffset = 0;
rginda2312fff2012-01-05 16:20:52 -08001319
rgindaa9abdd82012-08-06 18:05:09 -07001320 while (startOffset < str.length) {
rgindaa09e7332012-08-17 12:49:51 -07001321 if (this.options_.wraparound && this.screen_.cursorPosition.overflow) {
1322 this.screen_.commitLineOverflow();
rginda35c456b2012-02-09 17:29:05 -08001323 this.newLine();
rgindaa09e7332012-08-17 12:49:51 -07001324 }
rgindaa19afe22012-01-25 15:40:22 -08001325
rgindaa9abdd82012-08-06 18:05:09 -07001326 var count = str.length - startOffset;
1327 var didOverflow = false;
1328 var substr;
rgindaa19afe22012-01-25 15:40:22 -08001329
rgindaa9abdd82012-08-06 18:05:09 -07001330 if (this.screen_.cursorPosition.column + count >= this.screenSize.width) {
1331 didOverflow = true;
1332 count = this.screenSize.width - this.screen_.cursorPosition.column;
1333 }
rgindaa19afe22012-01-25 15:40:22 -08001334
rgindaa9abdd82012-08-06 18:05:09 -07001335 if (didOverflow && !this.options_.wraparound) {
1336 // If the string overflowed the line but wraparound is off, then the
1337 // last printed character should be the last of the string.
1338 // TODO: This will add to our problems with multibyte UTF-16 characters.
1339 substr = str.substr(startOffset, count - 1) +
1340 str.substr(str.length - 1);
1341 count = str.length;
1342 } else {
1343 substr = str.substr(startOffset, count);
1344 }
rgindaa19afe22012-01-25 15:40:22 -08001345
rgindaa9abdd82012-08-06 18:05:09 -07001346 if (this.options_.insertMode) {
1347 this.screen_.insertString(substr);
1348 } else {
1349 this.screen_.overwriteString(substr);
1350 }
1351
1352 this.screen_.maybeClipCurrentRow();
1353 startOffset += count;
rgindaa19afe22012-01-25 15:40:22 -08001354 }
rginda8ba33642011-12-14 12:31:31 -08001355
1356 this.scheduleSyncCursorPosition_();
rginda0f5c0292012-01-13 11:00:13 -08001357
rginda9f5222b2012-03-05 11:53:28 -08001358 if (this.scrollOnOutput_)
rginda0f5c0292012-01-13 11:00:13 -08001359 this.scrollPort_.scrollRowToBottom(this.getRowCount());
rginda8ba33642011-12-14 12:31:31 -08001360};
1361
1362/**
rginda87b86462011-12-14 13:48:03 -08001363 * Set the VT scroll region.
1364 *
rginda87b86462011-12-14 13:48:03 -08001365 * This also resets the cursor position to the absolute (0, 0) position, since
1366 * that's what xterm appears to do.
1367 *
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001368 * Setting the scroll region to the full height of the terminal will clear
1369 * the scroll region. This is *NOT* what most terminals do. We're explicitly
1370 * going "off-spec" here because it makes `screen` and `tmux` overflow into the
1371 * local scrollback buffer, which means the scrollbars and shift-pgup/pgdn
1372 * continue to work as most users would expect.
1373 *
rginda87b86462011-12-14 13:48:03 -08001374 * @param {integer} scrollTop The zero-based top of the scroll region.
1375 * @param {integer} scrollBottom The zero-based bottom of the scroll region,
1376 * inclusive.
1377 */
1378hterm.Terminal.prototype.setVTScrollRegion = function(scrollTop, scrollBottom) {
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001379 if (scrollTop == 0 && scrollBottom == this.screenSize.height - 1) {
Robert Ginda43684e22013-11-25 14:18:52 -08001380 this.vtScrollTop_ = null;
1381 this.vtScrollBottom_ = null;
Robert Ginda5b9fbe62013-10-30 14:05:53 -07001382 } else {
1383 this.vtScrollTop_ = scrollTop;
1384 this.vtScrollBottom_ = scrollBottom;
1385 }
rginda87b86462011-12-14 13:48:03 -08001386};
1387
1388/**
rginda8ba33642011-12-14 12:31:31 -08001389 * Return the top row index according to the VT.
1390 *
1391 * This will return 0 unless the terminal has been told to restrict scrolling
1392 * to some lower row. It is used for some VT cursor positioning and scrolling
1393 * commands.
1394 *
1395 * @return {integer} The topmost row in the terminal's scroll region.
1396 */
1397hterm.Terminal.prototype.getVTScrollTop = function() {
1398 if (this.vtScrollTop_ != null)
1399 return this.vtScrollTop_;
1400
1401 return 0;
rginda87b86462011-12-14 13:48:03 -08001402};
rginda8ba33642011-12-14 12:31:31 -08001403
1404/**
1405 * Return the bottom row index according to the VT.
1406 *
1407 * This will return the height of the terminal unless the it has been told to
1408 * restrict scrolling to some higher row. It is used for some VT cursor
1409 * positioning and scrolling commands.
1410 *
1411 * @return {integer} The bottommost row in the terminal's scroll region.
1412 */
1413hterm.Terminal.prototype.getVTScrollBottom = function() {
1414 if (this.vtScrollBottom_ != null)
1415 return this.vtScrollBottom_;
1416
rginda87b86462011-12-14 13:48:03 -08001417 return this.screenSize.height - 1;
rginda8ba33642011-12-14 12:31:31 -08001418}
1419
1420/**
1421 * Process a '\n' character.
1422 *
1423 * If the cursor is on the final row of the terminal this will append a new
1424 * blank row to the screen and scroll the topmost row into the scrollback
1425 * buffer.
1426 *
1427 * Otherwise, this moves the cursor to column zero of the next row.
1428 */
1429hterm.Terminal.prototype.newLine = function() {
Robert Ginda9937abc2013-07-25 16:09:23 -07001430 var cursorAtEndOfScreen = (this.screen_.cursorPosition.row ==
1431 this.screen_.rowsArray.length - 1);
1432
1433 if (this.vtScrollBottom_ != null) {
1434 // A VT Scroll region is active, we never append new rows.
1435 if (this.screen_.cursorPosition.row == this.vtScrollBottom_) {
1436 // We're at the end of the VT Scroll Region, perform a VT scroll.
1437 this.vtScrollUp(1);
1438 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1439 } else if (cursorAtEndOfScreen) {
1440 // We're at the end of the screen, the only thing to do is put the
1441 // cursor to column 0.
1442 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, 0);
1443 } else {
1444 // Anywhere else, advance the cursor row, and reset the column.
1445 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
1446 }
1447 } else if (cursorAtEndOfScreen) {
Robert Ginda1b06b372013-07-19 15:22:51 -07001448 // We're at the end of the screen. Append a new row to the terminal,
1449 // shifting the top row into the scrollback.
1450 this.appendRows_(1);
rginda8ba33642011-12-14 12:31:31 -08001451 } else {
rginda87b86462011-12-14 13:48:03 -08001452 // Anywhere else in the screen just moves the cursor.
1453 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row + 1, 0);
rginda8ba33642011-12-14 12:31:31 -08001454 }
1455};
1456
1457/**
1458 * Like newLine(), except maintain the cursor column.
1459 */
1460hterm.Terminal.prototype.lineFeed = function() {
1461 var column = this.screen_.cursorPosition.column;
1462 this.newLine();
1463 this.setCursorColumn(column);
1464};
1465
1466/**
rginda87b86462011-12-14 13:48:03 -08001467 * If autoCarriageReturn is set then newLine(), else lineFeed().
1468 */
1469hterm.Terminal.prototype.formFeed = function() {
1470 if (this.options_.autoCarriageReturn) {
1471 this.newLine();
1472 } else {
1473 this.lineFeed();
1474 }
1475};
1476
1477/**
1478 * Move the cursor up one row, possibly inserting a blank line.
1479 *
1480 * The cursor column is not changed.
1481 */
1482hterm.Terminal.prototype.reverseLineFeed = function() {
1483 var scrollTop = this.getVTScrollTop();
1484 var currentRow = this.screen_.cursorPosition.row;
1485
1486 if (currentRow == scrollTop) {
1487 this.insertLines(1);
1488 } else {
1489 this.setAbsoluteCursorRow(currentRow - 1);
1490 }
1491};
1492
1493/**
rginda8ba33642011-12-14 12:31:31 -08001494 * Replace all characters to the left of the current cursor with the space
1495 * character.
1496 *
1497 * TODO(rginda): This should probably *remove* the characters (not just replace
1498 * with a space) if there are no characters at or beyond the current cursor
Robert Gindaf2547f12012-10-25 20:36:21 -07001499 * position.
rginda8ba33642011-12-14 12:31:31 -08001500 */
1501hterm.Terminal.prototype.eraseToLeft = function() {
rginda87b86462011-12-14 13:48:03 -08001502 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001503 this.setCursorColumn(0);
rgindacbbd7482012-06-13 15:06:16 -07001504 this.screen_.overwriteString(lib.f.getWhitespace(cursor.column + 1));
rginda87b86462011-12-14 13:48:03 -08001505 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001506};
1507
1508/**
David Benjamin684a9b72012-05-01 17:19:58 -04001509 * Erase a given number of characters to the right of the cursor.
rginda8ba33642011-12-14 12:31:31 -08001510 *
1511 * The cursor position is unchanged.
1512 *
Robert Gindaf2547f12012-10-25 20:36:21 -07001513 * If the current background color is not the default background color this
1514 * will insert spaces rather than delete. This is unfortunate because the
1515 * trailing space will affect text selection, but it's difficult to come up
1516 * with a way to style empty space that wouldn't trip up the hterm.Screen
1517 * code.
Robert Gindacd5637d2013-10-30 14:59:10 -07001518 *
1519 * eraseToRight is ignored in the presence of a cursor overflow. This deviates
1520 * from xterm, but agrees with gnome-terminal and konsole, xfce4-terminal. See
1521 * crbug.com/232390 for details.
rginda8ba33642011-12-14 12:31:31 -08001522 */
1523hterm.Terminal.prototype.eraseToRight = function(opt_count) {
Robert Gindacd5637d2013-10-30 14:59:10 -07001524 if (this.screen_.cursorPosition.overflow)
1525 return;
1526
Robert Ginda7fd57082012-09-25 14:41:47 -07001527 var maxCount = this.screenSize.width - this.screen_.cursorPosition.column;
1528 var count = opt_count ? Math.min(opt_count, maxCount) : maxCount;
Robert Gindaf2547f12012-10-25 20:36:21 -07001529
1530 if (this.screen_.textAttributes.background ===
1531 this.screen_.textAttributes.DEFAULT_COLOR) {
1532 var cursorRow = this.screen_.rowsArray[this.screen_.cursorPosition.row];
1533 if (cursorRow.textContent.length <=
1534 this.screen_.cursorPosition.column + count) {
1535 this.screen_.deleteChars(count);
1536 this.clearCursorOverflow();
1537 return;
1538 }
1539 }
1540
rginda87b86462011-12-14 13:48:03 -08001541 var cursor = this.saveCursor();
Robert Ginda7fd57082012-09-25 14:41:47 -07001542 this.screen_.overwriteString(lib.f.getWhitespace(count));
rginda87b86462011-12-14 13:48:03 -08001543 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001544 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001545};
1546
1547/**
1548 * Erase the current line.
1549 *
1550 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001551 */
1552hterm.Terminal.prototype.eraseLine = function() {
rginda87b86462011-12-14 13:48:03 -08001553 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001554 this.screen_.clearCursorRow();
rginda87b86462011-12-14 13:48:03 -08001555 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001556 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001557};
1558
1559/**
David Benjamina08d78f2012-05-05 00:28:49 -04001560 * Erase all characters from the start of the screen to the current cursor
1561 * position, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001562 *
1563 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001564 */
1565hterm.Terminal.prototype.eraseAbove = function() {
rginda87b86462011-12-14 13:48:03 -08001566 var cursor = this.saveCursor();
1567
1568 this.eraseToLeft();
rginda8ba33642011-12-14 12:31:31 -08001569
David Benjamina08d78f2012-05-05 00:28:49 -04001570 for (var i = 0; i < cursor.row; i++) {
rginda87b86462011-12-14 13:48:03 -08001571 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001572 this.screen_.clearCursorRow();
1573 }
1574
rginda87b86462011-12-14 13:48:03 -08001575 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001576 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001577};
1578
1579/**
1580 * Erase all characters from the current cursor position to the end of the
David Benjamina08d78f2012-05-05 00:28:49 -04001581 * screen, regardless of scroll region.
rginda8ba33642011-12-14 12:31:31 -08001582 *
1583 * The cursor position is unchanged.
rginda8ba33642011-12-14 12:31:31 -08001584 */
1585hterm.Terminal.prototype.eraseBelow = function() {
rginda87b86462011-12-14 13:48:03 -08001586 var cursor = this.saveCursor();
1587
1588 this.eraseToRight();
rginda8ba33642011-12-14 12:31:31 -08001589
David Benjamina08d78f2012-05-05 00:28:49 -04001590 var bottom = this.screenSize.height - 1;
rginda87b86462011-12-14 13:48:03 -08001591 for (var i = cursor.row + 1; i <= bottom; i++) {
1592 this.setAbsoluteCursorPosition(i, 0);
rginda8ba33642011-12-14 12:31:31 -08001593 this.screen_.clearCursorRow();
1594 }
1595
rginda87b86462011-12-14 13:48:03 -08001596 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001597 this.clearCursorOverflow();
rginda87b86462011-12-14 13:48:03 -08001598};
1599
1600/**
1601 * Fill the terminal with a given character.
1602 *
1603 * This methods does not respect the VT scroll region.
1604 *
1605 * @param {string} ch The character to use for the fill.
1606 */
1607hterm.Terminal.prototype.fill = function(ch) {
1608 var cursor = this.saveCursor();
1609
1610 this.setAbsoluteCursorPosition(0, 0);
1611 for (var row = 0; row < this.screenSize.height; row++) {
1612 for (var col = 0; col < this.screenSize.width; col++) {
1613 this.setAbsoluteCursorPosition(row, col);
1614 this.screen_.overwriteString(ch);
1615 }
1616 }
1617
1618 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001619};
1620
1621/**
rginda9ea433c2012-03-16 11:57:00 -07001622 * Erase the entire display and leave the cursor at (0, 0).
rginda8ba33642011-12-14 12:31:31 -08001623 *
rginda9ea433c2012-03-16 11:57:00 -07001624 * This does not respect the scroll region.
1625 *
1626 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1627 * to the current screen.
rginda8ba33642011-12-14 12:31:31 -08001628 */
rginda9ea433c2012-03-16 11:57:00 -07001629hterm.Terminal.prototype.clearHome = function(opt_screen) {
1630 var screen = opt_screen || this.screen_;
1631 var bottom = screen.getHeight();
rginda8ba33642011-12-14 12:31:31 -08001632
rginda11057d52012-04-25 12:29:56 -07001633 if (bottom == 0) {
1634 // Empty screen, nothing to do.
1635 return;
1636 }
1637
rgindae4d29232012-01-19 10:47:13 -08001638 for (var i = 0; i < bottom; i++) {
rginda9ea433c2012-03-16 11:57:00 -07001639 screen.setCursorPosition(i, 0);
1640 screen.clearCursorRow();
rginda8ba33642011-12-14 12:31:31 -08001641 }
1642
rginda9ea433c2012-03-16 11:57:00 -07001643 screen.setCursorPosition(0, 0);
1644};
1645
1646/**
1647 * Erase the entire display without changing the cursor position.
1648 *
1649 * The cursor position is unchanged. This does not respect the scroll
1650 * region.
1651 *
1652 * @param {hterm.Screen} opt_screen Optional screen to operate on. Defaults
1653 * to the current screen.
rginda9ea433c2012-03-16 11:57:00 -07001654 */
1655hterm.Terminal.prototype.clear = function(opt_screen) {
1656 var screen = opt_screen || this.screen_;
1657 var cursor = screen.cursorPosition.clone();
1658 this.clearHome(screen);
1659 screen.setCursorPosition(cursor.row, cursor.column);
rginda8ba33642011-12-14 12:31:31 -08001660};
1661
1662/**
1663 * VT command to insert lines at the current cursor row.
1664 *
1665 * This respects the current scroll region. Rows pushed off the bottom are
1666 * lost (they won't show up in the scrollback buffer).
1667 *
rginda8ba33642011-12-14 12:31:31 -08001668 * @param {integer} count The number of lines to insert.
1669 */
1670hterm.Terminal.prototype.insertLines = function(count) {
Robert Ginda579186b2012-09-26 11:40:04 -07001671 var cursorRow = this.screen_.cursorPosition.row;
rginda8ba33642011-12-14 12:31:31 -08001672
1673 var bottom = this.getVTScrollBottom();
Robert Ginda579186b2012-09-26 11:40:04 -07001674 count = Math.min(count, bottom - cursorRow);
rginda8ba33642011-12-14 12:31:31 -08001675
Robert Ginda579186b2012-09-26 11:40:04 -07001676 // The moveCount is the number of rows we need to relocate to make room for
1677 // the new row(s). The count is the distance to move them.
1678 var moveCount = bottom - cursorRow - count + 1;
1679 if (moveCount)
1680 this.moveRows_(cursorRow, moveCount, cursorRow + count);
rginda8ba33642011-12-14 12:31:31 -08001681
Robert Ginda579186b2012-09-26 11:40:04 -07001682 for (var i = count - 1; i >= 0; i--) {
1683 this.setAbsoluteCursorPosition(cursorRow + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001684 this.screen_.clearCursorRow();
1685 }
rginda8ba33642011-12-14 12:31:31 -08001686};
1687
1688/**
1689 * VT command to delete lines at the current cursor row.
1690 *
1691 * New rows are added to the bottom of scroll region to take their place. New
1692 * rows are strictly there to take up space and have no content or style.
1693 */
1694hterm.Terminal.prototype.deleteLines = function(count) {
rginda87b86462011-12-14 13:48:03 -08001695 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001696
rginda87b86462011-12-14 13:48:03 -08001697 var top = cursor.row;
rginda8ba33642011-12-14 12:31:31 -08001698 var bottom = this.getVTScrollBottom();
1699
rginda87b86462011-12-14 13:48:03 -08001700 var maxCount = bottom - top + 1;
rginda8ba33642011-12-14 12:31:31 -08001701 count = Math.min(count, maxCount);
1702
rginda87b86462011-12-14 13:48:03 -08001703 var moveStart = bottom - count + 1;
rginda8ba33642011-12-14 12:31:31 -08001704 if (count != maxCount)
1705 this.moveRows_(top, count, moveStart);
1706
1707 for (var i = 0; i < count; i++) {
rginda87b86462011-12-14 13:48:03 -08001708 this.setAbsoluteCursorPosition(moveStart + i, 0);
rginda8ba33642011-12-14 12:31:31 -08001709 this.screen_.clearCursorRow();
1710 }
1711
rginda87b86462011-12-14 13:48:03 -08001712 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001713 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001714};
1715
1716/**
1717 * Inserts the given number of spaces at the current cursor position.
1718 *
rginda87b86462011-12-14 13:48:03 -08001719 * The cursor position is not changed.
rginda8ba33642011-12-14 12:31:31 -08001720 */
1721hterm.Terminal.prototype.insertSpace = function(count) {
rginda87b86462011-12-14 13:48:03 -08001722 var cursor = this.saveCursor();
1723
rgindacbbd7482012-06-13 15:06:16 -07001724 var ws = lib.f.getWhitespace(count || 1);
rginda8ba33642011-12-14 12:31:31 -08001725 this.screen_.insertString(ws);
rgindaa19afe22012-01-25 15:40:22 -08001726 this.screen_.maybeClipCurrentRow();
rginda87b86462011-12-14 13:48:03 -08001727
1728 this.restoreCursor(cursor);
David Benjamin54e8bf62012-06-01 22:31:40 -04001729 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001730};
1731
1732/**
1733 * Forward-delete the specified number of characters starting at the cursor
1734 * position.
1735 *
1736 * @param {integer} count The number of characters to delete.
1737 */
1738hterm.Terminal.prototype.deleteChars = function(count) {
Robert Ginda7fd57082012-09-25 14:41:47 -07001739 var deleted = this.screen_.deleteChars(count);
1740 if (deleted && !this.screen_.textAttributes.isDefault()) {
1741 var cursor = this.saveCursor();
1742 this.setCursorColumn(this.screenSize.width - deleted);
1743 this.screen_.insertString(lib.f.getWhitespace(deleted));
1744 this.restoreCursor(cursor);
1745 }
1746
David Benjamin54e8bf62012-06-01 22:31:40 -04001747 this.clearCursorOverflow();
rginda8ba33642011-12-14 12:31:31 -08001748};
1749
1750/**
1751 * Shift rows in the scroll region upwards by a given number of lines.
1752 *
1753 * New rows are inserted at the bottom of the scroll region to fill the
1754 * vacated rows. The new rows not filled out with the current text attributes.
1755 *
1756 * This function does not affect the scrollback rows at all. Rows shifted
1757 * off the top are lost.
1758 *
rginda87b86462011-12-14 13:48:03 -08001759 * The cursor position is not altered.
1760 *
rginda8ba33642011-12-14 12:31:31 -08001761 * @param {integer} count The number of rows to scroll.
1762 */
1763hterm.Terminal.prototype.vtScrollUp = function(count) {
rginda87b86462011-12-14 13:48:03 -08001764 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001765
rginda87b86462011-12-14 13:48:03 -08001766 this.setAbsoluteCursorRow(this.getVTScrollTop());
rginda8ba33642011-12-14 12:31:31 -08001767 this.deleteLines(count);
1768
rginda87b86462011-12-14 13:48:03 -08001769 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001770};
1771
1772/**
1773 * Shift rows below the cursor down by a given number of lines.
1774 *
1775 * This function respects the current scroll region.
1776 *
1777 * New rows are inserted at the top of the scroll region to fill the
1778 * vacated rows. The new rows not filled out with the current text attributes.
1779 *
1780 * This function does not affect the scrollback rows at all. Rows shifted
1781 * off the bottom are lost.
1782 *
1783 * @param {integer} count The number of rows to scroll.
1784 */
1785hterm.Terminal.prototype.vtScrollDown = function(opt_count) {
rginda87b86462011-12-14 13:48:03 -08001786 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08001787
rginda87b86462011-12-14 13:48:03 -08001788 this.setAbsoluteCursorPosition(this.getVTScrollTop(), 0);
rginda8ba33642011-12-14 12:31:31 -08001789 this.insertLines(opt_count);
1790
rginda87b86462011-12-14 13:48:03 -08001791 this.restoreCursor(cursor);
rginda8ba33642011-12-14 12:31:31 -08001792};
1793
rginda87b86462011-12-14 13:48:03 -08001794
rginda8ba33642011-12-14 12:31:31 -08001795/**
1796 * Set the cursor position.
1797 *
1798 * The cursor row is relative to the scroll region if the terminal has
1799 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1800 *
1801 * @param {integer} row The new zero-based cursor row.
1802 * @param {integer} row The new zero-based cursor column.
1803 */
1804hterm.Terminal.prototype.setCursorPosition = function(row, column) {
1805 if (this.options_.originMode) {
rginda87b86462011-12-14 13:48:03 -08001806 this.setRelativeCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001807 } else {
rginda87b86462011-12-14 13:48:03 -08001808 this.setAbsoluteCursorPosition(row, column);
rginda8ba33642011-12-14 12:31:31 -08001809 }
rginda87b86462011-12-14 13:48:03 -08001810};
rginda8ba33642011-12-14 12:31:31 -08001811
rginda87b86462011-12-14 13:48:03 -08001812hterm.Terminal.prototype.setRelativeCursorPosition = function(row, column) {
1813 var scrollTop = this.getVTScrollTop();
rgindacbbd7482012-06-13 15:06:16 -07001814 row = lib.f.clamp(row + scrollTop, scrollTop, this.getVTScrollBottom());
1815 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda87b86462011-12-14 13:48:03 -08001816 this.screen_.setCursorPosition(row, column);
1817};
1818
1819hterm.Terminal.prototype.setAbsoluteCursorPosition = function(row, column) {
rgindacbbd7482012-06-13 15:06:16 -07001820 row = lib.f.clamp(row, 0, this.screenSize.height - 1);
1821 column = lib.f.clamp(column, 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001822 this.screen_.setCursorPosition(row, column);
1823};
1824
1825/**
1826 * Set the cursor column.
1827 *
1828 * @param {integer} column The new zero-based cursor column.
1829 */
1830hterm.Terminal.prototype.setCursorColumn = function(column) {
rginda87b86462011-12-14 13:48:03 -08001831 this.setAbsoluteCursorPosition(this.screen_.cursorPosition.row, column);
rginda8ba33642011-12-14 12:31:31 -08001832};
1833
1834/**
1835 * Return the cursor column.
1836 *
1837 * @return {integer} The zero-based cursor column.
1838 */
1839hterm.Terminal.prototype.getCursorColumn = function() {
1840 return this.screen_.cursorPosition.column;
1841};
1842
1843/**
1844 * Set the cursor row.
1845 *
1846 * The cursor row is relative to the scroll region if the terminal has
1847 * 'origin mode' enabled, or relative to the addressable screen otherwise.
1848 *
1849 * @param {integer} row The new cursor row.
1850 */
rginda87b86462011-12-14 13:48:03 -08001851hterm.Terminal.prototype.setAbsoluteCursorRow = function(row) {
1852 this.setAbsoluteCursorPosition(row, this.screen_.cursorPosition.column);
rginda8ba33642011-12-14 12:31:31 -08001853};
1854
1855/**
1856 * Return the cursor row.
1857 *
1858 * @return {integer} The zero-based cursor row.
1859 */
1860hterm.Terminal.prototype.getCursorRow = function(row) {
1861 return this.screen_.cursorPosition.row;
1862};
1863
1864/**
1865 * Request that the ScrollPort redraw itself soon.
1866 *
1867 * The redraw will happen asynchronously, soon after the call stack winds down.
1868 * Multiple calls will be coalesced into a single redraw.
1869 */
1870hterm.Terminal.prototype.scheduleRedraw_ = function() {
rginda87b86462011-12-14 13:48:03 -08001871 if (this.timeouts_.redraw)
1872 return;
rginda8ba33642011-12-14 12:31:31 -08001873
1874 var self = this;
rginda87b86462011-12-14 13:48:03 -08001875 this.timeouts_.redraw = setTimeout(function() {
1876 delete self.timeouts_.redraw;
rginda8ba33642011-12-14 12:31:31 -08001877 self.scrollPort_.redraw_();
1878 }, 0);
1879};
1880
1881/**
1882 * Request that the ScrollPort be scrolled to the bottom.
1883 *
1884 * The scroll will happen asynchronously, soon after the call stack winds down.
1885 * Multiple calls will be coalesced into a single scroll.
1886 *
1887 * This affects the scrollbar position of the ScrollPort, and has nothing to
1888 * do with the VT scroll commands.
1889 */
1890hterm.Terminal.prototype.scheduleScrollDown_ = function() {
1891 if (this.timeouts_.scrollDown)
rginda87b86462011-12-14 13:48:03 -08001892 return;
rginda8ba33642011-12-14 12:31:31 -08001893
1894 var self = this;
1895 this.timeouts_.scrollDown = setTimeout(function() {
1896 delete self.timeouts_.scrollDown;
1897 self.scrollPort_.scrollRowToBottom(self.getRowCount());
1898 }, 10);
1899};
1900
1901/**
1902 * Move the cursor up a specified number of rows.
1903 *
1904 * @param {integer} count The number of rows to move the cursor.
1905 */
1906hterm.Terminal.prototype.cursorUp = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001907 return this.cursorDown(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001908};
1909
1910/**
1911 * Move the cursor down a specified number of rows.
1912 *
1913 * @param {integer} count The number of rows to move the cursor.
1914 */
1915hterm.Terminal.prototype.cursorDown = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001916 count = count || 1;
rginda8ba33642011-12-14 12:31:31 -08001917 var minHeight = (this.options_.originMode ? this.getVTScrollTop() : 0);
1918 var maxHeight = (this.options_.originMode ? this.getVTScrollBottom() :
1919 this.screenSize.height - 1);
1920
rgindacbbd7482012-06-13 15:06:16 -07001921 var row = lib.f.clamp(this.screen_.cursorPosition.row + count,
rginda8ba33642011-12-14 12:31:31 -08001922 minHeight, maxHeight);
rginda87b86462011-12-14 13:48:03 -08001923 this.setAbsoluteCursorRow(row);
rginda8ba33642011-12-14 12:31:31 -08001924};
1925
1926/**
1927 * Move the cursor left a specified number of columns.
1928 *
1929 * @param {integer} count The number of columns to move the cursor.
1930 */
1931hterm.Terminal.prototype.cursorLeft = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001932 return this.cursorRight(-(count || 1));
rginda8ba33642011-12-14 12:31:31 -08001933};
1934
1935/**
1936 * Move the cursor right a specified number of columns.
1937 *
1938 * @param {integer} count The number of columns to move the cursor.
1939 */
1940hterm.Terminal.prototype.cursorRight = function(count) {
rginda0f5c0292012-01-13 11:00:13 -08001941 count = count || 1;
rgindacbbd7482012-06-13 15:06:16 -07001942 var column = lib.f.clamp(this.screen_.cursorPosition.column + count,
rginda87b86462011-12-14 13:48:03 -08001943 0, this.screenSize.width - 1);
rginda8ba33642011-12-14 12:31:31 -08001944 this.setCursorColumn(column);
1945};
1946
1947/**
1948 * Reverse the foreground and background colors of the terminal.
1949 *
1950 * This only affects text that was drawn with no attributes.
1951 *
1952 * TODO(rginda): Test xterm to see if reverse is respected for text that has
1953 * been drawn with attributes that happen to coincide with the default
1954 * 'no-attribute' colors. My guess is probably not.
1955 */
1956hterm.Terminal.prototype.setReverseVideo = function(state) {
rginda87b86462011-12-14 13:48:03 -08001957 this.options_.reverseVideo = state;
rginda8ba33642011-12-14 12:31:31 -08001958 if (state) {
rginda9f5222b2012-03-05 11:53:28 -08001959 this.scrollPort_.setForegroundColor(this.prefs_.get('background-color'));
1960 this.scrollPort_.setBackgroundColor(this.prefs_.get('foreground-color'));
rginda8ba33642011-12-14 12:31:31 -08001961 } else {
rginda9f5222b2012-03-05 11:53:28 -08001962 this.scrollPort_.setForegroundColor(this.prefs_.get('foreground-color'));
1963 this.scrollPort_.setBackgroundColor(this.prefs_.get('background-color'));
rginda8ba33642011-12-14 12:31:31 -08001964 }
1965};
1966
1967/**
rginda87b86462011-12-14 13:48:03 -08001968 * Ring the terminal bell.
Robert Ginda92e18102013-03-14 13:56:37 -07001969 *
1970 * This will not play the bell audio more than once per second.
rginda87b86462011-12-14 13:48:03 -08001971 */
1972hterm.Terminal.prototype.ringBell = function() {
rginda6d397402012-01-17 10:58:29 -08001973 this.cursorNode_.style.backgroundColor =
1974 this.scrollPort_.getForegroundColor();
rginda87b86462011-12-14 13:48:03 -08001975
1976 var self = this;
1977 setTimeout(function() {
rginda9f5222b2012-03-05 11:53:28 -08001978 self.cursorNode_.style.backgroundColor = self.prefs_.get('cursor-color');
rginda6d397402012-01-17 10:58:29 -08001979 }, 200);
Robert Ginda92e18102013-03-14 13:56:37 -07001980
1981 if (this.bellAudio_.getAttribute('src')) {
Robert Gindaa6331372013-03-19 10:35:39 -07001982 if (this.bellSquelchTimeout_)
Robert Ginda92e18102013-03-14 13:56:37 -07001983 return;
1984
1985 this.bellAudio_.play();
1986
1987 this.bellSequelchTimeout_ = setTimeout(function() {
1988 delete this.bellSquelchTimeout_;
Robert Gindaa6331372013-03-19 10:35:39 -07001989 }.bind(this), 500);
Robert Ginda92e18102013-03-14 13:56:37 -07001990 } else {
1991 delete this.bellSquelchTimeout_;
1992 }
rginda87b86462011-12-14 13:48:03 -08001993};
1994
1995/**
rginda8ba33642011-12-14 12:31:31 -08001996 * Set the origin mode bit.
1997 *
1998 * If origin mode is on, certain VT cursor and scrolling commands measure their
1999 * row parameter relative to the VT scroll region. Otherwise, row 0 corresponds
2000 * to the top of the addressable screen.
2001 *
2002 * Defaults to off.
2003 *
2004 * @param {boolean} state True to set origin mode, false to unset.
2005 */
2006hterm.Terminal.prototype.setOriginMode = function(state) {
2007 this.options_.originMode = state;
rgindae4d29232012-01-19 10:47:13 -08002008 this.setCursorPosition(0, 0);
rginda8ba33642011-12-14 12:31:31 -08002009};
2010
2011/**
2012 * Set the insert mode bit.
2013 *
2014 * If insert mode is on, existing text beyond the cursor position will be
2015 * shifted right to make room for new text. Otherwise, new text overwrites
2016 * any existing text.
2017 *
2018 * Defaults to off.
2019 *
2020 * @param {boolean} state True to set insert mode, false to unset.
2021 */
2022hterm.Terminal.prototype.setInsertMode = function(state) {
2023 this.options_.insertMode = state;
2024};
2025
2026/**
rginda87b86462011-12-14 13:48:03 -08002027 * Set the auto carriage return bit.
2028 *
2029 * If auto carriage return is on then a formfeed character is interpreted
2030 * as a newline, otherwise it's the same as a linefeed. The difference boils
2031 * down to whether or not the cursor column is reset.
2032 */
2033hterm.Terminal.prototype.setAutoCarriageReturn = function(state) {
2034 this.options_.autoCarriageReturn = state;
2035};
2036
2037/**
rginda8ba33642011-12-14 12:31:31 -08002038 * Set the wraparound mode bit.
2039 *
2040 * If wraparound mode is on, certain VT commands will allow the cursor to wrap
2041 * to the start of the following row. Otherwise, the cursor is clamped to the
2042 * end of the screen and attempts to write past it are ignored.
2043 *
2044 * Defaults to on.
2045 *
2046 * @param {boolean} state True to set wraparound mode, false to unset.
2047 */
2048hterm.Terminal.prototype.setWraparound = function(state) {
2049 this.options_.wraparound = state;
2050};
2051
2052/**
2053 * Set the reverse-wraparound mode bit.
2054 *
2055 * If wraparound mode is off, certain VT commands will allow the cursor to wrap
2056 * to the end of the previous row. Otherwise, the cursor is clamped to column
2057 * 0.
2058 *
2059 * Defaults to off.
2060 *
2061 * @param {boolean} state True to set reverse-wraparound mode, false to unset.
2062 */
2063hterm.Terminal.prototype.setReverseWraparound = function(state) {
2064 this.options_.reverseWraparound = state;
2065};
2066
2067/**
2068 * Selects between the primary and alternate screens.
2069 *
2070 * If alternate mode is on, the alternate screen is active. Otherwise the
2071 * primary screen is active.
2072 *
2073 * Swapping screens has no effect on the scrollback buffer.
2074 *
2075 * Each screen maintains its own cursor position.
2076 *
2077 * Defaults to off.
2078 *
2079 * @param {boolean} state True to set alternate mode, false to unset.
2080 */
2081hterm.Terminal.prototype.setAlternateMode = function(state) {
rginda6d397402012-01-17 10:58:29 -08002082 var cursor = this.saveCursor();
rginda8ba33642011-12-14 12:31:31 -08002083 this.screen_ = state ? this.alternateScreen_ : this.primaryScreen_;
2084
rginda35c456b2012-02-09 17:29:05 -08002085 if (this.screen_.rowsArray.length &&
2086 this.screen_.rowsArray[0].rowIndex != this.scrollbackRows_.length) {
2087 // If the screen changed sizes while we were away, our rowIndexes may
2088 // be incorrect.
2089 var offset = this.scrollbackRows_.length;
2090 var ary = this.screen_.rowsArray;
rgindacbbd7482012-06-13 15:06:16 -07002091 for (var i = 0; i < ary.length; i++) {
rginda35c456b2012-02-09 17:29:05 -08002092 ary[i].rowIndex = offset + i;
2093 }
2094 }
rginda8ba33642011-12-14 12:31:31 -08002095
rginda35c456b2012-02-09 17:29:05 -08002096 this.realizeWidth_(this.screenSize.width);
2097 this.realizeHeight_(this.screenSize.height);
2098 this.scrollPort_.syncScrollHeight();
2099 this.scrollPort_.invalidate();
rginda8ba33642011-12-14 12:31:31 -08002100
rginda6d397402012-01-17 10:58:29 -08002101 this.restoreCursor(cursor);
rginda35c456b2012-02-09 17:29:05 -08002102 this.scrollPort_.resize();
rginda8ba33642011-12-14 12:31:31 -08002103};
2104
2105/**
2106 * Set the cursor-blink mode bit.
2107 *
2108 * If cursor-blink is on, the cursor will blink when it is visible. Otherwise
2109 * a visible cursor does not blink.
2110 *
2111 * You should make sure to turn blinking off if you're going to dispose of a
2112 * terminal, otherwise you'll leak a timeout.
2113 *
2114 * Defaults to on.
2115 *
2116 * @param {boolean} state True to set cursor-blink mode, false to unset.
2117 */
2118hterm.Terminal.prototype.setCursorBlink = function(state) {
2119 this.options_.cursorBlink = state;
2120
2121 if (!state && this.timeouts_.cursorBlink) {
2122 clearTimeout(this.timeouts_.cursorBlink);
2123 delete this.timeouts_.cursorBlink;
2124 }
2125
2126 if (this.options_.cursorVisible)
2127 this.setCursorVisible(true);
2128};
2129
2130/**
2131 * Set the cursor-visible mode bit.
2132 *
2133 * If cursor-visible is on, the cursor will be visible. Otherwise it will not.
2134 *
2135 * Defaults to on.
2136 *
2137 * @param {boolean} state True to set cursor-visible mode, false to unset.
2138 */
2139hterm.Terminal.prototype.setCursorVisible = function(state) {
2140 this.options_.cursorVisible = state;
2141
2142 if (!state) {
rginda87b86462011-12-14 13:48:03 -08002143 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002144 return;
2145 }
2146
rginda87b86462011-12-14 13:48:03 -08002147 this.syncCursorPosition_();
2148
2149 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002150
2151 if (this.options_.cursorBlink) {
2152 if (this.timeouts_.cursorBlink)
2153 return;
2154
2155 this.timeouts_.cursorBlink = setInterval(this.onCursorBlink_.bind(this),
2156 500);
2157 } else {
2158 if (this.timeouts_.cursorBlink) {
2159 clearTimeout(this.timeouts_.cursorBlink);
2160 delete this.timeouts_.cursorBlink;
2161 }
2162 }
2163};
2164
2165/**
rginda87b86462011-12-14 13:48:03 -08002166 * Synchronizes the visible cursor and document selection with the current
2167 * cursor coordinates.
rginda8ba33642011-12-14 12:31:31 -08002168 */
2169hterm.Terminal.prototype.syncCursorPosition_ = function() {
2170 var topRowIndex = this.scrollPort_.getTopRowIndex();
2171 var bottomRowIndex = this.scrollPort_.getBottomRowIndex(topRowIndex);
2172 var cursorRowIndex = this.scrollbackRows_.length +
2173 this.screen_.cursorPosition.row;
2174
2175 if (cursorRowIndex > bottomRowIndex) {
2176 // Cursor is scrolled off screen, move it outside of the visible area.
rginda35c456b2012-02-09 17:29:05 -08002177 this.cursorNode_.style.top = -this.scrollPort_.characterSize.height + 'px';
rginda8ba33642011-12-14 12:31:31 -08002178 return;
2179 }
2180
rginda35c456b2012-02-09 17:29:05 -08002181 this.cursorNode_.style.width = this.scrollPort_.characterSize.width + 'px';
rginda35c456b2012-02-09 17:29:05 -08002182
rginda8ba33642011-12-14 12:31:31 -08002183 this.cursorNode_.style.top = this.scrollPort_.visibleRowTopMargin +
rginda35c456b2012-02-09 17:29:05 -08002184 this.scrollPort_.characterSize.height * (cursorRowIndex - topRowIndex) +
2185 'px';
2186 this.cursorNode_.style.left = this.scrollPort_.characterSize.width *
2187 this.screen_.cursorPosition.column + 'px';
rginda87b86462011-12-14 13:48:03 -08002188
2189 this.cursorNode_.setAttribute('title',
2190 '(' + this.screen_.cursorPosition.row +
2191 ', ' + this.screen_.cursorPosition.column +
2192 ')');
2193
2194 // Update the caret for a11y purposes.
2195 var selection = this.document_.getSelection();
2196 if (selection && selection.isCollapsed)
2197 this.screen_.syncSelectionCaret(selection);
rginda8ba33642011-12-14 12:31:31 -08002198};
2199
Robert Ginda830583c2013-08-07 13:20:46 -07002200hterm.Terminal.prototype.restyleCursor_ = function() {
2201 var shape = this.cursorShape_;
2202
2203 if (this.cursorNode_.getAttribute('focus') == 'false') {
2204 // Always show a block cursor when unfocused.
2205 shape = hterm.Terminal.cursorShape.BLOCK;
2206 }
2207
2208 var style = this.cursorNode_.style;
2209
2210 switch (shape) {
2211 case hterm.Terminal.cursorShape.BEAM:
2212 style.height = this.scrollPort_.characterSize.height + 'px';
2213 style.backgroundColor = 'transparent';
2214 style.borderBottomStyle = null;
2215 style.borderLeftStyle = 'solid';
2216 break;
2217
2218 case hterm.Terminal.cursorShape.UNDERLINE:
2219 style.height = this.scrollPort_.characterSize.baseline + 'px';
2220 style.backgroundColor = 'transparent';
2221 style.borderBottomStyle = 'solid';
2222 // correct the size to put it exactly at the baseline
2223 style.borderLeftStyle = null;
2224 break;
2225
2226 default:
2227 style.height = this.scrollPort_.characterSize.height + 'px';
2228 style.backgroundColor = this.cursorColor_;
2229 style.borderBottomStyle = null;
2230 style.borderLeftStyle = null;
2231 break;
2232 }
2233};
2234
rginda8ba33642011-12-14 12:31:31 -08002235/**
2236 * Synchronizes the visible cursor with the current cursor coordinates.
2237 *
2238 * The sync will happen asynchronously, soon after the call stack winds down.
2239 * Multiple calls will be coalesced into a single sync.
2240 */
2241hterm.Terminal.prototype.scheduleSyncCursorPosition_ = function() {
2242 if (this.timeouts_.syncCursor)
rginda87b86462011-12-14 13:48:03 -08002243 return;
rginda8ba33642011-12-14 12:31:31 -08002244
2245 var self = this;
2246 this.timeouts_.syncCursor = setTimeout(function() {
2247 self.syncCursorPosition_();
2248 delete self.timeouts_.syncCursor;
rginda87b86462011-12-14 13:48:03 -08002249 }, 0);
2250};
2251
rgindacc2996c2012-02-24 14:59:31 -08002252/**
rgindaf522ce02012-04-17 17:49:17 -07002253 * Show or hide the zoom warning.
2254 *
2255 * The zoom warning is a message warning the user that their browser zoom must
2256 * be set to 100% in order for hterm to function properly.
2257 *
2258 * @param {boolean} state True to show the message, false to hide it.
2259 */
2260hterm.Terminal.prototype.showZoomWarning_ = function(state) {
2261 if (!this.zoomWarningNode_) {
2262 if (!state)
2263 return;
2264
2265 this.zoomWarningNode_ = this.document_.createElement('div');
2266 this.zoomWarningNode_.style.cssText = (
2267 'color: black;' +
2268 'background-color: #ff2222;' +
2269 'font-size: large;' +
2270 'border-radius: 8px;' +
2271 'opacity: 0.75;' +
2272 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2273 'top: 0.5em;' +
2274 'right: 1.2em;' +
2275 'position: absolute;' +
2276 '-webkit-text-size-adjust: none;' +
2277 '-webkit-user-select: none;');
rgindaf522ce02012-04-17 17:49:17 -07002278 }
2279
Robert Gindab4839c22013-02-28 16:52:10 -08002280 this.zoomWarningNode_.textContent = lib.MessageManager.replaceReferences(
2281 hterm.zoomWarningMessage,
2282 [parseInt(this.scrollPort_.characterSize.zoomFactor * 100)]);
2283
rgindaf522ce02012-04-17 17:49:17 -07002284 this.zoomWarningNode_.style.fontFamily = this.prefs_.get('font-family');
2285
2286 if (state) {
2287 if (!this.zoomWarningNode_.parentNode)
2288 this.div_.parentNode.appendChild(this.zoomWarningNode_);
2289 } else if (this.zoomWarningNode_.parentNode) {
2290 this.zoomWarningNode_.parentNode.removeChild(this.zoomWarningNode_);
2291 }
2292};
2293
2294/**
rgindacc2996c2012-02-24 14:59:31 -08002295 * Show the terminal overlay for a given amount of time.
2296 *
2297 * The terminal overlay appears in inverse video in a large font, centered
2298 * over the terminal. You should probably keep the overlay message brief,
2299 * since it's in a large font and you probably aren't going to check the size
2300 * of the terminal first.
2301 *
2302 * @param {string} msg The text (not HTML) message to display in the overlay.
2303 * @param {number} opt_timeout The amount of time to wait before fading out
2304 * the overlay. Defaults to 1.5 seconds. Pass null to have the overlay
2305 * stay up forever (or until the next overlay).
2306 */
2307hterm.Terminal.prototype.showOverlay = function(msg, opt_timeout) {
rgindaf0090c92012-02-10 14:58:52 -08002308 if (!this.overlayNode_) {
2309 if (!this.div_)
2310 return;
2311
2312 this.overlayNode_ = this.document_.createElement('div');
2313 this.overlayNode_.style.cssText = (
rgindaf0090c92012-02-10 14:58:52 -08002314 'border-radius: 15px;' +
rgindaf0090c92012-02-10 14:58:52 -08002315 'font-size: xx-large;' +
2316 'opacity: 0.75;' +
2317 'padding: 0.2em 0.5em 0.2em 0.5em;' +
2318 'position: absolute;' +
2319 '-webkit-user-select: none;' +
2320 '-webkit-transition: opacity 180ms ease-in;');
2321 }
2322
rginda9f5222b2012-03-05 11:53:28 -08002323 this.overlayNode_.style.color = this.prefs_.get('background-color');
2324 this.overlayNode_.style.backgroundColor = this.prefs_.get('foreground-color');
2325 this.overlayNode_.style.fontFamily = this.prefs_.get('font-family');
2326
rgindaf0090c92012-02-10 14:58:52 -08002327 this.overlayNode_.textContent = msg;
2328 this.overlayNode_.style.opacity = '0.75';
2329
2330 if (!this.overlayNode_.parentNode)
2331 this.div_.appendChild(this.overlayNode_);
2332
Robert Ginda97769282013-02-01 15:30:30 -08002333 var divSize = hterm.getClientSize(this.div_);
2334 var overlaySize = hterm.getClientSize(this.overlayNode_);
2335
2336 this.overlayNode_.style.top = (divSize.height - overlaySize.height) / 2;
2337 this.overlayNode_.style.left = (divSize.width - overlaySize.width -
2338 this.scrollPort_.currentScrollbarWidthPx) / 2;
rgindaf0090c92012-02-10 14:58:52 -08002339
2340 var self = this;
2341
2342 if (this.overlayTimeout_)
2343 clearTimeout(this.overlayTimeout_);
2344
rgindacc2996c2012-02-24 14:59:31 -08002345 if (opt_timeout === null)
2346 return;
2347
rgindaf0090c92012-02-10 14:58:52 -08002348 this.overlayTimeout_ = setTimeout(function() {
2349 self.overlayNode_.style.opacity = '0';
2350 setTimeout(function() {
rginda259dcca2012-03-14 16:37:11 -07002351 if (self.overlayNode_.parentNode)
2352 self.overlayNode_.parentNode.removeChild(self.overlayNode_);
rgindaf0090c92012-02-10 14:58:52 -08002353 self.overlayTimeout_ = null;
2354 self.overlayNode_.style.opacity = '0.75';
2355 }, 200);
rgindacc2996c2012-02-24 14:59:31 -08002356 }, opt_timeout || 1500);
rgindaf0090c92012-02-10 14:58:52 -08002357};
2358
rginda4bba5e12012-06-20 16:15:30 -07002359/**
2360 * Paste from the system clipboard to the terminal.
2361 */
2362hterm.Terminal.prototype.paste = function() {
2363 hterm.pasteFromClipboard(this.document_);
2364};
2365
2366/**
2367 * Copy a string to the system clipboard.
2368 *
2369 * Note: If there is a selected range in the terminal, it'll be cleared.
2370 */
2371hterm.Terminal.prototype.copyStringToClipboard = function(str) {
Robert Ginda9fb38222012-09-11 14:19:12 -07002372 if (this.prefs_.get('enable-clipboard-notice'))
Robert Gindab4839c22013-02-28 16:52:10 -08002373 setTimeout(this.showOverlay.bind(this, hterm.notifyCopyMessage, 500), 200);
rgindaa09e7332012-08-17 12:49:51 -07002374
2375 var copySource = this.document_.createElement('pre');
rginda4bba5e12012-06-20 16:15:30 -07002376 copySource.textContent = str;
2377 copySource.style.cssText = (
2378 '-webkit-user-select: text;' +
2379 'position: absolute;' +
2380 'top: -99px');
2381
2382 this.document_.body.appendChild(copySource);
rgindafaa74742012-08-21 13:34:03 -07002383
rginda4bba5e12012-06-20 16:15:30 -07002384 var selection = this.document_.getSelection();
rgindafaa74742012-08-21 13:34:03 -07002385 var anchorNode = selection.anchorNode;
2386 var anchorOffset = selection.anchorOffset;
2387 var focusNode = selection.focusNode;
2388 var focusOffset = selection.focusOffset;
2389
rginda4bba5e12012-06-20 16:15:30 -07002390 selection.selectAllChildren(copySource);
2391
rgindaa09e7332012-08-17 12:49:51 -07002392 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002393
rgindafaa74742012-08-21 13:34:03 -07002394 selection.collapse(anchorNode, anchorOffset);
2395 selection.extend(focusNode, focusOffset);
2396
rginda4bba5e12012-06-20 16:15:30 -07002397 copySource.parentNode.removeChild(copySource);
2398};
2399
rgindaa09e7332012-08-17 12:49:51 -07002400hterm.Terminal.prototype.getSelectionText = function() {
2401 var selection = this.scrollPort_.selection;
2402 selection.sync();
2403
2404 if (selection.isCollapsed)
2405 return null;
2406
2407
2408 // Start offset measures from the beginning of the line.
2409 var startOffset = selection.startOffset;
2410 var node = selection.startNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002411
Robert Gindafdbb3f22012-09-06 20:23:06 -07002412 if (node.nodeName != 'X-ROW') {
2413 // If the selection doesn't start on an x-row node, then it must be
2414 // somewhere inside the x-row. Add any characters from previous siblings
2415 // into the start offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002416
2417 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2418 // If node is the text node in a styled span, move up to the span node.
2419 node = node.parentNode;
2420 }
2421
Robert Gindafdbb3f22012-09-06 20:23:06 -07002422 while (node.previousSibling) {
2423 node = node.previousSibling;
2424 startOffset += node.textContent.length;
2425 }
rgindaa09e7332012-08-17 12:49:51 -07002426 }
2427
2428 // End offset measures from the end of the line.
2429 var endOffset = selection.endNode.textContent.length - selection.endOffset;
2430 var node = selection.endNode;
Robert Ginda0d190502012-10-02 10:59:00 -07002431
Robert Gindafdbb3f22012-09-06 20:23:06 -07002432 if (node.nodeName != 'X-ROW') {
2433 // If the selection doesn't end on an x-row node, then it must be
2434 // somewhere inside the x-row. Add any characters from following siblings
2435 // into the end offset.
Robert Ginda0d190502012-10-02 10:59:00 -07002436
2437 if (node.nodeName == '#text' && node.parentNode.nodeName == 'SPAN') {
2438 // If node is the text node in a styled span, move up to the span node.
2439 node = node.parentNode;
2440 }
2441
Robert Gindafdbb3f22012-09-06 20:23:06 -07002442 while (node.nextSibling) {
2443 node = node.nextSibling;
2444 endOffset += node.textContent.length;
2445 }
rgindaa09e7332012-08-17 12:49:51 -07002446 }
2447
2448 var rv = this.getRowsText(selection.startRow.rowIndex,
2449 selection.endRow.rowIndex + 1);
2450 return rv.substring(startOffset, rv.length - endOffset);
2451};
2452
rginda4bba5e12012-06-20 16:15:30 -07002453/**
2454 * Copy the current selection to the system clipboard, then clear it after a
2455 * short delay.
2456 */
2457hterm.Terminal.prototype.copySelectionToClipboard = function() {
rgindaa09e7332012-08-17 12:49:51 -07002458 var text = this.getSelectionText();
2459 if (text != null)
2460 this.copyStringToClipboard(text);
rginda4bba5e12012-06-20 16:15:30 -07002461};
2462
rgindaf0090c92012-02-10 14:58:52 -08002463hterm.Terminal.prototype.overlaySize = function() {
2464 this.showOverlay(this.screenSize.width + 'x' + this.screenSize.height);
2465};
2466
rginda87b86462011-12-14 13:48:03 -08002467/**
2468 * Invoked by hterm.Terminal.Keyboard when a VT keystroke is detected.
2469 *
Robert Ginda8cb7d902013-06-20 14:37:18 -07002470 * @param {string} string The VT string representing the keystroke, in UTF-16.
rginda87b86462011-12-14 13:48:03 -08002471 */
2472hterm.Terminal.prototype.onVTKeystroke = function(string) {
rginda9f5222b2012-03-05 11:53:28 -08002473 if (this.scrollOnKeystroke_)
rginda87b86462011-12-14 13:48:03 -08002474 this.scrollPort_.scrollRowToBottom(this.getRowCount());
2475
Robert Ginda8cb7d902013-06-20 14:37:18 -07002476 this.io.onVTKeystroke(this.keyboard.encode(string));
rginda8ba33642011-12-14 12:31:31 -08002477};
2478
2479/**
rgindad5613292012-06-19 15:40:37 -07002480 * Add the terminalRow and terminalColumn properties to mouse events and
2481 * then forward on to onMouse().
2482 *
2483 * The terminalRow and terminalColumn properties contain the (row, column)
2484 * coordinates for the mouse event.
2485 */
2486hterm.Terminal.prototype.onMouse_ = function(e) {
rgindafaa74742012-08-21 13:34:03 -07002487 if (e.processedByTerminalHandler_) {
2488 // We register our event handlers on the document, as well as the cursor
2489 // and the scroll blocker. Mouse events that occur on the cursor or
2490 // scroll blocker will also appear on the document, but we don't want to
2491 // process them twice.
2492 //
2493 // We can't just prevent bubbling because that has other side effects, so
2494 // we decorate the event object with this property instead.
2495 return;
2496 }
2497
2498 e.processedByTerminalHandler_ = true;
2499
John Macinnesfb683832013-07-22 14:46:30 -04002500 if (e.type == 'dblclick') {
2501 this.screen_.expandSelection(this.document_.getSelection());
2502 hterm.copySelectionToClipboard(this.document_);
2503 return;
2504 }
2505
rginda4bba5e12012-06-20 16:15:30 -07002506 if (e.type == 'mousedown' && e.which == this.mousePasteButton) {
2507 this.paste();
2508 return;
2509 }
2510
2511 if (e.type == 'mouseup' && e.which == 1 && this.copyOnSelect &&
2512 !this.document_.getSelection().isCollapsed) {
rgindafaa74742012-08-21 13:34:03 -07002513 hterm.copySelectionToClipboard(this.document_);
rginda4bba5e12012-06-20 16:15:30 -07002514 return;
2515 }
2516
rgindad5613292012-06-19 15:40:37 -07002517 e.terminalRow = parseInt((e.clientY - this.scrollPort_.visibleRowTopMargin) /
2518 this.scrollPort_.characterSize.height) + 1;
2519 e.terminalColumn = parseInt(e.clientX /
2520 this.scrollPort_.characterSize.width) + 1;
2521
2522 if (e.type == 'mousedown') {
2523 if (e.terminalColumn > this.screenSize.width) {
2524 // Mousedown in the scrollbar area.
2525 return;
2526 }
2527
2528 if (!this.enableMouseDragScroll) {
2529 // Move the scroll-blocker into place if we want to keep the scrollport
2530 // from scrolling.
2531 this.scrollBlockerNode_.engaged = true;
2532 this.scrollBlockerNode_.style.top = (e.clientY - 5) + 'px';
2533 this.scrollBlockerNode_.style.left = (e.clientX - 5) + 'px';
2534 }
2535 } else if (this.scrollBlockerNode_.engaged &&
2536 (e.type == 'mousemove' || e.type == 'mouseup')) {
2537 // Disengage the scroll-blocker after one of these events.
2538 this.scrollBlockerNode_.engaged = false;
2539 this.scrollBlockerNode_.style.top = '-99px';
2540 }
2541
rgindafaa74742012-08-21 13:34:03 -07002542 this.onMouse(e);
rgindad5613292012-06-19 15:40:37 -07002543};
2544
2545/**
2546 * Clients should override this if they care to know about mouse events.
2547 *
2548 * The event parameter will be a normal DOM mouse click event with additional
2549 * 'terminalRow' and 'terminalColumn' properties.
2550 */
2551hterm.Terminal.prototype.onMouse = function(e) { };
2552
2553/**
rginda8e92a692012-05-20 19:37:20 -07002554 * React when focus changes.
2555 */
2556hterm.Terminal.prototype.onFocusChange_ = function(state) {
2557 this.cursorNode_.setAttribute('focus', state ? 'true' : 'false');
Robert Ginda830583c2013-08-07 13:20:46 -07002558 this.restyleCursor_();
rginda8e92a692012-05-20 19:37:20 -07002559};
2560
2561/**
rginda8ba33642011-12-14 12:31:31 -08002562 * React when the ScrollPort is scrolled.
2563 */
2564hterm.Terminal.prototype.onScroll_ = function() {
2565 this.scheduleSyncCursorPosition_();
2566};
2567
2568/**
rginda9846e2f2012-01-27 13:53:33 -08002569 * React when text is pasted into the scrollPort.
2570 */
2571hterm.Terminal.prototype.onPaste_ = function(e) {
Robert Ginda8cb7d902013-06-20 14:37:18 -07002572 this.onVTKeystroke(e.text.replace(/\n/mg, '\r'));
rginda9846e2f2012-01-27 13:53:33 -08002573};
2574
2575/**
rgindaa09e7332012-08-17 12:49:51 -07002576 * React when the user tries to copy from the scrollPort.
2577 */
2578hterm.Terminal.prototype.onCopy_ = function(e) {
2579 e.preventDefault();
rgindafaa74742012-08-21 13:34:03 -07002580 this.copySelectionToClipboard();
rgindaa09e7332012-08-17 12:49:51 -07002581};
2582
2583/**
rginda8ba33642011-12-14 12:31:31 -08002584 * React when the ScrollPort is resized.
rgindac9bc5502012-01-18 11:48:44 -08002585 *
2586 * Note: This function should not directly contain code that alters the internal
2587 * state of the terminal. That kind of code belongs in realizeWidth or
2588 * realizeHeight, so that it can be executed synchronously in the case of a
2589 * programmatic width change.
rginda8ba33642011-12-14 12:31:31 -08002590 */
2591hterm.Terminal.prototype.onResize_ = function() {
rgindac9bc5502012-01-18 11:48:44 -08002592 var columnCount = Math.floor(this.scrollPort_.getScreenWidth() /
rginda35c456b2012-02-09 17:29:05 -08002593 this.scrollPort_.characterSize.width);
2594 var rowCount = Math.floor(this.scrollPort_.getScreenHeight() /
2595 this.scrollPort_.characterSize.height);
2596
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002597 if (columnCount <= 0 || rowCount <= 0) {
rginda35c456b2012-02-09 17:29:05 -08002598 // We avoid these situations since they happen sometimes when the terminal
Robert Ginda4e83f3a2012-09-04 15:25:25 -07002599 // gets removed from the document or during the initial load, and we can't
2600 // deal with that.
rginda35c456b2012-02-09 17:29:05 -08002601 return;
2602 }
2603
rgindaa8ba17d2012-08-15 14:41:10 -07002604 var isNewSize = (columnCount != this.screenSize.width ||
2605 rowCount != this.screenSize.height);
2606
2607 // We do this even if the size didn't change, just to be sure everything is
2608 // in sync.
Dmitry Polukhinbb2ef712012-01-19 19:00:37 +04002609 this.realizeSize_(columnCount, rowCount);
rgindaf522ce02012-04-17 17:49:17 -07002610 this.showZoomWarning_(this.scrollPort_.characterSize.zoomFactor != 1);
rgindaa8ba17d2012-08-15 14:41:10 -07002611
2612 if (isNewSize)
2613 this.overlaySize();
2614
2615 this.scheduleSyncCursorPosition_();
rginda8ba33642011-12-14 12:31:31 -08002616};
2617
2618/**
2619 * Service the cursor blink timeout.
2620 */
2621hterm.Terminal.prototype.onCursorBlink_ = function() {
Robert Ginda830583c2013-08-07 13:20:46 -07002622 if (this.cursorNode_.getAttribute('focus') == 'false' ||
2623 this.cursorNode_.style.opacity == '0') {
rginda87b86462011-12-14 13:48:03 -08002624 this.cursorNode_.style.opacity = '1';
rginda8ba33642011-12-14 12:31:31 -08002625 } else {
rginda87b86462011-12-14 13:48:03 -08002626 this.cursorNode_.style.opacity = '0';
rginda8ba33642011-12-14 12:31:31 -08002627 }
2628};
David Reveman8f552492012-03-28 12:18:41 -04002629
2630/**
2631 * Set the scrollbar-visible mode bit.
2632 *
2633 * If scrollbar-visible is on, the vertical scrollbar will be visible.
2634 * Otherwise it will not.
2635 *
2636 * Defaults to on.
2637 *
2638 * @param {boolean} state True to set scrollbar-visible mode, false to unset.
2639 */
2640hterm.Terminal.prototype.setScrollbarVisible = function(state) {
2641 this.scrollPort_.setScrollbarVisible(state);
2642};